tstring.nim 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. discard """
  2. output: '''OK
  3. @[@[], @[], @[], @[], @[]]
  4. '''
  5. """
  6. const characters = "abcdefghijklmnopqrstuvwxyz"
  7. const numbers = "1234567890"
  8. var s: string
  9. proc test_string_slice() =
  10. # test "slice of length == len(characters)":
  11. # replace characters completely by numbers
  12. s = characters
  13. s[0..^1] = numbers
  14. doAssert s == numbers
  15. # test "slice of length > len(numbers)":
  16. # replace characters by slice of same length
  17. s = characters
  18. s[1..16] = numbers
  19. doAssert s == "a1234567890rstuvwxyz"
  20. # test "slice of length == len(numbers)":
  21. # replace characters by slice of same length
  22. s = characters
  23. s[1..10] = numbers
  24. doAssert s == "a1234567890lmnopqrstuvwxyz"
  25. # test "slice of length < len(numbers)":
  26. # replace slice of length. and insert remaining chars
  27. s = characters
  28. s[1..4] = numbers
  29. doAssert s == "a1234567890fghijklmnopqrstuvwxyz"
  30. # test "slice of length == 1":
  31. # replace first character. and insert remaining 9 chars
  32. s = characters
  33. s[1..1] = numbers
  34. doAssert s == "a1234567890cdefghijklmnopqrstuvwxyz"
  35. # test "slice of length == 0":
  36. # insert chars at slice start index
  37. s = characters
  38. s[2..1] = numbers
  39. doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"
  40. # test "slice of negative length":
  41. # same as slice of zero length
  42. s = characters
  43. s[2..0] = numbers
  44. doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"
  45. # bug #6223
  46. doAssertRaises(IndexDefect):
  47. discard s[0..999]
  48. echo("OK")
  49. proc test_string_cmp() =
  50. let world = "hello\0world"
  51. let earth = "hello\0earth"
  52. let short = "hello\0"
  53. let hello = "hello"
  54. let goodbye = "goodbye"
  55. doAssert world == world
  56. doAssert world != earth
  57. doAssert world != short
  58. doAssert world != hello
  59. doAssert world != goodbye
  60. doAssert cmp(world, world) == 0
  61. doAssert cmp(world, earth) > 0
  62. doAssert cmp(world, short) > 0
  63. doAssert cmp(world, hello) > 0
  64. doAssert cmp(world, goodbye) > 0
  65. test_string_slice()
  66. test_string_cmp()
  67. #--------------------------
  68. # bug #7816
  69. import sugar
  70. import sequtils
  71. proc tester[T](x: T) =
  72. let test = toSeq(0..4).map(i => newSeq[int]())
  73. echo test
  74. tester(1)
  75. # #14497
  76. func reverse*(a: string): string =
  77. result = a
  78. for i in 0 ..< a.len div 2:
  79. swap(result[i], result[^(i + 1)])
  80. doAssert reverse("hello") == "olleh"