tsets_various.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. discard """
  2. output: '''
  3. set is empty
  4. '''
  5. """
  6. import sets, hashes
  7. block tsetpop:
  8. var a = initSet[int]()
  9. for i in 1..1000:
  10. a.incl(i)
  11. doAssert len(a) == 1000
  12. for i in 1..1000:
  13. discard a.pop()
  14. doAssert len(a) == 0
  15. try:
  16. echo a.pop()
  17. except KeyError as e:
  18. echo e.msg
  19. block tsets_lt:
  20. var s, s1: set[char]
  21. s = {'a'..'d'}
  22. s1 = {'a'..'c'}
  23. doAssert s1 < s
  24. doAssert s1 * s == {'a'..'c'}
  25. doAssert s1 <= s
  26. block tsets2:
  27. const
  28. data = [
  29. "34", "12",
  30. "90", "0",
  31. "1", "2",
  32. "3", "4",
  33. "5", "6",
  34. "7", "8",
  35. "9", "---00",
  36. "10", "11", "19",
  37. "20", "30", "40",
  38. "50", "60", "70",
  39. "80"]
  40. block tableTest1:
  41. var t = initSet[tuple[x, y: int]]()
  42. t.incl((0,0))
  43. t.incl((1,0))
  44. assert(not t.containsOrIncl((0,1)))
  45. t.incl((1,1))
  46. for x in 0..1:
  47. for y in 0..1:
  48. assert((x,y) in t)
  49. #assert($t ==
  50. # "{(x: 0, y: 0), (x: 0, y: 1), (x: 1, y: 0), (x: 1, y: 1)}")
  51. block setTest2:
  52. var t = initSet[string]()
  53. t.incl("test")
  54. t.incl("111")
  55. t.incl("123")
  56. t.excl("111")
  57. t.incl("012")
  58. t.incl("123") # test duplicates
  59. assert "123" in t
  60. assert "111" notin t # deleted
  61. assert t.missingOrExcl("000")
  62. assert "000" notin t
  63. assert t.missingOrExcl("012") == false
  64. assert "012" notin t
  65. assert t.containsOrIncl("012") == false
  66. assert t.containsOrIncl("012")
  67. assert "012" in t # added back
  68. for key in items(data): t.incl(key)
  69. for key in items(data): assert key in t
  70. for key in items(data): t.excl(key)
  71. for key in items(data): assert key notin t
  72. block orderedSetTest1:
  73. var t = data.toOrderedSet
  74. for key in items(data): assert key in t
  75. var i = 0
  76. # `items` needs to yield in insertion order:
  77. for key in items(t):
  78. assert key == data[i]
  79. inc(i)
  80. block tsets3:
  81. let
  82. s1: HashSet[int] = toSet([1, 2, 4, 8, 16])
  83. s2: HashSet[int] = toSet([1, 2, 3, 5, 8])
  84. s3: HashSet[int] = toSet([3, 5, 7])
  85. block union:
  86. let
  87. s1_s2 = union(s1, s2)
  88. s1_s3 = s1 + s3
  89. s2_s3 = s2 + s3
  90. assert s1_s2.len == 7
  91. assert s1_s3.len == 8
  92. assert s2_s3.len == 6
  93. for i in s1:
  94. assert i in s1_s2
  95. assert i in s1_s3
  96. for i in s2:
  97. assert i in s1_s2
  98. assert i in s2_s3
  99. for i in s3:
  100. assert i in s1_s3
  101. assert i in s2_s3
  102. assert((s1 + s1) == s1)
  103. assert((s2 + s1) == s1_s2)
  104. block intersection:
  105. let
  106. s1_s2 = intersection(s1, s2)
  107. s1_s3 = intersection(s1, s3)
  108. s2_s3 = s2 * s3
  109. assert s1_s2.len == 3
  110. assert s1_s3.len == 0
  111. assert s2_s3.len == 2
  112. for i in s1_s2:
  113. assert i in s1
  114. assert i in s2
  115. for i in s1_s3:
  116. assert i in s1
  117. assert i in s3
  118. for i in s2_s3:
  119. assert i in s2
  120. assert i in s3
  121. assert((s2 * s2) == s2)
  122. assert((s3 * s2) == s2_s3)
  123. block symmetricDifference:
  124. let
  125. s1_s2 = symmetricDifference(s1, s2)
  126. s1_s3 = s1 -+- s3
  127. s2_s3 = s2 -+- s3
  128. assert s1_s2.len == 4
  129. assert s1_s3.len == 8
  130. assert s2_s3.len == 4
  131. for i in s1:
  132. assert i in s1_s2 xor i in s2
  133. assert i in s1_s3 xor i in s3
  134. for i in s2:
  135. assert i in s1_s2 xor i in s1
  136. assert i in s2_s3 xor i in s3
  137. for i in s3:
  138. assert i in s1_s3 xor i in s1
  139. assert i in s2_s3 xor i in s2
  140. assert((s3 -+- s3) == initSet[int]())
  141. assert((s3 -+- s1) == s1_s3)
  142. block difference:
  143. let
  144. s1_s2 = difference(s1, s2)
  145. s1_s3 = difference(s1, s3)
  146. s2_s3 = s2 - s3
  147. assert s1_s2.len == 2
  148. assert s1_s3.len == 5
  149. assert s2_s3.len == 3
  150. for i in s1:
  151. assert i in s1_s2 xor i in s2
  152. assert i in s1_s3 xor i in s3
  153. for i in s2:
  154. assert i in s2_s3 xor i in s3
  155. assert((s2 - s2) == initSet[int]())
  156. block disjoint:
  157. assert(not disjoint(s1, s2))
  158. assert disjoint(s1, s3)
  159. assert(not disjoint(s2, s3))
  160. assert(not disjoint(s2, s2))