browser-codepath-test.js
6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
(function() {
buster.testCase(
"code path",
{
setUp: function() {
var size = 76543;
var testData = new (USE_TYPEDARRAY ? Uint8Array : Array)(size);
console.log("use typedarray:", USE_TYPEDARRAY);
this.testData = testData;
this.none = sinon.spy(Zlib.RawDeflate.prototype, "makeNocompressBlock");
this.fixed = sinon.spy(Zlib.RawDeflate.prototype, "makeFixedHuffmanBlock");
this.dynamic = sinon.spy(Zlib.RawDeflate.prototype, "makeDynamicHuffmanBlock");
},
tearDown: function() {
this.none.restore();
this.fixed.restore();
this.dynamic.restore();
},
"undercomitted": function() {
var data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23];
var compressed = new Zlib.Deflate(data).compress();
var decompressed = new Zlib.Inflate(compressed).decompress();
buster.assert(arrayEquals(data, Array.prototype.slice.call(decompressed)));
},
"uncompressed random data": function() {
makeRandomData(this.testData);
inflateTest('random', this.testData, Zlib.Deflate.CompressionType.NONE);
buster.assert(this.none.called);
buster.refute(this.fixed.called);
buster.refute(this.dynamic.called);
},
"fixed random data": function() {
makeRandomData(this.testData);
inflateTest('random', this.testData, Zlib.Deflate.CompressionType.FIXED);
buster.refute(this.none.called);
buster.assert(this.fixed.called);
buster.refute(this.dynamic.called);
},
"dynamic random data": function() {
makeRandomData(this.testData);
inflateTest('random', this.testData, Zlib.Deflate.CompressionType.DYNAMIC);
buster.refute(this.none.called);
buster.refute(this.fixed.called);
buster.assert(this.dynamic.called);
},
"uncompressed sequential data": function() {
makeSequentialData(this.testData);
inflateTest('sequential', this.testData, Zlib.Deflate.CompressionType.NONE);
buster.assert(this.none.called);
buster.refute(this.fixed.called);
buster.refute(this.dynamic.called);
},
"fixed sequential data": function() {
makeSequentialData(this.testData);
inflateTest('sequential', this.testData, Zlib.Deflate.CompressionType.FIXED);
buster.refute(this.none.called);
buster.assert(this.fixed.called);
buster.refute(this.dynamic.called);
},
"dynamic sequential data": function() {
makeSequentialData(this.testData);
inflateTest('sequential', this.testData, Zlib.Deflate.CompressionType.DYNAMIC);
buster.refute(this.none.called);
buster.refute(this.fixed.called);
buster.assert(this.dynamic.called);
},
"uncompressed random sequential data": function() {
makeRandomSequentialData(this.testData);
inflateTest('sequential', this.testData, Zlib.Deflate.CompressionType.NONE);
buster.assert(this.none.called);
buster.refute(this.fixed.called);
buster.refute(this.dynamic.called);
},
"fixed random sequential data": function() {
makeRandomSequentialData(this.testData);
inflateTest('sequential', this.testData, Zlib.Deflate.CompressionType.FIXED);
buster.refute(this.none.called);
buster.assert(this.fixed.called);
buster.refute(this.dynamic.called);
},
"dynamic random sequential data": function() {
makeRandomSequentialData(this.testData);
inflateTest('sequential', this.testData, Zlib.Deflate.CompressionType.DYNAMIC);
buster.refute(this.none.called);
buster.refute(this.fixed.called);
buster.assert(this.dynamic.called);
},
//-------------------------------------------------------------------------
// stream
//-------------------------------------------------------------------------
"uncompressed random sequential data (stream)": function() {
makeRandomSequentialData(this.testData);
inflateStreamTest('sequential', this.testData, Zlib.Deflate.CompressionType.NONE);
buster.assert(this.none.called);
buster.refute(this.fixed.called);
buster.refute(this.dynamic.called);
},
"fixed random sequential data (stream)": function() {
makeRandomSequentialData(this.testData);
inflateStreamTest('sequential', this.testData, Zlib.Deflate.CompressionType.FIXED);
buster.refute(this.none.called);
buster.assert(this.fixed.called);
buster.refute(this.dynamic.called);
},
"dynamic random sequential data (stream)": function() {
makeRandomSequentialData(this.testData);
inflateStreamTest('sequential', this.testData, Zlib.Deflate.CompressionType.DYNAMIC);
buster.refute(this.none.called);
buster.refute(this.fixed.called);
buster.assert(this.dynamic.called);
}
}
);
// inflate test
function inflateTest(mode, testData, compressionType) {
var deflate;
var inflate;
console.log("mode:", mode);
console.log("type:", compressionType);
// deflate
deflate = new Zlib.Deflate(testData, {
compressionType: compressionType
}).compress();
console.log("deflated data size:", deflate.length);
// inflate
inflate = (new Zlib.Inflate(deflate, {
verify: true
})).decompress();
console.log("inflated data size:", inflate.length)
// assertion
buster.assert(inflate.length, testData.length);
buster.assert(arrayEquals(inflate, testData));
}
// inflate test
function inflateStreamTest(mode, testData, compressionType) {
var deflate;
var inflate;
var inflator;
var buf;
var tmp;
var i;
var il;
console.log("mode:", mode);
console.log("type:", compressionType);
// deflate
deflate = Zlib.Deflate.compress(testData, {
compressionType: compressionType
});
console.log("deflated data size:", deflate.length);
// inflate
inflator = new Zlib.InflateStream();
inflate = new (USE_TYPEDARRAY ? Uint8Array : Array)();
for (i = 0, il = deflate.length; i < il; ++i) {
buf = inflator.decompress(deflate.subarray(i, i + 1));
tmp = new (USE_TYPEDARRAY ? Uint8Array : Array)(buf.length + inflate.length);
tmp.set(inflate, 0);
tmp.set(buf, inflate.length);
inflate = tmp;
}
console.log("inflated data size:", inflate.length)
// assertion
buster.assert(inflate.length, testData.length);
buster.assert(arrayEquals(inflate, testData));
}
})();