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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 import 'dart:typed_data'; |
7 | 8 |
8 import "package:async_helper/async_helper.dart"; | 9 import "package:async_helper/async_helper.dart"; |
9 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
10 | 11 |
11 void testZLibDeflateEmpty() { | 12 void testZLibDeflateEmpty() { |
12 asyncStart(); | 13 asyncStart(); |
13 var controller = new StreamController(sync: true); | 14 var controller = new StreamController(sync: true); |
14 controller.stream.transform(new ZLibEncoder(gzip: false, level: 6)) | 15 controller.stream.transform(new ZLibEncoder(gzip: false, level: 6)) |
15 .fold([], (buffer, data) { | 16 .fold([], (buffer, data) { |
16 buffer.addAll(data); | 17 buffer.addAll(data); |
(...skipping 15 matching lines...) Expand all Loading... |
32 return buffer; | 33 return buffer; |
33 }) | 34 }) |
34 .then((data) { | 35 .then((data) { |
35 Expect.isTrue(data.length > 0); | 36 Expect.isTrue(data.length > 0); |
36 Expect.listEquals([], new ZLibDecoder().convert(data)); | 37 Expect.listEquals([], new ZLibDecoder().convert(data)); |
37 asyncEnd(); | 38 asyncEnd(); |
38 }); | 39 }); |
39 controller.close(); | 40 controller.close(); |
40 } | 41 } |
41 | 42 |
42 void testZLibDeflate() { | 43 void testZLibDeflate(List<int> data) { |
43 asyncStart(); | 44 asyncStart(); |
44 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
45 var controller = new StreamController(sync: true); | 45 var controller = new StreamController(sync: true); |
46 controller.stream.transform(new ZLibEncoder(gzip: false, level: 6)) | 46 controller.stream.transform(new ZLibEncoder(gzip: false, level: 6)) |
47 .fold([], (buffer, data) { | 47 .fold([], (buffer, data) { |
48 buffer.addAll(data); | 48 buffer.addAll(data); |
49 return buffer; | 49 return buffer; |
50 }) | 50 }) |
51 .then((data) { | 51 .then((data) { |
52 Expect.listEquals( | 52 Expect.listEquals( |
53 [120, 156, 99, 96, 100, 98, 102, 97, 101, 99, 231, 224, 4, 0, 0, | 53 [120, 156, 99, 96, 100, 98, 102, 97, 101, 99, 231, 224, 4, 0, 0, |
54 175, 0, 46], | 54 175, 0, 46], |
55 data); | 55 data); |
56 asyncEnd(); | 56 asyncEnd(); |
57 }); | 57 }); |
58 controller.add(data); | 58 controller.add(data); |
59 controller.close(); | 59 controller.close(); |
60 } | 60 } |
61 | 61 |
62 void testZLibDeflateGZip() { | 62 void testZLibDeflateGZip(List<int> data) { |
63 asyncStart(); | 63 asyncStart(); |
64 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
65 var controller = new StreamController(sync: true); | 64 var controller = new StreamController(sync: true); |
66 controller.stream.transform(new ZLibEncoder(gzip: true)) | 65 controller.stream.transform(new ZLibEncoder(gzip: true)) |
67 .fold([], (buffer, data) { | 66 .fold([], (buffer, data) { |
68 buffer.addAll(data); | 67 buffer.addAll(data); |
69 return buffer; | 68 return buffer; |
70 }) | 69 }) |
71 .then((data) { | 70 .then((data) { |
72 Expect.equals(30, data.length); | 71 Expect.equals(30, data.length); |
73 Expect.listEquals([99, 96, 100, 98, 102, 97, 101, 99, 231, 224, 4, 0, | 72 Expect.listEquals([99, 96, 100, 98, 102, 97, 101, 99, 231, 224, 4, 0, |
74 70, 215, 108, 69, 10, 0, 0, 0], | 73 70, 215, 108, 69, 10, 0, 0, 0], |
75 // Skip header, as it can change. | 74 // Skip header, as it can change. |
76 data.sublist(10)); | 75 data.sublist(10)); |
77 asyncEnd(); | 76 asyncEnd(); |
78 }); | 77 }); |
79 controller.add(data); | 78 controller.add(data); |
80 controller.close(); | 79 controller.close(); |
81 } | 80 } |
82 | 81 |
83 void testZLibDeflateRaw() { | 82 void testZLibDeflateRaw(List<int> data) { |
84 asyncStart(); | 83 asyncStart(); |
85 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
86 var controller = new StreamController(sync: true); | 84 var controller = new StreamController(sync: true); |
87 controller.stream.transform(new ZLibEncoder(raw: true, level: 6)) | 85 controller.stream.transform(new ZLibEncoder(raw: true, level: 6)) |
88 .fold([], (buffer, data) { | 86 .fold([], (buffer, data) { |
89 buffer.addAll(data); | 87 buffer.addAll(data); |
90 return buffer; | 88 return buffer; |
91 }) | 89 }) |
92 .then((data) { | 90 .then((data) { |
93 Expect.listEquals([99, 96, 100, 98, 102, 97, 101, 99, 231, 224, 4, 0], | 91 Expect.listEquals([99, 96, 100, 98, 102, 97, 101, 99, 231, 224, 4, 0], |
94 data); | 92 data); |
95 asyncEnd(); | 93 asyncEnd(); |
96 }); | 94 }); |
97 controller.add(data); | 95 controller.add(data); |
98 controller.close(); | 96 controller.close(); |
99 } | 97 } |
100 | 98 |
101 void testZLibDeflateInvalidLevel() { | 99 void testZLibDeflateInvalidLevel() { |
102 test2(gzip, level) { | 100 test2(gzip, level) { |
103 [true, false].forEach((gzip) { | 101 [true, false].forEach((gzip) { |
104 [-2, -20, 10, 42, null, "9"].forEach((level) { | 102 [-2, -20, 10, 42, null, "9"].forEach((level) { |
105 Expect.throws( | 103 Expect.throws( |
106 () => new ZLibEncoder(gzip: gzip, level: level), | 104 () => new ZLibEncoder(gzip: gzip, level: level), |
107 (e) => e is ArgumentError, | 105 (e) => e is ArgumentError, |
108 "'level' must be in range -1..9" | 106 "'level' must be in range -1..9" |
109 ); | 107 ); |
110 }); | 108 }); |
111 }); | 109 }); |
112 }; | 110 }; |
113 } | 111 } |
114 | 112 |
115 void testZLibInflate() { | 113 void testZLibInflate(List<int> data) { |
116 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
117 | |
118 [true, false].forEach((gzip) { | 114 [true, false].forEach((gzip) { |
119 [ZLibOption.STRATEGY_FILTERED, ZLibOption.STRATEGY_HUFFMAN_ONLY, | 115 [ZLibOption.STRATEGY_FILTERED, ZLibOption.STRATEGY_HUFFMAN_ONLY, |
120 ZLibOption.STRATEGY_RLE, ZLibOption.STRATEGY_FIXED, | 116 ZLibOption.STRATEGY_RLE, ZLibOption.STRATEGY_FIXED, |
121 ZLibOption.STRATEGY_DEFAULT].forEach((strategy) { | 117 ZLibOption.STRATEGY_DEFAULT].forEach((strategy) { |
122 [3, 6, 9].forEach((level) { | 118 [3, 6, 9].forEach((level) { |
123 asyncStart(); | 119 asyncStart(); |
124 var controller = new StreamController(sync: true); | 120 var controller = new StreamController(sync: true); |
125 controller.stream | 121 controller.stream |
126 .transform(new ZLibEncoder(gzip: gzip, level: level, | 122 .transform(new ZLibEncoder(gzip: gzip, level: level, |
127 strategy: strategy)) | 123 strategy: strategy)) |
128 .transform(new ZLibDecoder()) | 124 .transform(new ZLibDecoder()) |
129 .fold([], (buffer, data) { | 125 .fold([], (buffer, data) { |
130 buffer.addAll(data); | 126 buffer.addAll(data); |
131 return buffer; | 127 return buffer; |
132 }) | 128 }) |
133 .then((inflated) { | 129 .then((inflated) { |
134 Expect.listEquals(data, inflated); | 130 Expect.listEquals(data, inflated); |
135 asyncEnd(); | 131 asyncEnd(); |
136 }); | 132 }); |
137 controller.add(data); | 133 controller.add(data); |
138 controller.close(); | 134 controller.close(); |
139 }); | 135 }); |
140 }); | 136 }); |
141 }); | 137 }); |
142 } | 138 } |
143 | 139 |
144 void testZLibInflateRaw() { | 140 void testZLibInflateRaw(List<int> data) { |
145 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
146 | |
147 [3, 6, 9].forEach((level) { | 141 [3, 6, 9].forEach((level) { |
148 asyncStart(); | 142 asyncStart(); |
149 var controller = new StreamController(sync: true); | 143 var controller = new StreamController(sync: true); |
150 controller.stream | 144 controller.stream |
151 .transform(new ZLibEncoder(raw: true, level: level)) | 145 .transform(new ZLibEncoder(raw: true, level: level)) |
152 .transform(new ZLibDecoder(raw: true)) | 146 .transform(new ZLibDecoder(raw: true)) |
153 .fold([], (buffer, data) { | 147 .fold([], (buffer, data) { |
154 buffer.addAll(data); | 148 buffer.addAll(data); |
155 return buffer; | 149 return buffer; |
156 }) | 150 }) |
157 .then((inflated) { | 151 .then((inflated) { |
158 Expect.listEquals(data, inflated); | 152 Expect.listEquals(data, inflated); |
159 asyncEnd(); | 153 asyncEnd(); |
160 }); | 154 }); |
161 controller.add(data); | 155 controller.add(data); |
162 controller.close(); | 156 controller.close(); |
163 }); | 157 }); |
164 } | 158 } |
165 | 159 |
166 void testZLibInflateSync() { | 160 void testZLibInflateSync(List<int> data) { |
167 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
168 | |
169 [true, false].forEach((gzip) { | 161 [true, false].forEach((gzip) { |
170 [3, 6, 9].forEach((level) { | 162 [3, 6, 9].forEach((level) { |
171 var encoded = new ZLibEncoder(gzip: gzip, level: level).convert(data); | 163 var encoded = new ZLibEncoder(gzip: gzip, level: level).convert(data); |
172 var decoded = new ZLibDecoder().convert(encoded); | 164 var decoded = new ZLibDecoder().convert(encoded); |
173 Expect.listEquals(data, decoded); | 165 Expect.listEquals(data, decoded); |
174 }); | 166 }); |
175 }); | 167 }); |
176 } | 168 } |
177 | 169 |
178 void testZlibInflateThrowsWithSmallerWindow() { | 170 void testZlibInflateThrowsWithSmallerWindow() { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 var data = [98, 97, 114, 102, 111, 111]; | 204 var data = [98, 97, 114, 102, 111, 111]; |
213 | 205 |
214 [3, 6, 9].forEach((level) { | 206 [3, 6, 9].forEach((level) { |
215 var encoded = new ZLibEncoder(level: level, dictionary: dict) | 207 var encoded = new ZLibEncoder(level: level, dictionary: dict) |
216 .convert(data); | 208 .convert(data); |
217 var decoded = new ZLibDecoder(dictionary: dict).convert(encoded); | 209 var decoded = new ZLibDecoder(dictionary: dict).convert(encoded); |
218 Expect.listEquals(data, decoded); | 210 Expect.listEquals(data, decoded); |
219 }); | 211 }); |
220 } | 212 } |
221 | 213 |
| 214 var generateListTypes = [ |
| 215 (list) => list, |
| 216 (list) => new Uint8List.fromList(list), |
| 217 (list) => new Int8List.fromList(list), |
| 218 (list) => new Uint16List.fromList(list), |
| 219 (list) => new Int16List.fromList(list), |
| 220 (list) => new Uint32List.fromList(list), |
| 221 (list) => new Int32List.fromList(list), |
| 222 ]; |
| 223 |
| 224 var generateViewTypes = [ |
| 225 (list) => new Uint8List.view((new Uint8List.fromList(list)).buffer, 1, 8), |
| 226 (list) => new Int8List.view((new Int8List.fromList(list)).buffer, 1, 8), |
| 227 (list) => new Uint16List.view((new Uint16List.fromList(list)).buffer, 2, 6), |
| 228 (list) => new Int16List.view((new Int16List.fromList(list)).buffer, 2, 6), |
| 229 (list) => new Uint32List.view((new Uint32List.fromList(list)).buffer, 4, 4), |
| 230 (list) => new Int32List.view((new Int32List.fromList(list)).buffer, 4, 4), |
| 231 ]; |
| 232 |
| 233 |
222 void main() { | 234 void main() { |
223 asyncStart(); | 235 asyncStart(); |
224 testZLibDeflate(); | |
225 testZLibDeflateEmpty(); | 236 testZLibDeflateEmpty(); |
226 testZLibDeflateEmptyGzip(); | 237 testZLibDeflateEmptyGzip(); |
227 testZLibDeflateGZip(); | |
228 testZLibDeflateInvalidLevel(); | 238 testZLibDeflateInvalidLevel(); |
229 testZLibInflate(); | 239 generateListTypes.forEach((f) { |
230 testZLibInflateSync(); | 240 var data = f([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
| 241 testZLibDeflate(data); |
| 242 testZLibDeflateGZip(data); |
| 243 testZLibDeflateRaw(data); |
| 244 testZLibInflate(data); |
| 245 testZLibInflateSync(data); |
| 246 testZLibInflateRaw(data); |
| 247 }); |
| 248 generateViewTypes.forEach((f) { |
| 249 var data = f([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
| 250 testZLibInflate(data); |
| 251 testZLibInflateSync(data); |
| 252 testZLibInflateRaw(data); |
| 253 }); |
231 testZlibInflateThrowsWithSmallerWindow(); | 254 testZlibInflateThrowsWithSmallerWindow(); |
232 testZlibInflateWithLargerWindow(); | 255 testZlibInflateWithLargerWindow(); |
233 testZLibDeflateRaw(); | |
234 testZLibInflateRaw(); | |
235 testZlibWithDictionary(); | 256 testZlibWithDictionary(); |
236 asyncEnd(); | 257 asyncEnd(); |
237 } | 258 } |
OLD | NEW |