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); | |
134 // start = 0, end varies. | 133 // start = 0, end varies. |
135 test("ABCDEFGH", iterable, 0); | 134 test("ABCDEFGH", iterable, 0); |
136 test("A", iterable, 0, 1); | 135 test("A", iterable, 0, 1); |
137 test("AB", iterable, 0, 2); | 136 test("AB", iterable, 0, 2); |
138 test("ABCDEFG", iterable, 0, 7); | 137 test("ABCDEFG", iterable, 0, 7); |
139 test("ABCDEFGH", iterable, 0, 8); | 138 test("ABCDEFGH", iterable, 0, 8); |
140 test("ABCDEFGH", iterable, 0, 9); | |
141 test("", iterable, 0, 0); | 139 test("", iterable, 0, 0); |
142 test("", iterable, 0, -1); | |
143 // Both varying. | 140 // Both varying. |
144 test("ABCDEFGH", iterable, 0, 8); | 141 test("ABCDEFGH", iterable, 0, 8); |
145 test("ABCDEFGH", iterable, 0, 9); | |
146 test("AB", iterable, 0, 2); | 142 test("AB", iterable, 0, 2); |
147 test("GH", iterable, 6, 8); | 143 test("GH", iterable, 6, 8); |
148 test("DE", iterable, 3, 5); | 144 test("DE", iterable, 3, 5); |
149 test("", iterable, 3, 3); | 145 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); | |
155 } | 146 } |
156 // Can split surrogates in input, but not a single big code point. | 147 // Can split surrogates in input, but not a single big code point. |
157 test(leadSurrogate, [0xDBFF, 0xDFFF], 0, 1); | 148 test(leadSurrogate, [0xDBFF, 0xDFFF], 0, 1); |
158 test(tailSurrogate, [0xDBFF, 0xDFFF], 1); | 149 test(tailSurrogate, [0xDBFF, 0xDFFF], 1); |
159 test("\u{10FFFF}", [0x10FFFF], 0, 1); | 150 test("\u{10FFFF}", [0x10FFFF], 0, 1); |
160 | 151 |
161 // Test varying slices of the code units of a string. | 152 // Test varying slices of the code units of a string. |
162 testSubstring(string) { | 153 testSubstring(string) { |
163 var codes = string.codeUnits; | 154 var codes = string.codeUnits; |
164 int length = string.length; | 155 int length = string.length; |
165 for (var iterable in [ | 156 for (var iterable in [ |
166 iter(length, codes), | 157 iter(length, codes), |
167 codes.toList(growable: true), | 158 codes.toList(growable: true), |
168 codes.toList(growable: false), | 159 codes.toList(growable: false), |
169 new Uint16List(length)..setRange(0, length, codes), | 160 new Uint16List(length)..setRange(0, length, codes), |
170 new Int32List(length)..setRange(0, length, codes), | 161 new Int32List(length)..setRange(0, length, codes), |
171 new Uint32List(length)..setRange(0, length, codes), | 162 new Uint32List(length)..setRange(0, length, codes), |
172 codes, | 163 codes, |
173 ]) { | 164 ]) { |
174 var newString = new String.fromCharCodes(iterable); | 165 var newString = new String.fromCharCodes(iterable); |
175 Expect.equals(string, newString); | 166 Expect.equals(string, newString); |
176 for (int i = 0; i < length; i = i * 2 + 1) { | 167 for (int i = 0; i < length; i = i * 2 + 1) { |
177 test(string.substring(i), iterable, i); | 168 test(string.substring(i), iterable, i); |
178 test(string.substring(0, i), iterable, 0, i); | 169 test(string.substring(0, i), iterable, 0, i); |
179 for (int j = 0; i + j < length; j = j * 2 + 1) { | 170 for (int j = 0; i + j < length; j = j * 2 + 1) { |
180 test(string.substring(i, i + j), iterable, i, i + j); | 171 test(string.substring(i, i + j), iterable, i, i + j); |
181 } | 172 } |
182 } | 173 } |
183 Expect.equals(string, new String.fromCharCodes(iterable, 0, length + 1)); | 174 |
| 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)); |
184 } | 182 } |
185 } | 183 } |
186 | 184 |
187 testSubstring(""); | 185 testSubstring(""); |
188 testSubstring("ABCDEFGH"); | 186 testSubstring("ABCDEFGH"); |
189 // length > 128 | 187 // length > 128 |
190 testSubstring("ABCDEFGH" * 33); | 188 testSubstring("ABCDEFGH" * 33); |
191 testSubstring("\x00" * 357); | 189 testSubstring("\x00" * 357); |
192 // length > 128 and non-ASCII. | 190 // length > 128 and non-ASCII. |
193 testSubstring("\uFFFD\uFFFE\u{10000}\u{10ffff}c\x00" * 37); | 191 testSubstring("\uFFFD\uFFFE\u{10000}\u{10ffff}c\x00" * 37); |
194 } | 192 } |
OLD | NEW |