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

Side by Side Diff: tests/lib/convert/chunked_conversion_utf82_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
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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 library utf8_test; 5 library utf8_test;
6 import "package:expect/expect.dart"; 6 import "package:expect/expect.dart";
7 import 'dart:convert'; 7 import 'dart:convert';
8 8
9 String decode(List<int> bytes) => new Utf8Decoder().convert(bytes); 9 String decode(List<int> bytes, int chunkSize) {
10 String decodeAllowMalformed(List<int> bytes) { 10 StringBuffer buffer = new StringBuffer();
11 return new Utf8Decoder(allowMalformed: true).convert(bytes); 11 ChunkedConversionSink stringSink =
12 new StringConversionSink.fromStringSink(buffer);
13 var byteSink = new Utf8Decoder().startChunkedConversion(stringSink);
14 int i = 0;
15 while (i < bytes.length) {
16 List nextChunk = [];
17 for (int j = 0; j < chunkSize; j++) {
18 if (i < bytes.length) {
19 nextChunk.add(bytes[i]);
20 i++;
21 }
22 }
23 byteSink.add(nextChunk);
24 }
25 byteSink.close();
26 return buffer.toString();
12 } 27 }
13 28
14 String decode2(List<int> bytes) => UTF8.decode(bytes); 29 String decodeAllowMalformed(List<int> bytes, int chunkSize) {
15 String decodeAllowMalformed2(List<int> bytes) { 30 StringBuffer buffer = new StringBuffer();
16 return UTF8.decode(bytes, allowMalformed: true); 31 ChunkedConversionSink stringSink =
17 } 32 new StringConversionSink.fromStringSink(buffer);
18 33 var decoder = new Utf8Decoder(allowMalformed: true);
19 String decode3(List<int> bytes) => new Utf8Codec().decode(bytes); 34 var byteSink = decoder.startChunkedConversion(stringSink);
20 String decodeAllowMalformed3(List<int> bytes) { 35 int i = 0;
21 return new Utf8Codec(allowMalformed: true).decode(bytes); 36 while (i < bytes.length) {
22 } 37 List nextChunk = [];
23 38 for (int j = 0; j < chunkSize; j++) {
24 String decode4(List<int> bytes) => new Utf8Codec().decoder.convert(bytes); 39 if (i < bytes.length) {
25 String decodeAllowMalformed4(List<int> bytes) { 40 nextChunk.add(bytes[i]);
26 return new Utf8Codec(allowMalformed: true).decoder.convert(bytes); 41 i++;
42 }
43 }
44 byteSink.add(nextChunk);
45 }
46 byteSink.close();
47 return buffer.toString();
27 } 48 }
28 49
29 final TESTS = [ 50 final TESTS = [
30 // Unfinished UTF-8 sequences. 51 // Unfinished UTF-8 sequences.
31 [ 0xc3 ], 52 [ 0xc3 ],
32 [ 0xE2, 0x82 ], 53 [ 0xE2, 0x82 ],
33 [ 0xF0, 0xA4, 0xAD ], 54 [ 0xF0, 0xA4, 0xAD ],
34 // Overlong encoding of euro-sign. 55 // Overlong encoding of euro-sign.
35 [ 0xF0, 0x82, 0x82, 0xAC ], 56 [ 0xF0, 0x82, 0x82, 0xAC ],
36 // Other overlong/unfinished sequences. 57 // Other overlong/unfinished sequences.
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 134
114 var allTests2 = TESTS2.map((test) { 135 var allTests2 = TESTS2.map((test) {
115 // Pairs of test and expected string output when malformed strings are 136 // Pairs of test and expected string output when malformed strings are
116 // allowed. Replacement character: U+FFFD 137 // allowed. Replacement character: U+FFFD
117 String expected = test[1].replaceAll("X", "\u{FFFD}"); 138 String expected = test[1].replaceAll("X", "\u{FFFD}");
118 return [test[0], expected]; 139 return [test[0], expected];
119 }); 140 });
120 141
121 for (var test in []..addAll(allTests)..addAll(allTests2)) { 142 for (var test in []..addAll(allTests)..addAll(allTests2)) {
122 List<int> bytes = test[0]; 143 List<int> bytes = test[0];
123 Expect.throws(() => decode(bytes), (e) => e is FormatException); 144 Expect.throws(() => decode(bytes, 1), (e) => e is FormatException);
124 Expect.throws(() => decode2(bytes), (e) => e is FormatException); 145 Expect.throws(() => decode(bytes, 2), (e) => e is FormatException);
125 Expect.throws(() => decode3(bytes), (e) => e is FormatException); 146 Expect.throws(() => decode(bytes, 3), (e) => e is FormatException);
126 Expect.throws(() => decode4(bytes), (e) => e is FormatException); 147 Expect.throws(() => decode(bytes, 4), (e) => e is FormatException);
127 148
128 String expected = test[1]; 149 String expected = test[1];
129 Expect.equals(expected, decodeAllowMalformed(bytes)); 150 Expect.equals(expected, decodeAllowMalformed(bytes, 1));
130 Expect.equals(expected, decodeAllowMalformed2(bytes)); 151 Expect.equals(expected, decodeAllowMalformed(bytes, 2));
131 Expect.equals(expected, decodeAllowMalformed3(bytes)); 152 Expect.equals(expected, decodeAllowMalformed(bytes, 3));
132 Expect.equals(expected, decodeAllowMalformed4(bytes)); 153 Expect.equals(expected, decodeAllowMalformed(bytes, 4));
133 } 154 }
134 } 155 }
OLDNEW
« no previous file with comments | « tests/lib/convert/chunked_conversion_json_encode1_test.dart ('k') | tests/lib/convert/chunked_conversion_utf83_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698