| 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 class StringBufferTest { |
| 8 static testConstructor() { | 8 static testConstructor() { |
| 9 StringBuffer bf = new StringBuffer(""); | 9 StringBuffer bf = new StringBuffer(""); |
| 10 Expect.equals(true, bf.isEmpty); | 10 Expect.equals(true, bf.isEmpty); |
| 11 | 11 |
| 12 bf = new StringBuffer("abc"); | 12 bf = new StringBuffer("abc"); |
| 13 Expect.equals(3, bf.length); | 13 Expect.equals(3, bf.length); |
| 14 Expect.equals("abc", bf.toString()); | 14 Expect.equals("abc", bf.toString()); |
| 15 } | 15 } |
| 16 | 16 |
| 17 static testAdd() { | 17 static testWrite() { |
| 18 StringBuffer bf = new StringBuffer(""); | 18 StringBuffer bf = new StringBuffer(""); |
| 19 Expect.equals(true, bf.isEmpty); | 19 Expect.equals(true, bf.isEmpty); |
| 20 | 20 |
| 21 bf.add("a"); | 21 bf.write("a"); |
| 22 Expect.equals(1, bf.length); | 22 Expect.equals(1, bf.length); |
| 23 Expect.equals("a", bf.toString()); | 23 Expect.equals("a", bf.toString()); |
| 24 | 24 |
| 25 bf = new StringBuffer(""); | 25 bf = new StringBuffer(""); |
| 26 bf.add("a"); | 26 bf.write("a"); |
| 27 bf.add("b"); | 27 bf.write("b"); |
| 28 Expect.equals("ab", bf.toString()); | 28 Expect.equals("ab", bf.toString()); |
| 29 | 29 |
| 30 bf = new StringBuffer("abc"); | 30 bf = new StringBuffer("abc"); |
| 31 bf.add("d"); | 31 bf.write("d"); |
| 32 bf.add("e"); | 32 bf.write("e"); |
| 33 bf.add("f"); | 33 bf.write("f"); |
| 34 bf.add("g"); | 34 bf.write("g"); |
| 35 bf.add("h"); | 35 bf.write("h"); |
| 36 bf.add("i"); | 36 bf.write("i"); |
| 37 bf.add("j"); | 37 bf.write("j"); |
| 38 bf.add("k"); | 38 bf.write("k"); |
| 39 bf.add("l"); | 39 bf.write("l"); |
| 40 bf.add("m"); | 40 bf.write("m"); |
| 41 bf.add("n"); | 41 bf.write("n"); |
| 42 bf.add("o"); | 42 bf.write("o"); |
| 43 bf.add("p"); | 43 bf.write("p"); |
| 44 bf.add("q"); | 44 bf.write("q"); |
| 45 bf.add("r"); | 45 bf.write("r"); |
| 46 bf.add("s"); | 46 bf.write("s"); |
| 47 bf.add("t"); | 47 bf.write("t"); |
| 48 bf.add("u"); | 48 bf.write("u"); |
| 49 bf.add("v"); | 49 bf.write("v"); |
| 50 bf.add("w"); | 50 bf.write("w"); |
| 51 bf.add("x"); | 51 bf.write("x"); |
| 52 bf.add("y"); | 52 bf.write("y"); |
| 53 bf.add("z"); | 53 bf.write("z"); |
| 54 bf.add("\n"); | 54 bf.write("\n"); |
| 55 bf.add("thequickbrownfoxjumpsoverthelazydog"); | 55 bf.write("thequickbrownfoxjumpsoverthelazydog"); |
| 56 Expect.equals("abcdefghijklmnopqrstuvwxyz\n" | 56 Expect.equals("abcdefghijklmnopqrstuvwxyz\n" |
| 57 "thequickbrownfoxjumpsoverthelazydog", | 57 "thequickbrownfoxjumpsoverthelazydog", |
| 58 bf.toString()); | 58 bf.toString()); |
| 59 | 59 |
| 60 bf = new StringBuffer(""); | 60 bf = new StringBuffer(""); |
| 61 for (int i = 0; i < 100000; i++) { | 61 for (int i = 0; i < 100000; i++) { |
| 62 bf.add(''); | 62 bf.write(''); |
| 63 bf.add(""); | 63 bf.write(""); |
| 64 } | 64 } |
| 65 Expect.equals("", bf.toString()); | 65 Expect.equals("", bf.toString()); |
| 66 } | 66 } |
| 67 | 67 |
| 68 static testLength() { | 68 static testLength() { |
| 69 StringBuffer bf = new StringBuffer(""); | 69 StringBuffer bf = new StringBuffer(""); |
| 70 Expect.equals(0, bf.length); | 70 Expect.equals(0, bf.length); |
| 71 bf.add("foo"); | 71 bf.write("foo"); |
| 72 Expect.equals(3, bf.length); | 72 Expect.equals(3, bf.length); |
| 73 bf.add("bar"); | 73 bf.write("bar"); |
| 74 Expect.equals(6, bf.length); | 74 Expect.equals(6, bf.length); |
| 75 bf.add(""); | 75 bf.write(""); |
| 76 Expect.equals(6, bf.length); | 76 Expect.equals(6, bf.length); |
| 77 } | 77 } |
| 78 | 78 |
| 79 static testIsEmpty() { | 79 static testIsEmpty() { |
| 80 StringBuffer bf = new StringBuffer(""); | 80 StringBuffer bf = new StringBuffer(""); |
| 81 Expect.equals(true, bf.isEmpty); | 81 Expect.equals(true, bf.isEmpty); |
| 82 bf.add("foo"); | 82 bf.write("foo"); |
| 83 Expect.equals(false, bf.isEmpty); | 83 Expect.equals(false, bf.isEmpty); |
| 84 } | 84 } |
| 85 | 85 |
| 86 static testAddAll() { | 86 static testWriteAll() { |
| 87 StringBuffer bf = new StringBuffer(""); | 87 StringBuffer bf = new StringBuffer(""); |
| 88 bf.addAll(["foo", "bar", "a", "b", "c"]); | 88 bf.writeAll(["foo", "bar", "a", "b", "c"]); |
| 89 Expect.equals("foobarabc", bf.toString()); | 89 Expect.equals("foobarabc", bf.toString()); |
| 90 | 90 |
| 91 bf.addAll([]); | 91 bf.writeAll([]); |
| 92 Expect.equals("foobarabc", bf.toString()); | 92 Expect.equals("foobarabc", bf.toString()); |
| 93 | 93 |
| 94 bf.addAll(["", "", ""]); | 94 bf.writeAll(["", "", ""]); |
| 95 Expect.equals("foobarabc", bf.toString()); | 95 Expect.equals("foobarabc", bf.toString()); |
| 96 } | 96 } |
| 97 | 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() { | 98 static testToString() { |
| 114 StringBuffer bf = new StringBuffer(""); | 99 StringBuffer bf = new StringBuffer(""); |
| 115 Expect.equals("", bf.toString()); | 100 Expect.equals("", bf.toString()); |
| 116 | 101 |
| 117 bf = new StringBuffer("foo"); | 102 bf = new StringBuffer("foo"); |
| 118 Expect.equals("foo", bf.toString()); | 103 Expect.equals("foo", bf.toString()); |
| 119 | 104 |
| 120 bf = new StringBuffer("foo"); | 105 bf = new StringBuffer("foo"); |
| 121 bf.add("bar"); | 106 bf.write("bar"); |
| 122 Expect.equals("foobar", bf.toString()); | 107 Expect.equals("foobar", bf.toString()); |
| 123 } | 108 } |
| 124 | 109 |
| 125 static testChaining() { | 110 static testChaining() { |
| 126 StringBuffer bf = new StringBuffer(""); | 111 StringBuffer bf = new StringBuffer(""); |
| 127 StringBuffer bf2 = new StringBuffer(""); | 112 StringBuffer bf2 = new StringBuffer(""); |
| 128 bf2.add("bf2"); | 113 bf2.write("bf2"); |
| 129 bf..add("foo") | 114 bf..write("foo") |
| 130 ..add("bar") | 115 ..write("bar") |
| 131 ..add(bf2) | 116 ..write(bf2) |
| 132 ..add(bf2) | 117 ..write(bf2) |
| 133 ..add("toto"); | 118 ..write("toto"); |
| 134 Expect.equals("foobarbf2bf2toto", bf.toString()); | 119 Expect.equals("foobarbf2bf2toto", bf.toString()); |
| 135 } | 120 } |
| 136 | 121 |
| 137 static testMain() { | 122 static testMain() { |
| 138 testToString(); | 123 testToString(); |
| 139 testConstructor(); | 124 testConstructor(); |
| 140 testLength(); | 125 testLength(); |
| 141 testIsEmpty(); | 126 testIsEmpty(); |
| 142 testAdd(); | 127 testWrite(); |
| 143 testAddAll(); | 128 testWriteAll(); |
| 144 testClear(); | |
| 145 testChaining(); | 129 testChaining(); |
| 146 } | 130 } |
| 147 } | 131 } |
| 148 | 132 |
| 149 main() { | 133 main() { |
| 150 StringBufferTest.testMain(); | 134 StringBufferTest.testMain(); |
| 151 } | 135 } |
| OLD | NEW |