tdeques.nim 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. targets: "c cpp js"
  4. """
  5. import std/deques
  6. from std/sequtils import toSeq
  7. import std/assertions
  8. block:
  9. proc index(self: Deque[int], idx: Natural): int =
  10. self[idx]
  11. proc main =
  12. var testDeque = initDeque[int]()
  13. testDeque.addFirst(1)
  14. doAssert testDeque.index(0) == 1
  15. main()
  16. block:
  17. var d = initDeque[int]()
  18. d.addLast(1)
  19. doAssert $d == "[1]"
  20. block:
  21. var d = initDeque[string]()
  22. d.addLast("1")
  23. doAssert $d == """["1"]"""
  24. block:
  25. var d = initDeque[char]()
  26. d.addLast('1')
  27. doAssert $d == "['1']"
  28. block:
  29. var deq = initDeque[int](1)
  30. deq.addLast(4)
  31. deq.addFirst(9)
  32. deq.addFirst(123)
  33. var first = deq.popFirst()
  34. deq.addLast(56)
  35. doAssert(deq.peekLast() == 56)
  36. deq.addLast(6)
  37. doAssert(deq.peekLast() == 6)
  38. var second = deq.popFirst()
  39. deq.addLast(789)
  40. doAssert(deq.peekLast() == 789)
  41. doAssert first == 123
  42. doAssert second == 9
  43. doAssert($deq == "[4, 56, 6, 789]")
  44. doAssert deq == [4, 56, 6, 789].toDeque
  45. doAssert deq[0] == deq.peekFirst and deq.peekFirst == 4
  46. #doAssert deq[^1] == deq.peekLast and deq.peekLast == 789
  47. deq[0] = 42
  48. deq[deq.len - 1] = 7
  49. doAssert 6 in deq and 789 notin deq
  50. doAssert deq.find(6) >= 0
  51. doAssert deq.find(789) < 0
  52. block:
  53. var d = initDeque[int](1)
  54. d.addLast 7
  55. d.addLast 8
  56. d.addLast 10
  57. d.addFirst 5
  58. d.addFirst 2
  59. d.addFirst 1
  60. d.addLast 20
  61. d.shrink(fromLast = 2)
  62. doAssert($d == "[1, 2, 5, 7, 8]")
  63. d.shrink(2, 1)
  64. doAssert($d == "[5, 7]")
  65. d.shrink(2, 2)
  66. doAssert d.len == 0
  67. for i in -2 .. 10:
  68. if i in deq:
  69. doAssert deq.contains(i) and deq.find(i) >= 0
  70. else:
  71. doAssert(not deq.contains(i) and deq.find(i) < 0)
  72. when compileOption("boundChecks"):
  73. try:
  74. echo deq[99]
  75. doAssert false
  76. except IndexDefect:
  77. discard
  78. try:
  79. doAssert deq.len == 4
  80. for i in 0 ..< 5: deq.popFirst()
  81. doAssert false
  82. except IndexDefect:
  83. discard
  84. # grabs some types of resize error.
  85. deq = initDeque[int]()
  86. for i in 1 .. 4: deq.addLast i
  87. deq.popFirst()
  88. deq.popLast()
  89. for i in 5 .. 8: deq.addFirst i
  90. doAssert $deq == "[8, 7, 6, 5, 2, 3]"
  91. # Similar to proc from the documentation example
  92. proc foo(a, b: Positive) = # assume random positive values for `a` and `b`.
  93. var deq = initDeque[int]()
  94. doAssert deq.len == 0
  95. for i in 1 .. a: deq.addLast i
  96. if b < deq.len: # checking before indexed access.
  97. doAssert deq[b] == b + 1
  98. # The following two lines don't need any checking on access due to the logic
  99. # of the program, but that would not be the case if `a` could be 0.
  100. doAssert deq.peekFirst == 1
  101. doAssert deq.peekLast == a
  102. while deq.len > 0: # checking if the deque is empty
  103. doAssert deq.popFirst() > 0
  104. #foo(0,0)
  105. foo(8, 5)
  106. foo(10, 9)
  107. foo(1, 1)
  108. foo(2, 1)
  109. foo(1, 5)
  110. foo(3, 2)
  111. import std/sets
  112. block t13310:
  113. proc main() =
  114. var q = initDeque[HashSet[int16]](2)
  115. q.addFirst([1'i16].toHashSet)
  116. q.addFirst([2'i16].toHashSet)
  117. q.addFirst([3'i16].toHashSet)
  118. doAssert $q == "[{3}, {2}, {1}]"
  119. static:
  120. main()
  121. proc main() =
  122. block:
  123. let a = [10, 20, 30].toDeque
  124. doAssert toSeq(a.pairs) == @[(0, 10), (1, 20), (2, 30)]
  125. block:
  126. let q = [7, 9].toDeque
  127. doAssert 7 in q
  128. doAssert q.contains(7)
  129. doAssert 8 notin q
  130. block:
  131. let a = [10, 20, 30, 40, 50].toDeque
  132. doAssert $a == "[10, 20, 30, 40, 50]"
  133. doAssert a.peekFirst == 10
  134. doAssert len(a) == 5
  135. block:
  136. let a = [10, 20, 30, 40, 50].toDeque
  137. doAssert $a == "[10, 20, 30, 40, 50]"
  138. doAssert a.peekLast == 50
  139. doAssert len(a) == 5
  140. block:
  141. var a = [10, 20, 30, 40, 50].toDeque
  142. doAssert $a == "[10, 20, 30, 40, 50]"
  143. doAssert a.popFirst == 10
  144. doAssert $a == "[20, 30, 40, 50]"
  145. block:
  146. var a = [10, 20, 30, 40, 50].toDeque
  147. doAssert $a == "[10, 20, 30, 40, 50]"
  148. doAssert a.popLast == 50
  149. doAssert $a == "[10, 20, 30, 40]"
  150. block:
  151. var a = [10, 20, 30, 40, 50].toDeque
  152. doAssert $a == "[10, 20, 30, 40, 50]"
  153. clear(a)
  154. doAssert len(a) == 0
  155. block: # bug #21278
  156. var a = [10, 20, 30, 40].toDeque
  157. a.shrink(fromFirst = 0, fromLast = 1)
  158. doAssert $a == "[10, 20, 30]"
  159. block:
  160. var a, b: Deque[int] = initDeque[int]()
  161. for i in 1 .. 256:
  162. a.addLast(i)
  163. for i in 1 .. 255:
  164. a.popLast
  165. b.addLast(1)
  166. doAssert a == b
  167. block:
  168. # Issue 23275
  169. # Test `==`.
  170. block:
  171. var a, b = initDeque[int]()
  172. doAssert a == b
  173. doAssert a.hash == b.hash
  174. a.addFirst(1)
  175. doAssert a != b
  176. doAssert a.hash != b.hash
  177. b.addLast(1)
  178. doAssert a == b
  179. doAssert a.hash == b.hash
  180. a.popFirst
  181. b.popLast
  182. doAssert a == b
  183. doAssert a.hash == b.hash
  184. a.addLast 2
  185. doAssert a != b
  186. doAssert a.hash != b.hash
  187. b.addFirst 2
  188. doAssert a == b
  189. doAssert a.hash == b.hash
  190. block:
  191. var a, b = initDeque[int]()
  192. for i in countdown(100, 1):
  193. a.addFirst(i)
  194. for i in 1..100:
  195. b.addLast(i)
  196. doAssert a == b
  197. for i in 1..99:
  198. a.popLast
  199. let a1 = [1].toDeque
  200. doAssert a == a1
  201. doAssert a.hash == a1.hash
  202. var c = initDeque[int]()
  203. c.addLast(1)
  204. doAssert a == c
  205. doAssert a.hash == c.hash
  206. static: main()
  207. main()