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, |
| 36 0, 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 [ZLibOption.STRATEGY_FILTERED, ZLibOption.STRATEGY_HUFFMAN_ONLY, |
| 121 ZLibOption.STRATEGY_RLE, ZLibOption.STRATEGY_FIXED, |
| 122 ZLibOption.STRATEGY_DEFAULT].forEach((strategy) { |
| 123 [3, 6, 9].forEach((level) { |
| 124 asyncStart(); |
| 125 var controller = new StreamController(sync: true); |
| 126 controller.stream |
| 127 .transform(new ZLibEncoder(gzip: gzip, level: level, |
| 128 strategy: strategy)) |
| 129 .transform(new ZLibDecoder()) |
| 130 .fold([], (buffer, data) { |
| 131 buffer.addAll(data); |
| 132 return buffer; |
| 133 }) |
| 134 .then((inflated) { |
| 135 Expect.listEquals(data, inflated); |
| 136 asyncEnd(); |
| 137 }); |
| 138 controller.add(data); |
| 139 controller.close(); |
| 140 }); |
| 141 }); |
| 142 }); |
| 143 } |
| 144 |
| 145 void testZLibInflateRaw() { |
| 146 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 147 |
| 148 [3, 6, 9].forEach((level) { |
93 asyncStart(); | 149 asyncStart(); |
94 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
95 var controller = new StreamController(sync: true); | 150 var controller = new StreamController(sync: true); |
96 controller.stream | 151 controller.stream |
97 .transform(new ZLibEncoder(gzip: gzip, level: level)) | 152 .transform(new ZLibEncoder(raw: true, level: level)) |
98 .transform(new ZLibDecoder()) | 153 .transform(new ZLibDecoder(raw: true)) |
99 .fold([], (buffer, data) { | 154 .fold([], (buffer, data) { |
100 buffer.addAll(data); | 155 buffer.addAll(data); |
101 return buffer; | 156 return buffer; |
102 }) | 157 }) |
103 .then((inflated) { | 158 .then((inflated) { |
104 Expect.listEquals(data, inflated); | 159 Expect.listEquals(data, inflated); |
105 asyncEnd(); | 160 asyncEnd(); |
106 }); | 161 }); |
107 controller.add(data); | 162 controller.add(data); |
108 controller.close(); | 163 controller.close(); |
109 } | 164 }); |
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 } | 165 } |
118 | 166 |
119 void testZLibInflateSync() { | 167 void testZLibInflateSync() { |
120 test2(bool gzip, int level) { | 168 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]; | 169 |
122 var encoded = new ZLibEncoder(gzip: gzip, level: level).convert(data); | 170 [true, false].forEach((gzip) { |
123 var decoded = new ZLibDecoder().convert(encoded); | 171 [3, 6, 9].forEach((level) { |
| 172 var encoded = new ZLibEncoder(gzip: gzip, level: level).convert(data); |
| 173 var decoded = new ZLibDecoder().convert(encoded); |
| 174 Expect.listEquals(data, decoded); |
| 175 }); |
| 176 }); |
| 177 } |
| 178 |
| 179 void testZlibInflateThrowsWithSmallerWindow() { |
| 180 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 181 var encoder = new ZLibEncoder(windowBits: 10); |
| 182 var encodedData = encoder.convert(data); |
| 183 var decoder = new ZLibDecoder(windowBits: 8); |
| 184 Expect.throws(() => decoder.convert(encodedData)); |
| 185 } |
| 186 |
| 187 void testZlibInflateWithLargerWindow() { |
| 188 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 189 |
| 190 [true, false].forEach((gzip) { |
| 191 [3, 6, 9].forEach((level) { |
| 192 asyncStart(); |
| 193 var controller = new StreamController(sync: true); |
| 194 controller.stream |
| 195 .transform(new ZLibEncoder(gzip: gzip, level: level, windowBits: 8)) |
| 196 .transform(new ZLibDecoder(windowBits: 10)) |
| 197 .fold([], (buffer, data) { |
| 198 buffer.addAll(data); |
| 199 return buffer; |
| 200 }) |
| 201 .then((inflated) { |
| 202 Expect.listEquals(data, inflated); |
| 203 asyncEnd(); |
| 204 }); |
| 205 controller.add(data); |
| 206 controller.close(); |
| 207 }); |
| 208 }); |
| 209 } |
| 210 |
| 211 void testZlibWithDictionary() { |
| 212 var dict = [102, 111, 111, 98, 97, 114]; |
| 213 var data = [98, 97, 114, 102, 111, 111]; |
| 214 |
| 215 [3, 6, 9].forEach((level) { |
| 216 var encoded = new ZLibEncoder(level: level, dictionary: dict) |
| 217 .convert(data); |
| 218 var decoded = new ZLibDecoder(dictionary: dict).convert(encoded); |
124 Expect.listEquals(data, decoded); | 219 Expect.listEquals(data, decoded); |
125 } | 220 }); |
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 } | 221 } |
134 | 222 |
135 void main() { | 223 void main() { |
136 asyncStart(); | 224 asyncStart(); |
137 testZLibDeflate(); | 225 testZLibDeflate(); |
138 testZLibDeflateEmpty(); | 226 testZLibDeflateEmpty(); |
| 227 testZLibDeflateEmptyGzip(); |
139 testZLibDeflateGZip(); | 228 testZLibDeflateGZip(); |
140 testZLibDeflateInvalidLevel(); | 229 testZLibDeflateInvalidLevel(); |
141 testZLibInflate(); | 230 testZLibInflate(); |
142 testZLibInflateSync(); | 231 testZLibInflateSync(); |
| 232 testZlibInflateThrowsWithSmallerWindow(); |
| 233 testZlibInflateWithLargerWindow(); |
| 234 testZLibDeflateRaw(); |
| 235 testZLibInflateRaw(); |
| 236 testZlibWithDictionary(); |
143 asyncEnd(); | 237 asyncEnd(); |
144 } | 238 } |
OLD | NEW |