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