OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 import "dart:typed_data"; | 6 import "dart:typed_data"; |
7 | 7 |
8 main() { | 8 main() { |
9 iter(count, [values]) => values is List | 9 iter(count, [values]) => values is List |
10 ? new Iterable.generate(count, (x) => values[x]) | 10 ? new Iterable.generate(count, (x) => values[x]) |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 new Uint16List(8)..setRange(0, 8, list), | 123 new Uint16List(8)..setRange(0, 8, list), |
124 new Uint32List(8)..setRange(0, 8, list), | 124 new Uint32List(8)..setRange(0, 8, list), |
125 "ABCDEFGH".codeUnits, | 125 "ABCDEFGH".codeUnits, |
126 ]) { | 126 ]) { |
127 test("ABCDEFGH", iterable); | 127 test("ABCDEFGH", iterable); |
128 // start varies, end is null. | 128 // start varies, end is null. |
129 test("ABCDEFGH", iterable, 0); | 129 test("ABCDEFGH", iterable, 0); |
130 test("BCDEFGH", iterable, 1); | 130 test("BCDEFGH", iterable, 1); |
131 test("H", iterable, 7); | 131 test("H", iterable, 7); |
132 test("", iterable, 8); | 132 test("", iterable, 8); |
| 133 test("", iterable, 9); |
133 // start = 0, end varies. | 134 // start = 0, end varies. |
134 test("ABCDEFGH", iterable, 0); | 135 test("ABCDEFGH", iterable, 0); |
135 test("A", iterable, 0, 1); | 136 test("A", iterable, 0, 1); |
136 test("AB", iterable, 0, 2); | 137 test("AB", iterable, 0, 2); |
137 test("ABCDEFG", iterable, 0, 7); | 138 test("ABCDEFG", iterable, 0, 7); |
138 test("ABCDEFGH", iterable, 0, 8); | 139 test("ABCDEFGH", iterable, 0, 8); |
| 140 test("ABCDEFGH", iterable, 0, 9); |
139 test("", iterable, 0, 0); | 141 test("", iterable, 0, 0); |
| 142 test("", iterable, 0, -1); |
140 // Both varying. | 143 // Both varying. |
141 test("ABCDEFGH", iterable, 0, 8); | 144 test("ABCDEFGH", iterable, 0, 8); |
| 145 test("ABCDEFGH", iterable, 0, 9); |
142 test("AB", iterable, 0, 2); | 146 test("AB", iterable, 0, 2); |
143 test("GH", iterable, 6, 8); | 147 test("GH", iterable, 6, 8); |
144 test("DE", iterable, 3, 5); | 148 test("DE", iterable, 3, 5); |
145 test("", iterable, 3, 3); | 149 test("", iterable, 3, 3); |
| 150 test("", iterable, 5, 3); |
| 151 test("", iterable, 4, -1); |
| 152 test("", iterable, 8, -1); |
| 153 test("", iterable, 0, -1); |
| 154 test("", iterable, 9, 9); |
146 } | 155 } |
147 // Can split surrogates in input, but not a single big code point. | 156 // Can split surrogates in input, but not a single big code point. |
148 test(leadSurrogate, [0xDBFF, 0xDFFF], 0, 1); | 157 test(leadSurrogate, [0xDBFF, 0xDFFF], 0, 1); |
149 test(tailSurrogate, [0xDBFF, 0xDFFF], 1); | 158 test(tailSurrogate, [0xDBFF, 0xDFFF], 1); |
150 test("\u{10FFFF}", [0x10FFFF], 0, 1); | 159 test("\u{10FFFF}", [0x10FFFF], 0, 1); |
151 | 160 |
152 // Test varying slices of the code units of a string. | 161 // Test varying slices of the code units of a string. |
153 testSubstring(string) { | 162 testSubstring(string) { |
154 var codes = string.codeUnits; | 163 var codes = string.codeUnits; |
155 int length = string.length; | 164 int length = string.length; |
156 for (var iterable in [ | 165 for (var iterable in [ |
157 iter(length, codes), | 166 iter(length, codes), |
158 codes.toList(growable: true), | 167 codes.toList(growable: true), |
159 codes.toList(growable: false), | 168 codes.toList(growable: false), |
160 new Uint16List(length)..setRange(0, length, codes), | 169 new Uint16List(length)..setRange(0, length, codes), |
161 new Int32List(length)..setRange(0, length, codes), | 170 new Int32List(length)..setRange(0, length, codes), |
162 new Uint32List(length)..setRange(0, length, codes), | 171 new Uint32List(length)..setRange(0, length, codes), |
163 codes, | 172 codes, |
164 ]) { | 173 ]) { |
165 var newString = new String.fromCharCodes(iterable); | 174 var newString = new String.fromCharCodes(iterable); |
166 Expect.equals(string, newString); | 175 Expect.equals(string, newString); |
167 for (int i = 0; i < length; i = i * 2 + 1) { | 176 for (int i = 0; i < length; i = i * 2 + 1) { |
168 test(string.substring(i), iterable, i); | 177 test(string.substring(i), iterable, i); |
169 test(string.substring(0, i), iterable, 0, i); | 178 test(string.substring(0, i), iterable, 0, i); |
170 for (int j = 0; i + j < length; j = j * 2 + 1) { | 179 for (int j = 0; i + j < length; j = j * 2 + 1) { |
171 test(string.substring(i, i + j), iterable, i, i + j); | 180 test(string.substring(i, i + j), iterable, i, i + j); |
172 } | 181 } |
173 } | 182 } |
174 | 183 Expect.equals(string, new String.fromCharCodes(iterable, 0, length + 1)); |
175 Expect.throws(() => new String.fromCharCodes(iterable, -1)); | |
176 Expect.throws(() => new String.fromCharCodes(iterable, 0, -1)); | |
177 Expect.throws(() => new String.fromCharCodes(iterable, 2, 1)); | |
178 Expect.throws(() => new String.fromCharCodes(iterable, 0, length + 1)); | |
179 Expect.throws(() => new String.fromCharCodes(iterable, length + 1)); | |
180 Expect.throws(() => new String.fromCharCodes(iterable, length + 1, | |
181 length + 2)); | |
182 } | 184 } |
183 } | 185 } |
184 | 186 |
185 testSubstring(""); | 187 testSubstring(""); |
186 testSubstring("ABCDEFGH"); | 188 testSubstring("ABCDEFGH"); |
187 // length > 128 | 189 // length > 128 |
188 testSubstring("ABCDEFGH" * 33); | 190 testSubstring("ABCDEFGH" * 33); |
189 testSubstring("\x00" * 357); | 191 testSubstring("\x00" * 357); |
190 // length > 128 and non-ASCII. | 192 // length > 128 and non-ASCII. |
191 testSubstring("\uFFFD\uFFFE\u{10000}\u{10ffff}c\x00" * 37); | 193 testSubstring("\uFFFD\uFFFE\u{10000}\u{10ffff}c\x00" * 37); |
192 } | 194 } |
OLD | NEW |