trepr.nim 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. discard """
  2. action: run
  3. """
  4. block ints:
  5. let
  6. na: int8 = -120'i8
  7. nb: int16 = -32700'i16
  8. nc: int32 = -2147483000'i32
  9. nd: int64 = -9223372036854775000'i64
  10. ne: int = -1234567
  11. pa: int8 = 120'i8
  12. pb: int16 = 32700'i16
  13. pc: int32 = 2147483000'i32
  14. pd: int64 = 9223372036854775000'i64
  15. pe: int = 1234567
  16. doAssert(repr(na) == "-120")
  17. doAssert(repr(nb) == "-32700")
  18. doAssert(repr(nc) == "-2147483000")
  19. doAssert(repr(nd) == "-9223372036854775000")
  20. doAssert(repr(ne) == "-1234567")
  21. doAssert(repr(pa) == "120")
  22. doAssert(repr(pb) == "32700")
  23. doAssert(repr(pc) == "2147483000")
  24. doAssert(repr(pd) == "9223372036854775000")
  25. doAssert(repr(pe) == "1234567")
  26. block uints:
  27. let
  28. a: uint8 = 254'u8
  29. b: uint16 = 65300'u16
  30. c: uint32 = 4294967290'u32
  31. # d: uint64 = 18446744073709551610'u64 -> unknown node type
  32. e: uint = 1234567
  33. doAssert(repr(a) == "254")
  34. doAssert(repr(b) == "65300")
  35. doAssert(repr(c) == "4294967290")
  36. # doAssert(repr(d) == "18446744073709551610")
  37. doAssert(repr(e) == "1234567")
  38. block floats:
  39. let
  40. a: float32 = 3.4e38'f32
  41. b: float64 = 1.7976931348623157e308'f64
  42. c: float = 1234.567e89
  43. when defined js:
  44. doAssert(repr(a) == "3.4e+38") # in C: 3.399999952144364e+038
  45. doAssert(repr(b) == "1.7976931348623157e+308") # in C: 1.797693134862316e+308
  46. doAssert(repr(c) == "1.234567e+92") # in C: 1.234567e+092
  47. block bools:
  48. let
  49. a: bool = true
  50. b: bool = false
  51. doAssert(repr(a) == "true")
  52. doAssert(repr(b) == "false")
  53. block enums:
  54. type
  55. AnEnum = enum
  56. aeA
  57. aeB
  58. aeC
  59. HoledEnum = enum
  60. heA = -12
  61. heB = 15
  62. heC = 123
  63. doAssert(repr(aeA) == "aeA")
  64. doAssert(repr(aeB) == "aeB")
  65. doAssert(repr(aeC) == "aeC")
  66. doAssert(repr(heA) == "heA")
  67. doAssert(repr(heB) == "heB")
  68. doAssert(repr(heC) == "heC")
  69. block emums_and_unicode: #6741
  70. type K = enum Kanji = "漢字"
  71. let kanji = Kanji
  72. doAssert(kanji == Kanji, "Enum values are not equal")
  73. doAssert($kanji == $Kanji, "Enum string values are not equal")
  74. block chars:
  75. let
  76. a = 'a'
  77. b = 'z'
  78. one = '1'
  79. nl = '\x0A'
  80. doAssert(repr(a) == "'a'")
  81. doAssert(repr(b) == "'z'")
  82. doAssert(repr(one) == "'1'")
  83. doAssert(repr(nl) == "'\\10'")
  84. block strings:
  85. let
  86. a: string = "12345"
  87. b: string = "hello,repr"
  88. c: string = "hi\nthere"
  89. when defined js: # C prepends the pointer, JS does not.
  90. doAssert(repr(a) == "\"12345\"")
  91. doAssert(repr(b) == "\"hello,repr\"")
  92. doAssert(repr(c) == "\"hi\\10there\"")
  93. block sets:
  94. let
  95. a: set[int16] = {1'i16, 2'i16, 3'i16}
  96. b: set[char] = {'A', 'k'}
  97. doAssert(repr(a) == "{1, 2, 3}")
  98. doAssert(repr(b) == "{'A', 'k'}")
  99. block ranges:
  100. let
  101. a: range[0..12] = 6
  102. b: range[-12..0] = -6
  103. doAssert(repr(a) == "6")
  104. doAssert(repr(b) == "-6")
  105. block tuples:
  106. type
  107. ATuple = tuple
  108. a: int
  109. b: float
  110. c: string
  111. d: OtherTuple
  112. OtherTuple = tuple
  113. a: bool
  114. b: int8
  115. let
  116. ot: OtherTuple = (a: true, b: 120'i8)
  117. t: ATuple = (a: 42, b: 12.34, c: "tuple", d: ot)
  118. when defined js:
  119. doAssert(repr(ot) == """
  120. [Field0 = true,
  121. Field1 = 120]
  122. """)
  123. doAssert(repr(t) == """
  124. [Field0 = 42,
  125. Field1 = 12.34,
  126. Field2 = "tuple",
  127. Field3 = [Field0 = true,
  128. Field1 = 120]]
  129. """)
  130. block objects:
  131. type
  132. AnObj = object
  133. a: int
  134. b: float
  135. c: OtherObj
  136. OtherObj = object
  137. a: bool
  138. b: int8
  139. let
  140. oo: OtherObj = OtherObj(a: true, b: 120'i8)
  141. o: AnObj = AnObj(a: 42, b: 12.34, c: oo)
  142. doAssert(repr(oo) == """
  143. [a = true,
  144. b = 120]
  145. """)
  146. doAssert(repr(o) == """
  147. [a = 42,
  148. b = 12.34,
  149. c = [a = true,
  150. b = 120]]
  151. """)
  152. block arrays:
  153. type
  154. AObj = object
  155. x: int
  156. y: array[3,float]
  157. let
  158. a = [0.0, 1, 2]
  159. b = [a, a, a]
  160. o = AObj(x: 42, y: a)
  161. c = [o, o, o]
  162. d = ["hi", "array", "!"]
  163. doAssert(repr(a) == "[0.0, 1.0, 2.0]\n")
  164. doAssert(repr(b) == "[[0.0, 1.0, 2.0], [0.0, 1.0, 2.0], [0.0, 1.0, 2.0]]\n")
  165. doAssert(repr(c) == """
  166. [[x = 42,
  167. y = [0.0, 1.0, 2.0]], [x = 42,
  168. y = [0.0, 1.0, 2.0]], [x = 42,
  169. y = [0.0, 1.0, 2.0]]]
  170. """)
  171. doAssert(repr(d) == "[\"hi\", \"array\", \"!\"]\n")
  172. block seqs:
  173. type
  174. AObj = object
  175. x: int
  176. y: seq[float]
  177. let
  178. a = @[0.0, 1, 2]
  179. b = @[a, a, a]
  180. o = AObj(x: 42, y: a)
  181. c = @[o, o, o]
  182. d = @["hi", "array", "!"]
  183. doAssert(repr(a) == "@[0.0, 1.0, 2.0]\n")
  184. doAssert(repr(b) == "@[@[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0]]\n")
  185. doAssert(repr(c) == """
  186. @[[x = 42,
  187. y = @[0.0, 1.0, 2.0]], [x = 42,
  188. y = @[0.0, 1.0, 2.0]], [x = 42,
  189. y = @[0.0, 1.0, 2.0]]]
  190. """)
  191. doAssert(repr(d) == "@[\"hi\", \"array\", \"!\"]\n")
  192. block ptrs:
  193. type
  194. AObj = object
  195. x: ptr array[2, AObj]
  196. y: int
  197. var
  198. a = [12.0, 13.0, 14.0]
  199. b = addr a[0]
  200. c = addr a[2]
  201. d = AObj()
  202. doAssert(repr(a) == "[12.0, 13.0, 14.0]\n")
  203. doAssert(repr(b) == "ref 0 --> 12.0\n")
  204. doAssert(repr(c) == "ref 2 --> 14.0\n")
  205. doAssert(repr(d) == """
  206. [x = nil,
  207. y = 0]
  208. """)
  209. block ptrs:
  210. type
  211. AObj = object
  212. x: ref array[2, AObj]
  213. y: int
  214. var
  215. a = AObj()
  216. new(a.x)
  217. doAssert(repr(a) == """
  218. [x = ref 0 --> [[x = nil,
  219. y = 0], [x = nil,
  220. y = 0]],
  221. y = 0]
  222. """)
  223. block procs:
  224. proc test(): int =
  225. echo "hello"
  226. var
  227. ptest = test
  228. nilproc: proc(): int
  229. doAssert(repr(test) == "0\n")
  230. doAssert(repr(ptest) == "0\n")
  231. doAssert(repr(nilproc) == "nil\n")
  232. block bunch:
  233. type
  234. AnEnum = enum
  235. eA, eB, eC
  236. B = object
  237. a: string
  238. b: seq[char]
  239. A = object
  240. a: uint32
  241. b: int
  242. c: float
  243. d: char
  244. e: AnEnum
  245. f: string
  246. g: set[char]
  247. h: set[int16]
  248. i: array[3,string]
  249. j: seq[string]
  250. k: range[-12..12]
  251. l: B
  252. m: ref B
  253. n: ptr B
  254. o: tuple[x: B, y: string]
  255. p: proc(b: B): ref B
  256. q: cstring
  257. proc refB(b:B):ref B =
  258. new result
  259. result[] = b
  260. var
  261. aa: A
  262. bb: B = B(a: "inner", b: @['o', 'b', 'j'])
  263. cc: A = A(a: 12, b: 1, c: 1.2, d: '\0', e: eC,
  264. f: "hello", g: {'A'}, h: {2'i16},
  265. i: ["hello", "world", "array"],
  266. j: @["hello", "world", "seq"], k: -1,
  267. l: bb, m: refB(bb), n: addr bb,
  268. o: (bb, "tuple!"), p: refB, q: "cstringtest" )
  269. doAssert(repr(aa) == """
  270. [a = 0,
  271. b = 0,
  272. c = 0.0,
  273. d = '\0',
  274. e = eA,
  275. f = "",
  276. g = {},
  277. h = {},
  278. i = ["", "", ""],
  279. j = @[],
  280. k = 0,
  281. l = [a = "",
  282. b = @[]],
  283. m = nil,
  284. n = nil,
  285. o = [Field0 = [a = "",
  286. b = @[]],
  287. Field1 = ""],
  288. p = nil,
  289. q = nil]
  290. """)
  291. doAssert(repr(cc) == """
  292. [a = 12,
  293. b = 1,
  294. c = 1.2,
  295. d = '\0',
  296. e = eC,
  297. f = "hello",
  298. g = {'A'},
  299. h = {2},
  300. i = ["hello", "world", "array"],
  301. j = @["hello", "world", "seq"],
  302. k = -1,
  303. l = [a = "inner",
  304. b = @['o', 'b', 'j']],
  305. m = ref 0 --> [a = "inner",
  306. b = @['o', 'b', 'j']],
  307. n = ref 0 --> [a = "inner",
  308. b = @['o', 'b', 'j']],
  309. o = [Field0 = [a = "inner",
  310. b = @['o', 'b', 'j']],
  311. Field1 = "tuple!"],
  312. p = 0,
  313. q = "cstringtest"]
  314. """)
  315. block another:
  316. type
  317. Size1 = enum
  318. s1a, s1b
  319. Size2 = enum
  320. s2c=0, s2d=20000
  321. Size3 = enum
  322. s3e=0, s3f=2000000000
  323. doAssert(repr([s1a, s1b]) == "[s1a, s1b]\n")
  324. doAssert(repr([s2c, s2d]) == "[s2c, s2d]\n")
  325. doAssert(repr([s3e, s3f]) == "[s3e, s3f]\n")
  326. block another2:
  327. type
  328. AnEnum = enum
  329. en1, en2, en3, en4, en5, en6
  330. Point {.final.} = object
  331. x, y, z: int
  332. s: array[0..1, string]
  333. e: AnEnum
  334. var
  335. p: Point
  336. q: ref Point
  337. s: seq[ref Point]
  338. p.x = 0
  339. p.y = 13
  340. p.z = 45
  341. p.s[0] = "abc"
  342. p.s[1] = "xyz"
  343. p.e = en6
  344. new(q)
  345. q[] = p
  346. s = @[q, q, q, q]
  347. doAssert(repr(p) == """
  348. [x = 0,
  349. y = 13,
  350. z = 45,
  351. s = ["abc", "xyz"],
  352. e = en6]
  353. """)
  354. doAssert(repr(q) == """
  355. ref 0 --> [x = 0,
  356. y = 13,
  357. z = 45,
  358. s = ["abc", "xyz"],
  359. e = en6]
  360. """)
  361. doAssert(repr(s) == """
  362. @[ref 0 --> [x = 0,
  363. y = 13,
  364. z = 45,
  365. s = ["abc", "xyz"],
  366. e = en6], ref 1 --> [x = 0,
  367. y = 13,
  368. z = 45,
  369. s = ["abc", "xyz"],
  370. e = en6], ref 2 --> [x = 0,
  371. y = 13,
  372. z = 45,
  373. s = ["abc", "xyz"],
  374. e = en6], ref 3 --> [x = 0,
  375. y = 13,
  376. z = 45,
  377. s = ["abc", "xyz"],
  378. e = en6]]
  379. """)
  380. doAssert(repr(en4) == "en4")
  381. doAssert(repr({'a'..'p'}) == "{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'}")