tostring.nim 2.3 KB

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