tsugar.nim 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. output: '''
  4. x + y = 30
  5. '''
  6. """
  7. import std/[sugar, algorithm, random, sets, tables, strutils, sequtils]
  8. import std/[syncio, assertions]
  9. type # for capture test, ref #20679
  10. FooCapture = ref object
  11. x: int
  12. template main() =
  13. block: # `=>`
  14. block:
  15. let f1 = () => 42
  16. doAssert f1() == 42
  17. let f2 = (x: int) => x + 1
  18. doAssert f2(42) == 43
  19. let f3 = (x, y: int) => x + y
  20. doAssert f3(1, 2) == 3
  21. var x = 0
  22. let f4 = () => (x = 12)
  23. f4()
  24. doAssert x == 12
  25. let f5 = () => (discard) # simplest proc that returns void
  26. f5()
  27. block:
  28. proc call1(f: () -> int): int = f()
  29. doAssert call1(() => 12) == 12
  30. proc call2(f: int -> int): int = f(42)
  31. doAssert call2(x => x) == 42
  32. doAssert call2((x) => x) == 42
  33. doAssert call2((x: int) => x) == 42
  34. proc call3(f: (int, int) -> int): int = f(1, 2)
  35. doAssert call3((x, y) => x + y) == 3
  36. doAssert call3((x, y: int) => x + y) == 3
  37. doAssert call3((x: int, y: int) => x + y) == 3
  38. var a = 0
  39. proc call4(f: int -> void) = f(42)
  40. call4((x: int) => (a = x))
  41. doAssert a == 42
  42. proc call5(f: (int {.noSideEffect.} -> int)): int = f(42)
  43. doAssert call5(x {.noSideEffect.} => x + 1) == 43
  44. block: # `->`
  45. doAssert $(() -> int) == "proc (): int{.closure.}"
  46. doAssert $(float -> int) == "proc (i0: float): int{.closure.}"
  47. doAssert $((float) -> int) == "proc (i0: float): int{.closure.}"
  48. doAssert $((float, bool) -> int) == "proc (i0: float, i1: bool): int{.closure.}"
  49. doAssert $(() -> void) == "proc (){.closure.}"
  50. doAssert $(float -> void) == "proc (i0: float){.closure.}"
  51. doAssert $((float) -> void) == "proc (i0: float){.closure.}"
  52. doAssert $((float, bool) -> void) == "proc (i0: float, i1: bool){.closure.}"
  53. doAssert $(() {.inline.} -> int) == "proc (): int{.inline.}"
  54. doAssert $(float {.inline.} -> int) == "proc (i0: float): int{.inline.}"
  55. doAssert $((float) {.inline.} -> int) == "proc (i0: float): int{.inline.}"
  56. doAssert $((float, bool) {.inline.} -> int) == "proc (i0: float, i1: bool): int{.inline.}"
  57. block: # capture
  58. var closure1: () -> int
  59. for i in 0 .. 10:
  60. if i == 5:
  61. capture i:
  62. closure1 = () => i
  63. doAssert closure1() == 5
  64. var closure2: () -> (int, int)
  65. for i in 0 .. 10:
  66. for j in 0 .. 10:
  67. if i == 5 and j == 3:
  68. capture i, j:
  69. closure2 = () => (i, j)
  70. doAssert closure2() == (5, 3)
  71. block: # bug #16967
  72. var s = newSeq[proc (): int](5)
  73. {.push exportc.}
  74. proc bar() =
  75. for i in 0 ..< s.len:
  76. let foo = i + 1
  77. capture foo:
  78. s[i] = proc(): int = foo
  79. {.pop.}
  80. bar()
  81. for i, p in s.pairs:
  82. let foo = i + 1
  83. doAssert p() == foo
  84. block: # issue #20679
  85. # this should compile. Previously was broken as `var int` is an `nnkHiddenDeref`
  86. # which was not handled correctly
  87. block:
  88. var x = 5
  89. var s1 = newSeq[proc (): int](2)
  90. proc function(data: var int) =
  91. for i in 0 ..< 2:
  92. data = (i+1) * data
  93. capture data:
  94. s1[i] = proc(): int = data
  95. function(x)
  96. doAssert s1[0]() == 5
  97. doAssert s1[1]() == 10
  98. block:
  99. var y = @[5, 10]
  100. var s2 = newSeq[proc (): seq[int]](2)
  101. proc functionS(data: var seq[int]) =
  102. for i in 0 ..< 2:
  103. data.add (i+1) * 5
  104. capture data:
  105. s2[i] = proc(): seq[int] = data
  106. functionS(y)
  107. doAssert s2[0]() == @[5, 10, 5]
  108. doAssert s2[1]() == @[5, 10, 5, 10]
  109. template typeT(typ, val: untyped): untyped =
  110. var x = val
  111. var s = newSeq[proc (): typ](2)
  112. proc functionT[T](data: var T) =
  113. for i in 0 ..< 2:
  114. if i == 1:
  115. data = default(T)
  116. capture data:
  117. s[i] = proc (): T = data
  118. functionT(x)
  119. doAssert s[0]() == val
  120. doAssert s[1]() == x # == default
  121. doAssert s[1]() == default(typ)
  122. block:
  123. var x = 1.1
  124. typeT(float, x)
  125. block:
  126. var x = "hello"
  127. typeT(string, x)
  128. block:
  129. var f = FooCapture(x: 5)
  130. typeT(FooCapture, f)
  131. block: # dup
  132. block dup_with_field:
  133. type
  134. Foo = object
  135. col, pos: int
  136. name: string
  137. proc inc_col(foo: var Foo) = inc(foo.col)
  138. proc inc_pos(foo: var Foo) = inc(foo.pos)
  139. proc name_append(foo: var Foo, s: string) = foo.name &= s
  140. let a = Foo(col: 1, pos: 2, name: "foo")
  141. block:
  142. let b = a.dup(inc_col, inc_pos):
  143. _.pos = 3
  144. name_append("bar")
  145. inc_pos
  146. doAssert(b == Foo(col: 2, pos: 4, name: "foobar"))
  147. block:
  148. let b = a.dup(inc_col, pos = 3, name = "bar"):
  149. name_append("bar")
  150. inc_pos
  151. doAssert(b == Foo(col: 2, pos: 4, name: "barbar"))
  152. block:
  153. var a = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
  154. doAssert dup(a, sort(_)) == sorted(a)
  155. doAssert a.dup(sort) == sorted(a)
  156. # Chaining:
  157. var aCopy = a
  158. aCopy.insert(10)
  159. doAssert a.dup(insert(10)).dup(sort()) == sorted(aCopy)
  160. block:
  161. when nimvm: discard
  162. else:
  163. const b = @[0, 1, 2]
  164. discard b.dup shuffle()
  165. doAssert b[0] == 0
  166. doAssert b[1] == 1
  167. block: # collect
  168. let data = @["bird", "word"] # if this gets stuck in your head, its not my fault
  169. doAssert collect(newSeq, for (i, d) in data.pairs: (if i mod 2 == 0: d)) == @["bird"]
  170. doAssert collect(initTable(2), for (i, d) in data.pairs: {i: d}) ==
  171. {0: "bird", 1: "word"}.toTable
  172. doAssert collect(initHashSet(), for d in data.items: {d}) == data.toHashSet
  173. block:
  174. let x = collect(newSeqOfCap(4)):
  175. for (i, d) in data.pairs:
  176. if i mod 2 == 0: d
  177. doAssert x == @["bird"]
  178. block: # bug #12874
  179. let bug = collect(
  180. newSeq,
  181. for (i, d) in data.pairs:(
  182. block:
  183. if i mod 2 == 0:
  184. d
  185. else:
  186. d & d
  187. )
  188. )
  189. doAssert bug == @["bird", "wordword"]
  190. block:
  191. let y = collect(newSeq):
  192. for (i, d) in data.pairs:
  193. try: parseInt(d) except: 0
  194. doAssert y == @[0, 0]
  195. block:
  196. let z = collect(newSeq):
  197. for (i, d) in data.pairs:
  198. case d
  199. of "bird": "word"
  200. else: d
  201. doAssert z == @["word", "word"]
  202. block:
  203. proc tforum(): seq[int] =
  204. collect(newSeq):
  205. for y in 0..10:
  206. if y mod 5 == 2:
  207. for x in 0..y:
  208. x
  209. doAssert tforum() == @[0, 1, 2, 0, 1, 2, 3, 4, 5, 6, 7]
  210. block:
  211. let x = collect:
  212. for d in data.items:
  213. when d is int: "word"
  214. else: d
  215. doAssert x == @["bird", "word"]
  216. block:
  217. doAssert collect(for (i, d) in pairs(data): (i, d)) == @[(0, "bird"), (1, "word")]
  218. doAssert collect(for d in data.items: (try: parseInt(d) except: 0)) == @[0, 0]
  219. doAssert collect(for (i, d) in pairs(data): {i: d}) ==
  220. {1: "word", 0: "bird"}.toTable
  221. doAssert collect(for d in data.items: {d}) == data.toHashSet
  222. block: # bug #14332
  223. template foo =
  224. discard collect(newSeq, for i in 1..3: i)
  225. foo()
  226. proc mainProc() =
  227. block: # dump
  228. # symbols in templates are gensym'd
  229. let
  230. x = 10
  231. y = 20
  232. dump(x + y) # x + y = 30
  233. block: # dumpToString
  234. template square(x): untyped = x * x
  235. let x = 10
  236. doAssert dumpToString(square(x)) == "square(x): x * x = 100"
  237. let s = dumpToString(doAssert 1+1 == 2)
  238. doAssert "failedAssertImpl" in s
  239. let s2 = dumpToString:
  240. doAssertRaises(AssertionDefect): doAssert false
  241. doAssert "except AssertionDefect" in s2
  242. block: # bug #20704
  243. proc test() =
  244. var xs, ys: seq[int]
  245. for i in 0..5:
  246. xs.add(i)
  247. xs.apply(d => ys.add(d))
  248. doAssert ys == @[0, 1, 2, 3, 4, 5]
  249. test()
  250. static:
  251. main()
  252. mainProc()
  253. main()
  254. mainProc()