ttablesthreads.nim 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. discard """
  2. cmd: "nim c --threads:on $file"
  3. output: '''true'''
  4. """
  5. import hashes, tables, sharedtables
  6. const
  7. data = {
  8. "34": 123456, "12": 789,
  9. "90": 343, "0": 34404,
  10. "1": 344004, "2": 344774,
  11. "3": 342244, "4": 3412344,
  12. "5": 341232144, "6": 34214544,
  13. "7": 3434544, "8": 344544,
  14. "9": 34435644, "---00": 346677844,
  15. "10": 34484, "11": 34474, "19": 34464,
  16. "20": 34454, "30": 34141244, "40": 344114,
  17. "50": 344490, "60": 344491, "70": 344492,
  18. "80": 344497}
  19. sorteddata = {
  20. "---00": 346677844,
  21. "0": 34404,
  22. "1": 344004,
  23. "10": 34484,
  24. "11": 34474,
  25. "12": 789,
  26. "19": 34464,
  27. "2": 344774, "20": 34454,
  28. "3": 342244, "30": 34141244,
  29. "34": 123456,
  30. "4": 3412344, "40": 344114,
  31. "5": 341232144, "50": 344490,
  32. "6": 34214544, "60": 344491,
  33. "7": 3434544, "70": 344492,
  34. "8": 344544, "80": 344497,
  35. "9": 34435644,
  36. "90": 343}
  37. block tableTest1:
  38. var t = initTable[tuple[x, y: int], string]()
  39. t[(0,0)] = "00"
  40. t[(1,0)] = "10"
  41. t[(0,1)] = "01"
  42. t[(1,1)] = "11"
  43. for x in 0..1:
  44. for y in 0..1:
  45. assert t[(x,y)] == $x & $y
  46. assert($t ==
  47. "{(x: 0, y: 1): \"01\", (x: 0, y: 0): \"00\", (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. assert t["123"] == 1.5
  57. try:
  58. echo t["111"] # deleted
  59. except KeyError:
  60. discard
  61. assert(not hasKey(t, "111"))
  62. assert "123" in t
  63. assert("111" notin t)
  64. for key, val in items(data): t[key] = val.toFloat
  65. for key, val in items(data): assert t[key] == val.toFloat
  66. assert(not t.hasKeyOrPut("456", 4.0)) # test absent key
  67. assert t.hasKeyOrPut("012", 3.0) # test present key
  68. var x = t.mgetOrPut("111", 1.5) # test absent key
  69. x = x * 2
  70. assert x == 3.0
  71. x = t.mgetOrPut("test", 1.5) # test present key
  72. x = x * 2
  73. assert 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): assert t[key] == val
  78. var i = 0
  79. # `pairs` needs to yield in insertion order:
  80. for key, val in pairs(t):
  81. assert key == data[i][0]
  82. assert val == data[i][1]
  83. inc(i)
  84. for key, val in mpairs(t): val = 99
  85. for val in mvalues(t): assert val == 99
  86. block orderedTableTest2:
  87. var
  88. s = initOrderedTable[string, int]()
  89. t = initOrderedTable[string, int]()
  90. assert s == t
  91. for key, val in items(data): t[key] = val
  92. assert s != t
  93. for key, val in items(sorteddata): s[key] = val
  94. assert s != t
  95. t.clear()
  96. assert s != t
  97. for key, val in items(sorteddata): t[key] = val
  98. assert 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: assert t[k] == 1
  104. t.inc("90", 3)
  105. t.inc("12", 2)
  106. t.inc("34", 1)
  107. assert t.largest()[0] == "90"
  108. t.sort()
  109. var i = 0
  110. for k, v in t.pairs:
  111. case i
  112. of 0: assert k == "90" and v == 4
  113. of 1: assert k == "12" and v == 3
  114. of 2: assert 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. assert s == t
  122. s.inc(1)
  123. assert s != t
  124. t.inc(2)
  125. assert s != t
  126. t.inc(1)
  127. assert s != t
  128. s.inc(2)
  129. assert s == t
  130. s.inc(1)
  131. assert s != t
  132. t.inc(1)
  133. assert 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. assert v == 9
  146. else:
  147. assert 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. assert testTable[nullHashKey] == value
  156. assert testTable.len == initialLen + 1
  157. testTable.del(nullHashKey)
  158. assert 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. assert t.len() != 0
  173. t.clear()
  174. assert t.len() == 0
  175. block clearOrderedTableTest:
  176. var t = data.toOrderedTable
  177. assert t.len() != 0
  178. t.clear()
  179. assert 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. assert t.len() != 0
  186. t.clear()
  187. assert 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. assert(v == 0)
  193. pairExists = true
  194. v = 42
  195. assert(t.mget(1) == 42)
  196. t.withKey(1) do (k: int, v: var int, pairExists: var bool):
  197. assert(v == 42)
  198. pairExists = false
  199. try:
  200. discard t.mget(1)
  201. assert(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. assert(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. assert(t.take("key", val))
  216. assert(val == 123)
  217. val = -1
  218. assert(not t.take("key", val))
  219. assert(val == -1)
  220. assert(not t.take("otherkey", val))
  221. assert(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): assert 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"