ttablesthreads.nim 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. discard """
  2. cmd: "nim c --threads:on $file"
  3. output: '''true'''
  4. """
  5. import hashes, tables, sharedtables, algorithm, sequtils
  6. proc sortedPairs[T](t: T): auto = toSeq(t.pairs).sorted
  7. const
  8. data = {
  9. "34": 123456, "12": 789,
  10. "90": 343, "0": 34404,
  11. "1": 344004, "2": 344774,
  12. "3": 342244, "4": 3412344,
  13. "5": 341232144, "6": 34214544,
  14. "7": 3434544, "8": 344544,
  15. "9": 34435644, "---00": 346677844,
  16. "10": 34484, "11": 34474, "19": 34464,
  17. "20": 34454, "30": 34141244, "40": 344114,
  18. "50": 344490, "60": 344491, "70": 344492,
  19. "80": 344497}
  20. sorteddata = {
  21. "---00": 346677844,
  22. "0": 34404,
  23. "1": 344004,
  24. "10": 34484,
  25. "11": 34474,
  26. "12": 789,
  27. "19": 34464,
  28. "2": 344774, "20": 34454,
  29. "3": 342244, "30": 34141244,
  30. "34": 123456,
  31. "4": 3412344, "40": 344114,
  32. "5": 341232144, "50": 344490,
  33. "6": 34214544, "60": 344491,
  34. "7": 3434544, "70": 344492,
  35. "8": 344544, "80": 344497,
  36. "9": 34435644,
  37. "90": 343}
  38. block tableTest1:
  39. var t = initTable[tuple[x, y: int], string]()
  40. t[(0,0)] = "00"
  41. t[(1,0)] = "10"
  42. t[(0,1)] = "01"
  43. t[(1,1)] = "11"
  44. for x in 0..1:
  45. for y in 0..1:
  46. doAssert t[(x,y)] == $x & $y
  47. doAssert t.sortedPairs == @[((x: 0, y: 0), "00"), ((x: 0, y: 1), "01"), ((x: 1, y: 0), "10"), ((x: 1, y: 1), "11")]
  48. block tableTest2:
  49. var t = initTable[string, float]()
  50. t["test"] = 1.2345
  51. t["111"] = 1.000043
  52. t["123"] = 1.23
  53. t.del("111")
  54. t["012"] = 67.9
  55. t["123"] = 1.5 # test overwriting
  56. doAssert t["123"] == 1.5
  57. try:
  58. echo t["111"] # deleted
  59. except KeyError:
  60. discard
  61. doAssert(not hasKey(t, "111"))
  62. doAssert "123" in t
  63. doAssert("111" notin t)
  64. for key, val in items(data): t[key] = val.toFloat
  65. for key, val in items(data): doAssert t[key] == val.toFloat
  66. doAssert(not t.hasKeyOrPut("456", 4.0)) # test absent key
  67. doAssert t.hasKeyOrPut("012", 3.0) # test present key
  68. var x = t.mgetOrPut("111", 1.5) # test absent key
  69. x = x * 2
  70. doAssert x == 3.0
  71. x = t.mgetOrPut("test", 1.5) # test present key
  72. x = x * 2
  73. doAssert x == 2 * 1.2345
  74. block orderedTableTest1:
  75. var t = initOrderedTable[string, int](2)
  76. for key, val in items(data): t[key] = val
  77. for key, val in items(data): doAssert t[key] == val
  78. var i = 0
  79. # `pairs` needs to yield in insertion order:
  80. for key, val in pairs(t):
  81. doAssert key == data[i][0]
  82. doAssert val == data[i][1]
  83. inc(i)
  84. for key, val in mpairs(t): val = 99
  85. for val in mvalues(t): doAssert val == 99
  86. block orderedTableTest2:
  87. var
  88. s = initOrderedTable[string, int]()
  89. t = initOrderedTable[string, int]()
  90. doAssert s == t
  91. for key, val in items(data): t[key] = val
  92. doAssert s != t
  93. for key, val in items(sorteddata): s[key] = val
  94. doAssert s != t
  95. t.clear()
  96. doAssert s != t
  97. for key, val in items(sorteddata): t[key] = val
  98. doAssert s == t
  99. block countTableTest1:
  100. var s = data.toTable
  101. var t = initCountTable[string]()
  102. for k in s.keys: t.inc(k)
  103. for k in t.keys: doAssert t[k] == 1
  104. t.inc("90", 3)
  105. t.inc("12", 2)
  106. t.inc("34", 1)
  107. doAssert t.largest()[0] == "90"
  108. t.sort()
  109. var i = 0
  110. for k, v in t.pairs:
  111. case i
  112. of 0: doAssert k == "90" and v == 4
  113. of 1: doAssert k == "12" and v == 3
  114. of 2: doAssert k == "34" and v == 2
  115. else: break
  116. inc i
  117. block countTableTest2:
  118. var
  119. s = initCountTable[int]()
  120. t = initCountTable[int]()
  121. doAssert s == t
  122. s.inc(1)
  123. doAssert s != t
  124. t.inc(2)
  125. doAssert s != t
  126. t.inc(1)
  127. doAssert s != t
  128. s.inc(2)
  129. doAssert s == t
  130. s.inc(1)
  131. doAssert s != t
  132. t.inc(1)
  133. doAssert s == t
  134. block mpairsTableTest1:
  135. var t = initTable[string, int]()
  136. t["a"] = 1
  137. t["b"] = 2
  138. t["c"] = 3
  139. t["d"] = 4
  140. for k, v in t.mpairs:
  141. if k == "a" or k == "c":
  142. v = 9
  143. for k, v in t.pairs:
  144. if k == "a" or k == "c":
  145. doAssert v == 9
  146. else:
  147. doAssert v != 1 and v != 3
  148. block SyntaxTest:
  149. var x = toTable[int, string]({:})
  150. block zeroHashKeysTest:
  151. proc doZeroHashValueTest[T, K, V](t: T, nullHashKey: K, value: V) =
  152. let initialLen = t.len
  153. var testTable = t
  154. testTable[nullHashKey] = value
  155. doAssert testTable[nullHashKey] == value
  156. doAssert testTable.len == initialLen + 1
  157. testTable.del(nullHashKey)
  158. doAssert testTable.len == initialLen
  159. # with empty table
  160. doZeroHashValueTest(toTable[int,int]({:}), 0, 42)
  161. doZeroHashValueTest(toTable[string,int]({:}), "", 23)
  162. doZeroHashValueTest(toOrderedTable[int,int]({:}), 0, 42)
  163. doZeroHashValueTest(toOrderedTable[string,int]({:}), "", 23)
  164. # with non-empty table
  165. doZeroHashValueTest(toTable[int,int]({1:2}), 0, 42)
  166. doZeroHashValueTest(toTable[string,string]({"foo": "bar"}), "", "zero")
  167. doZeroHashValueTest(toOrderedTable[int,int]({3:4}), 0, 42)
  168. doZeroHashValueTest(toOrderedTable[string,string]({"egg": "sausage"}),
  169. "", "spam")
  170. block clearTableTest:
  171. var t = data.toTable
  172. doAssert t.len() != 0
  173. t.clear()
  174. doAssert t.len() == 0
  175. block clearOrderedTableTest:
  176. var t = data.toOrderedTable
  177. doAssert t.len() != 0
  178. t.clear()
  179. doAssert t.len() == 0
  180. block clearCountTableTest:
  181. var t = initCountTable[string]()
  182. t.inc("90", 3)
  183. t.inc("12", 2)
  184. t.inc("34", 1)
  185. doAssert t.len() != 0
  186. t.clear()
  187. doAssert t.len() == 0
  188. block withKeyTest:
  189. var t: SharedTable[int, int]
  190. t.init()
  191. t.withKey(1) do (k: int, v: var int, pairExists: var bool):
  192. doAssert(v == 0)
  193. pairExists = true
  194. v = 42
  195. doAssert(t.mget(1) == 42)
  196. t.withKey(1) do (k: int, v: var int, pairExists: var bool):
  197. doAssert(v == 42)
  198. pairExists = false
  199. try:
  200. discard t.mget(1)
  201. doAssert(false, "KeyError expected")
  202. except KeyError:
  203. discard
  204. t.withKey(2) do (k: int, v: var int, pairExists: var bool):
  205. pairExists = false
  206. try:
  207. discard t.mget(2)
  208. doAssert(false, "KeyError expected")
  209. except KeyError:
  210. discard
  211. block takeTest:
  212. var t = initTable[string, int]()
  213. t["key"] = 123
  214. var val = 0
  215. doAssert(t.take("key", val))
  216. doAssert(val == 123)
  217. val = -1
  218. doAssert(not t.take("key", val))
  219. doAssert(val == -1)
  220. doAssert(not t.take("otherkey", val))
  221. doAssert(val == -1)
  222. proc orderedTableSortTest() =
  223. var t = initOrderedTable[string, int](2)
  224. for key, val in items(data): t[key] = val
  225. for key, val in items(data): doAssert t[key] == val
  226. t.sort(proc (x, y: tuple[key: string, val: int]): int = cmp(x.key, y.key))
  227. var i = 0
  228. # `pairs` needs to yield in sorted order:
  229. for key, val in pairs(t):
  230. doAssert key == sorteddata[i][0]
  231. doAssert val == sorteddata[i][1]
  232. inc(i)
  233. # check that lookup still works:
  234. for key, val in pairs(t):
  235. doAssert val == t[key]
  236. # check that insert still works:
  237. t["newKeyHere"] = 80
  238. orderedTableSortTest()
  239. echo "true"