Chromium Code Reviews| 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 import 'dart:convert'; | |
| 7 | |
| 8 var latin1Strings = [ | |
|
floitsch
2013/08/21 14:07:33
It would be good to create additional tests from t
Lasse Reichstein Nielsen
2013/08/22 08:28:37
Wouldn't it be great if String had an operator*, s
| |
| 9 "pure ascii", | |
| 10 "blåbærgrød", | |
| 11 "\x00 edge cases \xff" | |
| 12 ]; | |
| 13 | |
| 14 var nonLatin1Strings = [ | |
| 15 "Edge case \u{100}", | |
| 16 "Edge case super-BMP \u{10000}" | |
| 17 ]; | |
| 18 | |
| 19 void main() { | |
| 20 testDirectConversions(); | |
| 21 testChunkedConversions(); | |
| 22 } | |
| 23 | |
| 24 void testDirectConversions() { | |
| 25 for (var codec in [LATIN1, new Latin1Codec()]) { | |
| 26 for (var latin1String in latin1Strings) { | |
| 27 List bytes = codec.encoder.convert(latin1String); | |
| 28 Expect.listEquals(latin1String.codeUnits.toList(), bytes, latin1String); | |
| 29 String roundTripString = codec.decoder.convert(bytes); | |
| 30 Expect.equals(latin1String, roundTripString); | |
| 31 roundTripString = codec.decode(bytes); | |
| 32 Expect.equals(latin1String, roundTripString); | |
| 33 } | |
| 34 | |
| 35 for (var nonLatin1String in nonLatin1Strings) { | |
| 36 Expect.throws(() { | |
| 37 print(codec.encoder.convert(nonLatin1String)); | |
| 38 }, null, nonLatin1String); | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 var allowInvalidCodec = new Latin1Codec(allowInvalid: true); | |
| 43 var invalidBytes = [0, 1, 0xff, 0xdead, 0]; | |
| 44 String decoded = allowInvalidCodec.decode(invalidBytes); | |
| 45 Expect.equals("\x00\x01\xFF\uFFFD\x00", decoded); | |
| 46 decoded = allowInvalidCodec.decoder.convert(invalidBytes); | |
| 47 Expect.equals("\x00\x01\xFF\uFFFD\x00", decoded); | |
| 48 decoded = LATIN1.decode(invalidBytes, allowInvalid: true); | |
| 49 Expect.equals("\x00\x01\xFF\uFFFD\x00", decoded); | |
| 50 } | |
| 51 | |
| 52 List<int> encode(String str, int chunkSize, | |
| 53 Converter<String, List<int>> converter) { | |
| 54 List<int> bytes = <int>[]; | |
| 55 ChunkedConversionSink byteSink = | |
| 56 new ByteConversionSink.withCallback(bytes.addAll); | |
| 57 var stringConversionSink = converter.startChunkedConversion(byteSink); | |
| 58 for (int i = 0; i < str.length; i += chunkSize) { | |
| 59 if (i + chunkSize <= str.length) { | |
| 60 stringConversionSink.add(str.substring(i, i + chunkSize)); | |
| 61 } else { | |
| 62 stringConversionSink.add(str.substring(i)); | |
| 63 } | |
| 64 } | |
| 65 stringConversionSink.close(); | |
| 66 return bytes; | |
| 67 } | |
| 68 | |
| 69 String decode(List<int> bytes, int chunkSize, | |
| 70 Converter<List<int>, String> converter) { | |
| 71 StringBuffer buf = new StringBuffer(); | |
| 72 var stringSink = | |
| 73 new StringConversionSink.fromStringSink(buf); | |
| 74 var byteConversionSink = converter.startChunkedConversion(stringSink); | |
| 75 for (int i = 0; i < bytes.length; i += chunkSize) { | |
| 76 if (i + chunkSize <= bytes.length) { | |
| 77 byteConversionSink.add(bytes.sublist(i, i + chunkSize)); | |
| 78 } else { | |
| 79 byteConversionSink.add(bytes.sublist(i)); | |
| 80 } | |
| 81 } | |
| 82 byteConversionSink.close(); | |
| 83 return buf.toString(); | |
| 84 } | |
| 85 | |
| 86 void testChunkedConversions() { | |
| 87 // Check encoding. | |
| 88 for (var converter in [LATIN1.encoder, | |
| 89 new Latin1Codec().encoder, | |
| 90 new Latin1Encoder()]) { | |
| 91 for (int chunkSize in [1, 2, 5, 50]) { | |
| 92 for (var latin1String in latin1Strings) { | |
| 93 var units = latin1String.codeUnits.toList(); | |
| 94 List bytes = encode(latin1String, chunkSize, converter); | |
| 95 Expect.listEquals(units, bytes); | |
| 96 } | |
| 97 for (var nonLatin1String in nonLatin1Strings) { | |
| 98 Expect.throws(() { | |
| 99 encode(nonLatin1Strings, chunkSize, converter); | |
| 100 }); | |
| 101 } | |
| 102 } | |
| 103 } | |
| 104 // Check decoding. | |
| 105 for (var converter in [LATIN1.decoder, | |
| 106 new Latin1Codec().decoder, | |
| 107 new Latin1Decoder()]) { | |
| 108 for (int chunkSize in [1, 2, 5, 50]) { | |
| 109 for (var latin1String in latin1Strings) { | |
| 110 var units = latin1String.codeUnits.toList(); | |
| 111 Expect.equals(latin1String, decode(units, chunkSize, converter)); | |
| 112 } | |
| 113 for (var nonLatin1String in nonLatin1Strings) { | |
| 114 var units = nonLatin1String.codeUnits.toList(); | |
| 115 Expect.throws(() { | |
| 116 decode(units, chunkSize, converter); | |
| 117 }); | |
| 118 } | |
| 119 } | |
| 120 } | |
| 121 } | |
| OLD | NEW |