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 | 7 |
8 import "package:async_helper/async_helper.dart"; | 8 import "package:async_helper/async_helper.dart"; |
9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
10 | 10 |
11 void testZLibDeflate() { | |
12 test(int level, List<int> expected) { | |
13 asyncStart(); | |
14 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
15 var controller = new StreamController(sync: true); | |
16 controller.stream.transform(new ZLibEncoder(gzip: false, level: level)) | |
17 .fold([], (buffer, data) { | |
18 buffer.addAll(data); | |
19 return buffer; | |
20 }) | |
21 .then((data) { | |
22 Expect.listEquals(expected, data); | |
23 asyncEnd(); | |
24 }); | |
25 controller.add(data); | |
26 controller.close(); | |
27 } | |
28 test(6, [120, 156, 99, 96, 100, 98, 102, 97, 101, 99, 231, 224, 4, 0, 0, 175, | |
29 0, 46]); | |
30 } | |
31 | |
32 | |
33 void testZLibDeflateEmpty() { | 11 void testZLibDeflateEmpty() { |
34 asyncStart(); | 12 asyncStart(); |
35 var controller = new StreamController(sync: true); | 13 var controller = new StreamController(sync: true); |
36 controller.stream.transform(new ZLibEncoder(gzip: false, level: 6)) | 14 controller.stream.transform(new ZLibEncoder(gzip: false, level: 6)) |
37 .fold([], (buffer, data) { | 15 .fold([], (buffer, data) { |
38 buffer.addAll(data); | 16 buffer.addAll(data); |
39 return buffer; | 17 return buffer; |
40 }) | 18 }) |
41 .then((data) { | 19 .then((data) { |
42 Expect.listEquals([120, 156, 3, 0, 0, 0, 0, 1], data); | 20 Expect.listEquals([120, 156, 3, 0, 0, 0, 0, 1], data); |
43 asyncEnd(); | 21 asyncEnd(); |
44 }); | 22 }); |
45 controller.close(); | 23 controller.close(); |
46 } | 24 } |
47 | 25 |
26 void testZLibDeflateEmptyGzip() { | |
27 asyncStart(); | |
28 var controller = new StreamController(sync: true); | |
29 controller.stream.transform(new ZLibEncoder(gzip: true, level: 6)) | |
30 .fold([], (buffer, data) { | |
31 buffer.addAll(data); | |
32 return buffer; | |
33 }) | |
34 .then((data) { | |
35 Expect.listEquals([31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, | |
Lasse Reichstein Nielsen
2014/02/04 13:11:57
Long line.
vicb
2014/02/04 19:55:14
fixed
| |
36 0, 0], data); | |
37 asyncEnd(); | |
38 }); | |
39 controller.close(); | |
40 } | |
41 | |
42 void testZLibDeflate() { | |
43 asyncStart(); | |
44 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
45 var controller = new StreamController(sync: true); | |
46 controller.stream.transform(new ZLibEncoder(gzip: false, level: 6)) | |
47 .fold([], (buffer, data) { | |
48 buffer.addAll(data); | |
49 return buffer; | |
50 }) | |
51 .then((data) { | |
52 Expect.listEquals( | |
53 [120, 156, 99, 96, 100, 98, 102, 97, 101, 99, 231, 224, 4, 0, 0, | |
54 175, 0, 46], | |
55 data); | |
56 asyncEnd(); | |
57 }); | |
58 controller.add(data); | |
59 controller.close(); | |
60 } | |
48 | 61 |
49 void testZLibDeflateGZip() { | 62 void testZLibDeflateGZip() { |
50 asyncStart(); | 63 asyncStart(); |
51 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 64 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
52 var controller = new StreamController(sync: true); | 65 var controller = new StreamController(sync: true); |
53 controller.stream.transform(new ZLibEncoder(gzip: true)) | 66 controller.stream.transform(new ZLibEncoder(gzip: true)) |
54 .fold([], (buffer, data) { | 67 .fold([], (buffer, data) { |
55 buffer.addAll(data); | 68 buffer.addAll(data); |
56 return buffer; | 69 return buffer; |
57 }) | 70 }) |
58 .then((data) { | 71 .then((data) { |
59 Expect.equals(30, data.length); | 72 Expect.equals(30, data.length); |
60 Expect.listEquals([99, 96, 100, 98, 102, 97, 101, 99, 231, 224, 4, 0, | 73 Expect.listEquals([99, 96, 100, 98, 102, 97, 101, 99, 231, 224, 4, 0, |
61 70, 215, 108, 69, 10, 0, 0, 0], | 74 70, 215, 108, 69, 10, 0, 0, 0], |
62 // Skip header, as it can change. | 75 // Skip header, as it can change. |
63 data.sublist(10)); | 76 data.sublist(10)); |
64 asyncEnd(); | 77 asyncEnd(); |
65 }); | 78 }); |
66 controller.add(data); | 79 controller.add(data); |
67 controller.close(); | 80 controller.close(); |
68 } | 81 } |
69 | 82 |
83 void testZLibDeflateRaw() { | |
84 asyncStart(); | |
85 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
86 var controller = new StreamController(sync: true); | |
87 controller.stream.transform(new ZLibEncoder(raw: true, level: 6)) | |
88 .fold([], (buffer, data) { | |
89 buffer.addAll(data); | |
90 return buffer; | |
91 }) | |
92 .then((data) { | |
93 print(data); | |
94 Expect.listEquals([99, 96, 100, 98, 102, 97, 101, 99, 231, 224, 4, 0], | |
95 data); | |
96 asyncEnd(); | |
97 }); | |
98 controller.add(data); | |
99 controller.close(); | |
100 } | |
101 | |
70 void testZLibDeflateInvalidLevel() { | 102 void testZLibDeflateInvalidLevel() { |
71 test2(gzip, level) { | 103 test2(gzip, level) { |
72 try { | 104 [true, false].forEach((gzip) { |
73 new ZLibEncoder(gzip: gzip, level: level).startChunkedConversion(null); | 105 [-2, -20, 10, 42, null, "9"].forEach((level) { |
74 Expect.fail("No exception thrown"); | 106 Expect.throws( |
75 } catch (e) { | 107 () => new ZLibEncoder(gzip: gzip, level: level), |
76 } | 108 (e) => e is ArgumentError, |
77 } | 109 "'level' must be in range -1..9" |
78 test(level) { | 110 ); |
79 test2(false, level); | 111 }); |
80 test2(true, level); | 112 }); |
81 test2(9, level); | 113 }; |
82 } | |
83 test(-2); | |
84 test(-20); | |
85 test(10); | |
86 test(42); | |
87 test(null); | |
88 test("9"); | |
89 } | 114 } |
90 | 115 |
91 void testZLibInflate() { | 116 void testZLibInflate() { |
92 test2(bool gzip, int level) { | 117 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
118 | |
119 [true, false].forEach((gzip) { | |
120 [ZLIB_STRATEGY_FILTERED, ZLIB_STRATEGY_HUFFMAN_ONLY, ZLIB_STRATEGY_RLE, | |
121 ZLIB_STRATEGY_FIXED, ZLIB_STRATEGY_DEFAULT].forEach((strategy) { | |
122 [3, 6, 9].forEach((level) { | |
123 asyncStart(); | |
124 var controller = new StreamController(sync: true); | |
125 controller.stream | |
126 .transform(new ZLibEncoder(gzip: gzip, level: level, | |
127 strategy: strategy)) | |
128 .transform(new ZLibDecoder()) | |
129 .fold([], (buffer, data) { | |
130 buffer.addAll(data); | |
131 return buffer; | |
132 }) | |
133 .then((inflated) { | |
134 Expect.listEquals(data, inflated); | |
135 asyncEnd(); | |
136 }); | |
137 controller.add(data); | |
138 controller.close(); | |
139 }); | |
140 }); | |
141 }); | |
142 } | |
143 | |
144 void testZLibInflateRaw() { | |
145 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
146 | |
147 [3, 6, 9].forEach((level) { | |
93 asyncStart(); | 148 asyncStart(); |
94 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
95 var controller = new StreamController(sync: true); | 149 var controller = new StreamController(sync: true); |
96 controller.stream | 150 controller.stream |
97 .transform(new ZLibEncoder(gzip: gzip, level: level)) | 151 .transform(new ZLibEncoder(raw: true, level: level)) |
98 .transform(new ZLibDecoder()) | 152 .transform(new ZLibDecoder(raw: true)) |
99 .fold([], (buffer, data) { | 153 .fold([], (buffer, data) { |
100 buffer.addAll(data); | 154 buffer.addAll(data); |
101 return buffer; | 155 return buffer; |
102 }) | 156 }) |
103 .then((inflated) { | 157 .then((inflated) { |
104 Expect.listEquals(data, inflated); | 158 Expect.listEquals(data, inflated); |
105 asyncEnd(); | 159 asyncEnd(); |
106 }); | 160 }); |
107 controller.add(data); | 161 controller.add(data); |
108 controller.close(); | 162 controller.close(); |
109 } | 163 }); |
110 void test(int level) { | |
111 test2(false, level); | |
112 test2(true, level); | |
113 } | |
114 for (int i = -1; i < 10; i++) { | |
115 test(i); | |
116 } | |
117 } | 164 } |
118 | 165 |
119 void testZLibInflateSync() { | 166 void testZLibInflateSync() { |
120 test2(bool gzip, int level) { | 167 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
121 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 168 |
122 var encoded = new ZLibEncoder(gzip: gzip, level: level).convert(data); | 169 [true, false].forEach((gzip) { |
123 var decoded = new ZLibDecoder().convert(encoded); | 170 [3, 6, 9].forEach((level) { |
171 var encoded = new ZLibEncoder(gzip: gzip, level: level).convert(data); | |
172 var decoded = new ZLibDecoder().convert(encoded); | |
173 Expect.listEquals(data, decoded); | |
174 }); | |
175 }); | |
176 } | |
177 | |
178 void testZlibInflateThrowsWithSmallerWindow() { | |
179 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
180 var encoder = new ZLibEncoder(windowBits: 10); | |
181 var encodedData = encoder.convert(data); | |
182 var decoder = new ZLibDecoder(windowBits: 8); | |
183 Expect.throws(() => decoder.convert(encodedData)); | |
184 } | |
185 | |
186 void testZlibInflateWithLargerWindow() { | |
187 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
188 | |
189 [true, false].forEach((gzip) { | |
190 [3, 6, 9].forEach((level) { | |
191 asyncStart(); | |
192 var controller = new StreamController(sync: true); | |
193 controller.stream | |
194 .transform(new ZLibEncoder(gzip: gzip, level: level, windowBits: 8)) | |
195 .transform(new ZLibDecoder(windowBits: 10)) | |
196 .fold([], (buffer, data) { | |
197 buffer.addAll(data); | |
198 return buffer; | |
199 }) | |
200 .then((inflated) { | |
201 Expect.listEquals(data, inflated); | |
202 asyncEnd(); | |
203 }); | |
204 controller.add(data); | |
205 controller.close(); | |
206 }); | |
207 }); | |
208 } | |
209 | |
210 void testZlibWithDictionary() { | |
211 var dict = [102, 111, 111, 98, 97, 114]; | |
212 var data = [98, 97, 114, 102, 111, 111]; | |
213 | |
214 [3, 6, 9].forEach((level) { | |
215 var encoded = new ZLibEncoder(level: level, dictionary: dict) | |
216 .convert(data); | |
217 var decoded = new ZLibDecoder(dictionary: dict).convert(encoded); | |
124 Expect.listEquals(data, decoded); | 218 Expect.listEquals(data, decoded); |
125 } | 219 }); |
126 void test(int level) { | |
127 test2(false, level); | |
128 test2(true, level); | |
129 } | |
130 for (int i = -1; i < 10; i++) { | |
131 test(i); | |
132 } | |
133 } | 220 } |
134 | 221 |
135 void main() { | 222 void main() { |
136 asyncStart(); | 223 asyncStart(); |
137 testZLibDeflate(); | 224 testZLibDeflate(); |
138 testZLibDeflateEmpty(); | 225 testZLibDeflateEmpty(); |
226 testZLibDeflateEmptyGzip(); | |
139 testZLibDeflateGZip(); | 227 testZLibDeflateGZip(); |
140 testZLibDeflateInvalidLevel(); | 228 testZLibDeflateInvalidLevel(); |
141 testZLibInflate(); | 229 testZLibInflate(); |
142 testZLibInflateSync(); | 230 testZLibInflateSync(); |
231 testZlibInflateThrowsWithSmallerWindow(); | |
232 testZlibInflateWithLargerWindow(); | |
233 testZLibDeflateRaw(); | |
234 testZLibInflateRaw(); | |
235 testZlibWithDictionary(); | |
143 asyncEnd(); | 236 asyncEnd(); |
144 } | 237 } |
OLD | NEW |