Added deflate tests

This commit is contained in:
Vitaly Puzrin 2014-02-03 22:38:43 +04:00
parent 7254746060
commit 5d14848c85
4 changed files with 3365 additions and 0 deletions

7
index.js Normal file
View file

@ -0,0 +1,7 @@
'use strict';
var pako = {};
module.exports = pako;

154
test/deflate.js Normal file
View file

@ -0,0 +1,154 @@
/*global describe, it*/
'use strict';
var zlib = require('zlib');
var fs = require('fs');
var path = require('path');
var pako = require('../index');
var helpers = require('./helpers');
var testDeflate = helpers.testDeflate;
var sample_file = path.join(__dirname, 'fixtures/lorem_100k.txt');
var sample = new Uint8Array(fs.readFileSync(sample_file));
describe.skip('Deflate defaults', function () {
it('deflate', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, {}, done);
});
it('deflate raw', function(done) {
testDeflate(zlib.createDeflateRaw, pako.deflateRaw, sample, {}, done);
});
});
describe.skip('Deflate levels', function () {
it('level 9', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { level: 9 }, done);
});
it('level 8', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { level: 8 }, done);
});
it('level 7', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { level: 7 }, done);
});
it('level 6', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { level: 6 }, done);
});
it('level 5', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { level: 5 }, done);
});
it('level 4', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { level: 4 }, done);
});
it('level 3', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { level: 3 }, done);
});
it('level 2', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { level: 2 }, done);
});
it('level 1', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { level: 1 }, done);
});
it('level 0', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { level: 0 }, done);
});
});
describe.skip('Deflate windowBits', function () {
it('windowBits 16', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { windowBits: 16 }, done);
});
it('windowBits 15', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { windowBits: 15 }, done);
});
it('windowBits 14', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { windowBits: 14 }, done);
});
it('windowBits 13', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { windowBits: 13 }, done);
});
it('windowBits 12', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { windowBits: 12 }, done);
});
it('windowBits 11', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { windowBits: 11 }, done);
});
it('windowBits 10', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { windowBits: 10 }, done);
});
it('windowBits 9', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { windowBits: 9 }, done);
});
it('windowBits 8', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { windowBits: 8 }, done);
});
});
describe.skip('Deflate memLevel', function () {
it('memLevel 9', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { memLevel: 9 }, done);
});
it('memLevel 8', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { memLevel: 8 }, done);
});
it('memLevel 7', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { memLevel: 7 }, done);
});
it('memLevel 6', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { memLevel: 6 }, done);
});
it('memLevel 5', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { memLevel: 5 }, done);
});
it('memLevel 4', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { memLevel: 4 }, done);
});
it('memLevel 3', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { memLevel: 3 }, done);
});
it('memLevel 2', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { memLevel: 2 }, done);
});
it('memLevel 1', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { memLevel: 1 }, done);
});
});
describe.skip('Deflate strategy', function () {
it('Z_DEFAULT_STRATEGY', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { strategy: 0 }, done);
});
it('Z_FILTERED', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { strategy: 1 }, done);
});
it('Z_HUFFMAN_ONLY', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { strategy: 2 }, done);
});
it('Z_RLE', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { strategy: 3 }, done);
});
it('Z_FIXED', function(done) {
testDeflate(zlib.createDeflate, pako.deflate, sample, { strategy: 4 }, done);
});
});

3140
test/fixtures/lorem_100k.txt vendored Normal file

File diff suppressed because it is too large Load diff

64
test/helpers.js Normal file
View file

@ -0,0 +1,64 @@
'use strict';
// Compare 2 buffers (can be Array, Uint8Array, Buffer).
//
function cmpBuf(a, b) {
if (a.length !== b.length) {
return false;
}
for (var i=0, l=a.length; i<l; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
// Helper to test deflate/deflateRaw with different options.
// Use zlib streams, because it's the only way to define options.
//
function testDeflate(zlib_factory, pako_deflate, data, options, callback) {
var zlibStream = zlib_factory(options);
var buffers = [], nread = 0;
zlibStream.on('error', function(err) {
zlibStream.removeAllListeners();
zlibStream=null;
callback(err);
});
zlibStream.on('data', function(chunk) {
buffers.push(chunk);
nread += chunk.length;
});
zlibStream.on('end', function() {
zlibStream.removeAllListeners();
zlibStream=null;
var buffer = Buffer.concat(buffers);
var pako_result = pako_deflate(data, options);
if (!cmpBuf(buffer, pako_result)) {
callback(new Error('zlib result != pako result'));
return;
}
callback(null);
});
zlibStream.write(new Buffer(data));
zlibStream.end();
}
exports.cmpBuf = cmpBuf;
exports.testDeflate = testDeflate;