tropes.nim 1.9 KB

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