OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import "package:expect/expect.dart"; | |
6 | |
7 class IC { | |
8 int count = 0; | |
9 String toString() => "${count++}"; | |
10 } | |
11 | |
12 testJoin(String expect, Iterable iterable, [String separator]) { | |
13 if (separator != null) { | |
14 Expect.equals(expect, iterable.join(separator)); | |
15 } else { | |
16 Expect.equals(expect, iterable.join()); | |
17 } | |
18 } | |
19 | |
20 testCollections() { | |
21 testJoin("", [], ","); | |
22 testJoin("", [], ""); | |
23 testJoin("", []); | |
24 testJoin("", new Set(), ","); | |
25 testJoin("", new Set(), ""); | |
26 testJoin("", new Set()); | |
27 | |
28 testJoin("42", [42], ","); | |
29 testJoin("42", [42], ""); | |
30 testJoin("42", [42]); | |
31 testJoin("42", new Set()..add(42), ","); | |
32 testJoin("42", new Set()..add(42), ""); | |
33 testJoin("42", new Set()..add(42)); | |
34 | |
35 testJoin("a,b,c,d", ["a", "b", "c", "d"], ","); | |
36 testJoin("abcd", ["a", "b", "c", "d"], ""); | |
37 testJoin("abcd", ["a", "b", "c", "d"]); | |
38 testJoin("null,b,c,d", [null, "b", "c", "d"], ","); | |
39 testJoin("1,2,3,4", [1, 2, 3, 4], ","); | |
40 var ic = new IC(); | |
41 testJoin("0,1,2,3", [ic, ic, ic, ic], ","); | |
42 | |
43 var set = new Set()..add(1)..add(2)..add(3); | |
44 var perm = new Set() | |
45 ..add("123") | |
46 ..add("132") | |
47 ..add("213") | |
48 ..add("231") | |
49 ..add("312") | |
50 ..add("321"); | |
51 var setString = set.join(); | |
52 Expect.isTrue(perm.contains(setString), "set: $setString"); | |
53 | |
54 void testArray(List<int> array) { | |
55 testJoin("1,3,5,7,9", array.where((i) => i.isOdd), ","); | |
56 testJoin("0,2,4,6,8,10,12,14,16,18", array.map((i) => i * 2), ","); | |
57 testJoin("5,6,7,8,9", array.skip(5), ","); | |
58 testJoin("5,6,7,8,9", array.skipWhile((i) => i < 5), ","); | |
59 testJoin("0,1,2,3,4", array.take(5), ","); | |
60 testJoin("0,1,2,3,4", array.takeWhile((i) => i < 5), ","); | |
61 } | |
62 | |
63 testArray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | |
64 var fixedArray = new List<int>(10); | |
65 for (int i = 0; i < 10; i++) { | |
66 fixedArray[i] = i; | |
67 } | |
68 testArray(fixedArray); | |
69 testArray(const [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | |
70 | |
71 testJoin("a,b,c,d", ["a", "b", "c", "d"].map((x) => x), ","); | |
72 testJoin("abcd", ["a", "b", "c", "d"].map((x) => x), ""); | |
73 testJoin("abcd", ["a", "b", "c", "d"].map((x) => x)); | |
74 testJoin("null,b,c,d", [null, "b", "c", "d"].map((x) => x), ","); | |
75 testJoin("1,2,3,4", [1, 2, 3, 4].map((x) => x), ","); | |
76 testJoin("4,5,6,7", [ic, ic, ic, ic].map((x) => x), ","); | |
77 } | |
78 | |
79 void testStringVariants() { | |
80 // ASCII | |
81 testJoin("axbxcxd", ["a", "b", "c", "d"], "x"); | |
82 testJoin("a\u2000b\u2000c\u2000d", ["a", "b", "c", "d"], "\u2000"); | |
83 testJoin("abcd", ["a", "b", "c", "d"], ""); | |
84 testJoin("abcd", ["a", "b", "c", "d"]); | |
85 // Non-ASCII | |
86 testJoin("axbxcx\u2000", ["a", "b", "c", "\u2000"], "x"); | |
87 testJoin("a\u2000b\u2000c\u2000\u2000", ["a", "b", "c", "\u2000"], "\u2000"); | |
88 testJoin("abc\u2000", ["a", "b", "c", "\u2000"], ""); | |
89 testJoin("abc\u2000", ["a", "b", "c", "\u2000"]); | |
90 // Long-ASCII | |
91 testJoin("ax" * 255 + "a", new List.generate(256, (_) => "a"), "x"); | |
92 testJoin("a" * 256, new List.generate(256, (_) => "a")); | |
93 // Long-Non-ASCII | |
94 testJoin("a\u2000" * 255 + "a", new List.generate(256, (_) => "a"), "\u2000"); | |
95 testJoin("\u2000" * 256, new List.generate(256, (_) => "\u2000")); | |
96 testJoin( | |
97 "\u2000x" * 255 + "\u2000", new List.generate(256, (_) => "\u2000"), "x"); | |
98 | |
99 var o1 = new Stringable("x"); | |
100 var o2 = new Stringable("\ufeff"); | |
101 testJoin("xa" * 3 + "x", [o1, o1, o1, o1], "a"); | |
102 testJoin("x" * 4, [o1, o1, o1, o1], ""); | |
103 testJoin("x" * 4, [o1, o1, o1, o1]); | |
104 | |
105 testJoin("\ufeffx" * 3 + "\ufeff", [o2, o2, o2, o2], "x"); | |
106 testJoin("\ufeff" * 4, [o2, o2, o2, o2], ""); | |
107 testJoin("\ufeff" * 4, [o2, o2, o2, o2]); | |
108 | |
109 testJoin("a\u2000x\ufeff", ["a", "\u2000", o1, o2]); | |
110 testJoin("a\u2000\ufeffx", ["a", "\u2000", o2, o1]); | |
111 testJoin("ax\u2000\ufeff", ["a", o1, "\u2000", o2]); | |
112 testJoin("ax\ufeff\u2000", ["a", o1, o2, "\u2000"]); | |
113 testJoin("a\ufeffx\u2000", ["a", o2, o1, "\u2000"]); | |
114 testJoin("a\ufeff\u2000x", ["a", o2, "\u2000", o1]); | |
115 | |
116 testJoin("\u2000ax\ufeff", ["\u2000", "a", o1, o2]); | |
117 testJoin("\u2000a\ufeffx", ["\u2000", "a", o2, o1]); | |
118 testJoin("xa\u2000\ufeff", [o1, "a", "\u2000", o2]); | |
119 testJoin("xa\ufeff\u2000", [o1, "a", o2, "\u2000"]); | |
120 testJoin("\ufeffax\u2000", [o2, "a", o1, "\u2000"]); | |
121 testJoin("\ufeffa\u2000x", [o2, "a", "\u2000", o1]); | |
122 | |
123 testJoin("\u2000xa\ufeff", ["\u2000", o1, "a", o2]); | |
124 testJoin("\u2000\ufeffax", ["\u2000", o2, "a", o1]); | |
125 testJoin("x\u2000a\ufeff", [o1, "\u2000", "a", o2]); | |
126 testJoin("x\ufeffa\u2000", [o1, o2, "a", "\u2000"]); | |
127 testJoin("\ufeffxa\u2000", [o2, o1, "a", "\u2000"]); | |
128 testJoin("\ufeff\u2000ax", [o2, "\u2000", "a", o1]); | |
129 | |
130 testJoin("\u2000x\ufeffa", ["\u2000", o1, o2, "a"]); | |
131 testJoin("\u2000\ufeffxa", ["\u2000", o2, o1, "a"]); | |
132 testJoin("x\u2000\ufeffa", [o1, "\u2000", o2, "a"]); | |
133 testJoin("x\ufeff\u2000a", [o1, o2, "\u2000", "a"]); | |
134 testJoin("\ufeffx\u2000a", [o2, o1, "\u2000", "a"]); | |
135 testJoin("\ufeff\u2000xa", [o2, "\u2000", o1, "a"]); | |
136 } | |
137 | |
138 class Stringable { | |
139 final String value; | |
140 Stringable(this.value); | |
141 String toString() => value; | |
142 } | |
143 | |
144 main() { | |
145 testCollections(); | |
146 testStringVariants(); | |
147 // TODO(lrn): test scalar lists. | |
148 } | |
OLD | NEW |