tstring.nim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. from std/sequtils import toSeq, map
  5. from std/sugar import `=>`
  6. import std/assertions
  7. proc tester[T](x: T) =
  8. let test = toSeq(0..4).map(i => newSeq[int]())
  9. doAssert $test == "@[@[], @[], @[], @[], @[]]"
  10. func reverse*(a: string): string =
  11. result = a
  12. for i in 0 ..< a.len div 2:
  13. swap(result[i], result[^(i + 1)])
  14. proc main() =
  15. block: # ..
  16. const
  17. characters = "abcdefghijklmnopqrstuvwxyz"
  18. numbers = "1234567890"
  19. # test "slice of length == len(characters)":
  20. # replace characters completely by numbers
  21. var s: string
  22. s = characters
  23. s[0..^1] = numbers
  24. doAssert s == numbers
  25. # test "slice of length > len(numbers)":
  26. # replace characters by slice of same length
  27. s = characters
  28. s[1..16] = numbers
  29. doAssert s == "a1234567890rstuvwxyz"
  30. # test "slice of length == len(numbers)":
  31. # replace characters by slice of same length
  32. s = characters
  33. s[1..10] = numbers
  34. doAssert s == "a1234567890lmnopqrstuvwxyz"
  35. # test "slice of length < len(numbers)":
  36. # replace slice of length. and insert remaining chars
  37. s = characters
  38. s[1..4] = numbers
  39. doAssert s == "a1234567890fghijklmnopqrstuvwxyz"
  40. # test "slice of length == 1":
  41. # replace first character. and insert remaining 9 chars
  42. s = characters
  43. s[1..1] = numbers
  44. doAssert s == "a1234567890cdefghijklmnopqrstuvwxyz"
  45. # test "slice of length == 0":
  46. # insert chars at slice start index
  47. s = characters
  48. s[2..1] = numbers
  49. doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"
  50. # test "slice of negative length":
  51. # same as slice of zero length
  52. s = characters
  53. s[2..0] = numbers
  54. doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"
  55. when nimvm:
  56. discard
  57. else:
  58. # bug #6223
  59. doAssertRaises(IndexDefect):
  60. discard s[0..999]
  61. block: # ==, cmp
  62. let world = "hello\0world"
  63. let earth = "hello\0earth"
  64. let short = "hello\0"
  65. let hello = "hello"
  66. let goodbye = "goodbye"
  67. doAssert world == world
  68. doAssert world != earth
  69. doAssert world != short
  70. doAssert world != hello
  71. doAssert world != goodbye
  72. doAssert cmp(world, world) == 0
  73. doAssert cmp(world, earth) > 0
  74. doAssert cmp(world, short) > 0
  75. doAssert cmp(world, hello) > 0
  76. doAssert cmp(world, goodbye) > 0
  77. block: # bug #7816
  78. tester(1)
  79. block: # bug #14497, reverse
  80. doAssert reverse("hello") == "olleh"
  81. block: # len, high
  82. var a = "ab\0cd"
  83. var b = a.cstring
  84. doAssert a.len == 5
  85. block: # bug #16405
  86. when defined(js):
  87. when nimvm: doAssert b.len == 2
  88. else: doAssert b.len == 5
  89. else: doAssert b.len == 2
  90. doAssert a.high == a.len - 1
  91. doAssert b.high == b.len - 1
  92. doAssert "".len == 0
  93. doAssert "".high == -1
  94. doAssert "".cstring.len == 0
  95. doAssert "".cstring.high == -1
  96. block: # bug #16674
  97. var c: cstring = nil
  98. doAssert c.len == 0
  99. doAssert c.high == -1
  100. static: main()
  101. main()