tlists.nim 7.0 KB

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