| 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 class StringFromListTest { | 5 void main() { |
| 6 static testMain() { | 6 Expect.equals("", new String.fromCharCodes(new List(0))); |
| 7 Expect.equals("", new String.fromCharCodes(new List(0))); | 7 Expect.equals("", new String.fromCharCodes([])); |
| 8 Expect.equals("", new String.fromCharCodes([])); | 8 Expect.equals("", new String.fromCharCodes(const [])); |
| 9 Expect.equals("", new String.fromCharCodes(const [])); | 9 Expect.equals("AB", new String.fromCharCodes([65, 66])); |
| 10 Expect.equals("AB", new String.fromCharCodes([65, 66])); | 10 Expect.equals("AB", new String.fromCharCodes(const [65, 66])); |
| 11 Expect.equals("AB", new String.fromCharCodes(const [65, 66])); | 11 Expect.equals("Ærø", new String.fromCharCodes(const [0xc6, 0x72, 0xf8])); |
| 12 Expect.equals("Ærø", new String.fromCharCodes(const [0xc6, 0x72, 0xf8])); | 12 Expect.equals("\u{1234}", new String.fromCharCodes([0x1234])); |
| 13 Expect.equals("\u{1234}", new String.fromCharCodes([0x1234])); | 13 Expect.equals("\u{12345}*", new String.fromCharCodes([0x12345, 42])); |
| 14 Expect.equals("\u{12345}*", new String.fromCharCodes([0x12345, 42])); | 14 Expect.equals("", new String.fromCharCodes(new List())); |
| 15 Expect.equals("", new String.fromCharCodes(new List())); | 15 { |
| 16 { | 16 var a = new List(); |
| 17 var a = new List(); | 17 a.add(65); |
| 18 a.add(65); | 18 a.add(66); |
| 19 a.add(66); | 19 Expect.equals("AB", new String.fromCharCodes(a)); |
| 20 Expect.equals("AB", new String.fromCharCodes(a)); | 20 } |
| 21 |
| 22 // Long list (bug 6919). |
| 23 for (int len in [499, 500, 501, 999, 100000]) { |
| 24 List<int> list = new List(len); |
| 25 for (int i = 0; i < len; i++) { |
| 26 list[i] = 65 + (i % 26); |
| 21 } | 27 } |
| 28 for (int i = len - 9; i < len; i++) { |
| 29 list[i] = 48 + (len - i); |
| 30 } |
| 31 // We should not throw a stack overflow here. |
| 32 String long = new String.fromCharCodes(list); |
| 33 // Minimal sanity checking on the string. |
| 34 Expect.isTrue(long.startsWith('ABCDE')); |
| 35 Expect.isTrue(long.endsWith('987654321')); |
| 36 int middle = len ~/ 2; |
| 37 middle -= middle % 26; |
| 38 Expect.equals('XYZABC', long.substring(middle - 3, middle + 3)); |
| 39 Expect.equals(len, long.length); |
| 40 } |
| 22 | 41 |
| 23 // Long list (bug 6919). | 42 |
| 24 for (int len in [499, 500, 501, 999, 100000]) { | 43 // Should work with iterables and non-default-lists (http://dartbug.com/8922) |
| 25 List<int> list = new List(len); | 44 Expect.equals("CBA", new String.fromCharCodes([65, 66, 67].reversed)); |
| 26 for (int i = 0; i < len; i++) { | 45 Expect.equals("BCD", |
| 27 list[i] = 65 + (i % 26); | 46 new String.fromCharCodes([65, 66, 67].map((x) => x + 1))); |
| 28 } | 47 Expect.equals("AC", |
| 29 for (int i = len - 9; i < len; i++) { | 48 new String.fromCharCodes([0x41, 0x42, 0x43].where( |
| 30 list[i] = 48 + (len - i); | 49 (x) => x.isOdd))); |
| 31 } | 50 Expect.equals("CE", |
| 32 // We should not throw a stack overflow here. | 51 new String.fromCharCodes( |
| 33 String long = new String.fromCharCodes(list); | 52 [0x41, 0x42, 0x43].where((x) => x.isOdd) |
| 34 // Minimal sanity checking on the string. | 53 .map((x) => x + 2))); |
| 35 Expect.isTrue(long.startsWith('ABCDE')); | 54 Expect.equals("ABC", new String.fromCharCodes( |
| 36 Expect.isTrue(long.endsWith('987654321')); | 55 new Iterable.generate(3, (x) => 65 + x))); |
| 37 int middle = len ~/ 2; | 56 Expect.equals("ABC", new String.fromCharCodes("ABC".codeUnits)); |
| 38 middle -= middle % 26; | 57 Expect.equals("BCD", |
| 39 Expect.equals('XYZABC', long.substring(middle - 3, middle + 3)); | 58 new String.fromCharCodes("ABC".codeUnits.map((x) => x + 1))); |
| 40 Expect.equals(len, long.length); | 59 Expect.equals("BCD", |
| 41 } | 60 new String.fromCharCodes("ABC".runes.map((x) => x + 1))); |
| 42 } | 61 |
| 62 var nonBmpCharCodes = [0, 0xD812, 0xDC34, 0x14834, 0xDC34, 0xD812]; |
| 63 var nonBmp = new String.fromCharCodes(nonBmpCharCodes); |
| 64 Expect.equals(7, nonBmp.length); |
| 65 Expect.equals(0, nonBmp.codeUnitAt(0)); |
| 66 Expect.equals(0xD812, nonBmp.codeUnitAt(1)); // Separated surrogate pair |
| 67 Expect.equals(0xDC34, nonBmp.codeUnitAt(2)); |
| 68 Expect.equals(0xD812, nonBmp.codeUnitAt(3)); // Single non-BMP code point. |
| 69 Expect.equals(0xDC34, nonBmp.codeUnitAt(4)); |
| 70 Expect.equals(0xDC34, nonBmp.codeUnitAt(5)); // Unmatched surrogate. |
| 71 Expect.equals(0xD812, nonBmp.codeUnitAt(6)); // Unmatched surrogate. |
| 72 |
| 73 var reversedNonBmp = new String.fromCharCodes(nonBmpCharCodes.reversed); |
| 74 Expect.equals(7, reversedNonBmp.length); |
| 75 Expect.equals(0, reversedNonBmp.codeUnitAt(6)); |
| 76 Expect.equals(0xD812, reversedNonBmp.codeUnitAt(5)); |
| 77 Expect.equals(0xDC34, reversedNonBmp.codeUnitAt(4)); |
| 78 Expect.equals(0xDC34, reversedNonBmp.codeUnitAt(3)); |
| 79 Expect.equals(0xD812, reversedNonBmp.codeUnitAt(2)); |
| 80 Expect.equals(0xDC34, reversedNonBmp.codeUnitAt(1)); |
| 81 Expect.equals(0xD812, reversedNonBmp.codeUnitAt(0)); |
| 82 |
| 83 Expect.equals(nonBmp, new String.fromCharCodes(nonBmp.codeUnits)); |
| 84 Expect.equals(nonBmp, new String.fromCharCodes(nonBmp.runes)); |
| 43 } | 85 } |
| 44 | |
| 45 main() { | |
| 46 StringFromListTest.testMain(); | |
| 47 } | |
| OLD | NEW |