tdeque.nim 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import deques
  2. var deq = initDeque[int](1)
  3. deq.addLast(4)
  4. deq.addFirst(9)
  5. deq.addFirst(123)
  6. var first = deq.popFirst()
  7. deq.addLast(56)
  8. assert(deq.peekLast() == 56)
  9. deq.addLast(6)
  10. assert(deq.peekLast() == 6)
  11. var second = deq.popFirst()
  12. deq.addLast(789)
  13. assert(deq.peekLast() == 789)
  14. assert first == 123
  15. assert second == 9
  16. assert($deq == "[4, 56, 6, 789]")
  17. assert deq == [4, 56, 6, 789].toDeque
  18. assert deq[0] == deq.peekFirst and deq.peekFirst == 4
  19. #assert deq[^1] == deq.peekLast and deq.peekLast == 789
  20. deq[0] = 42
  21. deq[deq.len - 1] = 7
  22. assert 6 in deq and 789 notin deq
  23. assert deq.find(6) >= 0
  24. assert deq.find(789) < 0
  25. block:
  26. var d = initDeque[int](1)
  27. d.addLast 7
  28. d.addLast 8
  29. d.addLast 10
  30. d.addFirst 5
  31. d.addFirst 2
  32. d.addFirst 1
  33. d.addLast 20
  34. d.shrink(fromLast = 2)
  35. doAssert($d == "[1, 2, 5, 7, 8]")
  36. d.shrink(2, 1)
  37. doAssert($d == "[5, 7]")
  38. d.shrink(2, 2)
  39. doAssert d.len == 0
  40. for i in -2 .. 10:
  41. if i in deq:
  42. assert deq.contains(i) and deq.find(i) >= 0
  43. else:
  44. assert(not deq.contains(i) and deq.find(i) < 0)
  45. when compileOption("boundChecks"):
  46. try:
  47. echo deq[99]
  48. assert false
  49. except IndexDefect:
  50. discard
  51. try:
  52. assert deq.len == 4
  53. for i in 0 ..< 5: deq.popFirst()
  54. assert false
  55. except IndexDefect:
  56. discard
  57. # grabs some types of resize error.
  58. deq = initDeque[int]()
  59. for i in 1 .. 4: deq.addLast i
  60. deq.popFirst()
  61. deq.popLast()
  62. for i in 5 .. 8: deq.addFirst i
  63. assert $deq == "[8, 7, 6, 5, 2, 3]"
  64. # Similar to proc from the documentation example
  65. proc foo(a, b: Positive) = # assume random positive values for `a` and `b`.
  66. var deq = initDeque[int]()
  67. assert deq.len == 0
  68. for i in 1 .. a: deq.addLast i
  69. if b < deq.len: # checking before indexed access.
  70. assert deq[b] == b + 1
  71. # The following two lines don't need any checking on access due to the logic
  72. # of the program, but that would not be the case if `a` could be 0.
  73. assert deq.peekFirst == 1
  74. assert deq.peekLast == a
  75. while deq.len > 0: # checking if the deque is empty
  76. assert deq.popFirst() > 0
  77. #foo(0,0)
  78. foo(8, 5)
  79. foo(10, 9)
  80. foo(1, 1)
  81. foo(2, 1)
  82. foo(1, 5)
  83. foo(3, 2)
  84. import sets
  85. block t13310:
  86. proc main() =
  87. var q = initDeque[HashSet[int16]](2)
  88. q.addFirst([1'i16].toHashSet)
  89. q.addFirst([2'i16].toHashSet)
  90. q.addFirst([3'i16].toHashSet)
  91. assert $q == "[{3}, {2}, {1}]"
  92. static:
  93. main()