| OLD | NEW |
| 1 // Copyright (c) 2013, 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 | 6 |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 String decode(List<int> bytes, int chunkSize) { | 10 String decode(List<int> bytes, int chunkSize) { |
| 11 StringBuffer buffer = new StringBuffer(); | 11 StringBuffer buffer = new StringBuffer(); |
| 12 var stringSink = new StringConversionSink.fromStringSink(buffer); | 12 var stringSink = new StringConversionSink.fromStringSink(buffer); |
| 13 var byteSink = new Utf8Decoder().startChunkedConversion(stringSink); | 13 var byteSink = new Utf8Decoder().startChunkedConversion(stringSink); |
| 14 int i = 0; | 14 int i = 0; |
| 15 while (i < bytes.length) { | 15 while (i < bytes.length) { |
| 16 List nextChunk = []; | 16 var nextChunk = <int>[]; |
| 17 for (int j = 0; j < chunkSize; j++) { | 17 for (int j = 0; j < chunkSize; j++) { |
| 18 if (i < bytes.length) { | 18 if (i < bytes.length) { |
| 19 nextChunk.add(bytes[i]); | 19 nextChunk.add(bytes[i]); |
| 20 i++; | 20 i++; |
| 21 } | 21 } |
| 22 } | 22 } |
| 23 byteSink.add(nextChunk); | 23 byteSink.add(nextChunk); |
| 24 } | 24 } |
| 25 byteSink.close(); | 25 byteSink.close(); |
| 26 return buffer.toString(); | 26 return buffer.toString(); |
| 27 } | 27 } |
| 28 | 28 |
| 29 String decodeAllowMalformed(List<int> bytes, int chunkSize) { | 29 String decodeAllowMalformed(List<int> bytes, int chunkSize) { |
| 30 StringBuffer buffer = new StringBuffer(); | 30 StringBuffer buffer = new StringBuffer(); |
| 31 var stringSink = new StringConversionSink.fromStringSink(buffer); | 31 var stringSink = new StringConversionSink.fromStringSink(buffer); |
| 32 var decoder = new Utf8Decoder(allowMalformed: true); | 32 var decoder = new Utf8Decoder(allowMalformed: true); |
| 33 var byteSink = decoder.startChunkedConversion(stringSink); | 33 var byteSink = decoder.startChunkedConversion(stringSink); |
| 34 int i = 0; | 34 int i = 0; |
| 35 while (i < bytes.length) { | 35 while (i < bytes.length) { |
| 36 List nextChunk = []; | 36 var nextChunk = <int>[]; |
| 37 for (int j = 0; j < chunkSize; j++) { | 37 for (int j = 0; j < chunkSize; j++) { |
| 38 if (i < bytes.length) { | 38 if (i < bytes.length) { |
| 39 nextChunk.add(bytes[i]); | 39 nextChunk.add(bytes[i]); |
| 40 i++; | 40 i++; |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 byteSink.add(nextChunk); | 43 byteSink.add(nextChunk); |
| 44 } | 44 } |
| 45 byteSink.close(); | 45 byteSink.close(); |
| 46 return buffer.toString(); | 46 return buffer.toString(); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 ] | 219 ] |
| 220 ]; | 220 ]; |
| 221 | 221 |
| 222 main() { | 222 main() { |
| 223 var allTests = TESTS.expand((test) { | 223 var allTests = TESTS.expand((test) { |
| 224 // Pairs of test and expected string output when malformed strings are | 224 // Pairs of test and expected string output when malformed strings are |
| 225 // allowed. Replacement character: U+FFFD | 225 // allowed. Replacement character: U+FFFD |
| 226 return [ | 226 return [ |
| 227 [test, "\u{FFFD}"], | 227 [test, "\u{FFFD}"], |
| 228 [ | 228 [ |
| 229 new List.from([0x61])..addAll(test), | 229 new List<int>.from([0x61])..addAll(test), |
| 230 "a\u{FFFD}" | 230 "a\u{FFFD}" |
| 231 ], | 231 ], |
| 232 [ | 232 [ |
| 233 new List.from([0x61]) | 233 new List<int>.from([0x61]) |
| 234 ..addAll(test) | 234 ..addAll(test) |
| 235 ..add(0x61), | 235 ..add(0x61), |
| 236 "a\u{FFFD}a" | 236 "a\u{FFFD}a" |
| 237 ], | 237 ], |
| 238 [new List.from(test)..add(0x61), "\u{FFFD}a"], | 238 [new List<int>.from(test)..add(0x61), "\u{FFFD}a"], |
| 239 [new List.from(test)..addAll(test), "\u{FFFD}\u{FFFD}"], | 239 [new List<int>.from(test)..addAll(test), "\u{FFFD}\u{FFFD}"], |
| 240 [ | 240 [ |
| 241 new List.from(test) | 241 new List<int>.from(test) |
| 242 ..add(0x61) | 242 ..add(0x61) |
| 243 ..addAll(test), | 243 ..addAll(test), |
| 244 "\u{FFFD}a\u{FFFD}" | 244 "\u{FFFD}a\u{FFFD}" |
| 245 ], | 245 ], |
| 246 [ | 246 [ |
| 247 new List.from([0xc3, 0xa5])..addAll(test), | 247 new List<int>.from([0xc3, 0xa5])..addAll(test), |
| 248 "å\u{FFFD}" | 248 "å\u{FFFD}" |
| 249 ], | 249 ], |
| 250 [ | 250 [ |
| 251 new List.from([0xc3, 0xa5])..addAll(test)..addAll([0xc3, 0xa5]), | 251 new List<int>.from([0xc3, 0xa5])..addAll(test)..addAll([0xc3, 0xa5]), |
| 252 "å\u{FFFD}å" | 252 "å\u{FFFD}å" |
| 253 ], | 253 ], |
| 254 [ | 254 [ |
| 255 new List.from(test)..addAll([0xc3, 0xa5]), | 255 new List<int>.from(test)..addAll([0xc3, 0xa5]), |
| 256 "\u{FFFD}å" | 256 "\u{FFFD}å" |
| 257 ], | 257 ], |
| 258 [ | 258 [ |
| 259 new List.from(test)..addAll([0xc3, 0xa5])..addAll(test), | 259 new List<int>.from(test)..addAll([0xc3, 0xa5])..addAll(test), |
| 260 "\u{FFFD}å\u{FFFD}" | 260 "\u{FFFD}å\u{FFFD}" |
| 261 ] | 261 ] |
| 262 ]; | 262 ]; |
| 263 }); | 263 }); |
| 264 | 264 |
| 265 var allTests2 = TESTS2.map((test) { | 265 var allTests2 = TESTS2.map((test) { |
| 266 // Pairs of test and expected string output when malformed strings are | 266 // Pairs of test and expected string output when malformed strings are |
| 267 // allowed. Replacement character: U+FFFD | 267 // allowed. Replacement character: U+FFFD |
| 268 String expected = (test[1] as String).replaceAll("X", "\u{FFFD}"); | 268 String expected = (test[1] as String).replaceAll("X", "\u{FFFD}"); |
| 269 return [test[0], expected]; | 269 return [test[0], expected]; |
| 270 }); | 270 }); |
| 271 | 271 |
| 272 for (var test in []..addAll(allTests)..addAll(allTests2)) { | 272 for (var test in []..addAll(allTests)..addAll(allTests2)) { |
| 273 List<int> bytes = test[0]; | 273 List<int> bytes = test[0]; |
| 274 Expect.throws(() => decode(bytes, 1), (e) => e is FormatException); | 274 Expect.throws(() => decode(bytes, 1), (e) => e is FormatException); |
| 275 Expect.throws(() => decode(bytes, 2), (e) => e is FormatException); | 275 Expect.throws(() => decode(bytes, 2), (e) => e is FormatException); |
| 276 Expect.throws(() => decode(bytes, 3), (e) => e is FormatException); | 276 Expect.throws(() => decode(bytes, 3), (e) => e is FormatException); |
| 277 Expect.throws(() => decode(bytes, 4), (e) => e is FormatException); | 277 Expect.throws(() => decode(bytes, 4), (e) => e is FormatException); |
| 278 | 278 |
| 279 String expected = test[1]; | 279 String expected = test[1]; |
| 280 Expect.equals(expected, decodeAllowMalformed(bytes, 1)); | 280 Expect.equals(expected, decodeAllowMalformed(bytes, 1)); |
| 281 Expect.equals(expected, decodeAllowMalformed(bytes, 2)); | 281 Expect.equals(expected, decodeAllowMalformed(bytes, 2)); |
| 282 Expect.equals(expected, decodeAllowMalformed(bytes, 3)); | 282 Expect.equals(expected, decodeAllowMalformed(bytes, 3)); |
| 283 Expect.equals(expected, decodeAllowMalformed(bytes, 4)); | 283 Expect.equals(expected, decodeAllowMalformed(bytes, 4)); |
| 284 } | 284 } |
| 285 } | 285 } |
| OLD | NEW |