tintsets.nim 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import intsets
  2. import std/sets
  3. from sequtils import toSeq
  4. from algorithm import sorted
  5. proc sortedPairs[T](t: T): auto = toSeq(t.pairs).sorted
  6. template sortedItems(t: untyped): untyped = sorted(toSeq(t))
  7. block: # we use HashSet as groundtruth, it's well tested elsewhere
  8. template testDel(t, t0) =
  9. template checkEquals() =
  10. doAssert t.len == t0.len
  11. for k in t0:
  12. doAssert k in t
  13. for k in t:
  14. doAssert k in t0
  15. doAssert sortedItems(t) == sortedItems(t0)
  16. template incl2(i) =
  17. t.incl i
  18. t0.incl i
  19. template excl2(i) =
  20. t.excl i
  21. t0.excl i
  22. block:
  23. var expected: seq[int]
  24. let n = 100
  25. let n2 = n*2
  26. for i in 0..<n:
  27. incl2(i)
  28. checkEquals()
  29. for i in 0..<n:
  30. if i mod 3 == 0:
  31. if i < n div 2:
  32. excl2(i)
  33. else:
  34. t0.excl i
  35. doAssert i in t
  36. doAssert not t.missingOrExcl(i)
  37. checkEquals()
  38. for i in n..<n2:
  39. incl2(i)
  40. checkEquals()
  41. for i in 0..<n2:
  42. if i mod 7 == 0:
  43. excl2(i)
  44. checkEquals()
  45. # notin check
  46. for i in 0..<t.len:
  47. if i mod 7 == 0:
  48. doAssert i notin t0
  49. doAssert i notin t
  50. # issue #13505
  51. doAssert t.missingOrExcl(i)
  52. var t: IntSet
  53. var t0: HashSet[int]
  54. testDel(t, t0)