tdollars.nim 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. discard """
  2. matrix: "--mm:refc; --mm:orc; --backend:cpp; --backend:js --jsbigint64:off; --backend:js --jsbigint64:on"
  3. """
  4. #[
  5. if https://github.com/nim-lang/Nim/pull/14043 is merged (or at least its
  6. tests/system/tostring.nim diff subset), merge
  7. tests/system/tostring.nim into this file, named after dollars.nim
  8. The goal is to increase test coverage across backends while minimizing test code
  9. duplication (which always results in weaker test coverage in practice).
  10. ]#
  11. import std/unittest
  12. import std/private/jsutils
  13. template test[T](a: T, expected: string) =
  14. check $a == expected
  15. var b = a
  16. check $b == expected
  17. static:
  18. doAssert $a == expected
  19. template testType(T: typedesc) =
  20. when T is bool:
  21. test true, "true"
  22. test false, "false"
  23. elif T is char:
  24. test char, "\0"
  25. test char.high, static($T.high)
  26. else:
  27. test T.default, "0"
  28. test 1.T, "1"
  29. test T.low, static($T.low)
  30. test T.high, static($T.high)
  31. block: # `$`(SomeInteger)
  32. # direct tests
  33. check $0'u8 == "0"
  34. check $255'u8 == "255"
  35. check $(-127'i8) == "-127"
  36. # known limitation: Error: number out of range: '128'i8',
  37. # see https://github.com/timotheecour/Nim/issues/125
  38. # check $(-128'i8) == "-128"
  39. check $int8.low == "-128"
  40. check $int8(-128) == "-128"
  41. check $cast[int8](-128) == "-128"
  42. var a = 12345'u16
  43. check $a == "12345"
  44. check $12345678'u64 == "12345678"
  45. check $12345678'i64 == "12345678"
  46. check $(-12345678'i64) == "-12345678"
  47. # systematic tests
  48. testType uint8
  49. testType uint16
  50. testType uint32
  51. testType uint
  52. testType int8
  53. testType int16
  54. testType int32
  55. testType int
  56. testType bool
  57. whenJsNoBigInt64: discard
  58. do:
  59. testType uint64
  60. testType int64
  61. testType BiggestInt
  62. block: # #14350, #16674, #16686 for JS
  63. var cstr: cstring
  64. doAssert cstr == cstring(nil)
  65. doAssert cstr == nil
  66. doAssert cstr.isNil
  67. doAssert cstr != cstring("")
  68. doAssert cstr.len == 0
  69. when defined(js):
  70. cstr.add(cstring("abc"))
  71. doAssert cstr == cstring("abc")
  72. var nil1, nil2: cstring = nil
  73. nil1.add(nil2)
  74. doAssert nil1 == cstring(nil)
  75. doAssert nil2 == cstring(nil)
  76. nil1.add(cstring(""))
  77. doAssert nil1 == cstring("")
  78. doAssert nil2 == cstring(nil)
  79. nil1.add(nil2)
  80. doAssert nil1 == cstring("")
  81. doAssert nil2 == cstring(nil)
  82. nil2.add(nil1)
  83. doAssert nil1 == cstring("")
  84. doAssert nil2 == cstring("")
  85. block:
  86. when defined(js): # bug #18591
  87. let a1 = -1'i8
  88. let a2 = uint8(a1)
  89. # if `uint8(a1)` changes meaning to `cast[uint8](a1)` in future, update this test;
  90. # until then, this is the correct semantics.
  91. let a3 = $a2
  92. doAssert a2 < 3
  93. doAssert a3 == "-1"
  94. proc intToStr(a: uint8): cstring {.importjs: "(# + \"\")".}
  95. doAssert $intToStr(a2) == "-1"
  96. else:
  97. block:
  98. let x = -1'i8
  99. let y = uint32(x)
  100. doAssert $y == "4294967295"
  101. block:
  102. let x = -1'i16
  103. let y = uint32(x)
  104. doAssert $y == "4294967295"
  105. block:
  106. let x = -1'i32
  107. let y = uint32(x)
  108. doAssert $y == "4294967295"
  109. block:
  110. proc foo1(arg: int): string =
  111. let x = uint32(arg)
  112. $x
  113. doAssert $foo1(-1) == "4294967295"
  114. block:
  115. let x = 4294967295'u32
  116. doAssert $x == "4294967295"
  117. block:
  118. doAssert $(4294967295'u32) == "4294967295"
  119. proc main()=
  120. block:
  121. let a = -0.0
  122. doAssert $a == "-0.0"
  123. doAssert $(-0.0) == "-0.0"
  124. block:
  125. let a = 0.0
  126. doAssert $a == "0.0"
  127. doAssert $(0.0) == "0.0"
  128. block:
  129. let b = -0
  130. doAssert $b == "0"
  131. doAssert $(-0) == "0"
  132. block:
  133. let b = 0
  134. doAssert $b == "0"
  135. doAssert $(0) == "0"
  136. doAssert $uint32.high == "4294967295"
  137. block: # addInt
  138. var res = newStringOfCap(24)
  139. template test2(a, b) =
  140. res.setLen(0)
  141. res.addInt a
  142. doAssert res == b
  143. for i in 0 .. 9:
  144. res.addInt int64(i)
  145. doAssert res == "0123456789"
  146. res.setLen(0)
  147. for i in -9 .. 0:
  148. res.addInt int64(i)
  149. doAssert res == "-9-8-7-6-5-4-3-2-10"
  150. whenJsNoBigInt64: discard
  151. do:
  152. test2 high(int64), "9223372036854775807"
  153. test2 low(int64), "-9223372036854775808"
  154. test2 high(int32), "2147483647"
  155. test2 low(int32), "-2147483648"
  156. test2 high(int16), "32767"
  157. test2 low(int16), "-32768"
  158. test2 high(int8), "127"
  159. test2 low(int8), "-128"
  160. block:
  161. const
  162. a: array[3, char] = ['N', 'i', 'm']
  163. aStr = $(a)
  164. doAssert aStr == """['N', 'i', 'm']"""
  165. static: main()
  166. main()