tsequtils.nim 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. discard """
  2. targets: "c js"
  3. """
  4. # xxx move all tests under `main`
  5. import std/sequtils
  6. import strutils
  7. from algorithm import sorted
  8. import std/assertions
  9. {.experimental: "strictEffects".}
  10. {.push warningAsError[Effect]: on.}
  11. {.experimental: "strictFuncs".}
  12. # helper for testing double substitution side effects which are handled
  13. # by `evalOnceAs`
  14. var counter = 0
  15. proc identity[T](a: T): auto =
  16. counter.inc
  17. a
  18. block: # concat test
  19. let
  20. s1 = @[1, 2, 3]
  21. s2 = @[4, 5]
  22. s3 = @[6, 7]
  23. total = concat(s1, s2, s3)
  24. doAssert total == @[1, 2, 3, 4, 5, 6, 7]
  25. block: # count test
  26. let
  27. s1 = @[1, 2, 3, 2]
  28. s2 = @['a', 'b', 'x', 'a']
  29. a1 = [1, 2, 3, 2]
  30. a2 = ['a', 'b', 'x', 'a']
  31. r0 = count(s1, 0)
  32. r1 = count(s1, 1)
  33. r2 = count(s1, 2)
  34. r3 = count(s2, 'y')
  35. r4 = count(s2, 'x')
  36. r5 = count(s2, 'a')
  37. ar0 = count(a1, 0)
  38. ar1 = count(a1, 1)
  39. ar2 = count(a1, 2)
  40. ar3 = count(a2, 'y')
  41. ar4 = count(a2, 'x')
  42. ar5 = count(a2, 'a')
  43. doAssert r0 == 0
  44. doAssert r1 == 1
  45. doAssert r2 == 2
  46. doAssert r3 == 0
  47. doAssert r4 == 1
  48. doAssert r5 == 2
  49. doAssert ar0 == 0
  50. doAssert ar1 == 1
  51. doAssert ar2 == 2
  52. doAssert ar3 == 0
  53. doAssert ar4 == 1
  54. doAssert ar5 == 2
  55. block: # cycle tests
  56. let
  57. a = @[1, 2, 3]
  58. b: seq[int] = @[]
  59. c = [1, 2, 3]
  60. doAssert a.cycle(3) == @[1, 2, 3, 1, 2, 3, 1, 2, 3]
  61. doAssert a.cycle(0) == @[]
  62. #doAssert a.cycle(-1) == @[] # will not compile!
  63. doAssert b.cycle(3) == @[]
  64. doAssert c.cycle(3) == @[1, 2, 3, 1, 2, 3, 1, 2, 3]
  65. doAssert c.cycle(0) == @[]
  66. block: # repeat tests
  67. doAssert repeat(10, 5) == @[10, 10, 10, 10, 10]
  68. doAssert repeat(@[1, 2, 3], 2) == @[@[1, 2, 3], @[1, 2, 3]]
  69. doAssert repeat([1, 2, 3], 2) == @[[1, 2, 3], [1, 2, 3]]
  70. block: # deduplicates test
  71. let
  72. dup1 = @[1, 1, 3, 4, 2, 2, 8, 1, 4]
  73. dup2 = @["a", "a", "c", "d", "d"]
  74. dup3 = [1, 1, 3, 4, 2, 2, 8, 1, 4]
  75. dup4 = ["a", "a", "c", "d", "d"]
  76. unique1 = deduplicate(dup1)
  77. unique2 = deduplicate(dup2)
  78. unique3 = deduplicate(dup3)
  79. unique4 = deduplicate(dup4)
  80. unique5 = deduplicate(dup1.sorted, true)
  81. unique6 = deduplicate(dup2, true)
  82. unique7 = deduplicate(dup3.sorted, true)
  83. unique8 = deduplicate(dup4, true)
  84. doAssert unique1 == @[1, 3, 4, 2, 8]
  85. doAssert unique2 == @["a", "c", "d"]
  86. doAssert unique3 == @[1, 3, 4, 2, 8]
  87. doAssert unique4 == @["a", "c", "d"]
  88. doAssert unique5 == @[1, 2, 3, 4, 8]
  89. doAssert unique6 == @["a", "c", "d"]
  90. doAssert unique7 == @[1, 2, 3, 4, 8]
  91. doAssert unique8 == @["a", "c", "d"]
  92. block: # zip test
  93. let
  94. short = @[1, 2, 3]
  95. long = @[6, 5, 4, 3, 2, 1]
  96. words = @["one", "two", "three"]
  97. ashort = [1, 2, 3]
  98. along = [6, 5, 4, 3, 2, 1]
  99. awords = ["one", "two", "three"]
  100. zip1 = zip(short, long)
  101. zip2 = zip(short, words)
  102. zip3 = zip(ashort, along)
  103. doAssert zip1 == @[(1, 6), (2, 5), (3, 4)]
  104. doAssert zip2 == @[(1, "one"), (2, "two"), (3, "three")]
  105. doAssert zip3 == @[(1, 6), (2, 5), (3, 4)]
  106. doAssert zip1[2][1] == 4
  107. doAssert zip2[2][1] == "three"
  108. doAssert zip3[2][1] == 4
  109. when (NimMajor, NimMinor) <= (1, 0):
  110. let
  111. # In Nim 1.0.x and older, zip returned a seq of tuple strictly
  112. # with fields named "a" and "b".
  113. zipAb = zip(ashort, awords)
  114. doAssert zipAb == @[(a: 1, b: "one"), (2, "two"), (3, "three")]
  115. doAssert zipAb[2].b == "three"
  116. else:
  117. let
  118. # As zip returns seq of anonymous tuples, they can be assigned
  119. # to any variable that's a sequence of named tuples too.
  120. zipXy: seq[tuple[x: int, y: string]] = zip(ashort, awords)
  121. zipMn: seq[tuple[m: int, n: string]] = zip(ashort, words)
  122. doAssert zipXy == @[(x: 1, y: "one"), (2, "two"), (3, "three")]
  123. doAssert zipMn == @[(m: 1, n: "one"), (2, "two"), (3, "three")]
  124. doAssert zipXy[2].y == "three"
  125. doAssert zipMn[2].n == "three"
  126. block: # distribute tests
  127. let numbers = @[1, 2, 3, 4, 5, 6, 7]
  128. doAssert numbers.distribute(3) == @[@[1, 2, 3], @[4, 5], @[6, 7]]
  129. doAssert numbers.distribute(6)[0] == @[1, 2]
  130. doAssert numbers.distribute(6)[5] == @[7]
  131. let a = @[1, 2, 3, 4, 5, 6, 7]
  132. doAssert a.distribute(1, true) == @[@[1, 2, 3, 4, 5, 6, 7]]
  133. doAssert a.distribute(1, false) == @[@[1, 2, 3, 4, 5, 6, 7]]
  134. doAssert a.distribute(2, true) == @[@[1, 2, 3, 4], @[5, 6, 7]]
  135. doAssert a.distribute(2, false) == @[@[1, 2, 3, 4], @[5, 6, 7]]
  136. doAssert a.distribute(3, true) == @[@[1, 2, 3], @[4, 5], @[6, 7]]
  137. doAssert a.distribute(3, false) == @[@[1, 2, 3], @[4, 5, 6], @[7]]
  138. doAssert a.distribute(4, true) == @[@[1, 2], @[3, 4], @[5, 6], @[7]]
  139. doAssert a.distribute(4, false) == @[@[1, 2], @[3, 4], @[5, 6], @[7]]
  140. doAssert a.distribute(5, true) == @[@[1, 2], @[3, 4], @[5], @[6], @[7]]
  141. doAssert a.distribute(5, false) == @[@[1, 2], @[3, 4], @[5, 6], @[7], @[]]
  142. doAssert a.distribute(6, true) == @[@[1, 2], @[3], @[4], @[5], @[6], @[7]]
  143. doAssert a.distribute(6, false) == @[
  144. @[1, 2], @[3, 4], @[5, 6], @[7], @[], @[]]
  145. doAssert a.distribute(8, false) == a.distribute(8, true)
  146. doAssert a.distribute(90, false) == a.distribute(90, true)
  147. var b = @[0]
  148. for f in 1 .. 25: b.add(f)
  149. doAssert b.distribute(5, true)[4].len == 5
  150. doAssert b.distribute(5, false)[4].len == 2
  151. block: # map test
  152. let
  153. numbers = @[1, 4, 5, 8, 9, 7, 4]
  154. anumbers = [1, 4, 5, 8, 9, 7, 4]
  155. m1 = map(numbers, proc(x: int): int = 2*x)
  156. m2 = map(anumbers, proc(x: int): int = 2*x)
  157. doAssert m1 == @[2, 8, 10, 16, 18, 14, 8]
  158. doAssert m2 == @[2, 8, 10, 16, 18, 14, 8]
  159. block: # apply test
  160. var a = @["1", "2", "3", "4"]
  161. apply(a, proc(x: var string) = x &= "42")
  162. doAssert a == @["142", "242", "342", "442"]
  163. block: # filter proc test
  164. let
  165. colors = @["red", "yellow", "black"]
  166. acolors = ["red", "yellow", "black"]
  167. f1 = filter(colors, proc(x: string): bool = x.len < 6)
  168. f2 = filter(colors) do (x: string) -> bool: x.len > 5
  169. f3 = filter(acolors, proc(x: string): bool = x.len < 6)
  170. f4 = filter(acolors) do (x: string) -> bool: x.len > 5
  171. doAssert f1 == @["red", "black"]
  172. doAssert f2 == @["yellow"]
  173. doAssert f3 == @["red", "black"]
  174. doAssert f4 == @["yellow"]
  175. block: # filter iterator test
  176. let numbers = @[1, 4, 5, 8, 9, 7, 4]
  177. let anumbers = [1, 4, 5, 8, 9, 7, 4]
  178. doAssert toSeq(filter(numbers, proc (x: int): bool = x mod 2 == 0)) ==
  179. @[4, 8, 4]
  180. doAssert toSeq(filter(anumbers, proc (x: int): bool = x mod 2 == 0)) ==
  181. @[4, 8, 4]
  182. block: # keepIf test
  183. var floats = @[13.0, 12.5, 5.8, 2.0, 6.1, 9.9, 10.1]
  184. keepIf(floats, proc(x: float): bool = x > 10)
  185. doAssert floats == @[13.0, 12.5, 10.1]
  186. block: # insert tests
  187. var dest = @[1, 1, 1, 1, 1, 1, 1, 1]
  188. let
  189. src = @[2, 2, 2, 2, 2, 2]
  190. outcome = @[1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1]
  191. dest.insert(src, 3)
  192. doAssert dest == outcome, """\
  193. Inserting [2,2,2,2,2,2] into [1,1,1,1,1,1,1,1]
  194. at 3 is [1,1,1,2,2,2,2,2,2,1,1,1,1,1]"""
  195. block: # filterIt test
  196. let
  197. temperatures = @[-272.15, -2.0, 24.5, 44.31, 99.9, -113.44]
  198. acceptable = filterIt(temperatures, it < 50 and it > -10)
  199. notAcceptable = filterIt(temperatures, it > 50 or it < -10)
  200. doAssert acceptable == @[-2.0, 24.5, 44.31]
  201. doAssert notAcceptable == @[-272.15, 99.9, -113.44]
  202. block: # keepItIf test
  203. var candidates = @["foo", "bar", "baz", "foobar"]
  204. keepItIf(candidates, it.len == 3 and it[0] == 'b')
  205. doAssert candidates == @["bar", "baz"]
  206. block: # all
  207. let
  208. numbers = @[1, 4, 5, 8, 9, 7, 4]
  209. anumbers = [1, 4, 5, 8, 9, 7, 4]
  210. len0seq: seq[int] = @[]
  211. doAssert all(numbers, proc (x: int): bool = return x < 10) == true
  212. doAssert all(numbers, proc (x: int): bool = return x < 9) == false
  213. doAssert all(len0seq, proc (x: int): bool = return false) == true
  214. doAssert all(anumbers, proc (x: int): bool = return x < 10) == true
  215. doAssert all(anumbers, proc (x: int): bool = return x < 9) == false
  216. block: # allIt
  217. let
  218. numbers = @[1, 4, 5, 8, 9, 7, 4]
  219. anumbers = [1, 4, 5, 8, 9, 7, 4]
  220. len0seq: seq[int] = @[]
  221. doAssert allIt(numbers, it < 10) == true
  222. doAssert allIt(numbers, it < 9) == false
  223. doAssert allIt(len0seq, false) == true
  224. doAssert allIt(anumbers, it < 10) == true
  225. doAssert allIt(anumbers, it < 9) == false
  226. block: # any
  227. let
  228. numbers = @[1, 4, 5, 8, 9, 7, 4]
  229. anumbers = [1, 4, 5, 8, 9, 7, 4]
  230. len0seq: seq[int] = @[]
  231. doAssert any(numbers, proc (x: int): bool = return x > 8) == true
  232. doAssert any(numbers, proc (x: int): bool = return x > 9) == false
  233. doAssert any(len0seq, proc (x: int): bool = return true) == false
  234. doAssert any(anumbers, proc (x: int): bool = return x > 8) == true
  235. doAssert any(anumbers, proc (x: int): bool = return x > 9) == false
  236. block: # anyIt
  237. let
  238. numbers = @[1, 4, 5, 8, 9, 7, 4]
  239. anumbers = [1, 4, 5, 8, 9, 7, 4]
  240. len0seq: seq[int] = @[]
  241. doAssert anyIt(numbers, it > 8) == true
  242. doAssert anyIt(numbers, it > 9) == false
  243. doAssert anyIt(len0seq, true) == false
  244. doAssert anyIt(anumbers, it > 8) == true
  245. doAssert anyIt(anumbers, it > 9) == false
  246. block: # toSeq test
  247. block:
  248. let
  249. numeric = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
  250. oddNumbers = toSeq(filter(numeric) do (x: int) -> bool:
  251. if x mod 2 == 1:
  252. result = true)
  253. doAssert oddNumbers == @[1, 3, 5, 7, 9]
  254. block:
  255. doAssert [1, 2].toSeq == @[1, 2]
  256. doAssert @[1, 2].toSeq == @[1, 2]
  257. doAssert @[1, 2].toSeq == @[1, 2]
  258. doAssert toSeq(@[1, 2]) == @[1, 2]
  259. block:
  260. iterator myIter(seed: int): auto =
  261. for i in 0..<seed:
  262. yield i
  263. doAssert toSeq(myIter(2)) == @[0, 1]
  264. block:
  265. iterator myIter(): auto {.inline.} =
  266. yield 1
  267. yield 2
  268. doAssert myIter.toSeq == @[1, 2]
  269. doAssert toSeq(myIter) == @[1, 2]
  270. when not defined(js):
  271. # pending #4695
  272. block:
  273. iterator myIter(): int {.closure.} =
  274. yield 1
  275. yield 2
  276. doAssert myIter.toSeq == @[1, 2]
  277. doAssert toSeq(myIter) == @[1, 2]
  278. block:
  279. proc myIter(): auto =
  280. iterator ret(): int {.closure.} =
  281. yield 1
  282. yield 2
  283. result = ret
  284. doAssert myIter().toSeq == @[1, 2]
  285. doAssert toSeq(myIter()) == @[1, 2]
  286. block:
  287. proc myIter(n: int): auto =
  288. var counter = 0
  289. iterator ret(): int {.closure.} =
  290. while counter < n:
  291. yield counter
  292. counter.inc
  293. result = ret
  294. block:
  295. let myIter3 = myIter(3)
  296. doAssert myIter3.toSeq == @[0, 1, 2]
  297. block:
  298. let myIter3 = myIter(3)
  299. doAssert toSeq(myIter3) == @[0, 1, 2]
  300. block:
  301. # makes sure this does not hang forever
  302. doAssert myIter(3).toSeq == @[0, 1, 2]
  303. doAssert toSeq(myIter(3)) == @[0, 1, 2]
  304. block:
  305. # tests https://github.com/nim-lang/Nim/issues/7187
  306. counter = 0
  307. let ret = toSeq(@[1, 2, 3].identity().filter(proc (x: int): bool = x < 3))
  308. doAssert ret == @[1, 2]
  309. doAssert counter == 1
  310. block: # foldl tests
  311. let
  312. numbers = @[5, 9, 11]
  313. addition = foldl(numbers, a + b)
  314. subtraction = foldl(numbers, a - b)
  315. multiplication = foldl(numbers, a * b)
  316. words = @["nim", "is", "cool"]
  317. concatenation = foldl(words, a & b)
  318. doAssert addition == 25, "Addition is (((5)+9)+11)"
  319. doAssert subtraction == -15, "Subtraction is (((5)-9)-11)"
  320. doAssert multiplication == 495, "Multiplication is (((5)*9)*11)"
  321. doAssert concatenation == "nimiscool"
  322. block: # foldr tests
  323. let
  324. numbers = @[5, 9, 11]
  325. addition = foldr(numbers, a + b)
  326. subtraction = foldr(numbers, a - b)
  327. multiplication = foldr(numbers, a * b)
  328. words = @["nim", "is", "cool"]
  329. concatenation = foldr(words, a & b)
  330. doAssert addition == 25, "Addition is (5+(9+(11)))"
  331. doAssert subtraction == 7, "Subtraction is (5-(9-(11)))"
  332. doAssert multiplication == 495, "Multiplication is (5*(9*(11)))"
  333. doAssert concatenation == "nimiscool"
  334. doAssert toSeq(1..3).foldr(a + b) == 6 # issue #14404
  335. block: # mapIt + applyIt test
  336. counter = 0
  337. var
  338. nums = @[1, 2, 3, 4]
  339. strings = nums.identity.mapIt($(4 * it))
  340. doAssert counter == 1
  341. nums.applyIt(it * 3)
  342. doAssert nums[0] + nums[3] == 15
  343. doAssert strings[2] == "12"
  344. block: # newSeqWith tests
  345. var seq2D = newSeqWith(4, newSeq[bool](2))
  346. seq2D[0][0] = true
  347. seq2D[1][0] = true
  348. seq2D[0][1] = true
  349. doAssert seq2D == @[@[true, true], @[true, false], @[false, false], @[false, false]]
  350. block: # mapLiterals tests
  351. let x = mapLiterals([0.1, 1.2, 2.3, 3.4], int)
  352. doAssert x is array[4, int]
  353. doAssert mapLiterals((1, ("abc"), 2), float, nested = false) ==
  354. (float(1), "abc", float(2))
  355. doAssert mapLiterals(([1], ("abc"), 2), `$`, nested = true) ==
  356. (["1"], "abc", "2")
  357. block: # mapIt with openArray
  358. counter = 0
  359. proc foo(x: openArray[int]): seq[int] = x.mapIt(it * 10)
  360. doAssert foo([identity(1), identity(2)]) == @[10, 20]
  361. doAssert counter == 2
  362. block: # mapIt with direct openArray
  363. proc foo1(x: openArray[int]): seq[int] = x.mapIt(it * 10)
  364. counter = 0
  365. doAssert foo1(openArray[int]([identity(1), identity(2)])) == @[10, 20]
  366. doAssert counter == 2
  367. # Corner cases (openArray literals should not be common)
  368. template foo2(x: openArray[int]): seq[int] = x.mapIt(it * 10)
  369. counter = 0
  370. doAssert foo2(openArray[int]([identity(1), identity(2)])) == @[10, 20]
  371. doAssert counter == 2
  372. counter = 0
  373. doAssert openArray[int]([identity(1), identity(2)]).mapIt(it) == @[1, 2]
  374. doAssert counter == 2
  375. block: # mapIt empty test, see https://github.com/nim-lang/Nim/pull/8584#pullrequestreview-144723468
  376. # NOTE: `[].mapIt(it)` is illegal, just as `let a = @[]` is (lacks type
  377. # of elements)
  378. doAssert: not compiles(mapIt(@[], it))
  379. doAssert: not compiles(mapIt([], it))
  380. doAssert newSeq[int](0).mapIt(it) == @[]
  381. block: # mapIt redifinition check, see https://github.com/nim-lang/Nim/issues/8580
  382. let s2 = [1, 2].mapIt(it)
  383. doAssert s2 == @[1, 2]
  384. block:
  385. counter = 0
  386. doAssert [1, 2].identity().mapIt(it*2).mapIt(it*10) == @[20, 40]
  387. # https://github.com/nim-lang/Nim/issues/7187 test case
  388. doAssert counter == 1
  389. block: # mapIt with invalid RHS for `let` (#8566)
  390. type X = enum
  391. A, B
  392. doAssert mapIt(X, $it) == @["A", "B"]
  393. block:
  394. # bug #9093
  395. let inp = "a:b,c:d"
  396. let outp = inp.split(",").mapIt(it.split(":"))
  397. doAssert outp == @[@["a", "b"], @["c", "d"]]
  398. block:
  399. proc iter(len: int): auto =
  400. result = iterator(): int =
  401. for i in 0..<len:
  402. yield i
  403. # xxx: obscure CT error: basic_types.nim(16, 16) Error: internal error: symbol has no generated name: true
  404. when not defined(js):
  405. doAssert: iter(3).mapIt(2*it).foldl(a + b) == 6
  406. block: # strictFuncs tests with ref object
  407. type Foo = ref object
  408. let foo1 = Foo()
  409. let foo2 = Foo()
  410. let foos = @[foo1, foo2]
  411. # Procedures that are `func`
  412. discard concat(foos, foos)
  413. discard count(foos, foo1)
  414. discard cycle(foos, 3)
  415. discard deduplicate(foos)
  416. discard minIndex(foos)
  417. discard maxIndex(foos)
  418. discard distribute(foos, 2)
  419. var mutableFoos = foos
  420. mutableFoos.delete(0..1)
  421. mutableFoos.insert(foos)
  422. # Some procedures that are `proc`, but were reverted from `func`
  423. discard repeat(foo1, 3)
  424. discard zip(foos, foos)
  425. let fooTuples = @[(foo1, 1), (foo2, 2)]
  426. discard unzip(fooTuples)
  427. template main =
  428. # xxx move all tests here
  429. block: # delete tests
  430. let outcome = @[1, 1, 1, 1, 1, 1, 1, 1]
  431. var dest = @[1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1]
  432. dest.delete(3, 8)
  433. doAssert outcome == dest, """\
  434. Deleting range 3-9 from [1,1,1,2,2,2,2,2,2,1,1,1,1,1]
  435. is [1,1,1,1,1,1,1,1]"""
  436. var x = @[1, 2, 3]
  437. x.delete(100, 100)
  438. doAssert x == @[1, 2, 3]
  439. block: # delete tests
  440. var a = @[10, 11, 12, 13, 14]
  441. doAssertRaises(IndexDefect): a.delete(4..5)
  442. doAssertRaises(IndexDefect): a.delete(4..<6)
  443. doAssertRaises(IndexDefect): a.delete(-1..1)
  444. doAssertRaises(IndexDefect): a.delete(-1 .. -1)
  445. doAssertRaises(IndexDefect): a.delete(5..5)
  446. doAssertRaises(IndexDefect): a.delete(5..3)
  447. doAssertRaises(IndexDefect): a.delete(5..<5) # edge case
  448. doAssert a == @[10, 11, 12, 13, 14]
  449. a.delete(4..4)
  450. doAssert a == @[10, 11, 12, 13]
  451. a.delete(1..2)
  452. doAssert a == @[10, 13]
  453. a.delete(1..<1) # empty slice
  454. doAssert a == @[10, 13]
  455. a.delete(0..<0)
  456. doAssert a == @[10, 13]
  457. a.delete(0..0)
  458. doAssert a == @[13]
  459. a.delete(0..0)
  460. doAssert a == @[]
  461. doAssertRaises(IndexDefect): a.delete(0..0)
  462. doAssertRaises(IndexDefect): a.delete(0..<0) # edge case
  463. block:
  464. type A = object
  465. a0: int
  466. var a = @[A(a0: 10), A(a0: 11), A(a0: 12)]
  467. a.delete(0..1)
  468. doAssert a == @[A(a0: 12)]
  469. block:
  470. type A = ref object
  471. let a0 = A()
  472. let a1 = A()
  473. let a2 = A()
  474. var a = @[a0, a1, a2]
  475. a.delete(0..1)
  476. doAssert a == @[a2]
  477. static: main()
  478. main()
  479. {.pop.}