OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // TODO(srdjan): Move StringBuffer to visible names. | 5 // TODO(srdjan): Move StringBuffer to visible names. |
6 | 6 |
7 class StringBufferTest { | 7 void testConstructor() { |
8 static testConstructor() { | 8 StringBuffer bf = new StringBuffer(""); |
9 StringBuffer bf = new StringBuffer(""); | 9 Expect.equals(true, bf.isEmpty); |
10 Expect.equals(true, bf.isEmpty); | |
11 | 10 |
12 bf = new StringBuffer("abc"); | 11 bf = new StringBuffer("abc"); |
13 Expect.equals(3, bf.length); | 12 Expect.equals(3, bf.length); |
14 Expect.equals("abc", bf.toString()); | 13 Expect.equals("abc", bf.toString()); |
15 } | |
16 | 14 |
17 static testAdd() { | 15 bf = new StringBuffer("\x00"); |
18 StringBuffer bf = new StringBuffer(""); | |
19 Expect.equals(true, bf.isEmpty); | |
20 | |
21 bf.add("a"); | |
22 Expect.equals(1, bf.length); | |
23 Expect.equals("a", bf.toString()); | |
24 | |
25 bf = new StringBuffer(""); | |
26 bf.add("a"); | |
27 bf.add("b"); | |
28 Expect.equals("ab", bf.toString()); | |
29 | |
30 bf = new StringBuffer("abc"); | |
31 bf.add("d"); | |
32 bf.add("e"); | |
33 bf.add("f"); | |
34 bf.add("g"); | |
35 bf.add("h"); | |
36 bf.add("i"); | |
37 bf.add("j"); | |
38 bf.add("k"); | |
39 bf.add("l"); | |
40 bf.add("m"); | |
41 bf.add("n"); | |
42 bf.add("o"); | |
43 bf.add("p"); | |
44 bf.add("q"); | |
45 bf.add("r"); | |
46 bf.add("s"); | |
47 bf.add("t"); | |
48 bf.add("u"); | |
49 bf.add("v"); | |
50 bf.add("w"); | |
51 bf.add("x"); | |
52 bf.add("y"); | |
53 bf.add("z"); | |
54 bf.add("\n"); | |
55 bf.add("thequickbrownfoxjumpsoverthelazydog"); | |
56 Expect.equals("abcdefghijklmnopqrstuvwxyz\n" | |
57 "thequickbrownfoxjumpsoverthelazydog", | |
58 bf.toString()); | |
59 | |
60 bf = new StringBuffer(""); | |
61 for (int i = 0; i < 100000; i++) { | |
62 bf.add(''); | |
63 bf.add(""); | |
64 } | |
65 Expect.equals("", bf.toString()); | |
66 } | |
67 | |
68 static testLength() { | |
69 StringBuffer bf = new StringBuffer(""); | |
70 Expect.equals(0, bf.length); | |
71 bf.add("foo"); | |
72 Expect.equals(3, bf.length); | |
73 bf.add("bar"); | |
74 Expect.equals(6, bf.length); | |
75 bf.add(""); | |
76 Expect.equals(6, bf.length); | |
77 } | |
78 | |
79 static testIsEmpty() { | |
80 StringBuffer bf = new StringBuffer(""); | |
81 Expect.equals(true, bf.isEmpty); | |
82 bf.add("foo"); | |
83 Expect.equals(false, bf.isEmpty); | |
84 } | |
85 | |
86 static testAddAll() { | |
87 StringBuffer bf = new StringBuffer(""); | |
88 bf.addAll(["foo", "bar", "a", "b", "c"]); | |
89 Expect.equals("foobarabc", bf.toString()); | |
90 | |
91 bf.addAll([]); | |
92 Expect.equals("foobarabc", bf.toString()); | |
93 | |
94 bf.addAll(["", "", ""]); | |
95 Expect.equals("foobarabc", bf.toString()); | |
96 } | |
97 | |
98 static testClear() { | |
99 StringBuffer bf = new StringBuffer(""); | |
100 bf.add("foo"); | |
101 bf.clear(); | |
102 Expect.equals("", bf.toString()); | |
103 Expect.equals(0, bf.length); | |
104 | |
105 bf.add("bar"); | |
106 Expect.equals("bar", bf.toString()); | |
107 Expect.equals(3, bf.length); | |
108 bf.clear(); | |
109 Expect.equals("", bf.toString()); | |
110 Expect.equals(0, bf.length); | |
111 } | |
112 | |
113 static testToString() { | |
114 StringBuffer bf = new StringBuffer(""); | |
115 Expect.equals("", bf.toString()); | |
116 | |
117 bf = new StringBuffer("foo"); | |
118 Expect.equals("foo", bf.toString()); | |
119 | |
120 bf = new StringBuffer("foo"); | |
121 bf.add("bar"); | |
122 Expect.equals("foobar", bf.toString()); | |
123 } | |
124 | |
125 static testChaining() { | |
126 StringBuffer bf = new StringBuffer(""); | |
127 StringBuffer bf2 = new StringBuffer(""); | |
128 bf2.add("bf2"); | |
129 bf..add("foo") | |
130 ..add("bar") | |
131 ..add(bf2) | |
132 ..add(bf2) | |
133 ..add("toto"); | |
134 Expect.equals("foobarbf2bf2toto", bf.toString()); | |
135 } | |
136 | |
137 static testMain() { | |
138 testToString(); | |
139 testConstructor(); | |
140 testLength(); | |
141 testIsEmpty(); | |
142 testAdd(); | |
143 testAddAll(); | |
144 testClear(); | |
145 testChaining(); | |
146 } | |
147 } | 16 } |
148 | 17 |
149 main() { | 18 void testWrite() { |
150 StringBufferTest.testMain(); | 19 StringBuffer bf = new StringBuffer(""); |
| 20 Expect.equals(true, bf.isEmpty); |
| 21 |
| 22 bf.write("a"); |
| 23 Expect.equals(1, bf.length); |
| 24 Expect.equals("a", bf.toString()); |
| 25 |
| 26 bf = new StringBuffer(""); |
| 27 bf.write("a"); |
| 28 bf.write("b"); |
| 29 Expect.equals("ab", bf.toString()); |
| 30 |
| 31 bf = new StringBuffer("abc"); |
| 32 bf.write("d"); |
| 33 bf.write("e"); |
| 34 bf.write("f"); |
| 35 bf.write("g"); |
| 36 bf.write("h"); |
| 37 bf.write("i"); |
| 38 bf.write("j"); |
| 39 bf.write("k"); |
| 40 bf.write("l"); |
| 41 bf.write("m"); |
| 42 bf.write("n"); |
| 43 bf.write("o"); |
| 44 bf.write("p"); |
| 45 bf.write("q"); |
| 46 bf.write("r"); |
| 47 bf.write("s"); |
| 48 bf.write("t"); |
| 49 bf.write("u"); |
| 50 bf.write("v"); |
| 51 bf.write("w"); |
| 52 bf.write("x"); |
| 53 bf.write("y"); |
| 54 bf.write("z"); |
| 55 bf.write("\n"); |
| 56 bf.write("thequickbrownfoxjumpsoverthelazydog"); |
| 57 Expect.equals("abcdefghijklmnopqrstuvwxyz\n" |
| 58 "thequickbrownfoxjumpsoverthelazydog", |
| 59 bf.toString()); |
| 60 |
| 61 bf = new StringBuffer(""); |
| 62 for (int i = 0; i < 100000; i++) { |
| 63 bf.write(''); |
| 64 bf.write(""); |
| 65 } |
| 66 Expect.equals("", bf.toString()); |
151 } | 67 } |
| 68 |
| 69 void testLength() { |
| 70 StringBuffer bf = new StringBuffer(""); |
| 71 Expect.equals(0, bf.length); |
| 72 bf.write("foo"); |
| 73 Expect.equals(3, bf.length); |
| 74 bf.write("bar"); |
| 75 Expect.equals(6, bf.length); |
| 76 bf.write(""); |
| 77 Expect.equals(6, bf.length); |
| 78 } |
| 79 |
| 80 void testIsEmpty() { |
| 81 StringBuffer bf = new StringBuffer(""); |
| 82 Expect.equals(true, bf.isEmpty); |
| 83 bf.write("foo"); |
| 84 Expect.equals(false, bf.isEmpty); |
| 85 } |
| 86 |
| 87 void testWriteAll() { |
| 88 StringBuffer bf = new StringBuffer(""); |
| 89 bf.writeAll(["foo", "bar", "a", "b", "c"]); |
| 90 Expect.equals("foobarabc", bf.toString()); |
| 91 |
| 92 bf.writeAll([]); |
| 93 Expect.equals("foobarabc", bf.toString()); |
| 94 |
| 95 bf.writeAll(["", "", ""]); |
| 96 Expect.equals("foobarabc", bf.toString()); |
| 97 } |
| 98 |
| 99 void testClear() { |
| 100 StringBuffer bf = new StringBuffer(""); |
| 101 bf.write("foo"); |
| 102 bf.clear(); |
| 103 Expect.equals("", bf.toString()); |
| 104 Expect.equals(0, bf.length); |
| 105 |
| 106 bf.write("bar"); |
| 107 Expect.equals("bar", bf.toString()); |
| 108 Expect.equals(3, bf.length); |
| 109 bf.clear(); |
| 110 Expect.equals("", bf.toString()); |
| 111 Expect.equals(0, bf.length); |
| 112 } |
| 113 |
| 114 void testToString() { |
| 115 StringBuffer bf = new StringBuffer(""); |
| 116 Expect.equals("", bf.toString()); |
| 117 |
| 118 bf = new StringBuffer("foo"); |
| 119 Expect.equals("foo", bf.toString()); |
| 120 |
| 121 bf = new StringBuffer("foo"); |
| 122 bf.write("bar"); |
| 123 Expect.equals("foobar", bf.toString()); |
| 124 } |
| 125 |
| 126 void testChaining() { |
| 127 StringBuffer bf = new StringBuffer(""); |
| 128 StringBuffer bf2 = new StringBuffer(""); |
| 129 bf2.write("bf2"); |
| 130 bf..write("foo") |
| 131 ..write("bar") |
| 132 ..write(bf2) |
| 133 ..write(bf2) |
| 134 ..write("toto"); |
| 135 Expect.equals("foobarbf2bf2toto", bf.toString()); |
| 136 } |
| 137 |
| 138 void testWriteCharCode() { |
| 139 StringBuffer bf1 = new StringBuffer(); |
| 140 StringBuffer bf2 = new StringBuffer(); |
| 141 bf1.write("a"); |
| 142 bf2.writeCharCode(0x61); // a |
| 143 bf1.write("b"); |
| 144 bf2.writeCharCode(0x62); // b |
| 145 bf1.write("c"); |
| 146 bf2.writeCharCode(0x63); // c |
| 147 bf1.write(new String.fromCharCode(0xD823)); |
| 148 bf2.writeCharCode(0xD823); |
| 149 bf1.write(new String.fromCharCode(0xDC23)); |
| 150 bf2.writeCharCode(0xDC23); |
| 151 bf1.write("\u{1d49e}"); |
| 152 bf2.writeCharCode(0x1d49e); |
| 153 bf1.write("\x00"); |
| 154 bf2.writeCharCode(0); |
| 155 Expect.equals(bf1.toString(), bf2.toString()); |
| 156 Expect.equals("abc\u{18c23}\u{1d49e}\x00", bf2.toString()); |
| 157 |
| 158 // Mixing strings and char-codes. |
| 159 bf1.clear(); |
| 160 bf1.write("abcde"); |
| 161 bf1.writeCharCode(0x61); |
| 162 bf1.writeCharCode(0x62); |
| 163 bf1.writeCharCode(0x63); |
| 164 bf1.write("d"); |
| 165 bf1.writeCharCode(0x65); |
| 166 Expect.equals("abcdeabcde", bf1.toString()); |
| 167 |
| 168 // Out-of-range character codes are not allowed. |
| 169 Expect.throws(() { bf2.writeCharCode(-1); }); |
| 170 Expect.throws(() { bf2.writeCharCode(0x110000); }); |
| 171 } |
| 172 |
| 173 void main() { |
| 174 testToString(); |
| 175 testConstructor(); |
| 176 testLength(); |
| 177 testIsEmpty(); |
| 178 testWrite(); |
| 179 testWriteCharCode(); |
| 180 testWriteAll(); |
| 181 testClear(); |
| 182 testChaining(); |
| 183 } |
OLD | NEW |