tpackedsets.nim 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. """
  4. import std/packedsets
  5. import std/sets
  6. import sequtils
  7. import algorithm
  8. import std/assertions
  9. block basicIntSetTests:
  10. var y = initPackedSet[int]()
  11. y.incl(1)
  12. y.incl(2)
  13. y.incl(7)
  14. y.incl(1056)
  15. y.incl(1044)
  16. y.excl(1044)
  17. doAssert y == [1, 2, 7, 1056].toPackedSet
  18. doAssert toSeq(y.items) == [1, 2, 7, 1056]
  19. doAssert y.containsOrIncl(888) == false
  20. doAssert 888 in y
  21. doAssert y.containsOrIncl(888) == true
  22. doAssert y.missingOrExcl(888) == false
  23. doAssert 888 notin y
  24. doAssert y.missingOrExcl(888) == true
  25. proc sortedPairs[T](t: T): auto = toSeq(t.pairs).sorted
  26. template sortedItems(t: untyped): untyped = sorted(toSeq(t))
  27. type Id = distinct int
  28. proc `$`(x: Id): string {.borrow.}
  29. proc cmp(a: Id, b: Id): int {.borrow.}
  30. proc `==`(a: Id, b: Id): bool {.borrow.}
  31. proc `<`(a: Id, b: Id): bool {.borrow.}
  32. block genericTests:
  33. # we use HashSet as groundtruth, it's well tested elsewhere
  34. template testDel(A: typedesc, t: typed, t0: typed) =
  35. block:
  36. template checkEquals() =
  37. doAssert t.len == t0.len
  38. for k in t0:
  39. doAssert k in t
  40. for k in t:
  41. doAssert k in t0
  42. doAssert sortedItems(t) == sortedItems(t0)
  43. template incl2(i) =
  44. t.incl i
  45. t0.incl i
  46. template excl2(i) =
  47. t.excl i
  48. t0.excl i
  49. var expected: seq[A]
  50. let n = 100
  51. let n2 = n*2
  52. for i in 0..<n:
  53. incl2(A(i))
  54. checkEquals()
  55. for i in 0..<n:
  56. if i mod 3 == 0:
  57. if i < n div 2:
  58. excl2(A(i))
  59. else:
  60. t0.excl A(i)
  61. doAssert A(i) in t
  62. doAssert not t.missingOrExcl A(i)
  63. checkEquals()
  64. for i in n..<n2:
  65. incl2(A(i))
  66. checkEquals()
  67. for i in 0..<n2:
  68. if i mod 7 == 0:
  69. excl2(A(i))
  70. checkEquals()
  71. # notin check
  72. for i in 0..<t.len:
  73. if i mod 7 == 0:
  74. doAssert A(i) notin t0
  75. doAssert A(i) notin t
  76. # issue #13505
  77. doAssert t.missingOrExcl(A(i))
  78. var t: PackedSet[int]
  79. var t0: HashSet[int]
  80. testDel(int, t, t0)
  81. var distT: PackedSet[Id]
  82. var distT0: HashSet[Id]
  83. testDel(Id, distT, distT0)
  84. doAssert union(distT, initPackedSet[Id]()) == distT
  85. var charT: PackedSet[char]
  86. var charT0: HashSet[char]
  87. testDel(char, charT, charT0)
  88. block typeSafetyTest:
  89. # mixing sets of different types shouldn't compile
  90. doAssert not compiles( union(initPackedSet[Id](), initPackedSet[int]()) )
  91. doAssert compiles( union(initPackedSet[Id](), initPackedSet[Id]()))
  92. var ids: PackedSet[Id]
  93. doAssert not compiles( ids.incl(3) )
  94. doAssert compiles( ids.incl(Id(3)) )
  95. type NonOrdinal = string
  96. doAssert not compiles( initPackedSet[NonOrdinal]() )
  97. type EnumABCD = enum A, B, C, D
  98. block enumTest:
  99. var letterSet = initPackedSet[EnumABCD]()
  100. for x in [A, C]:
  101. letterSet.incl(x)
  102. doAssert A in letterSet
  103. doAssert B notin letterSet
  104. doAssert C in letterSet
  105. doAssert D notin letterSet
  106. type Foo = distinct int16
  107. proc `$`(a: Foo): string {.borrow.} # `echo a` below won't work without `$` defined, as expected
  108. block printTest:
  109. var a = initPackedSet[EnumABCD]()
  110. a.incl A
  111. a.incl C
  112. doAssert $a == "{A, C}"
  113. import intsets
  114. block legacyMainModuleTests:
  115. template genericTests(A: typedesc[Ordinal], x: typed) =
  116. block:
  117. proc typSeq(s: seq[int]): seq[A] = s.map(proc (i: int): A = A(i))
  118. x.incl(A(1))
  119. x.incl(A(2))
  120. x.incl(A(7))
  121. x.incl(A(1056))
  122. x.incl(A(1044))
  123. x.excl(A(1044))
  124. doAssert x == typSeq(@[1, 2, 7, 1056]).toPackedSet
  125. doAssert x.containsOrIncl(A(888)) == false
  126. doAssert A(888) in x
  127. doAssert x.containsOrIncl(A(888)) == true
  128. doAssert x.missingOrExcl(A(888)) == false
  129. doAssert A(888) notin x
  130. doAssert x.missingOrExcl(A(888)) == true
  131. var xs = toSeq(items(x))
  132. xs.sort(cmp[A])
  133. doAssert xs == typSeq(@[1, 2, 7, 1056])
  134. var y: PackedSet[A]
  135. assign(y, x)
  136. var ys = toSeq(items(y))
  137. ys.sort(cmp[A])
  138. doAssert ys == typSeq(@[1, 2, 7, 1056])
  139. doAssert x == y
  140. var z: PackedSet[A]
  141. for i in 0..1000:
  142. incl z, A(i)
  143. doAssert z.len() == i+1
  144. for i in 0..1000:
  145. doAssert z.contains(A(i))
  146. var w = initPackedSet[A]()
  147. w.incl(A(1))
  148. w.incl(A(4))
  149. w.incl(A(50))
  150. w.incl(A(1001))
  151. w.incl(A(1056))
  152. var xuw = x.union(w)
  153. var xuws = toSeq(items(xuw))
  154. xuws.sort(cmp)
  155. doAssert xuws == typSeq(@[1, 2, 4, 7, 50, 1001, 1056])
  156. var xiw = x.intersection(w)
  157. var xiws = toSeq(items(xiw))
  158. xiws.sort(cmp)
  159. doAssert xiws == @[A(1), A(1056)]
  160. var xdw = x.difference(w)
  161. var xdws = toSeq(items(xdw))
  162. xdws.sort(cmp[A])
  163. doAssert xdws == @[A(2), A(7)]
  164. var xsw = x.symmetricDifference(w)
  165. var xsws = toSeq(items(xsw))
  166. xsws.sort(cmp[A])
  167. doAssert xsws == typSeq(@[2, 4, 7, 50, 1001])
  168. x.incl(w)
  169. xs = toSeq(items(x))
  170. xs.sort(cmp[A])
  171. doAssert xs == typSeq(@[1, 2, 4, 7, 50, 1001, 1056])
  172. doAssert w <= x
  173. doAssert w < x
  174. doAssert(not disjoint(w, x))
  175. var u = initPackedSet[A]()
  176. u.incl(A(3))
  177. u.incl(A(5))
  178. u.incl(A(500))
  179. doAssert disjoint(u, x)
  180. var v = initPackedSet[A]()
  181. v.incl(A(2))
  182. v.incl(A(50))
  183. x.excl(v)
  184. xs = toSeq(items(x))
  185. xs.sort(cmp[A])
  186. doAssert xs == typSeq(@[1, 4, 7, 1001, 1056])
  187. proc bug12366 =
  188. var
  189. x = initPackedSet[A]()
  190. y = initPackedSet[A]()
  191. n = 3584
  192. for i in 0..n:
  193. x.incl(A(i))
  194. y.incl(A(i))
  195. let z = symmetricDifference(x, y)
  196. doAssert z.len == 0
  197. doAssert $z == "{}"
  198. bug12366()
  199. var legacyInit = initIntSet()
  200. genericTests(int, legacyInit)
  201. var intGenericInit = initPackedSet[int]()
  202. genericTests(int, intGenericInit)
  203. var intDistinct = initPackedSet[Id]()
  204. genericTests(Id, intDistinct)