tropes.nim 2.0 KB

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