tsets_various.nim 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. import std/[sets, hashes]
  5. from std/sequtils import toSeq
  6. from std/algorithm import sorted
  7. from stdtest/testutils import whenVMorJs
  8. proc sortedPairs[T](t: T): auto = toSeq(t.pairs).sorted
  9. template sortedItems(t: untyped): untyped = sorted(toSeq(t))
  10. block tsetpop:
  11. var a = initHashSet[int]()
  12. for i in 1..1000:
  13. a.incl(i)
  14. doAssert len(a) == 1000
  15. for i in 1..1000:
  16. discard a.pop()
  17. doAssert len(a) == 0
  18. var msg = ""
  19. try:
  20. echo a.pop()
  21. except KeyError as e:
  22. msg = e.msg
  23. doAssert msg == "set is empty"
  24. block tsets_lt:
  25. var s, s1: set[char]
  26. s = {'a'..'d'}
  27. s1 = {'a'..'c'}
  28. doAssert s1 < s
  29. doAssert s1 * s == {'a'..'c'}
  30. doAssert s1 <= s
  31. block tsets2:
  32. const
  33. data = [
  34. "34", "12",
  35. "90", "0",
  36. "1", "2",
  37. "3", "4",
  38. "5", "6",
  39. "7", "8",
  40. "9", "---00",
  41. "10", "11", "19",
  42. "20", "30", "40",
  43. "50", "60", "70",
  44. "80"]
  45. block tableTest1:
  46. var t = initHashSet[tuple[x, y: int]]()
  47. t.incl((0,0))
  48. t.incl((1,0))
  49. doAssert(not t.containsOrIncl((0,1)))
  50. t.incl((1,1))
  51. for x in 0..1:
  52. for y in 0..1:
  53. doAssert((x,y) in t)
  54. #doAssert($t ==
  55. # "{(x: 0, y: 0), (x: 0, y: 1), (x: 1, y: 0), (x: 1, y: 1)}")
  56. block setTest2:
  57. var t = initHashSet[string]()
  58. t.incl("test")
  59. t.incl("111")
  60. t.incl("123")
  61. t.excl("111")
  62. t.incl("012")
  63. t.incl("123") # test duplicates
  64. doAssert "123" in t
  65. doAssert "111" notin t # deleted
  66. doAssert t.missingOrExcl("000")
  67. doAssert "000" notin t
  68. doAssert t.missingOrExcl("012") == false
  69. doAssert "012" notin t
  70. doAssert t.containsOrIncl("012") == false
  71. doAssert t.containsOrIncl("012")
  72. doAssert "012" in t # added back
  73. for key in items(data): t.incl(key)
  74. for key in items(data): doAssert key in t
  75. for key in items(data): t.excl(key)
  76. for key in items(data): doAssert key notin t
  77. block orderedSetTest1:
  78. var t = data.toOrderedSet
  79. for key in items(data): doAssert key in t
  80. var i = 0
  81. # `items` needs to yield in insertion order:
  82. for key in items(t):
  83. doAssert key == data[i]
  84. inc(i)
  85. block tsets3:
  86. let
  87. s1: HashSet[int] = toHashSet([1, 2, 4, 8, 16])
  88. s2: HashSet[int] = toHashSet([1, 2, 3, 5, 8])
  89. s3: HashSet[int] = toHashSet([3, 5, 7])
  90. block union:
  91. let
  92. s1_s2 = union(s1, s2)
  93. s1_s3 = s1 + s3
  94. s2_s3 = s2 + s3
  95. doAssert s1_s2.len == 7
  96. doAssert s1_s3.len == 8
  97. doAssert s2_s3.len == 6
  98. for i in s1:
  99. doAssert i in s1_s2
  100. doAssert i in s1_s3
  101. for i in s2:
  102. doAssert i in s1_s2
  103. doAssert i in s2_s3
  104. for i in s3:
  105. doAssert i in s1_s3
  106. doAssert i in s2_s3
  107. doAssert((s1 + s1) == s1)
  108. doAssert((s2 + s1) == s1_s2)
  109. block intersection:
  110. let
  111. s1_s2 = intersection(s1, s2)
  112. s1_s3 = intersection(s1, s3)
  113. s2_s3 = s2 * s3
  114. doAssert s1_s2.len == 3
  115. doAssert s1_s3.len == 0
  116. doAssert s2_s3.len == 2
  117. for i in s1_s2:
  118. doAssert i in s1
  119. doAssert i in s2
  120. for i in s1_s3:
  121. doAssert i in s1
  122. doAssert i in s3
  123. for i in s2_s3:
  124. doAssert i in s2
  125. doAssert i in s3
  126. doAssert((s2 * s2) == s2)
  127. doAssert((s3 * s2) == s2_s3)
  128. block symmetricDifference:
  129. let
  130. s1_s2 = symmetricDifference(s1, s2)
  131. s1_s3 = s1 -+- s3
  132. s2_s3 = s2 -+- s3
  133. doAssert s1_s2.len == 4
  134. doAssert s1_s3.len == 8
  135. doAssert s2_s3.len == 4
  136. for i in s1:
  137. doAssert i in s1_s2 xor i in s2
  138. doAssert i in s1_s3 xor i in s3
  139. for i in s2:
  140. doAssert i in s1_s2 xor i in s1
  141. doAssert i in s2_s3 xor i in s3
  142. for i in s3:
  143. doAssert i in s1_s3 xor i in s1
  144. doAssert i in s2_s3 xor i in s2
  145. doAssert((s3 -+- s3) == initHashSet[int]())
  146. doAssert((s3 -+- s1) == s1_s3)
  147. block difference:
  148. let
  149. s1_s2 = difference(s1, s2)
  150. s1_s3 = difference(s1, s3)
  151. s2_s3 = s2 - s3
  152. doAssert s1_s2.len == 2
  153. doAssert s1_s3.len == 5
  154. doAssert s2_s3.len == 3
  155. for i in s1:
  156. doAssert i in s1_s2 xor i in s2
  157. doAssert i in s1_s3 xor i in s3
  158. for i in s2:
  159. doAssert i in s2_s3 xor i in s3
  160. doAssert((s2 - s2) == initHashSet[int]())
  161. block disjoint:
  162. doAssert(not disjoint(s1, s2))
  163. doAssert disjoint(s1, s3)
  164. doAssert(not disjoint(s2, s3))
  165. doAssert(not disjoint(s2, s2))
  166. block: # https://github.com/nim-lang/Nim/issues/13496
  167. template testDel(body) =
  168. block:
  169. body
  170. t.incl(15)
  171. t.incl(19)
  172. t.incl(17)
  173. t.incl(150)
  174. t.excl(150)
  175. doAssert t.len == 3
  176. doAssert sortedItems(t) == @[15, 17, 19]
  177. var s = newSeq[int]()
  178. for v in t: s.add(v)
  179. doAssert s.len == 3
  180. doAssert sortedItems(s) == @[15, 17, 19]
  181. when t is OrderedSet:
  182. doAssert sortedPairs(t) == @[(a: 0, b: 15), (a: 1, b: 19), (a: 2, b: 17)]
  183. doAssert toSeq(t) == @[15, 19, 17]
  184. testDel(): (var t: HashSet[int])
  185. testDel(): (var t: OrderedSet[int])
  186. block: # test correctness after a number of inserts/deletes
  187. template testDel(body) =
  188. block:
  189. body
  190. var expected: seq[int]
  191. let n = 100
  192. let n2 = n*2
  193. for i in 0..<n:
  194. t.incl(i)
  195. for i in 0..<n:
  196. if i mod 3 == 0:
  197. t.excl(i)
  198. for i in n..<n2:
  199. t.incl(i)
  200. for i in 0..<n2:
  201. if i mod 7 == 0:
  202. t.excl(i)
  203. for i in 0..<n2:
  204. if (i>=n or i mod 3 != 0) and i mod 7 != 0:
  205. expected.add i
  206. for i in expected: doAssert i in t
  207. doAssert t.len == expected.len
  208. doAssert sortedItems(t) == expected
  209. testDel(): (var t: HashSet[int])
  210. testDel(): (var t: OrderedSet[int])
  211. template main() =
  212. # xxx move all tests inside this
  213. block:
  214. let a = {true, false}
  215. doAssert $a == "{false, true}"
  216. doAssert a.len == 2
  217. block:
  218. let a = {false .. true}
  219. doAssert $a == "{false, true}"
  220. doAssert a.len == 2
  221. block:
  222. let a = {false .. false}
  223. doAssert $a == "{false}"
  224. doAssert a.len == 1
  225. block: # bug #16123
  226. whenVMorJs: discard
  227. do:
  228. type CallType = proc() {.closure.}
  229. var setA = initHashSet[CallType]()
  230. let foo = proc() = discard
  231. setA.incl(foo)
  232. doAssert setA.contains(foo)
  233. static: main()
  234. main()