forked from imaya/zlib.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-test.js
307 lines (264 loc) · 9.16 KB
/
node-test.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
var buster = require('buster');
var nodeZlib = require('zlib');
var zlib = require('../bin/node-zlib.js');
var mt_rand = require('../vendor/mt.js/node-mt.js');
//-----------------------------------------------------------------------------
// array assertion
//-----------------------------------------------------------------------------
function arrayEquals(expected, actuals) {
var i, il;
if (!(expected instanceof actuals.constructor)) {
return false;
}
if (expected.length !== actuals.length) {
return false;
}
for (i = 0, il = expected.length; i < il; ++i) {
if (expected[i] !== actuals[i]) {
return false;
}
}
return true;
}
//-----------------------------------------------------------------------------
// test cases
//-----------------------------------------------------------------------------
buster.testCase(
"node inflate and deflate",
{
setUp: function() {
var size = 1234567;
var testData = new Buffer(size);
this.timeout = 1000;
this.testData = testData;
},
"inflate pre-deflated data": function() {
var size = 123456;
var plain = new Buffer(size);
var i, il;
var testData =
'eJztzydhGAAAALDd/v////////////////////////////////9no6BxkF+///z99x8A'+
'EAgYBBQMHAISChoGFg4eAREJGQUVDR0DEwsbBxcPn4CQiJiElIycgpKKmoaWjp6BkYmZ'+
'hZWNnYOTi5uHl49fQFBIWERUTFxCUkpaRlZOXkFRSVlFVU1dQ1NLW0dXT9/A0MjYxNTM'+
'3MLSytrG1s7ewdHJ2cXVzd3D08vbx9fPPyAwKDgkNCw8IjIqOiY2Lj4hMSk5JTUtPSMz'+
'KzsnNy+/oLCouKS0rLyisqq6prauvqGxqbmlta29o7Oru6e3r39gcGh4ZHRsfGJyanpm'+
'dm5+YXFpeWV1bX1jc2t7Z3dv/+Dw6Pjk9Oz84vLq+ub27v7h8en55fXt/ePz5//z//n/'+
'/H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f'+
'/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z'+
'//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+'+
'P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//'+
'5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n/'+
'/H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f'+
'/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z'+
'//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+'+
'P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//'+
'5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n/'+
'/H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f'+
'/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z'+
'//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+'+
'P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//'+
'5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n/'+
'/H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f'+
'/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z//n//H/+P/+f/8//5//z'+
'//n//H/+P/+f/8//5//z//n//H/+3+P/Ba1OJPE=';
// make plain data
for (i = 0, il = size; i < il; ++i) {
plain[i] = i & 0xff;
}
var decodedData = new Buffer(testData, 'base64');
// testdata size
buster.assert.equals(testData.length, 1604, "source data size");
buster.assert.equals(decodedData.length, 1202, "base64 decoded data size");
var inflated = zlib.inflateSync(decodedData);
buster.assert.equals(inflated.length, size, "inflated data size");
buster.assert(arrayEquals(inflated, plain));
},
// native deflate, js inflate
"uncompressed random data": function(done) {
var data = this.testData;
makeRandomData(data);
zlibBuffer(new nodeZlib.Deflate({level: 0}), data, function(err, buf) {
var inflated = zlib.inflateSync(buf);
buster.assert.equals(inflated.length, data.length);
buster.assert.equals(inflated, data);
done();
});
},
"maxlevel random data": function(done) {
var data = this.testData;
makeRandomData(data);
zlibBuffer(new nodeZlib.Deflate({level: 9}), data, function(err, buf) {
var inflated = zlib.inflateSync(buf);
buster.assert.equals(inflated.length, data.length);
buster.assert(arrayEquals(inflated, data));
done();
});
},
"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 = zlib.deflateSync(data);
var decompressed = zlib.inflateSync(compressed);
buster.assert(arrayEquals(data, Array.prototype.slice.call(decompressed)));
},
// js deflate, native inflate
"random data": function(done) {
makeRandomData(this.testData);
inflateTest(done, this.testData);
},
"sequential data": function(done) {
makeSequentialData(this.testData);
inflateTest(done, this.testData);
},
"random sequential data": function(done) {
makeRandomSequentialData(this.testData);
inflateTest(done, this.testData);
},
// native gzip, js gunzip
"gunzip random data": function(done) {
makeRandomData(this.testData);
gunzipTest(done, this.testData);
},
"gunzip sequential data": function(done) {
makeSequentialData(this.testData);
gunzipTest(done, this.testData);
},
"gunzip random sequential data": function(done) {
makeRandomSequentialData(this.testData);
gunzipTest(done, this.testData);
},
// js gzip, native gunzip
"gzip random data": function(done) {
makeRandomData(this.testData);
gzipTest(done, this.testData);
},
"gzip sequential data": function(done) {
makeSequentialData(this.testData);
gzipTest(done, this.testData);
},
"gzip random sequential data": function(done) {
makeRandomSequentialData(this.testData);
gzipTest(done, this.testData);
},
// 過去に失敗したことのあるテスト
"bitbuflen error": function(done) {
makeRandomSequentialData(this.testData, 1339494909128);
gunzipTest(done, this.testData);
}
}
);
// inflate test
function inflateTest(done, testData) {
var deflate;
// deflate
deflate = zlib.deflateSync(testData);
console.log("deflated data size:", deflate.length);
// inflate
nodeZlib.inflate(deflate, function(err, buffer) {
console.log("inflated data size:", buffer.length);
// assertion
buster.assert(buffer.length, testData.length);
buster.assert(arrayEquals(buffer, testData));
done();
});
}
// gzip test
function gzipTest(done, testData) {
var deflated = zlib.gzipSync(testData);
console.log("Source:", testData.length);
console.log("Deflate:", deflated.length);
nodeZlib.gunzip(deflated, function(err, buf) {
console.log("Inflate:", buf.length);
buster.assert.equals(buf.length, testData.length);
buster.assert(arrayEquals(buf, testData));
done();
});
}
// gunzip test
function gunzipTest(done, testData) {
nodeZlib.gzip(testData, function(err, buf) {
var inflated = zlib.gunzipSync(buf);
buster.assert.equals(inflated.length, testData.length);
buster.assert(arrayEquals(inflated, testData));
done();
});
}
// random
function makeRandomData(data, opt_seed) {
var seed = typeof opt_seed === 'number' ? opt_seed : +new Date();
var mt = new mt_rand.MersenneTwister(seed);
var i, il;
console.log("seed:", seed);
// make random data
for (i = 0, il = data.length; i < il; ++i) {
data[i] = mt.nextInt(256);
}
}
// sequential
function makeSequentialData(data) {
var i, il;
// make sequential data
for (i = 0, il = data.length; i < il; ++i) {
data[i] = i & 0xff;
}
}
// random sequential
function makeRandomSequentialData(data, opt_seed) {
var seed = typeof opt_seed === 'number' ? opt_seed : +new Date();
var mt = new mt_rand.MersenneTwister(seed);
var i, il;
var random1, random2;
console.log("seed:", seed);
// make random data
for (i = 0, il = data.length; i < il;) {
random1 = mt.nextInt(256);
random2 = mt.nextInt(256);
while (random2--) {
if (i === il) {
break;
}
data[i++] = random1++ & 0xff;
}
}
}
//-----------------------------------------------------------------------------
// extract from node.js implementation
//-----------------------------------------------------------------------------
function zlibBuffer(engine, buffer, callback) {
var buffers = [];
var nread = 0;
function onError(err) {
engine.removeListener('end', onEnd);
engine.removeListener('error', onError);
callback(err);
}
function onData(chunk) {
buffers.push(chunk);
nread += chunk.length;
}
function onEnd() {
var buffer;
switch (buffers.length) {
case 0:
buffer = new Buffer(0);
break;
case 1:
buffer = buffers[0];
break;
default:
buffer = new Buffer(nread);
var n = 0;
buffers.forEach(function(b) {
var l = b.length;
b.copy(buffer, n, 0, l);
n += l;
});
break;
}
callback(null, buffer);
}
engine.on('error', onError);
engine.on('data', onData);
engine.on('end', onEnd);
engine.write(buffer);
engine.end();
}