tstring.nim 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. discard """
  2. output: "OK"
  3. """
  4. const characters = "abcdefghijklmnopqrstuvwxyz"
  5. const numbers = "1234567890"
  6. var s: string
  7. proc test_string_slice() =
  8. # test "slice of length == len(characters)":
  9. # replace characters completely by numbers
  10. s = characters
  11. s[0..^1] = numbers
  12. doAssert s == numbers
  13. # test "slice of length > len(numbers)":
  14. # replace characters by slice of same length
  15. s = characters
  16. s[1..16] = numbers
  17. doAssert s == "a1234567890rstuvwxyz"
  18. # test "slice of length == len(numbers)":
  19. # replace characters by slice of same length
  20. s = characters
  21. s[1..10] = numbers
  22. doAssert s == "a1234567890lmnopqrstuvwxyz"
  23. # test "slice of length < len(numbers)":
  24. # replace slice of length. and insert remaining chars
  25. s = characters
  26. s[1..4] = numbers
  27. doAssert s == "a1234567890fghijklmnopqrstuvwxyz"
  28. # test "slice of length == 1":
  29. # replace first character. and insert remaining 9 chars
  30. s = characters
  31. s[1..1] = numbers
  32. doAssert s == "a1234567890cdefghijklmnopqrstuvwxyz"
  33. # test "slice of length == 0":
  34. # insert chars at slice start index
  35. s = characters
  36. s[2..1] = numbers
  37. doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"
  38. # test "slice of negative length":
  39. # same as slice of zero length
  40. s = characters
  41. s[2..0] = numbers
  42. doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"
  43. # bug #6223
  44. doAssertRaises(IndexError):
  45. discard s[0..999]
  46. echo("OK")
  47. proc test_string_cmp() =
  48. let world = "hello\0world"
  49. let earth = "hello\0earth"
  50. let short = "hello\0"
  51. let hello = "hello"
  52. let goodbye = "goodbye"
  53. doAssert world == world
  54. doAssert world != earth
  55. doAssert world != short
  56. doAssert world != hello
  57. doAssert world != goodbye
  58. doAssert cmp(world, world) == 0
  59. doAssert cmp(world, earth) > 0
  60. doAssert cmp(world, short) > 0
  61. doAssert cmp(world, hello) > 0
  62. doAssert cmp(world, goodbye) > 0
  63. test_string_slice()
  64. test_string_cmp()