1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
import core::*;
import str;
import std::rope::*;
import option;
import uint;
import vec;
//Utility function, used for sanity check
fn rope_to_string(r: rope) -> str {
alt(r) {
node::empty. { ret "" }
node::content(x) {
let str = @mutable "";
fn aux(str: @mutable str, node: @node::node) {
alt(*node) {
node::leaf(x) {
*str += str::substr(*x.content, x.byte_offset, x.byte_len);
}
node::concat(x) {
aux(str, x.left);
aux(str, x.right);
}
}
}
aux(str, x);
ret *str
}
}
}
#[test]
fn trivial() {
assert char_len(empty()) == 0u;
assert byte_len(empty()) == 0u;
}
#[test]
fn of_string1() {
let sample = @"0123456789ABCDE";
let r = of_str(sample);
assert char_len(r) == str::char_len(*sample);
assert rope_to_string(r) == *sample;
}
#[test]
fn of_string2() {
let buf = @ mutable "1234567890";
let i = 0;
while i < 10 { *buf = *buf + *buf; i+=1;}
let sample = @*buf;
let r = of_str(sample);
assert char_len(r) == str::char_len(*sample);
assert rope_to_string(r) == *sample;
let string_iter = 0u;
let string_len = str::byte_len(*sample);
let rope_iter = iterator::char::start(r);
let equal = true;
let pos = 0u;
while equal {
alt(node::char_iterator::next(rope_iter)) {
option::none. {
if string_iter < string_len {
equal = false;
} break; }
option::some(c) {
let {ch, next} = str::char_range_at(*sample, string_iter);
string_iter = next;
if ch != c { equal = false; break; }
}
}
pos += 1u;
}
assert equal;
}
#[test]
fn iter1() {
let buf = @ mutable "1234567890";
let i = 0;
while i < 10 { *buf = *buf + *buf; i+=1;}
let sample = @*buf;
let r = of_str(sample);
let len = 0u;
let it = iterator::char::start(r);
while true {
alt(node::char_iterator::next(it)) {
option::none. { break; }
option::some(_) { len += 1u; }
}
}
assert len == str::char_len(*sample);
}
#[test]
fn bal1() {
let init = @ "1234567890";
let buf = @ mutable * init;
let i = 0;
while i < 16 { *buf = *buf + *buf; i+=1;}
let sample = @*buf;
let r1 = of_str(sample);
let r2 = of_str(init);
i = 0;
while i < 16 { r2 = append_rope(r2, r2); i+= 1;}
assert eq(r1, r2);
let r3 = bal(r2);
assert char_len(r1) == char_len(r3);
assert eq(r1, r3);
}
#[test]
fn char_at1() {
//Generate a large rope
let r = of_str(@ "123456789");
uint::range(0u, 10u){|_i|
r = append_rope(r, r);
}
//Copy it in the slowest possible way
let r2 = empty();
uint::range(0u, char_len(r)){|i|
r2 = append_char(r2, char_at(r, i));
}
assert eq(r, r2);
let r3 = empty();
uint::range(0u, char_len(r)){|i|
r3 = prepend_char(r3, char_at(r, char_len(r) - i - 1u));
}
assert eq(r, r3);
//Additional sanity checks
let balr = bal(r);
let bal2 = bal(r2);
let bal3 = bal(r3);
assert eq(r, balr);
assert eq(r, bal2);
assert eq(r, bal3);
assert eq(r2, r3);
assert eq(bal2, bal3);
}
#[test]
fn concat1() {
//Generate a reasonable rope
let chunk = of_str(@ "123456789");
let r = empty();
uint::range(0u, 10u){|_i|
r = append_rope(r, chunk);
}
//Same rope, obtained with rope::concat
let r2 = concat(vec::init_elt(chunk, 10u));
assert eq(r, r2);
}
|