ttasks.nim 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. discard """
  2. targets: "c cpp"
  3. matrix: "--gc:orc"
  4. """
  5. import std/[tasks, strformat]
  6. block:
  7. var s = ""
  8. proc `+`(x: int, y: string) =
  9. s.add $x & y
  10. let literal = "Nim"
  11. let t = toTask(521 + literal)
  12. t.invoke()
  13. doAssert s == "521Nim"
  14. block:
  15. var s = ""
  16. proc `!`(x: int) =
  17. s.add $x
  18. let t = toTask !12
  19. t.invoke()
  20. doAssert s == "12"
  21. block:
  22. block:
  23. var called = 0
  24. proc hello(x: static range[1 .. 5]) =
  25. called += x
  26. let b = toTask hello(3)
  27. b.invoke()
  28. doAssert called == 3
  29. b.invoke()
  30. doAssert called == 6
  31. block:
  32. var called = 0
  33. proc hello(x: range[1 .. 5]) =
  34. called += x
  35. let b = toTask hello(3)
  36. b.invoke()
  37. doAssert called == 3
  38. b.invoke()
  39. doAssert called == 6
  40. block:
  41. var called = 0
  42. proc hello(x: 1 .. 5) =
  43. called += x
  44. let b = toTask hello(3)
  45. b.invoke()
  46. doAssert called == 3
  47. b.invoke()
  48. doAssert called == 6
  49. block:
  50. var temp = ""
  51. proc hello(a: int or seq[string]) =
  52. when a is seq[string]:
  53. for s in a:
  54. temp.add s
  55. else:
  56. temp.addInt a
  57. let x = @["1", "2", "3", "4"]
  58. let b = toTask hello(x)
  59. b.invoke()
  60. doAssert temp == "1234"
  61. b.invoke()
  62. doAssert temp == "12341234"
  63. block:
  64. var temp = ""
  65. proc hello(a: int or string) =
  66. when a is string:
  67. temp.add a
  68. let x = "!2"
  69. let b = toTask hello(x)
  70. b.invoke()
  71. doAssert temp == x
  72. block:
  73. var temp = ""
  74. proc hello(a: int or string) =
  75. when a is string:
  76. temp.add a
  77. let x = "!2"
  78. let b = toTask hello(x)
  79. b.invoke()
  80. doAssert temp == x
  81. block:
  82. var x = 0
  83. proc hello(typ: typedesc) =
  84. x += typ(12)
  85. let b = toTask hello(int)
  86. b.invoke()
  87. doAssert x == 12
  88. block:
  89. var temp = ""
  90. proc hello(a: int or seq[string]) =
  91. when a is seq[string]:
  92. for s in a:
  93. temp.add s
  94. let x = @["1", "2", "3", "4"]
  95. let b = toTask hello(x)
  96. b.invoke()
  97. doAssert temp == "1234"
  98. block:
  99. var temp = ""
  100. proc hello(a: int | string) =
  101. when a is string:
  102. temp.add a
  103. let x = "!2"
  104. let b = toTask hello(x)
  105. b.invoke()
  106. doAssert temp == x
  107. block:
  108. var x = 0
  109. proc hello(a: int | string) =
  110. when a is int:
  111. x = a
  112. let b = toTask hello(12)
  113. b.invoke()
  114. doAssert x == 12
  115. block:
  116. var a1: seq[int]
  117. var a2 = 0
  118. proc hello(c: seq[int], a: int) =
  119. a1 = c
  120. a2 = a
  121. let x = 12
  122. var y = @[1, 3, 1, 4, 5, x, 1]
  123. let b = toTask hello(y, 12)
  124. b.invoke()
  125. doAssert a1 == y
  126. doAssert a2 == x
  127. block:
  128. var a1: seq[int]
  129. var a2 = 0
  130. proc hello(c: seq[int], a: int) =
  131. a1 = c
  132. a2 = a
  133. var x = 2
  134. let b = toTask hello(@[1, 3, 1, 4, 5, x, 1], 12)
  135. b.invoke()
  136. doAssert a1 == @[1, 3, 1, 4, 5, x, 1]
  137. doAssert a2 == 12
  138. block:
  139. var a1: array[7, int]
  140. var a2 = 0
  141. proc hello(c: array[7, int], a: int) =
  142. a1 = c
  143. a2 = a
  144. let b = toTask hello([1, 3, 1, 4, 5, 2, 1], 12)
  145. b.invoke()
  146. doAssert a1 == [1, 3, 1, 4, 5, 2, 1]
  147. doAssert a2 == 12
  148. block:
  149. var a1: seq[int]
  150. var a2 = 0
  151. proc hello(c: seq[int], a: int) =
  152. a1 = c
  153. a2 = a
  154. let b = toTask hello(@[1, 3, 1, 4, 5, 2, 1], 12)
  155. b.invoke()
  156. doAssert a1 == @[1, 3, 1, 4, 5, 2, 1]
  157. doAssert a2 == 12
  158. block:
  159. var a1: seq[int]
  160. var a2 = 0
  161. proc hello(a: int, c: seq[int]) =
  162. a1 = c
  163. a2 = a
  164. let b = toTask hello(8, @[1, 3, 1, 4, 5, 2, 1])
  165. b.invoke()
  166. doAssert a1 == @[1, 3, 1, 4, 5, 2, 1]
  167. doAssert a2 == 8
  168. let c = toTask 8.hello(@[1, 3, 1, 4, 5, 2, 1])
  169. c.invoke()
  170. doAssert a1 == @[1, 3, 1, 4, 5, 2, 1]
  171. doAssert a2 == 8
  172. block:
  173. var a1: seq[seq[int]]
  174. var a2: int
  175. proc hello(a: int, c: openArray[seq[int]]) =
  176. a1 = @c
  177. a2 = a
  178. let b = toTask hello(8, @[@[3], @[4], @[5], @[6], @[12], @[7]])
  179. b.invoke()
  180. doAssert a1 == @[@[3], @[4], @[5], @[6], @[12], @[7]]
  181. doAssert a2 == 8
  182. block:
  183. var a1: seq[int]
  184. var a2: int
  185. proc hello(a: int, c: openArray[int]) =
  186. a1 = @c
  187. a2 = a
  188. let b = toTask hello(8, @[3, 4, 5, 6, 12, 7])
  189. b.invoke()
  190. doAssert a1 == @[3, 4, 5, 6, 12, 7]
  191. doAssert a2 == 8
  192. block:
  193. var a1: seq[int]
  194. var a2: int
  195. proc hello(a: int, c: static varargs[int]) =
  196. a1 = @c
  197. a2 = a
  198. let b = toTask hello(8, @[3, 4, 5, 6, 12, 7])
  199. b.invoke()
  200. doAssert a1 == @[3, 4, 5, 6, 12, 7]
  201. doAssert a2 == 8
  202. block:
  203. var a1: seq[int]
  204. var a2: int
  205. proc hello(a: int, c: static varargs[int]) =
  206. a1 = @c
  207. a2 = a
  208. let b = toTask hello(8, [3, 4, 5, 6, 12, 7])
  209. b.invoke()
  210. doAssert a1 == @[3, 4, 5, 6, 12, 7]
  211. doAssert a2 == 8
  212. block:
  213. var a1: seq[int]
  214. var a2: int
  215. proc hello(a: int, c: varargs[int]) =
  216. a1 = @c
  217. a2 = a
  218. let x = 12
  219. let b = toTask hello(8, 3, 4, 5, 6, x, 7)
  220. b.invoke()
  221. doAssert a1 == @[3, 4, 5, 6, 12, 7]
  222. doAssert a2 == 8
  223. block:
  224. var x = 12
  225. proc hello(x: ptr int) =
  226. x[] += 12
  227. let b = toTask hello(addr x)
  228. b.invoke()
  229. doAssert x == 24
  230. let c = toTask x.addr.hello
  231. invoke(c)
  232. doAssert x == 36
  233. block:
  234. type
  235. Test = ref object
  236. id: int
  237. var x = 0
  238. proc hello(a: int, c: static Test) =
  239. x += a
  240. x += c.id
  241. let b = toTask hello(8, Test(id: 12))
  242. b.invoke()
  243. doAssert x == 20
  244. block:
  245. type
  246. Test = object
  247. id: int
  248. var x = 0
  249. proc hello(a: int, c: static Test) =
  250. x += a
  251. x += c.id
  252. let b = toTask hello(8, Test(id: 12))
  253. b.invoke()
  254. doAssert x == 20
  255. block:
  256. var x = 0
  257. proc hello(a: int, c: static seq[int]) =
  258. x += a
  259. for i in c:
  260. x += i
  261. let b = toTask hello(8, @[3, 4, 5, 6, 12, 7])
  262. b.invoke()
  263. doAssert x == 45
  264. block:
  265. var x = 0
  266. proc hello(a: int, c: static array[5, int]) =
  267. x += a
  268. for i in c:
  269. x += i
  270. let b = toTask hello(8, [3, 4, 5, 6, 12])
  271. b.invoke()
  272. doAssert x == 38
  273. block:
  274. var aVal = 0
  275. var cVal = ""
  276. proc hello(a: int, c: static string) =
  277. aVal += a
  278. cVal.add c
  279. var x = 1314
  280. let b = toTask hello(x, "hello")
  281. b.invoke()
  282. doAssert aVal == x
  283. doAssert cVal == "hello"
  284. block:
  285. var aVal = ""
  286. proc hello(a: static string) =
  287. aVal.add a
  288. let b = toTask hello("hello")
  289. b.invoke()
  290. doAssert aVal == "hello"
  291. block:
  292. var aVal = 0
  293. var cVal = ""
  294. proc hello(a: static int, c: static string) =
  295. aVal += a
  296. cVal.add c
  297. let b = toTask hello(8, "hello")
  298. b.invoke()
  299. doAssert aVal == 8
  300. doAssert cVal == "hello"
  301. block:
  302. var aVal = 0
  303. var cVal = 0
  304. proc hello(a: static int, c: int) =
  305. aVal += a
  306. cVal += c
  307. let b = toTask hello(c = 0, a = 8)
  308. b.invoke()
  309. doAssert aVal == 8
  310. doAssert cVal == 0
  311. block:
  312. var aVal = 0
  313. var cVal = 0
  314. proc hello(a: int, c: static int) =
  315. aVal += a
  316. cVal += c
  317. let b = toTask hello(c = 0, a = 8)
  318. b.invoke()
  319. doAssert aVal == 8
  320. doAssert cVal == 0
  321. block:
  322. var aVal = 0
  323. var cVal = 0
  324. proc hello(a: static int, c: static int) =
  325. aVal += a
  326. cVal += c
  327. let b = toTask hello(0, 8)
  328. b.invoke()
  329. doAssert aVal == 0
  330. doAssert cVal == 8
  331. block:
  332. var temp = ""
  333. proc hello(x: int, y: seq[string], d = 134) =
  334. temp = fmt"{x=} {y=} {d=}"
  335. proc main() =
  336. var x = @["23456"]
  337. let t = toTask hello(2233, x)
  338. t.invoke()
  339. doAssert temp == """x=2233 y=@["23456"] d=134"""
  340. main()
  341. block:
  342. var temp = ""
  343. proc hello(x: int, y: seq[string], d = 134) =
  344. temp.add fmt"{x=} {y=} {d=}"
  345. proc ok() =
  346. temp = "ok"
  347. proc main() =
  348. var x = @["23456"]
  349. let t = toTask hello(2233, x)
  350. t.invoke()
  351. t.invoke()
  352. doAssert temp == """x=2233 y=@["23456"] d=134x=2233 y=@["23456"] d=134"""
  353. main()
  354. var x = @["4"]
  355. let m = toTask hello(2233, x, 7)
  356. m.invoke()
  357. doAssert temp == """x=2233 y=@["23456"] d=134x=2233 y=@["23456"] d=134x=2233 y=@["4"] d=7"""
  358. let n = toTask ok()
  359. n.invoke()
  360. doAssert temp == "ok"
  361. block:
  362. var called = 0
  363. block:
  364. proc hello() =
  365. inc called
  366. let a = toTask hello()
  367. invoke(a)
  368. doAssert called == 1
  369. block:
  370. proc hello(a: int) =
  371. inc called, a
  372. let b = toTask hello(13)
  373. let c = toTask hello(a = 14)
  374. b.invoke()
  375. c.invoke()
  376. doAssert called == 28
  377. block:
  378. proc hello(a: int, c: int) =
  379. inc called, a
  380. let b = toTask hello(c = 0, a = 8)
  381. b.invoke()
  382. doAssert called == 36