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