tdeques.nim 4.0 KB

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