tropes.nim 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. discard """
  2. targets: "c js"
  3. """
  4. import std/ropes
  5. import std/assertions
  6. template main() =
  7. block:
  8. let r: Rope = nil
  9. doAssert r[0] == '\0'
  10. doAssert $r == ""
  11. block:
  12. var
  13. r1 = rope("Hello, ")
  14. r2 = rope("Nim-Lang")
  15. let r = r1 & r2
  16. let s = $r
  17. doAssert s == "Hello, Nim-Lang"
  18. for i in 0 ..< r.len:
  19. doAssert r[i] == s[i]
  20. doAssert r[66] == '\0'
  21. block:
  22. let r = rope("Hello, Nim-Lang")
  23. let s = $r
  24. doAssert s == "Hello, Nim-Lang"
  25. for i in 0 ..< r.len:
  26. doAssert r[i] == s[i]
  27. doAssert r[66] == '\0'
  28. block:
  29. var r: Rope
  30. r.add rope("Nim ")
  31. r.add rope("is ")
  32. r.add rope("a ")
  33. r.add rope("great ")
  34. r.add rope("language")
  35. let s = $r
  36. doAssert s == "Nim is a great language"
  37. for i in 0 ..< r.len:
  38. doAssert r[i] == s[i]
  39. doAssert r[66] == '\0'
  40. block:
  41. var r: Rope
  42. r.add rope("My Conquest")
  43. r.add rope(" is ")
  44. r.add rope("the Sea of Stars")
  45. let s = $r
  46. doAssert s == "My Conquest is the Sea of Stars"
  47. for i in 0 ..< r.len:
  48. doAssert r[i] == s[i]
  49. doAssert r[66] == '\0'
  50. block:
  51. var r: Rope
  52. r.add rope("My Conquest")
  53. r.add rope(" is ")
  54. r.add rope("the Sea of Stars")
  55. doAssert $r == "My Conquest is the Sea of Stars"
  56. var i: int
  57. for item in r:
  58. doAssert r[i] == item
  59. inc i
  60. doAssert r[66] == '\0'
  61. block:
  62. let r1 = "$1 $2 $3" % [rope("Nim"), rope("is"), rope("a great language")]
  63. doAssert $r1 == "Nim is a great language"
  64. let r2 = "$# $# $#" % [rope("Nim"), rope("is"), rope("a great language")]
  65. doAssert $r2 == "Nim is a great language"
  66. block: # `[]`
  67. let r1 = rope("Hello, Nim!")
  68. doAssert r1[-2] == '\0'
  69. doAssert r1[0] == 'H'
  70. doAssert r1[7] == 'N'
  71. doAssert r1[22] == '\0'
  72. let r2 = rope("Hello") & rope(", Nim!")
  73. doAssert r2[-2] == '\0'
  74. doAssert r2[0] == 'H'
  75. doAssert r2[7] == 'N'
  76. doAssert r2[22] == '\0'
  77. static: main()
  78. main()