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 asciiStrings = [ |
| 9 "pure ascii", |
| 10 "\x00 with control characters \n", |
| 11 "\x01 edge cases \x7f" |
| 12 ]; |
| 13 |
| 14 var nonAsciiStrings = [ |
| 15 "\x80 edge case first", |
| 16 "Edge case ASCII \u{80}", |
| 17 "Edge case byte \u{ff}", |
| 18 "Edge case super-BMP \u{10000}" |
| 19 ]; |
| 20 |
| 21 void main() { |
| 22 // Build longer versions of the example strings. |
| 23 for (int i = 0, n = asciiStrings.length; i < n ; i++) { |
| 24 var string = asciiStrings[i]; |
| 25 while (string.length < 1024) { |
| 26 string += string; |
| 27 } |
| 28 asciiStrings.add(string); |
| 29 } |
| 30 for (int i = 0, n = nonAsciiStrings.length; i < n ; i++) { |
| 31 var string = nonAsciiStrings[i]; |
| 32 while (string.length < 1024) { |
| 33 string += string; |
| 34 } |
| 35 nonAsciiStrings.add(string); |
| 36 } |
| 37 testDirectConversions(); |
| 38 testChunkedConversions(); |
| 39 } |
| 40 |
| 41 void testDirectConversions() { |
| 42 for (var codec in [ASCII, new AsciiCodec()]) { |
| 43 for (var asciiString in asciiStrings) { |
| 44 List bytes = codec.encoder.convert(asciiString); |
| 45 Expect.listEquals(asciiString.codeUnits.toList(), bytes, asciiString); |
| 46 String roundTripString = codec.decoder.convert(bytes); |
| 47 Expect.equals(asciiString, roundTripString); |
| 48 roundTripString = codec.decode(bytes); |
| 49 Expect.equals(asciiString, roundTripString); |
| 50 } |
| 51 |
| 52 for (var nonAsciiString in nonAsciiStrings) { |
| 53 Expect.throws(() { |
| 54 print(codec.encoder.convert(nonAsciiString)); |
| 55 }, null, nonAsciiString); |
| 56 } |
| 57 |
| 58 var encode = codec.encoder.convert; |
| 59 Expect.listEquals([0x42, 0x43, 0x44], encode("ABCDE", 1, 4)); |
| 60 Expect.listEquals([0x42, 0x43, 0x44, 0x45], encode("ABCDE", 1)); |
| 61 Expect.listEquals([0x42, 0x43, 0x44], encode("\xffBCD\xff", 1, 4)); |
| 62 Expect.throws(() { encode("\xffBCD\xff", 0, 4); }); |
| 63 Expect.throws(() { encode("\xffBCD\xff", 1); }); |
| 64 Expect.throws(() { encode("\xffBCD\xff", 1, 5); }); |
| 65 Expect.throws(() { encode("\xffBCD\xff", -1, 4); }); |
| 66 Expect.throws(() { encode("\xffBCD\xff", 1, -1); }); |
| 67 Expect.throws(() { encode("\xffBCD\xff", 3, 2); }); |
| 68 |
| 69 var decode = codec.decoder.convert; |
| 70 Expect.equals("BCD", decode([0x41, 0x42, 0x43, 0x44, 0x45], 1, 4)); |
| 71 Expect.equals("BCDE", decode([0x41, 0x42, 0x43, 0x44, 0x45], 1)); |
| 72 Expect.equals("BCD", decode([0xFF, 0x42, 0x43, 0x44, 0xFF], 1, 4)); |
| 73 Expect.throws(() { decode([0xFF, 0x42, 0x43, 0x44, 0xFF], 0, 4); }); |
| 74 Expect.throws(() { decode([0xFF, 0x42, 0x43, 0x44, 0xFF], 1); }); |
| 75 Expect.throws(() { decode([0xFF, 0x42, 0x43, 0x44, 0xFF], 1, 5); }); |
| 76 Expect.throws(() { decode([0xFF, 0x42, 0x43, 0x44, 0xFF], -1, 4); }); |
| 77 Expect.throws(() { decode([0xFF, 0x42, 0x43, 0x44, 0xFF], 1, -1); }); |
| 78 Expect.throws(() { decode([0xFF, 0x42, 0x43, 0x44, 0xFF], 3, 2); }); |
| 79 } |
| 80 |
| 81 var allowInvalidCodec = new AsciiCodec(allowInvalid: true); |
| 82 var invalidBytes = [0, 1, 0xff, 0xdead, 0]; |
| 83 String decoded = allowInvalidCodec.decode(invalidBytes); |
| 84 Expect.equals("\x00\x01\uFFFD\uFFFD\x00", decoded); |
| 85 decoded = allowInvalidCodec.decoder.convert(invalidBytes); |
| 86 Expect.equals("\x00\x01\uFFFD\uFFFD\x00", decoded); |
| 87 decoded = ASCII.decode(invalidBytes, allowInvalid: true); |
| 88 Expect.equals("\x00\x01\uFFFD\uFFFD\x00", decoded); |
| 89 } |
| 90 |
| 91 List<int> encode(String str, int chunkSize, |
| 92 Converter<String, List<int>> converter) { |
| 93 List<int> bytes = <int>[]; |
| 94 ChunkedConversionSink byteSink = |
| 95 new ByteConversionSink.withCallback(bytes.addAll); |
| 96 var stringConversionSink = converter.startChunkedConversion(byteSink); |
| 97 for (int i = 0; i < str.length; i += chunkSize) { |
| 98 if (i + chunkSize <= str.length) { |
| 99 stringConversionSink.add(str.substring(i, i + chunkSize)); |
| 100 } else { |
| 101 stringConversionSink.add(str.substring(i)); |
| 102 } |
| 103 } |
| 104 stringConversionSink.close(); |
| 105 return bytes; |
| 106 } |
| 107 |
| 108 String decode(List<int> bytes, int chunkSize, |
| 109 Converter<List<int>, String> converter) { |
| 110 StringBuffer buf = new StringBuffer(); |
| 111 var stringSink = |
| 112 new StringConversionSink.fromStringSink(buf); |
| 113 var byteConversionSink = converter.startChunkedConversion(stringSink); |
| 114 for (int i = 0; i < bytes.length; i += chunkSize) { |
| 115 if (i + chunkSize <= bytes.length) { |
| 116 byteConversionSink.add(bytes.sublist(i, i + chunkSize)); |
| 117 } else { |
| 118 byteConversionSink.add(bytes.sublist(i)); |
| 119 } |
| 120 } |
| 121 byteConversionSink.close(); |
| 122 return buf.toString(); |
| 123 } |
| 124 |
| 125 void testChunkedConversions() { |
| 126 // Check encoding. |
| 127 for (var converter in [ASCII.encoder, |
| 128 new AsciiCodec().encoder, |
| 129 new AsciiEncoder()]) { |
| 130 for (int chunkSize in [1, 2, 5, 50]) { |
| 131 for (var asciiString in asciiStrings) { |
| 132 var units = asciiString.codeUnits.toList(); |
| 133 List bytes = encode(asciiString, chunkSize, converter); |
| 134 Expect.listEquals(units, bytes); |
| 135 } |
| 136 for (var nonAsciiString in nonAsciiStrings) { |
| 137 Expect.throws(() { |
| 138 encode(nonAsciiString, chunkSize, converter); |
| 139 }); |
| 140 } |
| 141 } |
| 142 } |
| 143 // Check decoding. |
| 144 for (var converter in [ASCII.decoder, |
| 145 new AsciiCodec().decoder, |
| 146 new AsciiDecoder()]) { |
| 147 for (int chunkSize in [1, 2, 5, 50]) { |
| 148 for (var asciiString in asciiStrings) { |
| 149 var units = asciiString.codeUnits.toList(); |
| 150 Expect.equals(asciiString, decode(units, chunkSize, converter)); |
| 151 } |
| 152 for (var nonAsciiString in nonAsciiStrings) { |
| 153 var units = nonAsciiString.codeUnits.toList(); |
| 154 Expect.throws(() { |
| 155 decode(units, chunkSize, converter); |
| 156 }); |
| 157 } |
| 158 } |
| 159 } |
| 160 } |
OLD | NEW |