t9410.nim 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. template doAssert(exp: untyped) =
  2. when defined(echot9410):
  3. let r = exp
  4. echo $(instantiationInfo().line) & ":\n " & astToStr(exp) & "\n was " & repr(r)
  5. when not defined(noassertt9410):
  6. system.doAssert r
  7. else:
  8. when not defined(noassertt9410):
  9. system.doAssert exp
  10. template tests =
  11. block:
  12. var i = 0
  13. i = 2
  14. var y: ptr int
  15. doAssert y == nil
  16. doAssert isNil(y)
  17. y = i.addr
  18. y[] = 3
  19. doAssert i == 3
  20. doAssert i == y[]
  21. let z = i.addr
  22. z[] = 4
  23. doAssert i == 4
  24. doAssert i == y[] and y[] == z[]
  25. var hmm = (a: (b: z))
  26. var hmmptr = hmm.a.b.addr
  27. hmmptr[][] = 5
  28. doAssert i == 5
  29. doAssert y == z
  30. doAssert z == hmmptr[]
  31. doAssert 5 == y[] and 5 == z[] and 5 == hmmptr[][]
  32. block:
  33. var someint = 500
  34. let p: ptr int = someint.addr
  35. let tup = (f: p)
  36. let tcopy = tup
  37. var vtcopy = tcopy
  38. p[] = 654
  39. doAssert p[] == 654
  40. doAssert tup.f[] == 654
  41. doAssert tcopy.f[] == 654
  42. doAssert vtcopy.f[] == 654
  43. block:
  44. var someint = 500
  45. var p: ptr int = someint.addr
  46. let arr = [p]
  47. let arrc = arr
  48. p[] = 256
  49. doAssert someint == 256
  50. doAssert p[] == 256
  51. doAssert arr[0][] == 256
  52. doAssert arrc[0][] == 256
  53. block:
  54. var someref: ref int
  55. new(someref)
  56. var someref2 = someref
  57. var tup1 = (f: someref)
  58. tup1.f = someref
  59. let tup2 = tup1
  60. someref[] = 543
  61. proc passref(r: var ref int): var ref int = r
  62. new(passref(someref))
  63. doAssert someref[] == 0
  64. doAssert tup1.f[] == 543
  65. doAssert tup2.f[] == 543
  66. doAssert someref2[] == 543
  67. block:
  68. type Whatever = object
  69. i: ref int
  70. var someref: ref int
  71. new(someref)
  72. someref[] = 10
  73. let w = Whatever(i: someref)
  74. var wcopy = w
  75. someref[] = 20
  76. doAssert w.i[] == 20
  77. doAssert someref[] == 20
  78. doAssert wcopy.i[] == 20
  79. doAssert w.i == wcopy.i
  80. #echo w.i[], " ", someref[], " ", wcopy.i[]
  81. block:
  82. var oneseq: ref seq[ref int]
  83. new(oneseq)
  84. var aref: ref int
  85. new(aref)
  86. aref[] = 123
  87. let arefs = [aref]
  88. oneseq[] &= arefs[0]
  89. oneseq[] &= aref
  90. aref[] = 222
  91. new(aref)
  92. doAssert oneseq[0] == oneseq[1]
  93. doAssert oneseq[0][] == 222
  94. doAssert oneseq[1][] == 222
  95. doAssert aref[] == 0
  96. block:
  97. var seqs: ref seq[ref seq[ref int]]
  98. new(seqs)
  99. seqs[] = newSeq[ref seq[ref int]](1)
  100. new(seqs[0])
  101. seqs[0][] = newSeq[ref int](0)
  102. var aref: ref int
  103. new aref
  104. aref[] = 654
  105. let arefs = [aref]
  106. doAssert arefs[0] == aref
  107. seqs[0][] &= arefs[0]
  108. seqs[0][] &= aref
  109. seqs[0][1][] = 456
  110. let seqs2 = seqs
  111. let same = seqs2[0][0] == seqs2[0][1]
  112. doAssert arefs[0] == aref
  113. doAssert aref[] == 456
  114. doAssert seqs[].len == 1
  115. doAssert seqs[0][].len == 2
  116. doAssert seqs[0][0][] == 456
  117. doAssert seqs[0][1][] == 456
  118. doAssert same
  119. block:
  120. type Obj = object
  121. x, y: int
  122. var objrefs: seq[ref Obj] = @[(ref Obj)(nil), nil, nil]
  123. objrefs[2].new
  124. objrefs[2][] = Obj(x: 123, y: 321)
  125. objrefs[1] = objrefs[2]
  126. doAssert objrefs[0] == nil
  127. doAssert objrefs[1].y == 321
  128. doAssert objrefs[2].y == 321
  129. doAssert objrefs[1] == objrefs[2]
  130. block:
  131. var refs: seq[ref string] = @[(ref string)(nil), nil, nil]
  132. refs[1].new
  133. refs[1][] = "it's a ref!"
  134. refs[0] = refs[1]
  135. refs[2] = refs[1]
  136. new(refs[0])
  137. doAssert refs[0][] == ""
  138. doAssert refs[1][] == "it's a ref!"
  139. doAssert refs[2][] == "it's a ref!"
  140. doAssert refs[1] == refs[2]
  141. block:
  142. var retaddr_calls = 0
  143. proc retaddr(p: var int): var int =
  144. retaddr_calls += 1
  145. p
  146. var tfoo_calls = 0
  147. proc tfoo(x: var int) =
  148. tfoo_calls += 1
  149. x += 10
  150. var y = x.addr
  151. y[] += 20
  152. retaddr(x) += 30
  153. let z = retaddr(x).addr
  154. z[] += 40
  155. var ints = @[1, 2, 3]
  156. tfoo(ints[1])
  157. doAssert retaddr_calls == 2
  158. doAssert tfoo_calls == 1
  159. doAssert ints[1] == 102
  160. var tbar_calls = 0
  161. proc tbar(x: var int): var int =
  162. tbar_calls += 1
  163. x
  164. tbar(ints[2]) += 10
  165. tbar(ints[2]) *= 2
  166. doAssert tbar_calls == 2
  167. var tqux_calls = 0
  168. proc tqux(x: var int): ptr int =
  169. tqux_calls += 1
  170. x.addr
  171. discard tqux(ints[2]) == tqux(ints[2])
  172. doAssert tqux_calls == 2
  173. doAssert isNil(tqux(ints[2])) == false
  174. doAssert tqux_calls == 3
  175. var tseq_calls = 0
  176. proc tseq(x: var seq[int]): var seq[int] =
  177. tseq_calls += 1
  178. x
  179. tseq(ints) &= 999
  180. doAssert tseq_calls == 1
  181. doAssert ints == @[1, 102, 26, 999]
  182. var rawints = @[555]
  183. rawints &= 666
  184. doAssert rawints == @[555, 666]
  185. var resetints_calls = 0
  186. proc resetInts(): int =
  187. resetints_calls += 1
  188. ints = @[0, 0, 0]
  189. 1
  190. proc incr(x: var int; b: int): var int =
  191. x = x + b
  192. x
  193. var q = 0
  194. var qp = q.addr
  195. qp[] += 123
  196. doAssert q == 123
  197. # check order of evaluation
  198. doAssert (resetInts() + incr(q, tqux(ints[2])[])) == 124
  199. block: # reset
  200. var calls = 0
  201. proc passsomething(x: var int): var int =
  202. calls += 1
  203. x
  204. var
  205. a = 123
  206. b = 500
  207. c = a.addr
  208. reset(passsomething(a))
  209. doAssert calls == 1
  210. reset(b)
  211. doAssert a == b
  212. reset(c)
  213. doAssert c == nil
  214. block: # strings
  215. var calls = 0
  216. proc stringtest(s: var string): var string =
  217. calls += 1
  218. s
  219. var somestr: string
  220. stringtest(somestr) &= 'a'
  221. stringtest(somestr) &= 'b'
  222. doAssert calls == 2
  223. doAssert somestr == "ab"
  224. stringtest(somestr) &= "woot!"
  225. doAssert somestr == "abwoot!"
  226. doAssert calls == 3
  227. doAssert stringtest(somestr).len == 7
  228. doAssert calls == 4
  229. doAssert high(stringtest(somestr)) == 6
  230. doAssert calls == 5
  231. var somestr2: string
  232. stringtest(somestr2).setLen(stringtest(somestr).len)
  233. doAssert calls == 7
  234. doAssert somestr2.len == somestr.len
  235. var somestr3: string
  236. doAssert (somestr3 & "foo") == "foo"
  237. block:
  238. var a, b, c, d: string
  239. d = a & b & c
  240. doAssert d == ""
  241. d = stringtest(a) & stringtest(b) & stringtest(c)
  242. doAssert calls == 10
  243. doAssert d == ""
  244. block: # seqs
  245. var calls = 0
  246. proc seqtest(s: var seq[int]): var seq[int] =
  247. calls += 1
  248. s
  249. var someseq: seq[int]
  250. seqtest(someseq) &= 1
  251. seqtest(someseq) &= 2
  252. doAssert calls == 2
  253. doAssert someseq == @[1, 2]
  254. seqtest(someseq) &= @[3, 4, 5]
  255. doAssert someseq == @[1, 2, 3, 4, 5]
  256. doAssert calls == 3
  257. doAssert seqtest(someseq).len == 5
  258. doAssert calls == 4
  259. doAssert high(seqtest(someseq)) == 4
  260. doAssert calls == 5
  261. # genArrayAddr
  262. doAssert seqtest(someseq)[2] == 3
  263. doAssert calls == 6
  264. seqtest(someseq).setLen(seqtest(someseq).len)
  265. doAssert calls == 8
  266. var somenilseq: seq[int]
  267. seqtest(somenilseq).setLen(3)
  268. doAssert calls == 9
  269. doAssert somenilseq[1] == 0
  270. someseq = @[1, 2, 3]
  271. doAssert (seqtest(someseq) & seqtest(someseq)) == @[1, 2, 3, 1, 2, 3]
  272. block: # mInc, mDec
  273. var calls = 0
  274. proc someint(x: var int): var int =
  275. calls += 1
  276. x
  277. var x = 10
  278. inc(someint(x))
  279. doAssert x == 11
  280. doAssert calls == 1
  281. dec(someint(x))
  282. doAssert x == 10
  283. doAssert calls == 2
  284. block: # uints
  285. var calls = 0
  286. proc passuint(x: var uint32): var uint32 =
  287. calls += 1
  288. x
  289. var u: uint32 = 5
  290. passuint(u) += 1
  291. doAssert u == 6
  292. doAssert calls == 1
  293. passuint(u) -= 1
  294. doAssert u == 5
  295. doAssert calls == 2
  296. passuint(u) *= 2
  297. doAssert u == 10
  298. doAssert calls == 3
  299. block: # objs
  300. type Thing = ref object
  301. x, y: int
  302. var a, b: Thing
  303. a = Thing()
  304. b = a
  305. doAssert a == b
  306. var calls = 0
  307. proc passobj(o: var Thing): var Thing =
  308. calls += 1
  309. o
  310. passobj(b) = Thing(x: 123)
  311. doAssert calls == 1
  312. doAssert a != b
  313. doAssert b.x == 123
  314. var passobjptr_calls = 0
  315. proc passobjptr(o: var Thing): ptr Thing =
  316. passobjptr_calls += 1
  317. o.addr
  318. passobjptr(b)[] = Thing(x: 234)
  319. doAssert passobjptr_calls == 1
  320. doAssert a != b
  321. doAssert b.x == 234
  322. passobjptr(b)[].x = 500
  323. doAssert b.x == 500
  324. var pptr = passobjptr(b)
  325. pptr.x += 100
  326. doAssert b.x == 600
  327. proc getuninitptr(): ptr int =
  328. return
  329. doAssert getuninitptr() == nil
  330. block: # pointer casting
  331. var obj = (x: 321, y: 543)
  332. var x = 500
  333. var objptr = obj.addr
  334. var xptr = x.addr
  335. var p1, p2: pointer
  336. p1 = cast[pointer](objptr)
  337. p2 = cast[pointer](xptr)
  338. doAssert p1 != p2
  339. p1 = cast[pointer](objptr)
  340. p2 = cast[pointer](objptr)
  341. doAssert p1 == p2
  342. let objptr2 = cast[type(objptr)](p2)
  343. doAssert objptr == objptr2
  344. p1 = cast[pointer](xptr)
  345. p2 = cast[pointer](xptr)
  346. doAssert p1 == p2
  347. let xptr2 = cast[type(xptr)](p2)
  348. doAssert xptr == xptr2
  349. when false:
  350. block: # openarray
  351. # Error: internal error: genAddr: nkStmtListExpr
  352. var calls = 0
  353. proc getvarint(x: var openarray[int]): var int =
  354. calls += 1
  355. if true:
  356. x[1]
  357. else:
  358. x[0]
  359. var arr = [1, 2, 3]
  360. getvarint(arr) += 5
  361. doAssert calls == 1
  362. doAssert arr[1] == 7
  363. proc tests_in_proc =
  364. tests
  365. # since pointers are handled differently in global/local contexts
  366. # let's just run all of them twice
  367. tests_in_proc()
  368. tests