Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(552)

Side by Side Diff: test/codegen/corelib/string_test.dart

Issue 1945153002: Add corelib tests (Closed) Base URL: https://github.com/dart-lang/dev_compiler@master
Patch Set: error_test and range_error_test now pass Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import "package:expect/expect.dart";
6
7 void main() {
8 testOutOfRange();
9 testIllegalArgument();
10 testConcat();
11 testIndex();
12 testCodeUnitAt();
13 testEquals();
14 testEndsWith();
15 testStartsWith();
16 testIndexOf();
17 testLastIndexOf();
18 testContains();
19 testReplaceAll();
20 testCompareTo();
21 testCharCodes();
22 testRepeat();
23 testPadLeft();
24 testPadRight();
25 }
26
27 void testLength() {
28 String str = "";
29 for (var i = 0; i < 20; i++) {
30 testStringLength(i, str);
31 str += " ";
32 }
33 }
34
35 void testOutOfRange() {
36 String a = "Hello";
37 bool exception_caught = false;
38 try {
39 var c = a[20]; // Throw exception.
40 } on RangeError catch (e) {
41 exception_caught = true;
42 }
43 Expect.isTrue(exception_caught);
44 }
45
46 void testIllegalArgument() {
47 String a = "Hello";
48 bool exception_caught = false;
49 try {
50 var c = a[2.2]; // Throw exception.
51 Expect.fail("Accepting double as index");
52 } on ArgumentError catch (e) {
53 exception_caught = true;
54 } on TypeError catch (e) { // Thrown in checked mode only.
55 exception_caught = true;
56 }
57 Expect.isTrue(exception_caught);
58 }
59
60 void testIndex() {
61 String str = "string";
62 for (int i = 0; i < str.length; i++) {
63 Expect.isTrue(str[i] is String);
64 testStringLength(1, str[i]);
65 }
66 }
67
68 void testCodeUnitAt() {
69 String str = "string";
70 for (int i = 0; i < str.length; i++) {
71 Expect.isTrue(str.codeUnitAt(i) is int);
72 }
73 }
74
75 void testConcat() {
76 var a = "One";
77 var b = "Four";
78 var c = a + b;
79 testStringLength(7, c);
80 Expect.equals("OneFour", c);
81 }
82
83 void testEquals() {
84 Expect.equals("str", "str");
85
86 Expect.equals("str", "s" + "t" + "r");
87 Expect.equals("s" + "t" + "r", "str");
88
89 Expect.isFalse("str" == "s");
90 Expect.isFalse("str" == "r");
91 Expect.isFalse("str" == "st");
92 Expect.isFalse("str" == "tr");
93
94 Expect.isFalse("s" == "str");
95 Expect.isFalse("r" == "str");
96 Expect.isFalse("st" == "str");
97 Expect.isFalse("tr" == "str");
98
99 Expect.isFalse("" == "s");
100 Expect.equals("", "");
101 }
102
103 void testEndsWith() {
104 Expect.isTrue("str".endsWith("r"));
105 Expect.isTrue("str".endsWith("tr"));
106 Expect.isTrue("str".endsWith("str"));
107
108 Expect.isFalse("str".endsWith("stri"));
109 Expect.isFalse("str".endsWith("t"));
110 Expect.isFalse("str".endsWith("st"));
111 Expect.isFalse("str".endsWith("s"));
112
113 Expect.isTrue("".endsWith(""));
114 Expect.isFalse("".endsWith("s"));
115 }
116
117 void testStartsWith() {
118 Expect.isTrue("str".startsWith("s"));
119 Expect.isTrue("str".startsWith("st"));
120 Expect.isTrue("str".startsWith("str"));
121
122 Expect.isFalse("str".startsWith("stri"));
123 Expect.isFalse("str".startsWith("r"));
124 Expect.isFalse("str".startsWith("tr"));
125 Expect.isFalse("str".startsWith("t"));
126
127 Expect.isTrue("".startsWith(""));
128 Expect.isFalse("".startsWith("s"));
129
130 Expect.isFalse("strstr".startsWith("s", 1));
131 Expect.isFalse("strstr".startsWith("s", 2));
132 Expect.isTrue("strstr".startsWith("s", 3));
133 Expect.isFalse("strstr".startsWith("s", 4));
134
135 Expect.isFalse("strstr".startsWith("st", 1));
136 Expect.isFalse("strstr".startsWith("st", 2));
137 Expect.isTrue("strstr".startsWith("st", 3));
138 Expect.isFalse("strstr".startsWith("st", 4));
139
140 Expect.isFalse("strstr".startsWith("str", 1));
141 Expect.isFalse("strstr".startsWith("str", 2));
142 Expect.isTrue("strstr".startsWith("str", 3));
143 Expect.isFalse("strstr".startsWith("str", 4));
144
145 Expect.isTrue("str".startsWith("", 0));
146 Expect.isTrue("str".startsWith("", 1));
147 Expect.isTrue("str".startsWith("", 2));
148 Expect.isTrue("str".startsWith("", 3));
149
150 Expect.throws(() => "str".startsWith("", -1));
151 Expect.throws(() => "str".startsWith("", 4));
152
153 var regexp = new RegExp("s(?:tr?)?");
154 Expect.isTrue("s".startsWith(regexp));
155 Expect.isTrue("st".startsWith(regexp));
156 Expect.isTrue("str".startsWith(regexp));
157 Expect.isTrue("sX".startsWith(regexp));
158 Expect.isTrue("stX".startsWith(regexp));
159 Expect.isTrue("strX".startsWith(regexp));
160
161 Expect.isFalse("".startsWith(regexp));
162 Expect.isFalse("astr".startsWith(regexp));
163
164 Expect.isTrue("".startsWith(new RegExp("")));
165 Expect.isTrue("".startsWith(new RegExp("a?")));
166
167 Expect.isFalse("strstr".startsWith(regexp, 1));
168 Expect.isFalse("strstr".startsWith(regexp, 2));
169 Expect.isTrue("strstr".startsWith(regexp, 3));
170 Expect.isFalse("strstr".startsWith(regexp, 4));
171
172 Expect.isTrue("str".startsWith(new RegExp(""), 0));
173 Expect.isTrue("str".startsWith(new RegExp(""), 1));
174 Expect.isTrue("str".startsWith(new RegExp(""), 2));
175 Expect.isTrue("str".startsWith(new RegExp(""), 3));
176 Expect.isTrue("str".startsWith(new RegExp("a?"), 0));
177 Expect.isTrue("str".startsWith(new RegExp("a?"), 1));
178 Expect.isTrue("str".startsWith(new RegExp("a?"), 2));
179 Expect.isTrue("str".startsWith(new RegExp("a?"), 3));
180
181 Expect.throws(() => "str".startsWith(regexp, -1));
182 Expect.throws(() => "str".startsWith(regexp, 4));
183
184 regexp = new RegExp("^str");
185 Expect.isTrue("strstr".startsWith(regexp));
186 Expect.isTrue("strstr".startsWith(regexp, 0));
187 Expect.isFalse("strstr".startsWith(regexp, 1));
188 Expect.isFalse("strstr".startsWith(regexp, 2));
189 Expect.isFalse("strstr".startsWith(regexp, 3)); // Second "str" isn't at ^.
190 }
191
192 void testIndexOf() {
193 Expect.equals(0, "str".indexOf("", 0));
194 Expect.equals(0, "".indexOf("", 0));
195 Expect.equals(-1, "".indexOf("a", 0));
196
197 Expect.equals(1, "str".indexOf("t", 0));
198 Expect.equals(1, "str".indexOf("tr", 0));
199 Expect.equals(0, "str".indexOf("str", 0));
200 Expect.equals(0, "str".indexOf("st", 0));
201 Expect.equals(0, "str".indexOf("s", 0));
202 Expect.equals(2, "str".indexOf("r", 0));
203 Expect.equals(-1, "str".indexOf("string", 0));
204
205 Expect.equals(1, "strstr".indexOf("t", 0));
206 Expect.equals(1, "strstr".indexOf("tr", 0));
207 Expect.equals(0, "strstr".indexOf("str", 0));
208 Expect.equals(0, "strstr".indexOf("st", 0));
209 Expect.equals(0, "strstr".indexOf("s", 0));
210 Expect.equals(2, "strstr".indexOf("r", 0));
211 Expect.equals(-1, "str".indexOf("string", 0));
212
213 Expect.equals(4, "strstr".indexOf("t", 2));
214 Expect.equals(4, "strstr".indexOf("tr", 2));
215 Expect.equals(3, "strstr".indexOf("str", 1));
216 Expect.equals(3, "strstr".indexOf("str", 2));
217 Expect.equals(3, "strstr".indexOf("str", 3));
218 Expect.equals(3, "strstr".indexOf("st", 1));
219 Expect.equals(3, "strstr".indexOf("s", 3));
220 Expect.equals(5, "strstr".indexOf("r", 3));
221 Expect.equals(5, "strstr".indexOf("r", 4));
222 Expect.equals(5, "strstr".indexOf("r", 5));
223
224 String str = "hello";
225 for (int i = 0; i < 10; i++) {
226 if (i > str.length) {
227 Expect.throws(() => str.indexOf("", i));
228 } else {
229 int result = str.indexOf("", i);
230 Expect.equals(i, result);
231 }
232 }
233
234 var re = new RegExp("an?");
235 Expect.equals(1, "banana".indexOf(re));
236 Expect.equals(1, "banana".indexOf(re, 0));
237 Expect.equals(1, "banana".indexOf(re, 1));
238 Expect.equals(3, "banana".indexOf(re, 2));
239 Expect.equals(3, "banana".indexOf(re, 3));
240 Expect.equals(5, "banana".indexOf(re, 4));
241 Expect.equals(5, "banana".indexOf(re, 5));
242 Expect.equals(-1, "banana".indexOf(re, 6));
243 Expect.throws(() => "banana".indexOf(re, -1));
244 Expect.throws(() => "banana".indexOf(re, 7));
245 re = new RegExp("x?");
246 for (int i = 0; i <= str.length; i++) {
247 Expect.equals(i, str.indexOf(re, i));
248 }
249 }
250
251 void testLastIndexOf() {
252 Expect.equals(2, "str".lastIndexOf("", 2));
253 Expect.equals(0, "".lastIndexOf("", 0));
254 Expect.equals(-1, "".lastIndexOf("a", 0));
255
256 Expect.equals(1, "str".lastIndexOf("t", 2));
257 Expect.equals(1, "str".lastIndexOf("tr", 2));
258 Expect.equals(0, "str".lastIndexOf("str", 2));
259 Expect.equals(0, "str".lastIndexOf("st", 2));
260 Expect.equals(0, "str".lastIndexOf("s", 2));
261 Expect.equals(2, "str".lastIndexOf("r", 2));
262 Expect.equals(-1, "str".lastIndexOf("string", 2));
263
264 Expect.equals(4, "strstr".lastIndexOf("t", 5));
265 Expect.equals(4, "strstr".lastIndexOf("tr", 5));
266 Expect.equals(3, "strstr".lastIndexOf("str", 5));
267 Expect.equals(3, "strstr".lastIndexOf("st", 5));
268 Expect.equals(3, "strstr".lastIndexOf("s", 5));
269 Expect.equals(5, "strstr".lastIndexOf("r", 5));
270 Expect.throws(() {
271 "str".lastIndexOf("string", 5);
272 });
273 Expect.equals(4, "strstr".lastIndexOf("t", 5));
274 Expect.equals(4, "strstr".lastIndexOf("tr", 5));
275 Expect.equals(3, "strstr".lastIndexOf("str", 5));
276 Expect.equals(3, "strstr".lastIndexOf("str", 5));
277 Expect.equals(3, "strstr".lastIndexOf("str", 5));
278 Expect.equals(3, "strstr".lastIndexOf("st", 5));
279 Expect.equals(3, "strstr".lastIndexOf("s", 5));
280 Expect.equals(5, "strstr".lastIndexOf("r", 5));
281 Expect.equals(2, "strstr".lastIndexOf("r", 4));
282 Expect.equals(2, "strstr".lastIndexOf("r", 3));
283 Expect.equals(5, "strstr".lastIndexOf("r"));
284 Expect.equals(5, "strstr".lastIndexOf("r", null));
285
286 String str = "hello";
287 for (int i = 0; i < 10; i++) {
288 if (i > str.length) {
289 Expect.throws(() => str.indexOf("", i));
290 } else {
291 int result = str.lastIndexOf("", i);
292 Expect.equals(i, result);
293 }
294 }
295
296 var re = new RegExp("an?");
297 Expect.equals(5, "banana".lastIndexOf(re));
298 Expect.equals(5, "banana".lastIndexOf(re, 6));
299 Expect.equals(5, "banana".lastIndexOf(re, 5));
300 Expect.equals(3, "banana".lastIndexOf(re, 4));
301 Expect.equals(3, "banana".lastIndexOf(re, 3));
302 Expect.equals(1, "banana".lastIndexOf(re, 2));
303 Expect.equals(1, "banana".lastIndexOf(re, 1));
304 Expect.equals(-1, "banana".lastIndexOf(re, 0));
305 Expect.throws(() => "banana".lastIndexOf(re, -1));
306 Expect.throws(() => "banana".lastIndexOf(re, 7));
307 re = new RegExp("x?");
308 for (int i = 0; i <= str.length; i++) {
309 Expect.equals(i, str.indexOf(re, i));
310 }
311 }
312
313 void testContains() {
314 Expect.isTrue("str".contains("s", 0));
315 Expect.isTrue("str".contains("st", 0));
316 Expect.isTrue("str".contains("str", 0));
317 Expect.isTrue("str".contains("t", 0));
318 Expect.isTrue("str".contains("r", 0));
319 Expect.isTrue("str".contains("tr", 0));
320
321 Expect.isFalse("str".contains("sr", 0));
322 Expect.isFalse("str".contains("string", 0));
323
324 Expect.isTrue("str".contains("", 0));
325 Expect.isTrue("".contains("", 0));
326 Expect.isFalse("".contains("s", 0));
327 }
328
329 void testReplaceAll() {
330 Expect.equals(
331 "AtoBtoCDtoE", "AfromBfromCDfromE".replaceAll("from", "to"));
332
333 // Test with the replaced string at the begining.
334 Expect.equals(
335 "toABtoCDtoE", "fromABfromCDfromE".replaceAll("from", "to"));
336
337 // Test with the replaced string at the end.
338 Expect.equals(
339 "toABtoCDtoEto", "fromABfromCDfromEfrom".replaceAll("from", "to"));
340
341 // Test when there are no occurence of the string to replace.
342 Expect.equals("ABC", "ABC".replaceAll("from", "to"));
343
344 // Test when the string to change is the empty string.
345 Expect.equals("", "".replaceAll("from", "to"));
346
347 // Test when the string to change is a substring of the string to
348 // replace.
349 Expect.equals("fro", "fro".replaceAll("from", "to"));
350
351 // Test when the string to change is the replaced string.
352 Expect.equals("to", "from".replaceAll("from", "to"));
353
354 // Test when the string to change is the replacement string.
355 Expect.equals("to", "to".replaceAll("from", "to"));
356
357 // Test replacing by the empty string.
358 Expect.equals("", "from".replaceAll("from", ""));
359 Expect.equals("AB", "AfromB".replaceAll("from", ""));
360
361 // Test changing the empty string.
362 Expect.equals("to", "".replaceAll("", "to"));
363
364 // Test replacing the empty string.
365 Expect.equals("toAtoBtoCto", "ABC".replaceAll("", "to"));
366 }
367
368 void testCompareTo() {
369 Expect.equals(0, "".compareTo(""));
370 Expect.equals(0, "str".compareTo("str"));
371 Expect.equals(-1, "str".compareTo("string"));
372 Expect.equals(1, "string".compareTo("str"));
373 Expect.equals(1, "string".compareTo(""));
374 Expect.equals(-1, "".compareTo("string"));
375 }
376
377 void testCharCodes() {
378 test(str) {
379 var list = str.codeUnits;
380 Expect.equals(str.length, list.length);
381 for (int i = 0; i < str.length; i++) {
382 Expect.equals(str.codeUnitAt(i), list[i]);
383 }
384 }
385 test("abc");
386 test("");
387 test(" ");
388 }
389
390 void testStringLength(int length, String str) {
391 Expect.equals(length, str.length);
392 (length == 0 ? Expect.isTrue : Expect.isFalse)(str.isEmpty);
393 (length != 0 ? Expect.isTrue : Expect.isFalse)(str.isNotEmpty);
394 }
395
396 void testRepeat() {
397 List<String> testStrings = [
398 "",
399 "\x00",
400 "a",
401 "ab",
402 "\x80",
403 "\xff",
404 "\u2028",
405 "abcdef\u2028",
406 "\u{10002}",
407 "abcdef\u{10002}"
408 ];
409 List<int> counts = [
410 0,
411 1,
412 2,
413 3,
414 4,
415 5,
416 6,
417 7,
418 8,
419 9,
420 10,
421 11,
422 12,
423 13,
424 14,
425 15,
426 16,
427 17,
428 127,
429 128,
430 129
431 ];
432 void testRepeat(str, repeat) {
433 String expect;
434 if (repeat <= 0) {
435 expect = "";
436 } else if (repeat == 1) {
437 expect = str;
438 } else {
439 StringBuffer buf = new StringBuffer();
440 for (int i = 0; i < repeat; i++) {
441 buf.write(str);
442 }
443 expect = buf.toString();
444 }
445 String actual = str * repeat;
446 Expect.equals(expect, actual,
447 "$str#${str.length} * $repeat");
448 }
449 for (String str in testStrings) {
450 for (int repeat in counts) {
451 testRepeat(str, repeat);
452 }
453 }
454 }
455
456 void testPadLeft() {
457 Expect.equals(" 1", "1".padLeft(5, ' '));
458 Expect.equals(" 11", "11".padLeft(5, ' '));
459 Expect.equals(" 111", "111".padLeft(5, ' '));
460 Expect.equals(" 1111", "1111".padLeft(5, ' '));
461 Expect.equals("11111", "11111".padLeft(5, ' '));
462 Expect.equals("111111", "111111".padLeft(5, ' '));
463 Expect.equals(" \u{10002}", "\u{10002}".padLeft(5, ' '));
464 Expect.equals('', ''.padLeft(0, 'a'));
465 Expect.equals('a', ''.padLeft(1, 'a'));
466 Expect.equals('aaaaa', ''.padLeft(5, 'a'));
467 Expect.equals('', ''.padLeft(-2, 'a'));
468
469 Expect.equals('xyzxyzxyzxyzxyz', ''.padLeft(5, 'xyz'));
470 Expect.equals('xyzxyzxyzxyza', 'a'.padLeft(5, 'xyz'));
471 Expect.equals('xyzxyzxyzaa', 'aa'.padLeft(5, 'xyz'));
472 Expect.equals('\u{10002}\u{10002}\u{10002}aa', 'aa'.padLeft(5, '\u{10002}'));
473 Expect.equals('a', 'a'.padLeft(10, ''));
474 }
475
476 void testPadRight() {
477 Expect.equals("1 ", "1".padRight(5, ' '));
478 Expect.equals("11 ", "11".padRight(5, ' '));
479 Expect.equals("111 ", "111".padRight(5, ' '));
480 Expect.equals("1111 ", "1111".padRight(5, ' '));
481 Expect.equals("11111", "11111".padRight(5, ' '));
482 Expect.equals("111111", "111111".padRight(5, ' '));
483 Expect.equals("\u{10002} ", "\u{10002}".padRight(5, ' '));
484 Expect.equals('', ''.padRight(0, 'a'));
485 Expect.equals('a', ''.padRight(1, 'a'));
486 Expect.equals('aaaaa', ''.padRight(5, 'a'));
487 Expect.equals('', ''.padRight(-2, 'a'));
488
489 Expect.equals('xyzxyzxyzxyzxyz', ''.padRight(5, 'xyz'));
490 Expect.equals('axyzxyzxyzxyz', 'a'.padRight(5, 'xyz'));
491 Expect.equals('aaxyzxyzxyz', 'aa'.padRight(5, 'xyz'));
492 Expect.equals('aa\u{10002}\u{10002}\u{10002}', 'aa'.padRight(5, '\u{10002}'));
493 Expect.equals('a', 'a'.padRight(10, ''));
494 }
OLDNEW
« no previous file with comments | « test/codegen/corelib/string_substring_test.dart ('k') | test/codegen/corelib/string_to_lower_case_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698