tdollars.nim 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. discard """
  2. matrix: "--mm:refc; --mm:orc; --backend:cpp; --backend:js --jsbigint64:off -d:nimStringHash2; --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. when hasWorkingInt64:
  58. testType uint64
  59. testType int64
  60. testType BiggestInt
  61. block: # #14350, #16674, #16686 for JS
  62. var cstr: cstring
  63. doAssert cstr == cstring(nil)
  64. doAssert cstr == nil
  65. doAssert cstr.isNil
  66. doAssert cstr != cstring("")
  67. doAssert cstr.len == 0
  68. when defined(js):
  69. cstr.add(cstring("abc"))
  70. doAssert cstr == cstring("abc")
  71. var nil1, nil2: cstring = nil
  72. nil1.add(nil2)
  73. doAssert nil1 == cstring(nil)
  74. doAssert nil2 == cstring(nil)
  75. nil1.add(cstring(""))
  76. doAssert nil1 == cstring("")
  77. doAssert nil2 == cstring(nil)
  78. nil1.add(nil2)
  79. doAssert nil1 == cstring("")
  80. doAssert nil2 == cstring(nil)
  81. nil2.add(nil1)
  82. doAssert nil1 == cstring("")
  83. doAssert nil2 == cstring("")
  84. block:
  85. when defined(js): # bug #18591
  86. let a1 = -1'i8
  87. let a2 = uint8(a1)
  88. # if `uint8(a1)` changes meaning to `cast[uint8](a1)` in future, update this test;
  89. # until then, this is the correct semantics.
  90. let a3 = $a2
  91. doAssert a2 == 255'u8
  92. doAssert a3 == "255"
  93. proc intToStr(a: uint8): cstring {.importjs: "(# + \"\")".}
  94. doAssert $intToStr(a2) == "255"
  95. else:
  96. block:
  97. let x = -1'i8
  98. let y = uint32(x)
  99. doAssert $y == "4294967295"
  100. block:
  101. let x = -1'i16
  102. let y = uint32(x)
  103. doAssert $y == "4294967295"
  104. block:
  105. let x = -1'i32
  106. let y = uint32(x)
  107. doAssert $y == "4294967295"
  108. block:
  109. proc foo1(arg: int): string =
  110. let x = uint32(arg)
  111. $x
  112. doAssert $foo1(-1) == "4294967295"
  113. block:
  114. let x = 4294967295'u32
  115. doAssert $x == "4294967295"
  116. block:
  117. doAssert $(4294967295'u32) == "4294967295"
  118. proc main()=
  119. block:
  120. let a = -0.0
  121. doAssert $a == "-0.0"
  122. doAssert $(-0.0) == "-0.0"
  123. block:
  124. let a = 0.0
  125. doAssert $a == "0.0"
  126. doAssert $(0.0) == "0.0"
  127. block:
  128. let b = -0
  129. doAssert $b == "0"
  130. doAssert $(-0) == "0"
  131. block:
  132. let b = 0
  133. doAssert $b == "0"
  134. doAssert $(0) == "0"
  135. doAssert $uint32.high == "4294967295"
  136. block: # addInt
  137. var res = newStringOfCap(24)
  138. template test2(a, b) =
  139. res.setLen(0)
  140. res.addInt a
  141. doAssert res == b
  142. for i in 0 .. 9:
  143. res.addInt int64(i)
  144. doAssert res == "0123456789"
  145. res.setLen(0)
  146. for i in -9 .. 0:
  147. res.addInt int64(i)
  148. doAssert res == "-9-8-7-6-5-4-3-2-10"
  149. when hasWorkingInt64:
  150. test2 high(int64), "9223372036854775807"
  151. test2 low(int64), "-9223372036854775808"
  152. test2 high(int32), "2147483647"
  153. test2 low(int32), "-2147483648"
  154. test2 high(int16), "32767"
  155. test2 low(int16), "-32768"
  156. test2 high(int8), "127"
  157. test2 low(int8), "-128"
  158. block:
  159. const
  160. a: array[3, char] = ['N', 'i', 'm']
  161. aStr = $(a)
  162. doAssert aStr == """['N', 'i', 'm']"""
  163. static: main()
  164. main()