tstring.nim 2.9 KB

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