tstring.nim 3.0 KB

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