tostring.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. doAssert "@[23, 45]" == $(@[23, 45])
  2. doAssert "[32, 45]" == $([32, 45])
  3. doAssert """@["", "foo", "bar"]""" == $(@["", "foo", "bar"])
  4. doAssert """["", "foo", "bar"]""" == $(["", "foo", "bar"])
  5. doAssert """["", "foo", "bar"]""" == $(@["", "foo", "bar"].toOpenArray(0, 2))
  6. # bug #2395
  7. let alphaSet: set[char] = {'a'..'c'}
  8. doAssert "{'a', 'b', 'c'}" == $alphaSet
  9. doAssert "2.3242" == $(2.3242)
  10. doAssert "2.982" == $(2.982)
  11. doAssert "123912.1" == $(123912.1)
  12. doAssert "123912.1823" == $(123912.1823)
  13. doAssert "5.0" == $(5.0)
  14. doAssert "1e+100" == $(1e100)
  15. doAssert "inf" == $(1e1000000)
  16. doAssert "-inf" == $(-1e1000000)
  17. doAssert "nan" == $(0.0/0.0)
  18. # nil tests
  19. # maybe a bit inconsistent in types
  20. var x: seq[string]
  21. doAssert "@[]" == $(x)
  22. var y: string
  23. doAssert "" == $(y)
  24. type
  25. Foo = object
  26. a: int
  27. b: string
  28. var foo1: Foo
  29. # nil string should be an some point in time equal to the empty string
  30. doAssert(($foo1)[0..9] == "(a: 0, b: ")
  31. const
  32. data = @['a','b', '\0', 'c','d']
  33. dataStr = $data
  34. # ensure same result when on VM or when at program execution
  35. doAssert dataStr == $data
  36. import strutils
  37. # array test
  38. let arr = ['H','e','l','l','o',' ','W','o','r','l','d','!','\0']
  39. doAssert $arr == "['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\\x00']"
  40. doAssert $cast[cstring](addr arr) == "Hello World!"
  41. proc takes(c: cstring) =
  42. doAssert c == cstring""
  43. proc testm() =
  44. var x: string
  45. # nil is mapped to "":
  46. takes(x)
  47. testm()
  48. # nil tests
  49. var xx: seq[string]
  50. var yy: string
  51. doAssert xx == @[]
  52. doAssert yy == ""
  53. proc bar(arg: cstring) =
  54. doAssert arg[0] == '\0'
  55. proc baz(arg: openArray[char]) =
  56. doAssert arg.len == 0
  57. proc stringCompare() =
  58. var a,b,c,d,e,f,g: string
  59. a.add 'a'
  60. doAssert a == "a"
  61. b.add "bee"
  62. doAssert b == "bee"
  63. b.add g
  64. doAssert b == "bee"
  65. c.addFloat 123.456
  66. doAssert c == "123.456"
  67. d.addInt 123456
  68. doAssert d == "123456"
  69. doAssert e == ""
  70. doAssert "" == e
  71. doAssert f == g
  72. doAssert "" == ""
  73. g.setLen(10)
  74. doAssert g == "\0\0\0\0\0\0\0\0\0\0"
  75. doAssert "" != "\0\0\0\0\0\0\0\0\0\0"
  76. var nilstring: string
  77. #bar(nilstring)
  78. baz(nilstring)
  79. stringCompare()
  80. var nilstring: string
  81. bar(nilstring)
  82. static:
  83. stringCompare()
  84. # issue #8847
  85. var a2: cstring = "fo\"o2"
  86. block:
  87. var s: string
  88. s.addQuoted a2
  89. doAssert s == "\"fo\\\"o2\""
  90. # issue #16650
  91. template fn() =
  92. doAssert len(cstring"ab\0c") == 5
  93. doAssert len(cstring("ab\0c")) == 2
  94. when nimvm:
  95. discard
  96. else:
  97. let c = cstring("ab\0c")
  98. doAssert len(c) == 2
  99. fn()
  100. static: fn()