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

Side by Side Diff: tests/lib/convert/chunked_conversion_utf88_test.dart

Issue 19883003: Add chunked conversion to converters. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 4 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 // Copyright (c) 2013, 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 library utf8_test;
6 import "package:expect/expect.dart";
7 import 'dart:convert';
8
9 List<int> encode(String str) {
10 List<int> bytes;
11 ChunkedConversionSink byteSink =
12 new ByteConversionSink.withCallback((result) => bytes = result);
13 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink);
14 stringConversionSink.add(str);
15 stringConversionSink.close();
16 return bytes;
17 }
18
19 List<int> encode2(String str) {
20 List<int> bytes;
21 ChunkedConversionSink byteSink =
22 new ByteConversionSink.withCallback((result) => bytes = result);
23 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink);
24 ClosableStringSink stringSink = stringConversionSink.asStringSink();
25 stringSink.write(str);
26 stringSink.close();
27 return bytes;
28 }
29
30 List<int> encode3(String str) {
31 List<int> bytes;
32 ChunkedConversionSink byteSink =
33 new ByteConversionSink.withCallback((result) => bytes = result);
34 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink);
35 ClosableStringSink stringSink = stringConversionSink.asStringSink();
36 str.codeUnits.forEach(stringSink.writeCharCode);
37 stringSink.close();
38 return bytes;
39 }
40
41 List<int> encode4(String str) {
42 List<int> bytes;
43 ChunkedConversionSink byteSink =
44 new ByteConversionSink.withCallback((result) => bytes = result);
45 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink);
46 ClosableStringSink stringSink = stringConversionSink.asStringSink();
47 str.runes.forEach(stringSink.writeCharCode);
48 stringSink.close();
49 return bytes;
50 }
51
52 List<int> encode5(String str) {
53 List<int> bytes;
54 ChunkedConversionSink byteSink =
55 new ByteConversionSink.withCallback((result) => bytes = result);
56 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink);
57 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false);
58 List<int> tmpBytes = UTF8.encode(str);
59 inputByteSink.add(tmpBytes);
60 inputByteSink.close();
61 return bytes;
62 }
63
64 List<int> encode6(String str) {
65 List<int> bytes;
66 ChunkedConversionSink byteSink =
67 new ByteConversionSink.withCallback((result) => bytes = result);
68 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink);
69 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false);
70 List<int> tmpBytes = UTF8.encode(str);
71 tmpBytes.forEach((b) => inputByteSink.addSlice([0, b, 1], 1, 2, false));
72 inputByteSink.close();
73 return bytes;
74 }
75
76 List<int> encode7(String str) {
77 List<int> bytes;
78 ChunkedConversionSink byteSink =
79 new ByteConversionSink.withCallback((result) => bytes = result);
80 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink);
81 stringConversionSink.addSlice("1" + str + "2", 1, str.length + 1, false);
82 stringConversionSink.close();
83 return bytes;
84 }
85
86
87 int _nextPowerOf2(v) {
88 assert(v > 0);
89 v--;
90 v |= v >> 1;
91 v |= v >> 2;
92 v |= v >> 4;
93 v |= v >> 8;
94 v |= v >> 16;
95 v++;
96 return v;
97 }
98
99 main() {
100 const LEADING_SURROGATE = 0xd801;
101 const TRAILING_SURROGATE = 0xdc12;
102 const UTF8_ENCODING = const [0xf0, 0x90, 0x90, 0x92];
103 const UTF8_LEADING = const [0xed, 0xa0, 0x81];
104 const UTF8_TRAILING = const [0xed, 0xb0, 0x92];
105 const CHAR_A = 0x61;
106
107 // Test surrogates at all kinds of locations.
108 var tests = [];
109 List codeUnits = <int>[];
110 for (int i = 0; i < 2049; i++) {
111 // Invariant: codeUnits[0..i - 1] is filled with CHAR_A (character 'a').
112 codeUnits.length = i + 1;
113 codeUnits[i] = CHAR_A;
114
115 // Only test for problem zones, close to powers of two.
116 if (i > 20 && _nextPowerOf2(i - 15) - i > 30) continue;
117
118 codeUnits[i] = LEADING_SURROGATE;
119 var str = new String.fromCharCodes(codeUnits);
120 var bytes = new List.filled(i + 3, CHAR_A);
121 bytes[i] = UTF8_LEADING[0];
122 bytes[i + 1] = UTF8_LEADING[1];
123 bytes[i + 2] = UTF8_LEADING[2];
124 tests.add([bytes, str]);
125
126 codeUnits[i] = TRAILING_SURROGATE;
127 str = new String.fromCharCodes(codeUnits);
128 bytes = new List.filled(i + 3, CHAR_A);
129 bytes[i] = UTF8_TRAILING[0];
130 bytes[i + 1] = UTF8_TRAILING[1];
131 bytes[i + 2] = UTF8_TRAILING[2];
132 tests.add([bytes, str]);
133
134 codeUnits.length = i + 2;
135 codeUnits[i] = LEADING_SURROGATE;
136 codeUnits[i + 1] = TRAILING_SURROGATE;
137 str = new String.fromCharCodes(codeUnits);
138 bytes = new List.filled(i + 4, CHAR_A);
139 bytes[i] = UTF8_ENCODING[0];
140 bytes[i + 1] = UTF8_ENCODING[1];
141 bytes[i + 2] = UTF8_ENCODING[2];
142 bytes[i + 3] = UTF8_ENCODING[3];
143 tests.add([bytes, str]);
144
145 codeUnits[i] = TRAILING_SURROGATE;
146 codeUnits[i + 1] = TRAILING_SURROGATE;
147 str = new String.fromCharCodes(codeUnits);
148 bytes = new List.filled(i + 6, CHAR_A);
149 bytes[i] = UTF8_TRAILING[0];
150 bytes[i + 1] = UTF8_TRAILING[1];
151 bytes[i + 2] = UTF8_TRAILING[2];
152 bytes[i + 3] = UTF8_TRAILING[0];
153 bytes[i + 4] = UTF8_TRAILING[1];
154 bytes[i + 5] = UTF8_TRAILING[2];
155 tests.add([bytes, str]);
156
157 codeUnits[i] = LEADING_SURROGATE;
158 codeUnits[i + 1] = LEADING_SURROGATE;
159 str = new String.fromCharCodes(codeUnits);
160 bytes = new List.filled(i + 6, CHAR_A);
161 bytes[i] = UTF8_LEADING[0];
162 bytes[i + 1] = UTF8_LEADING[1];
163 bytes[i + 2] = UTF8_LEADING[2];
164 bytes[i + 3] = UTF8_LEADING[0];
165 bytes[i + 4] = UTF8_LEADING[1];
166 bytes[i + 5] = UTF8_LEADING[2];
167 tests.add([bytes, str]);
168
169 codeUnits[i] = TRAILING_SURROGATE;
170 codeUnits[i + 1] = LEADING_SURROGATE;
171 str = new String.fromCharCodes(codeUnits);
172 bytes = new List.filled(i + 6, CHAR_A);
173 bytes[i] = UTF8_TRAILING[0];
174 bytes[i + 1] = UTF8_TRAILING[1];
175 bytes[i + 2] = UTF8_TRAILING[2];
176 bytes[i + 3] = UTF8_LEADING[0];
177 bytes[i + 4] = UTF8_LEADING[1];
178 bytes[i + 5] = UTF8_LEADING[2];
179 tests.add([bytes, str]);
180
181 codeUnits.length = i + 3;
182 codeUnits[i] = LEADING_SURROGATE;
183 codeUnits[i + 1] = TRAILING_SURROGATE;
184 codeUnits[i + 2] = CHAR_A; // Add trailing 'a'.
185 str = new String.fromCharCodes(codeUnits);
186 bytes = new List.filled(i + 5, CHAR_A);
187 bytes[i] = UTF8_ENCODING[0];
188 bytes[i + 1] = UTF8_ENCODING[1];
189 bytes[i + 2] = UTF8_ENCODING[2];
190 bytes[i + 3] = UTF8_ENCODING[3];
191 // No need to assign the 'a' character. The whole list is already filled
192 // with it.
193 tests.add([bytes, str]);
194
195 codeUnits[i] = TRAILING_SURROGATE;
196 codeUnits[i + 1] = TRAILING_SURROGATE;
197 codeUnits[i + 2] = CHAR_A; // Add trailing 'a'.
198 str = new String.fromCharCodes(codeUnits);
199 bytes = new List.filled(i + 7, CHAR_A);
200 bytes[i] = UTF8_TRAILING[0];
201 bytes[i + 1] = UTF8_TRAILING[1];
202 bytes[i + 2] = UTF8_TRAILING[2];
203 bytes[i + 3] = UTF8_TRAILING[0];
204 bytes[i + 4] = UTF8_TRAILING[1];
205 bytes[i + 5] = UTF8_TRAILING[2];
206 tests.add([bytes, str]);
207
208 codeUnits[i] = LEADING_SURROGATE;
209 codeUnits[i + 1] = LEADING_SURROGATE;
210 codeUnits[i + 2] = CHAR_A; // Add trailing 'a'.
211 str = new String.fromCharCodes(codeUnits);
212 bytes = new List.filled(i + 7, CHAR_A);
213 bytes[i] = UTF8_LEADING[0];
214 bytes[i + 1] = UTF8_LEADING[1];
215 bytes[i + 2] = UTF8_LEADING[2];
216 bytes[i + 3] = UTF8_LEADING[0];
217 bytes[i + 4] = UTF8_LEADING[1];
218 bytes[i + 5] = UTF8_LEADING[2];
219 tests.add([bytes, str]);
220
221 codeUnits[i] = TRAILING_SURROGATE;
222 codeUnits[i + 1] = LEADING_SURROGATE;
223 codeUnits[i + 2] = CHAR_A; // Add trailing 'a'.
224 str = new String.fromCharCodes(codeUnits);
225 bytes = new List.filled(i + 7, CHAR_A);
226 bytes[i] = UTF8_TRAILING[0];
227 bytes[i + 1] = UTF8_TRAILING[1];
228 bytes[i + 2] = UTF8_TRAILING[2];
229 bytes[i + 3] = UTF8_LEADING[0];
230 bytes[i + 4] = UTF8_LEADING[1];
231 bytes[i + 5] = UTF8_LEADING[2];
232 tests.add([bytes, str]);
233
234 // Make sure the invariant is correct.
235 codeUnits[i] = CHAR_A;
236 }
237
238 for (var test in tests) {
239 List<int> bytes = test[0];
240 String string = test[1];
241 Expect.listEquals(bytes, encode(string));
242 Expect.listEquals(bytes, encode2(string));
243 Expect.listEquals(bytes, encode3(string));
244 Expect.listEquals(bytes, encode4(string));
245 Expect.listEquals(bytes, encode5(string));
246 Expect.listEquals(bytes, encode6(string));
247 Expect.listEquals(bytes, encode7(string));
248 }
249 }
OLDNEW
« no previous file with comments | « tests/lib/convert/chunked_conversion_utf87_test.dart ('k') | tests/lib/convert/chunked_conversion_utf8_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698