tlists.nim 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. discard """
  2. targets: "c js"
  3. """
  4. import std/[lists, sequtils]
  5. const
  6. data = [1, 2, 3, 4, 5, 6]
  7. template main =
  8. block SinglyLinkedListTest1:
  9. var L: SinglyLinkedList[int]
  10. for d in items(data): L.prepend(d)
  11. for d in items(data): L.add(d)
  12. doAssert($L == "[6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6]")
  13. doAssert(4 in L)
  14. block SinglyLinkedListTest2:
  15. var L: SinglyLinkedList[string]
  16. for d in items(data): L.prepend($d)
  17. doAssert($L == """["6", "5", "4", "3", "2", "1"]""")
  18. doAssert("4" in L)
  19. block DoublyLinkedListTest1:
  20. var L: DoublyLinkedList[int]
  21. for d in items(data): L.prepend(d)
  22. for d in items(data): L.add(d)
  23. L.remove(L.find(1))
  24. doAssert($L == "[6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6]")
  25. doAssert(4 in L)
  26. block SinglyLinkedRingTest1:
  27. var L: SinglyLinkedRing[int]
  28. L.prepend(4)
  29. doAssert($L == "[4]")
  30. L.prepend(4)
  31. doAssert($L == "[4, 4]")
  32. doAssert(4 in L)
  33. block DoublyLinkedRingTest1:
  34. var L: DoublyLinkedRing[int]
  35. L.prepend(4)
  36. doAssert($L == "[4]")
  37. L.prepend(4)
  38. doAssert($L == "[4, 4]")
  39. doAssert(4 in L)
  40. L.add(3)
  41. L.add(5)
  42. doAssert($L == "[4, 4, 3, 5]")
  43. L.remove(L.find(3))
  44. L.remove(L.find(5))
  45. L.remove(L.find(4))
  46. L.remove(L.find(4))
  47. doAssert($L == "[]")
  48. doAssert(4 notin L)
  49. block tlistsToString:
  50. block:
  51. var l = initDoublyLinkedList[int]()
  52. l.add(1)
  53. l.add(2)
  54. l.add(3)
  55. doAssert $l == "[1, 2, 3]"
  56. block:
  57. var l = initDoublyLinkedList[string]()
  58. l.add("1")
  59. l.add("2")
  60. l.add("3")
  61. doAssert $l == """["1", "2", "3"]"""
  62. block:
  63. var l = initDoublyLinkedList[char]()
  64. l.add('1')
  65. l.add('2')
  66. l.add('3')
  67. doAssert $l == """['1', '2', '3']"""
  68. # Copied here until it is merged into sequtils
  69. template take(a: untyped, max: int): untyped =
  70. type T = typeof(block: (for ai in a: ai))
  71. var ret: seq[T]
  72. var i = 0
  73. if max > 0:
  74. for ai in a:
  75. ret.add ai
  76. i.inc
  77. if i >= max: break
  78. ret
  79. template testCommon(initList, toList) =
  80. block: # toSinglyLinkedList, toDoublyLinkedList
  81. let l = seq[int].default
  82. doAssert l.toList.toSeq == []
  83. doAssert [1].toList.toSeq == [1]
  84. doAssert [1, 2, 3].toList.toSeq == [1, 2, 3]
  85. block copy:
  86. doAssert array[0, int].default.toList.copy.toSeq == []
  87. doAssert [1].toList.copy.toSeq == [1]
  88. doAssert [1, 2].toList.copy.toSeq == [1, 2]
  89. doAssert [1, 2, 3].toList.copy.toSeq == [1, 2, 3]
  90. type Foo = ref object
  91. x: int
  92. var f0 = Foo(x: 0)
  93. let f1 = Foo(x: 1)
  94. var a = [f0].toList
  95. var b = a.copy
  96. b.add f1
  97. doAssert a.toSeq == [f0]
  98. doAssert b.toSeq == [f0, f1]
  99. f0.x = 42
  100. doAssert a.head.value.x == 42
  101. doAssert b.head.value.x == 42
  102. block: # add, addMoved
  103. block:
  104. var
  105. l0 = initList[int]()
  106. l1 = [1].toList
  107. l2 = [2, 3].toList
  108. l3 = [4, 5, 6].toList
  109. l0.add l3
  110. l1.add l3
  111. l2.addMoved l3
  112. doAssert l0.toSeq == [4, 5, 6]
  113. doAssert l1.toSeq == [1, 4, 5, 6]
  114. doAssert l2.toSeq == [2, 3, 4, 5, 6]
  115. doAssert l3.toSeq == []
  116. l2.add l3 # re-adding l3 that was destroyed is now a no-op
  117. doAssert l2.toSeq == [2, 3, 4, 5, 6]
  118. doAssert l3.toSeq == []
  119. block:
  120. var
  121. l0 = initList[int]()
  122. l1 = [1].toList
  123. l2 = [2, 3].toList
  124. l3 = [4, 5, 6].toList
  125. l3.addMoved l0
  126. l2.addMoved l1
  127. doAssert l3.toSeq == [4, 5, 6]
  128. doAssert l2.toSeq == [2, 3, 1]
  129. l3.add l0
  130. doAssert l3.toSeq == [4, 5, 6]
  131. block:
  132. var c = [0, 1].toList
  133. c.addMoved c
  134. doAssert c.take(6) == [0, 1, 0, 1, 0, 1]
  135. block: # prepend, prependMoved
  136. block:
  137. var
  138. l0 = initList[int]()
  139. l1 = [1].toList
  140. l2 = [2, 3].toList
  141. l3 = [4, 5, 6].toList
  142. l0.prepend l3
  143. l1.prepend l3
  144. doAssert l3.toSeq == [4, 5, 6]
  145. l2.prependMoved l3
  146. doAssert l0.toSeq == [4, 5, 6]
  147. doAssert l1.toSeq == [4, 5, 6, 1]
  148. doAssert l2.toSeq == [4, 5, 6, 2, 3]
  149. doAssert l3.toSeq == []
  150. l2.prepend l3 # re-prepending l3 that was destroyed is now a no-op
  151. doAssert l2.toSeq == [4, 5, 6, 2, 3]
  152. doAssert l3.toSeq == []
  153. block:
  154. var
  155. l0 = initList[int]()
  156. l1 = [1].toList
  157. l2 = [2, 3].toList
  158. l3 = [4, 5, 6].toList
  159. l3.prependMoved l0
  160. l2.prependMoved l1
  161. doAssert l3.toSeq == [4, 5, 6]
  162. doAssert l2.toSeq == [1, 2, 3]
  163. l3.prepend l0
  164. doAssert l3.toSeq == [4, 5, 6]
  165. block:
  166. var c = [0, 1].toList
  167. c.prependMoved c
  168. doAssert c.take(6) == [0, 1, 0, 1, 0, 1]
  169. block remove:
  170. var l = [0, 1, 2, 3].toList
  171. let
  172. l0 = l.head
  173. l1 = l0.next
  174. l2 = l1.next
  175. l3 = l2.next
  176. l.remove l0
  177. doAssert l.toSeq == [1, 2, 3]
  178. l.remove l2
  179. doAssert l.toSeq == [1, 3]
  180. l.remove l2
  181. doAssert l.toSeq == [1, 3]
  182. l.remove l3
  183. doAssert l.toSeq == [1]
  184. l.remove l1
  185. doAssert l.toSeq == []
  186. # Cycle preservation
  187. var a = [10, 11, 12].toList
  188. a.addMoved a
  189. doAssert a.take(6) == @[10, 11, 12, 10, 11, 12]
  190. a.remove a.head.next
  191. doAssert a.take(6) == @[10, 12, 10, 12, 10, 12]
  192. a.remove a.head
  193. doAssert a.take(6) == @[12, 12, 12, 12, 12, 12]
  194. testCommon initSinglyLinkedList, toSinglyLinkedList
  195. testCommon initDoublyLinkedList, toDoublyLinkedList
  196. block remove: # return value check
  197. var l = [0, 1, 2, 3].toSinglyLinkedList
  198. let n = l.head.next.next
  199. doAssert l.remove(n) == true
  200. doAssert l.toSeq == [0, 1, 3]
  201. doAssert l.remove(n) == false
  202. doAssert l.toSeq == [0, 1, 3]
  203. doAssert l.remove(l.head) == true
  204. doAssert l.toSeq == [1, 3]
  205. doAssert l.remove(l.head.next) == true
  206. doAssert l.toSeq == [1]
  207. doAssert l.remove(l.head) == true
  208. doAssert l.toSeq == []
  209. static: main()
  210. main()