Change tests to use binary comparision with fixtures

This commit is contained in:
Alex Kocharin 2021-07-22 17:35:51 +03:00
parent b967322bef
commit 1ac0a4c7ec
46 changed files with 291 additions and 212 deletions

View file

@ -40,6 +40,7 @@
"test": "npm run lint && mocha",
"coverage": "npm run lint && nyc mocha && nyc report --reporter html",
"build": "rollup -c",
"build_fixtures": "node support/build_fixtures.js",
"doc": "node support/build_doc.js",
"gh-doc": "npm run doc && gh-pages -d doc -f",
"prepublishOnly": "npm run gh-doc"

81
support/build_fixtures.js Normal file
View file

@ -0,0 +1,81 @@
#!/usr/bin/env node
'use strict';
/* eslint-env es6 */
/* eslint-disable no-console */
// node.js switched to chromium zlib implementation in version v12.17.0,
// which generates slightly different fixtures.
// https://github.com/nodejs/node/pull/31201
//
// so we create fixtures generated by older node.js versions
const ver = process.version.match(/^v(\d+)\.(\d+)\.\d+$/);
if (!(+ver[1] < 12 || +ver[1] === 12 && +ver[2] <= 16)) {
throw new Error('Please use node.js <= 12.16.x to generate these fixtures');
}
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const sample = fs.readFileSync(path.join(__dirname, '../test/fixtures/samples/lorem_en_100k.txt'));
let dirname = path.join(__dirname, '../test/fixtures/binary_compare');
if (!fs.existsSync(dirname)) fs.mkdirSync(dirname);
function createSample(method, options, filename) {
let file = path.join(dirname, filename);
let data = zlib[method](sample, options);
fs.writeFileSync(file, zlib[method](sample, options));
console.log(file, data.length);
}
createSample('deflateSync', {}, 'deflate.bin');
createSample('deflateRawSync', {}, 'deflateRaw.bin');
createSample('gzipSync', {}, 'gzip.bin');
createSample('deflateSync', { level: 9 }, 'deflate_level=9.bin');
createSample('deflateSync', { level: 8 }, 'deflate_level=8.bin');
createSample('deflateSync', { level: 7 }, 'deflate_level=7.bin');
createSample('deflateSync', { level: 6 }, 'deflate_level=6.bin');
createSample('deflateSync', { level: 5 }, 'deflate_level=5.bin');
createSample('deflateSync', { level: 4 }, 'deflate_level=4.bin');
createSample('deflateSync', { level: 3 }, 'deflate_level=3.bin');
createSample('deflateSync', { level: 2 }, 'deflate_level=2.bin');
createSample('deflateSync', { level: 1 }, 'deflate_level=1.bin');
createSample('deflateSync', { level: -1 }, 'deflate_level=-1.bin');
createSample('deflateSync', { windowBits: 15 }, 'deflate_windowBits=15.bin');
createSample('deflateSync', { windowBits: 14 }, 'deflate_windowBits=14.bin');
createSample('deflateSync', { windowBits: 13 }, 'deflate_windowBits=13.bin');
createSample('deflateSync', { windowBits: 12 }, 'deflate_windowBits=12.bin');
createSample('deflateSync', { windowBits: 11 }, 'deflate_windowBits=11.bin');
createSample('deflateSync', { windowBits: 10 }, 'deflate_windowBits=10.bin');
createSample('deflateSync', { windowBits: 9 }, 'deflate_windowBits=9.bin');
createSample('deflateSync', { windowBits: 8 }, 'deflate_windowBits=8.bin');
createSample('deflateRawSync', { windowBits: 15 }, 'deflateRaw_windowBits=15.bin');
createSample('deflateSync', { memLevel: 9 }, 'deflate_memLevel=9.bin');
createSample('deflateSync', { memLevel: 8 }, 'deflate_memLevel=8.bin');
createSample('deflateSync', { memLevel: 7 }, 'deflate_memLevel=7.bin');
createSample('deflateSync', { memLevel: 6 }, 'deflate_memLevel=6.bin');
createSample('deflateSync', { memLevel: 5 }, 'deflate_memLevel=5.bin');
createSample('deflateSync', { memLevel: 4 }, 'deflate_memLevel=4.bin');
createSample('deflateSync', { memLevel: 3 }, 'deflate_memLevel=3.bin');
createSample('deflateSync', { memLevel: 2 }, 'deflate_memLevel=2.bin');
createSample('deflateSync', { memLevel: 1 }, 'deflate_memLevel=1.bin');
createSample('deflateSync', { strategy: 0 }, 'deflate_strategy=0.bin');
createSample('deflateSync', { strategy: 1 }, 'deflate_strategy=1.bin');
createSample('deflateSync', { strategy: 2 }, 'deflate_strategy=2.bin');
createSample('deflateSync', { strategy: 3 }, 'deflate_strategy=3.bin');
createSample('deflateSync', { strategy: 4 }, 'deflate_strategy=4.bin');
createSample('deflateRawSync', { level: 4 }, 'deflateRaw_level=4.bin');
createSample('deflateRawSync', { level: 1 }, 'deflateRaw_level=1.bin');
const dict = Buffer.from('abcdefghijklmnoprstuvwxyz');
createSample('deflateSync', { dictionary: dict }, 'deflate_dictionary=trivial.bin');
const spdyDict = fs.readFileSync(path.join(__dirname, '..', 'test', 'fixtures', 'spdy_dict.txt'));
createSample('deflateSync', { dictionary: spdyDict }, 'deflate_dictionary=spdy.bin');

200
test/binary_compare.js Normal file
View file

@ -0,0 +1,200 @@
'use strict';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const pako = require('../index');
const loadSamples = require('./helpers').loadSamples;
const sample = loadSamples().lorem_en_100k;
// Helper used to compare pako deflate using various options
// with reference values from old node.js zlib.
//
// node.js switched to chromium zlib implementation in version v12.17.0,
// which generates slightly different fixtures.
// https://github.com/nodejs/node/pull/31201
//
// So we use fixtures generated by node.js versions below v12.17.0.
//
function testSample(pako_method, sample, options, filename) {
const dir = path.join(__dirname, 'fixtures', 'binary_compare');
const pako_result = pako_method(sample, options);
const zlib_result = fs.readFileSync(path.join(dir, filename));
// One more hack: gzip header contains OS code, that can vary.
// Override OS code if requested. For simplicity, we assume it on fixed
// position (= no additional gzip headers used)
if (options.ignore_os) zlib_result[9] = pako_result[9];
assert.deepStrictEqual(pako_result, new Uint8Array(zlib_result));
}
describe('Deflate defaults', () => {
it('deflate, no options', () => {
testSample(pako.deflate, sample, {}, 'deflate.bin');
});
it('deflate raw, no options', () => {
testSample(pako.deflateRaw, sample, {}, 'deflateRaw.bin');
});
// OS code in header can vary. Use hack flag to ignore it.
it('gzip, no options', () => {
testSample(pako.gzip, sample, { ignore_os: true }, 'gzip.bin');
});
});
describe('Deflate levels', () => {
it('level 9', () => {
testSample(pako.deflate, sample, { level: 9 }, 'deflate_level=9.bin');
});
it('level 8', () => {
testSample(pako.deflate, sample, { level: 8 }, 'deflate_level=8.bin');
});
it('level 7', () => {
testSample(pako.deflate, sample, { level: 7 }, 'deflate_level=7.bin');
});
it('level 6', () => {
testSample(pako.deflate, sample, { level: 6 }, 'deflate_level=6.bin');
});
it('level 5', () => {
testSample(pako.deflate, sample, { level: 5 }, 'deflate_level=5.bin');
});
it('level 4', () => {
testSample(pako.deflate, sample, { level: 4 }, 'deflate_level=4.bin');
});
it('level 3', () => {
testSample(pako.deflate, sample, { level: 3 }, 'deflate_level=3.bin');
});
it('level 2', () => {
testSample(pako.deflate, sample, { level: 2 }, 'deflate_level=2.bin');
});
it('level 1', () => {
testSample(pako.deflate, sample, { level: 1 }, 'deflate_level=1.bin');
});
it('level -1 (implicit default)', () => {
testSample(pako.deflate, sample, { level: -1 }, 'deflate_level=-1.bin');
});
});
describe('Deflate windowBits', () => {
it('windowBits 15', () => {
testSample(pako.deflate, sample, { windowBits: 15 }, 'deflate_windowBits=15.bin');
});
it('windowBits 14', () => {
testSample(pako.deflate, sample, { windowBits: 14 }, 'deflate_windowBits=14.bin');
});
it('windowBits 13', () => {
testSample(pako.deflate, sample, { windowBits: 13 }, 'deflate_windowBits=13.bin');
});
it('windowBits 12', () => {
testSample(pako.deflate, sample, { windowBits: 12 }, 'deflate_windowBits=12.bin');
});
it('windowBits 11', () => {
testSample(pako.deflate, sample, { windowBits: 11 }, 'deflate_windowBits=11.bin');
});
it('windowBits 10', () => {
testSample(pako.deflate, sample, { windowBits: 10 }, 'deflate_windowBits=10.bin');
});
it('windowBits 9', () => {
testSample(pako.deflate, sample, { windowBits: 9 }, 'deflate_windowBits=9.bin');
});
it('windowBits 8', () => {
testSample(pako.deflate, sample, { windowBits: 8 }, 'deflate_windowBits=8.bin');
});
it('windowBits -15 (implicit raw)', () => {
testSample(pako.deflate, sample, { windowBits: -15 }, 'deflateRaw_windowBits=15.bin');
});
});
describe('Deflate memLevel', () => {
it('memLevel 9', () => {
testSample(pako.deflate, sample, { memLevel: 9 }, 'deflate_memLevel=9.bin');
});
it('memLevel 8', () => {
testSample(pako.deflate, sample, { memLevel: 8 }, 'deflate_memLevel=8.bin');
});
it('memLevel 7', () => {
testSample(pako.deflate, sample, { memLevel: 7 }, 'deflate_memLevel=7.bin');
});
it('memLevel 6', () => {
testSample(pako.deflate, sample, { memLevel: 6 }, 'deflate_memLevel=6.bin');
});
it('memLevel 5', () => {
testSample(pako.deflate, sample, { memLevel: 5 }, 'deflate_memLevel=5.bin');
});
it('memLevel 4', () => {
testSample(pako.deflate, sample, { memLevel: 4 }, 'deflate_memLevel=4.bin');
});
it('memLevel 3', () => {
testSample(pako.deflate, sample, { memLevel: 3 }, 'deflate_memLevel=3.bin');
});
it('memLevel 2', () => {
testSample(pako.deflate, sample, { memLevel: 2 }, 'deflate_memLevel=2.bin');
});
it('memLevel 1', () => {
testSample(pako.deflate, sample, { memLevel: 1 }, 'deflate_memLevel=1.bin');
});
});
describe('Deflate strategy', () => {
it('Z_DEFAULT_STRATEGY', () => {
testSample(pako.deflate, sample, { strategy: 0 }, 'deflate_strategy=0.bin');
});
it('Z_FILTERED', () => {
testSample(pako.deflate, sample, { strategy: 1 }, 'deflate_strategy=1.bin');
});
it('Z_HUFFMAN_ONLY', () => {
testSample(pako.deflate, sample, { strategy: 2 }, 'deflate_strategy=2.bin');
});
it('Z_RLE', () => {
testSample(pako.deflate, sample, { strategy: 3 }, 'deflate_strategy=3.bin');
});
it('Z_FIXED', () => {
testSample(pako.deflate, sample, { strategy: 4 }, 'deflate_strategy=4.bin');
});
});
describe('Deflate RAW', () => {
// Since difference is only in wrapper, test for store/fast/slow methods are enough
it('level 4', () => {
testSample(pako.deflateRaw, sample, { level: 4 }, 'deflateRaw_level=4.bin');
});
it('level 1', () => {
testSample(pako.deflateRaw, sample, { level: 1 }, 'deflateRaw_level=1.bin');
});
});
describe('Deflate dictionary', () => {
it('trivial dictionary', () => {
const dict = Buffer.from('abcdefghijklmnoprstuvwxyz');
testSample(pako.deflate, sample, { dictionary: dict }, 'deflate_dictionary=trivial.bin');
});
it('spdy dictionary', () => {
const spdyDict = require('fs').readFileSync(require('path').join(__dirname, 'fixtures', 'spdy_dict.txt'));
testSample(pako.deflate, sample, { dictionary: spdyDict }, 'deflate_dictionary=spdy.bin');
});
});

View file

@ -1,188 +1,14 @@
'use strict';
const zlib = require('zlib');
const pako = require('../index');
const { testSamples, loadSamples } = require('./helpers');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const samples = loadSamples();
describe('Deflate defaults', () => {
it('deflate, no options', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, {});
});
it('deflate raw, no options', () => {
testSamples(zlib.deflateRawSync, pako.deflateRaw, samples, {});
});
// OS code in header can vary. Use hack flag to ignore it.
it('gzip, no options', () => {
testSamples(zlib.gzipSync, pako.gzip, samples, { ignore_os: true });
});
});
describe('Deflate levels', () => {
it('level 9', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { level: 9 });
});
it('level 8', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { level: 8 });
});
it('level 7', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { level: 7 });
});
it('level 6', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { level: 6 });
});
it('level 5', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { level: 5 });
});
it('level 4', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { level: 4 });
});
it('level 3', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { level: 3 });
});
it('level 2', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { level: 2 });
});
it('level 1', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { level: 1 });
});
it.skip('level 0', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { level: 0 });
});
it('level -1 (implicit default)', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { level: -1 });
});
});
describe('Deflate windowBits', () => {
it('windowBits 15', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { windowBits: 15 });
});
it('windowBits 14', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { windowBits: 14 });
});
it('windowBits 13', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { windowBits: 13 });
});
it('windowBits 12', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { windowBits: 12 });
});
it('windowBits 11', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { windowBits: 11 });
});
it('windowBits 10', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { windowBits: 10 });
});
it('windowBits 9', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { windowBits: 9 });
});
it('windowBits 8', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { windowBits: 8 });
});
it('windowBits -15 (implicit raw)', () => {
testSamples(zlib.deflateRawSync, pako.deflate, samples, { windowBits: -15 });
});
});
describe('Deflate memLevel', () => {
it('memLevel 9', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { memLevel: 9 });
});
it('memLevel 8', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { memLevel: 8 });
});
it('memLevel 7', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { memLevel: 7 });
});
it('memLevel 6', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { memLevel: 6 });
});
it('memLevel 5', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { memLevel: 5 });
});
it('memLevel 4', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { memLevel: 4 });
});
it('memLevel 3', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { memLevel: 3 });
});
it('memLevel 2', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { memLevel: 2 });
});
it('memLevel 1', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { memLevel: 1 });
});
});
describe('Deflate strategy', () => {
it('Z_DEFAULT_STRATEGY', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { strategy: 0 });
});
it('Z_FILTERED', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { strategy: 1 });
});
it('Z_HUFFMAN_ONLY', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { strategy: 2 });
});
it('Z_RLE', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { strategy: 3 });
});
it('Z_FIXED', () => {
testSamples(zlib.deflateSync, pako.deflate, samples, { strategy: 4 });
});
});
describe('Deflate RAW', () => {
// Since difference is only in rwapper, test for store/fast/slow methods are enough
it('level 4', () => {
testSamples(zlib.deflateRawSync, pako.deflateRaw, samples, { level: 4 });
});
it('level 1', () => {
testSamples(zlib.deflateRawSync, pako.deflateRaw, samples, { level: 1 });
});
it.skip('level 0', () => {
testSamples(zlib.deflateRawSync, pako.deflateRaw, samples, { level: 0 });
});
});
describe('Deflate dictionary', () => {
it('trivial dictionary', () => {
const dict = Buffer.from('abcdefghijklmnoprstuvwxyz');
testSamples(zlib.deflateSync, pako.deflate, samples, { dictionary: dict });
});
it('spdy dictionary', () => {
const spdyDict = require('fs').readFileSync(require('path').join(__dirname, 'fixtures', 'spdy_dict.txt'));
testSamples(zlib.deflateSync, pako.deflate, samples, { dictionary: spdyDict });
});
it('handles multiple pushes', () => {
const dict = Buffer.from('abcd');
const deflate = new pako.Deflate({ dictionary: dict });

BIN
test/fixtures/binary_compare/deflate.bin vendored Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
test/fixtures/binary_compare/gzip.bin vendored Normal file

Binary file not shown.

View file

@ -29,37 +29,6 @@ function loadSamples(subdir) {
}
// Helper to test deflate/inflate with different options.
// Use zlib streams, because it's the only way to define options.
//
function testSingle(zlib_method, pako_method, data, options) {
const zlib_options = Object.assign({}, options);
// hack for testing negative windowBits
if (zlib_options.windowBits < 0) { zlib_options.windowBits = -zlib_options.windowBits; }
const zlib_result = zlib_method(data, zlib_options);
const pako_result = pako_method(data, options);
// One more hack: gzip header contains OS code, that can vary.
// Override OS code if requested. For simplicity, we assume it on fixed
// position (= no additional gzip headers used)
if (options.ignore_os) zlib_result[9] = pako_result[9];
assert.deepStrictEqual(pako_result, new Uint8Array(zlib_result));
}
function testSamples(zlib_method, pako_method, samples, options) {
Object.keys(samples).forEach(function (name) {
const data = samples[name];
testSingle(zlib_method, pako_method, data, options);
});
}
function testInflate(samples, inflateOptions, deflateOptions) {
let name, data, deflated, inflated;
@ -77,6 +46,5 @@ function testInflate(samples, inflateOptions, deflateOptions) {
}
module.exports.testSamples = testSamples;
module.exports.testInflate = testInflate;
module.exports.loadSamples = loadSamples;

View file

@ -1,13 +1,13 @@
'use strict';
const zlib = require('zlib');
const assert = require('assert');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const pako = require('../index');
const { testInflate, testSamples, loadSamples } = require('./helpers');
const pako = require('../index');
const { testInflate, loadSamples } = require('./helpers');
const samples = loadSamples();
@ -23,8 +23,11 @@ describe('Inflate defaults', () => {
});
it('inflate raw from compressed samples', () => {
const compressed_samples = loadSamples('samples_deflated_raw');
testSamples(zlib.inflateRawSync, pako.inflateRaw, compressed_samples, {});
Object.values(loadSamples('samples_deflated_raw')).forEach(function (sample) {
const pako_result = pako.inflateRaw(sample);
const zlib_result = zlib.inflateRawSync(sample);
assert.deepStrictEqual(pako_result, new Uint8Array(zlib_result));
});
});
});