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

Side by Side Diff: utils/tests/string_encoding/Utf8Tests.dart

Issue 9233041: String encoding utility methods and tests for Unicode, UTF-8, -16 and -32. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: String encoding utility methods and tests for Unicode, UTF-8, -16 and -32. Created 8 years, 10 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 #!/usr/bin/env dart
2 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
3 // for details. All rights reserved. Use of this source code is governed by a
4 // BSD-style license that can be found in the LICENSE file.
5
6 #library("Utf8Tests");
7 #import("DUnit.dart");
8 #import("../../string_encoding/Unicode.dart");
9 #import("../../string_encoding/UnicodeCore.dart");
10 #source("../../string_encoding/Utf8_impl.dart");
11
12 void main() {
13 TestSuite suite = new TestSuite();
14 suite.registerTestClass(new Utf8Tests());
15 suite.run();
16 }
17
18 class Utf8Tests extends TestClass {
19 void registerTests(TestSuite suite) {
20 register("testUtf8bytesToCodepoints", testUtf8bytesToCodepoints, suite);
21 register("testUtf8BytesToString", testUtf8BytesToString, suite);
22 register("testEncodeToUtf8", testEncodeToUtf8, suite);
23 }
24
25 void testEncodeToUtf8() {
26 Expect.listEquals([
27 0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63,
28 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20,
29 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70,
30 0x73, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74,
31 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20,
32 0x64, 0x6f, 0x67, 0x2e],
33 encodeAsUtf8("The quick brown fox jumps over the lazy dog."),
34 "english to utf8");
35
36 Expect.listEquals([
37 0x51, 0x75, 0x69, 0x7a, 0x64, 0x65, 0x6c, 0x74,
38 0x61, 0x67, 0x65, 0x72, 0x6e, 0x65, 0x20, 0x73,
39 0x70, 0x69, 0x73, 0x74, 0x65, 0x20, 0x6a, 0x6f,
40 0x72, 0x64, 0x62, 0xc3, 0xa6, 0x72, 0x20, 0x6d,
41 0x65, 0x64, 0x20, 0x66, 0x6c, 0xc3, 0xb8, 0x64,
42 0x65, 0x2c, 0x20, 0x6d, 0x65, 0x6e, 0x73, 0x20,
43 0x63, 0x69, 0x72, 0x6b, 0x75, 0x73, 0x6b, 0x6c,
44 0x6f, 0x76, 0x6e, 0x65, 0x6e, 0x20, 0x57, 0x6f,
45 0x6c, 0x74, 0x68, 0x65, 0x72, 0x20, 0x73, 0x70,
46 0x69, 0x6c, 0x6c, 0x65, 0x64, 0x65, 0x20, 0x70,
47 0xc3, 0xa5, 0x20, 0x78, 0x79, 0x6c, 0x6f, 0x66,
48 0x6f, 0x6e, 0x2e],
49 encodeAsUtf8("Quizdeltagerne spiste jordbær med fløde, mens " +
ahe 2012/01/31 14:46:53 Remove comma.
dcarlson 2012/01/31 15:07:34 Done. Thanks, my Danish is rusty ;).
50 "cirkusklovnen Wolther spillede på xylofon."),
51 "encode danish to utf8");
52
53
54 Expect.listEquals([
55 0x3f, 0x20, 0xd7, 0x93, 0xd7, 0x92, 0x20, 0xd7,
56 0xa1, 0xd7, 0xa7, 0xd7, 0xa8, 0xd7, 0x9f, 0x20,
57 0xd7, 0xa9, 0xd7, 0x98, 0x20, 0xd7, 0x91, 0xd7,
58 0x99, 0xd7, 0x9d, 0x20, 0xd7, 0x9e, 0xd7, 0x90,
59 0xd7, 0x95, 0xd7, 0x9b, 0xd7, 0x96, 0xd7, 0x91,
60 0x20, 0xd7, 0x95, 0xd7, 0x9c, 0xd7, 0xa4, 0xd7,
61 0xaa, 0xd7, 0xa2, 0x20, 0xd7, 0x9e, 0xd7, 0xa6,
62 0xd7, 0x90, 0x20, 0xd7, 0x9c, 0xd7, 0x95, 0x20,
63 0xd7, 0x97, 0xd7, 0x91, 0xd7, 0xa8, 0xd7, 0x94,
64 0x20, 0xd7, 0x90, 0xd7, 0x99, 0xd7, 0x9a, 0x20,
65 0xd7, 0x94, 0xd7, 0xa7, 0xd7, 0x9c, 0xd7, 0x99],
66 encodeAsUtf8("? דג סקרן שט בים מאוכזב ולפתע מצא לו חברה איך הקלי"),
67 "Hebrew to utf8");
68
69 Expect.listEquals([
70 0xd0, 0xa1, 0xd1, 0x8a, 0xd0, 0xb5, 0xd1, 0x88,
71 0xd1, 0x8c, 0x20, 0xd0, 0xb6, 0xd0, 0xb5, 0x20,
72 0xd0, 0xb5, 0xd1, 0x89, 0xd1, 0x91, 0x20, 0xd1,
73 0x8d, 0xd1, 0x82, 0xd0, 0xb8, 0xd1, 0x85, 0x20,
74 0xd0, 0xbc, 0xd1, 0x8f, 0xd0, 0xb3, 0xd0, 0xba,
75 0xd0, 0xb8, 0xd1, 0x85, 0x20, 0xd1, 0x84, 0xd1,
76 0x80, 0xd0, 0xb0, 0xd0, 0xbd, 0xd1, 0x86, 0xd1,
77 0x83, 0xd0, 0xb7, 0xd1, 0x81, 0xd0, 0xba, 0xd0,
78 0xb8, 0xd1, 0x85, 0x20, 0xd0, 0xb1, 0xd1, 0x83,
79 0xd0, 0xbb, 0xd0, 0xbe, 0xd0, 0xba, 0x20, 0xd0,
80 0xb4, 0xd0, 0xb0, 0x20, 0xd0, 0xb2, 0xd1, 0x8b,
81 0xd0, 0xbf, 0xd0, 0xb5, 0xd0, 0xb9, 0x20, 0xd1,
82 0x87, 0xd0, 0xb0, 0xd1, 0x8e],
83 encodeAsUtf8("Съешь же ещё этих мягких французских булок да выпей" +
84 " чаю"), "Russian #2 to utf8");
85
86 Expect.listEquals([
87 0xce, 0x93, 0xce, 0xb1, 0xce, 0xb6, 0xce, 0xad,
88 0xce, 0xb5, 0xcf, 0x82, 0x20, 0xce, 0xba, 0xce,
89 0xb1, 0xe1, 0xbd, 0xb6, 0x20, 0xce, 0xbc, 0xcf,
90 0x85, 0xcf, 0x81, 0xcf, 0x84, 0xce, 0xb9, 0xe1,
91 0xbd, 0xb2, 0xcf, 0x82, 0x20, 0xce, 0xb4, 0xe1,
92 0xbd, 0xb2, 0xce, 0xbd, 0x20, 0xce, 0xb8, 0xe1,
93 0xbd, 0xb0, 0x20, 0xce, 0xb2, 0xcf, 0x81, 0xe1,
94 0xbf, 0xb6, 0x20, 0xcf, 0x80, 0xce, 0xb9, 0xe1,
95 0xbd, 0xb0, 0x20, 0xcf, 0x83, 0xcf, 0x84, 0xe1,
96 0xbd, 0xb8, 0x20, 0xcf, 0x87, 0xcf, 0x81, 0xcf,
97 0x85, 0xcf, 0x83, 0xce, 0xb1, 0xcf, 0x86, 0xe1,
98 0xbd, 0xb6, 0x20, 0xce, 0xbe, 0xce, 0xad, 0xcf,
99 0x86, 0xcf, 0x89, 0xcf, 0x84, 0xce, 0xbf],
100 encodeAsUtf8("Γαζέες καὶ μυρτιὲς δὲν θὰ βρῶ πιὰ στὸ χρυσαφὶ ξέφωτο"),
101 "Greek to utf8");
102
103 Expect.listEquals([
104 0xe3, 0x82, 0xa4, 0xe3, 0x83, 0xad, 0xe3, 0x83,
105 0x8f, 0xe3, 0x83, 0x8b, 0xe3, 0x83, 0x9b, 0xe3,
106 0x83, 0x98, 0xe3, 0x83, 0x88, 0x20, 0xe3, 0x83,
107 0x81, 0xe3, 0x83, 0xaa, 0xe3, 0x83, 0x8c, 0xe3,
108 0x83, 0xab, 0xe3, 0x83, 0xb2, 0x20, 0xe3, 0x83,
109 0xaf, 0xe3, 0x82, 0xab, 0xe3, 0x83, 0xa8, 0xe3,
110 0x82, 0xbf, 0xe3, 0x83, 0xac, 0xe3, 0x82, 0xbd,
111 0x20, 0xe3, 0x83, 0x84, 0xe3, 0x83, 0x8d, 0xe3,
112 0x83, 0x8a, 0xe3, 0x83, 0xa9, 0xe3, 0x83, 0xa0,
113 0x0a, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xb0, 0xe3,
114 0x83, 0x8e, 0xe3, 0x82, 0xaa, 0xe3, 0x82, 0xaf,
115 0xe3, 0x83, 0xa4, 0xe3, 0x83, 0x9e, 0x20, 0xe3,
116 0x82, 0xb1, 0xe3, 0x83, 0x95, 0xe3, 0x82, 0xb3,
117 0xe3, 0x82, 0xa8, 0xe3, 0x83, 0x86, 0x20, 0xe3,
118 0x82, 0xa2, 0xe3, 0x82, 0xb5, 0xe3, 0x82, 0xad,
119 0xe3, 0x83, 0xa6, 0xe3, 0x83, 0xa1, 0xe3, 0x83,
120 0x9f, 0xe3, 0x82, 0xb7, 0x20, 0xe3, 0x83, 0xb1,
121 0xe3, 0x83, 0x92, 0xe3, 0x83, 0xa2, 0xe3, 0x82,
122 0xbb, 0xe3, 0x82, 0xb9, 0xe3, 0x83, 0xb3],
123 encodeAsUtf8( """
124 イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム
125 ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン"""),
126 "Katakana");
127 }
128
129 void testUtf8bytesToCodepoints() {
130 Expect.listEquals([954, 972, 963, 956, 949],
131 _utf8ToCodepoints([0xce, 0xba, 0xcf, 0x8c, 0xcf,
132 0x83, 0xce, 0xbc, 0xce, 0xb5]), "κόσμε");
133
134 // boundary conditions: First possible sequence of a certain length
135 Expect.listEquals([0x0], _utf8ToCodepoints([0x0]), "0");
136 Expect.listEquals([0x80], _utf8ToCodepoints([0xc2, 0x80]), "80");
137 Expect.listEquals([0x800],
138 _utf8ToCodepoints([0xe0, 0xa0, 0x80]), "800");
139 Expect.listEquals([0x10000],
140 _utf8ToCodepoints([0xf0, 0x90, 0x80, 0x80]), "10000");
141 Expect.listEquals([0x200000],
142 _utf8ToCodepoints([0xf8, 0x88, 0x80, 0x80, 0x80]), "200000");
143 Expect.listEquals([0x4000000],
144 _utf8ToCodepoints([0xfc, 0x84, 0x80, 0x80, 0x80, 0x80]),
145 "4000000");
146
147 // boundary conditions: Last possible sequence of a certain length
148 Expect.listEquals([0x7f], _utf8ToCodepoints([0x7f]), "7f");
149 Expect.listEquals([0x7ff], _utf8ToCodepoints([0xdf, 0xbf]), "7ff");
150 Expect.listEquals([0xffff],
151 _utf8ToCodepoints([0xef, 0xbf, 0xbf]), "ffff");
152 Expect.listEquals([0x1fffff],
153 _utf8ToCodepoints([0xf7, 0xbf, 0xbf, 0xbf]), "1fffff");
154 Expect.listEquals([0x3ffffff],
155 _utf8ToCodepoints([0xfb, 0xbf, 0xbf, 0xbf, 0xbf]), "3ffffff");
156 Expect.listEquals([0x7fffffff],
157 _utf8ToCodepoints([0xfd, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf]),
158 "4000000");
159
160 // other boundary conditions
161 Expect.listEquals([0xd7ff],
162 _utf8ToCodepoints([0xed, 0x9f, 0xbf]), "d7ff");
163 Expect.listEquals([0xe000],
164 _utf8ToCodepoints([0xee, 0x80, 0x80]), "e000");
165 Expect.listEquals([0xfffd],
166 _utf8ToCodepoints([0xef, 0xbf, 0xbd]), "fffd");
167 Expect.listEquals([0x10ffff],
168 _utf8ToCodepoints([0xf4, 0x8f, 0xbf, 0xbf]), "10ffff");
169 Expect.listEquals([0x110000],
170 _utf8ToCodepoints([0xf4, 0x90, 0x80, 0x80]), "110000");
171
172 // unexpected continuation bytes
173 Expect.listEquals([0xfffd],
jat 2012/01/31 15:19:22 All these 0xfffd references should refer to the co
dcarlson 2012/01/31 22:11:38 Done.
174 _utf8ToCodepoints([0x80]), "80 => replacement character");
175 Expect.listEquals([0xfffd],
176 _utf8ToCodepoints([0xbf]), "bf => replacement character");
177
178 List<int> allContinuationBytes = <int>[];
179 List<int> matchingReplacementChars = <int>[];
180 for (int i = 0x80; i < 0xc0; i++) {
181 allContinuationBytes.add(i);
182 matchingReplacementChars.add(0xfffd);
183 }
184 Expect.listEquals(matchingReplacementChars,
185 _utf8ToCodepoints(allContinuationBytes),
186 "80 - bf => replacement character x 64");
187
188 List<int> allFirstTwoByteSeq = <int>[];
189 matchingReplacementChars = <int>[];
190 for (int i = 0xc0; i < 0xe0; i++) {
191 allFirstTwoByteSeq.addAll([i, 0x20]);
192 matchingReplacementChars.addAll([0xfffd]);
193 }
194 Expect.listEquals(matchingReplacementChars,
195 _utf8ToCodepoints(allFirstTwoByteSeq),
196 "c0 - df + space => replacement character + space x 32");
197
198 List<int> allFirstThreeByteSeq = <int>[];
199 matchingReplacementChars = <int>[];
200 for (int i = 0xe0; i < 0xf0; i++) {
201 allFirstThreeByteSeq.addAll([i, 0x20]);
202 matchingReplacementChars.addAll([0xfffd]);
203 }
204 Expect.listEquals(matchingReplacementChars,
205 _utf8ToCodepoints(allFirstThreeByteSeq),
206 "e0 - ef + space => replacement character x 16");
207
208 List<int> allFirstFourByteSeq = <int>[];
209 matchingReplacementChars = <int>[];
210 for (int i = 0xf0; i < 0xf8; i++) {
211 allFirstFourByteSeq.addAll([i, 0x20]);
212 matchingReplacementChars.addAll([0xfffd]);
213 }
214 Expect.listEquals(matchingReplacementChars,
215 _utf8ToCodepoints(allFirstFourByteSeq),
216 "f0 - f7 + space => replacement character x 8");
217
218 List<int> allFirstFiveByteSeq = <int>[];
219 matchingReplacementChars = <int>[];
220 for (int i = 0xf8; i < 0xfc; i++) {
221 allFirstFiveByteSeq.addAll([i, 0x20]);
222 matchingReplacementChars.addAll([0xfffd]);
223 }
224 Expect.listEquals(matchingReplacementChars,
225 _utf8ToCodepoints(allFirstFiveByteSeq),
226 "f8 - fb + space => replacement character x 4");
227
228 List<int> allFirstSixByteSeq = <int>[];
229 matchingReplacementChars = <int>[];
230 for (int i = 0xfc; i < 0xfe; i++) {
231 allFirstSixByteSeq.addAll([i, 0x20]);
232 matchingReplacementChars.addAll([0xfffd]);
233 }
234 Expect.listEquals(matchingReplacementChars,
235 _utf8ToCodepoints(allFirstSixByteSeq),
236 "fc - fd + space => replacement character x 2");
237
238 // Sequences with last continuation byte missing
239 Expect.listEquals([0xfffd],
240 _utf8ToCodepoints([0xc2]),
241 "2-byte sequence with last byte missing");
242 Expect.listEquals([0xfffd],
243 _utf8ToCodepoints([0xe0, 0x80]),
244 "3-byte sequence with last byte missing");
245 Expect.listEquals([0xfffd],
246 _utf8ToCodepoints([0xf0, 0x80, 0x80]),
247 "4-byte sequence with last byte missing");
248 Expect.listEquals([0xfffd],
249 _utf8ToCodepoints([0xf8, 0x88, 0x80, 0x80]),
250 "5-byte sequence with last byte missing");
251 Expect.listEquals([0xfffd],
252 _utf8ToCodepoints([0xfc, 0x80, 0x80, 0x80, 0x80]),
253 "6-byte sequence with last byte missing");
254
255 Expect.listEquals([0xfffd],
256 _utf8ToCodepoints([0xdf]),
257 "2-byte sequence with last byte missing (hi)");
258 Expect.listEquals([0xfffd],
259 _utf8ToCodepoints([0xef, 0xbf]),
260 "3-byte sequence with last byte missing (hi)");
261 Expect.listEquals([0xfffd],
262 _utf8ToCodepoints([0xf7, 0xbf, 0xbf]),
263 "4-byte sequence with last byte missing (hi)");
264 Expect.listEquals([0xfffd],
265 _utf8ToCodepoints([0xfb, 0xbf, 0xbf, 0xbf]),
266 "5-byte sequence with last byte missing (hi)");
267 Expect.listEquals([0xfffd],
268 _utf8ToCodepoints([0xfd, 0xbf, 0xbf, 0xbf, 0xbf]),
269 "6-byte sequence with last byte missing (hi)");
270
271 // Concatenation of incomplete sequences
272 Expect.listEquals(
273 [ 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd,
274 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd ],
275 _utf8ToCodepoints(
276 [ 0xc2,
277 0xe0, 0x80,
278 0xf0, 0x80, 0x80,
279 0xf8, 0x88, 0x80, 0x80,
280 0xfc, 0x80, 0x80, 0x80, 0x80,
281 0xdf,
282 0xef, 0xbf,
283 0xf7, 0xbf, 0xbf,
284 0xfb, 0xbf, 0xbf, 0xbf,
285 0xfd, 0xbf, 0xbf, 0xbf, 0xbf ]),
286 "Concatenation of incomplete sequences");
287
288 // Impossible bytes
289 Expect.listEquals([0xfffd], _utf8ToCodepoints([0xfe]), "fe");
290 Expect.listEquals([0xfffd], _utf8ToCodepoints([0xff]), "ff");
291 Expect.listEquals([0xfffd, 0xfffd, 0xfffd, 0xfffd],
292 _utf8ToCodepoints([0xfe, 0xfe, 0xff, 0xff]), "fe fe ff ff");
293
294 // Overlong sequences
295 Expect.listEquals([0xfffd], _utf8ToCodepoints([0xc0, 0xaf]),
296 "c0 af");
297 Expect.listEquals([0xfffd], _utf8ToCodepoints([0xe0, 0x80, 0xaf]),
298 "e0 80 af");
299 Expect.listEquals([0xfffd], _utf8ToCodepoints([0xf0, 0x80, 0x80, 0xaf]),
300 "f0 80 80 af");
301 Expect.listEquals([0xfffd],
302 _utf8ToCodepoints([0xf8, 0x80, 0x80, 0x80, 0xaf]),
303 "f8 80 80 80 af");
304 Expect.listEquals([0xfffd],
305 _utf8ToCodepoints([0xfc, 0x80, 0x80, 0x80, 0x80, 0xaf]),
306 "fc 80 80 80 80 af");
307
308 Expect.listEquals([0xfffd], _utf8ToCodepoints([0xc1, 0xbf]),
309 "c1 bf");
310 Expect.listEquals([0xfffd], _utf8ToCodepoints([0xe0, 0x9f, 0xbf]),
311 "e0 9f bf");
312 Expect.listEquals([0xfffd], _utf8ToCodepoints([0xf0, 0x8f, 0xbf, 0xbf]),
313 "f0 8f bf bf");
314 Expect.listEquals([0xfffd],
315 _utf8ToCodepoints([0xf8, 0x87, 0xbf, 0xbf, 0xbf]),
316 "f8 87 bf bf bf");
317 Expect.listEquals([0xfffd],
318 _utf8ToCodepoints([0xfc, 0x83, 0xbf, 0xbf, 0xbf, 0xbf]),
319 "fc 83 bf bf bf bf");
320
321 Expect.listEquals([0xfffd], _utf8ToCodepoints([0xc0, 0x80]),
322 "c0 80");
323 Expect.listEquals([0xfffd], _utf8ToCodepoints([0xe0, 0x80, 0x80]),
324 "e0 80 80");
325 Expect.listEquals([0xfffd], _utf8ToCodepoints([0xf0, 0x80, 0x80, 0x80]),
326 "f0 80 80 80");
327 Expect.listEquals([0xfffd],
328 _utf8ToCodepoints([0xf8, 0x80, 0x80, 0x80, 0x80]),
329 "f8 80 80 80 80");
330 Expect.listEquals([0xfffd],
331 _utf8ToCodepoints([0xfc, 0x80, 0x80, 0x80, 0x80, 0x80]),
332 "fc 80 80 80 80 80");
333
334 // Illegal code positions
335 Expect.listEquals([0xfffd],
336 _utf8ToCodepoints([0xed, 0xa0, 0x80]), "U+D800");
337 Expect.listEquals([0xfffd],
338 _utf8ToCodepoints([0xed, 0xad, 0xbf]), "U+DB7F");
339 Expect.listEquals([0xfffd],
340 _utf8ToCodepoints([0xed, 0xae, 0x80]), "U+DB80");
341 Expect.listEquals([0xfffd],
342 _utf8ToCodepoints([0xed, 0xaf, 0xbf]), "U+DBFF");
343 Expect.listEquals([0xfffd],
344 _utf8ToCodepoints([0xed, 0xb0, 0x80]), "U+DC00");
345 Expect.listEquals([0xfffd],
346 _utf8ToCodepoints([0xed, 0xbe, 0x80]), "U+DF80");
347 Expect.listEquals([0xfffd],
348 _utf8ToCodepoints([0xed, 0xbf, 0xbf]), "U+DFFF");
349
350 // Paired UTF-16 surrogates
351 Expect.listEquals([0xfffd, 0xfffd],
352 _utf8ToCodepoints([0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80]),
353 "U+D800 U+DC00");
354 Expect.listEquals([0xfffd, 0xfffd],
355 _utf8ToCodepoints([0xed, 0xa0, 0x80, 0xed, 0xbf, 0xbf]),
356 "U+D800 U+DFFF");
357 Expect.listEquals([0xfffd, 0xfffd],
358 _utf8ToCodepoints([0xed, 0xad, 0xbf, 0xed, 0xb0, 0x80]),
359 "U+DB7F U+DC00");
360 Expect.listEquals([0xfffd, 0xfffd],
361 _utf8ToCodepoints([0xed, 0xad, 0xbf, 0xed, 0xbf, 0xbf]),
362 "U+DB7F U+DFFF");
363 Expect.listEquals([0xfffd, 0xfffd],
364 _utf8ToCodepoints([0xed, 0xae, 0x80, 0xed, 0xb0, 0x80]),
365 "U+DB80 U+DC00");
366 Expect.listEquals([0xfffd, 0xfffd],
367 _utf8ToCodepoints([0xed, 0xae, 0x80, 0xed, 0xbf, 0xbf]),
368 "U+DB80 U+DFFF");
369 Expect.listEquals([0xfffd, 0xfffd],
370 _utf8ToCodepoints([0xed, 0xaf, 0xbf, 0xed, 0xb0, 0x80]),
371 "U+DBFF U+DC00");
372 Expect.listEquals([0xfffd, 0xfffd],
373 _utf8ToCodepoints([0xed, 0xaf, 0xbf, 0xed, 0xbf, 0xbf]),
374 "U+DBFF U+DFFF");
375
376 // Other illegal code positions (???)
377 Expect.listEquals([0xfffe], _utf8ToCodepoints([0xef, 0xbf, 0xbe]),
378 "U+FFFE");
379 Expect.listEquals([0xffff], _utf8ToCodepoints([0xef, 0xbf, 0xbf]),
380 "U+FFFF");
381 }
382
383 void testUtf8BytesToString() {
384
385 Expect.stringEquals("The quick brown fox jumps over the lazy dog.",
386 decodeFromUtf8([
387 0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63,
388 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20,
389 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70,
390 0x73, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74,
391 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20,
392 0x64, 0x6f, 0x67, 0x2e]), "English");
393
394 Expect.stringEquals("Quizdeltagerne spiste jordbær med fløde, mens " +
395 "cirkusklovnen Wolther spillede på xylofon.",
396 decodeFromUtf8([
397 0x51, 0x75, 0x69, 0x7a, 0x64, 0x65, 0x6c, 0x74,
398 0x61, 0x67, 0x65, 0x72, 0x6e, 0x65, 0x20, 0x73,
399 0x70, 0x69, 0x73, 0x74, 0x65, 0x20, 0x6a, 0x6f,
400 0x72, 0x64, 0x62, 0xc3, 0xa6, 0x72, 0x20, 0x6d,
401 0x65, 0x64, 0x20, 0x66, 0x6c, 0xc3, 0xb8, 0x64,
402 0x65, 0x2c, 0x20, 0x6d, 0x65, 0x6e, 0x73, 0x20,
403 0x63, 0x69, 0x72, 0x6b, 0x75, 0x73, 0x6b, 0x6c,
404 0x6f, 0x76, 0x6e, 0x65, 0x6e, 0x20, 0x57, 0x6f,
405 0x6c, 0x74, 0x68, 0x65, 0x72, 0x20, 0x73, 0x70,
406 0x69, 0x6c, 0x6c, 0x65, 0x64, 0x65, 0x20, 0x70,
407 0xc3, 0xa5, 0x20, 0x78, 0x79, 0x6c, 0x6f, 0x66,
408 0x6f, 0x6e, 0x2e]), "Danish");
409
410 Expect.stringEquals("? דג סקרן שט בים מאוכזב ולפתע מצא לו חברה איך הקלי",
411 decodeFromUtf8([
412 0x3f, 0x20, 0xd7, 0x93, 0xd7, 0x92, 0x20, 0xd7,
413 0xa1, 0xd7, 0xa7, 0xd7, 0xa8, 0xd7, 0x9f, 0x20,
414 0xd7, 0xa9, 0xd7, 0x98, 0x20, 0xd7, 0x91, 0xd7,
415 0x99, 0xd7, 0x9d, 0x20, 0xd7, 0x9e, 0xd7, 0x90,
416 0xd7, 0x95, 0xd7, 0x9b, 0xd7, 0x96, 0xd7, 0x91,
417 0x20, 0xd7, 0x95, 0xd7, 0x9c, 0xd7, 0xa4, 0xd7,
418 0xaa, 0xd7, 0xa2, 0x20, 0xd7, 0x9e, 0xd7, 0xa6,
419 0xd7, 0x90, 0x20, 0xd7, 0x9c, 0xd7, 0x95, 0x20,
420 0xd7, 0x97, 0xd7, 0x91, 0xd7, 0xa8, 0xd7, 0x94,
421 0x20, 0xd7, 0x90, 0xd7, 0x99, 0xd7, 0x9a, 0x20,
422 0xd7, 0x94, 0xd7, 0xa7, 0xd7, 0x9c, 0xd7, 0x99]), "Hebrew");
423
424 Expect.stringEquals("Съешь же ещё этих мягких французских булок да выпей " +
425 "чаю",
426 decodeFromUtf8([
427 0xd0, 0xa1, 0xd1, 0x8a, 0xd0 ,0xb5, 0xd1, 0x88,
428 0xd1, 0x8c, 0x20, 0xd0, 0xb6, 0xd0, 0xb5, 0x20,
429 0xd0, 0xb5, 0xd1, 0x89, 0xd1, 0x91, 0x20, 0xd1,
430 0x8d, 0xd1, 0x82, 0xd0, 0xb8, 0xd1, 0x85, 0x20,
431 0xd0, 0xbc, 0xd1, 0x8f, 0xd0, 0xb3, 0xd0, 0xba,
432 0xd0, 0xb8, 0xd1, 0x85, 0x20, 0xd1, 0x84, 0xd1,
433 0x80, 0xd0, 0xb0, 0xd0, 0xbd, 0xd1, 0x86, 0xd1,
434 0x83, 0xd0, 0xb7, 0xd1, 0x81, 0xd0, 0xba, 0xd0,
435 0xb8, 0xd1, 0x85, 0x20, 0xd0, 0xb1, 0xd1, 0x83,
436 0xd0, 0xbb, 0xd0, 0xbe, 0xd0, 0xba, 0x20, 0xd0,
437 0xb4, 0xd0, 0xb0, 0x20, 0xd0, 0xb2, 0xd1, 0x8b,
438 0xd0, 0xbf, 0xd0, 0xb5, 0xd0, 0xb9, 0x20, 0xd1,
439 0x87, 0xd0, 0xb0, 0xd1, 0x8e]), "Russian #2");
440
441 Expect.stringEquals("Γαζέες καὶ μυρτιὲς δὲν θὰ βρῶ πιὰ στὸ χρυσαφὶ ξέφωτο",
442 decodeFromUtf8([
443 0xce, 0x93, 0xce, 0xb1, 0xce, 0xb6, 0xce, 0xad,
444 0xce, 0xb5, 0xcf, 0x82, 0x20, 0xce, 0xba, 0xce,
445 0xb1, 0xe1, 0xbd, 0xb6, 0x20, 0xce, 0xbc, 0xcf,
446 0x85, 0xcf, 0x81, 0xcf, 0x84, 0xce, 0xb9, 0xe1,
447 0xbd, 0xb2, 0xcf, 0x82, 0x20, 0xce, 0xb4, 0xe1,
448 0xbd, 0xb2, 0xce, 0xbd, 0x20, 0xce, 0xb8, 0xe1,
449 0xbd, 0xb0, 0x20, 0xce, 0xb2, 0xcf, 0x81, 0xe1,
450 0xbf, 0xb6, 0x20, 0xcf, 0x80, 0xce, 0xb9, 0xe1,
451 0xbd, 0xb0, 0x20, 0xcf, 0x83, 0xcf, 0x84, 0xe1,
452 0xbd, 0xb8, 0x20, 0xcf, 0x87, 0xcf, 0x81, 0xcf,
453 0x85, 0xcf, 0x83, 0xce, 0xb1, 0xcf, 0x86, 0xe1,
454 0xbd, 0xb6, 0x20, 0xce, 0xbe, 0xce, 0xad, 0xcf,
455 0x86, 0xcf, 0x89, 0xcf, 0x84, 0xce, 0xbf]), "Greek");
456 Expect.stringEquals("""
457 イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム
458 ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン""",
459 decodeFromUtf8([
460 0xe3, 0x82, 0xa4, 0xe3, 0x83, 0xad, 0xe3, 0x83,
461 0x8f, 0xe3, 0x83, 0x8b, 0xe3, 0x83, 0x9b, 0xe3,
462 0x83, 0x98, 0xe3, 0x83, 0x88, 0x20, 0xe3, 0x83,
463 0x81, 0xe3, 0x83, 0xaa, 0xe3, 0x83, 0x8c, 0xe3,
464 0x83, 0xab, 0xe3, 0x83, 0xb2, 0x20, 0xe3, 0x83,
465 0xaf, 0xe3, 0x82, 0xab, 0xe3, 0x83, 0xa8, 0xe3,
466 0x82, 0xbf, 0xe3, 0x83, 0xac, 0xe3, 0x82, 0xbd,
467 0x20, 0xe3, 0x83, 0x84, 0xe3, 0x83, 0x8d, 0xe3,
468 0x83, 0x8a, 0xe3, 0x83, 0xa9, 0xe3, 0x83, 0xa0,
469 0x0a, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xb0, 0xe3,
470 0x83, 0x8e, 0xe3, 0x82, 0xaa, 0xe3, 0x82, 0xaf,
471 0xe3, 0x83, 0xa4, 0xe3, 0x83, 0x9e, 0x20, 0xe3,
472 0x82, 0xb1, 0xe3, 0x83, 0x95, 0xe3, 0x82, 0xb3,
473 0xe3, 0x82, 0xa8, 0xe3, 0x83, 0x86, 0x20, 0xe3,
474 0x82, 0xa2, 0xe3, 0x82, 0xb5, 0xe3, 0x82, 0xad,
475 0xe3, 0x83, 0xa6, 0xe3, 0x83, 0xa1, 0xe3, 0x83,
476 0x9f, 0xe3, 0x82, 0xb7, 0x20, 0xe3, 0x83, 0xb1,
477 0xe3, 0x83, 0x92, 0xe3, 0x83, 0xa2, 0xe3, 0x82,
478 0xbb, 0xe3, 0x82, 0xb9, 0xe3, 0x83, 0xb3]), "Katakana");
479 }
480 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698