-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathtest.js
79 lines (70 loc) · 3.11 KB
/
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
var fs = require('fs');
var assert = require('assert');
var brotli = require('../');
var decompress = require('../decompress');
var compress = require('../compress');
describe('brotli', function() {
describe('compress', function() {
it('should compress some binary data', async function() {
// We need this timer, because otherwise the brotli object is empty at the time we try to compress.
await new Promise(r=>setTimeout(r, 10));
var data = fs.readFileSync('build/encode.js').slice(0, 1024 * 4);
var res = brotli.compress(data);
assert(res.length < data.length);
});
it('should compress some binary data using standalone version', function() {
var data = fs.readFileSync('build/encode.js').slice(0, 1024 * 4);
var res = compress(data);
assert(res.length < data.length);
});
it('should compress some text data', function() {
this.timeout(100000); // not sure why the first time text data is compressed it is slow...
var data = fs.readFileSync('build/encode.js', 'utf8').slice(0, 1024 * 4);
var res = brotli.compress(data, true);
assert(res.length < data.length);
});
it('should compress some text data using standalone version', function() {
var data = fs.readFileSync('build/encode.js', 'utf8').slice(0, 1024 * 4);
var res = compress(data, true);
assert(res.length < data.length);
});
it('compress some text with a dictionary', function() {
var dictionary = fs.readFileSync(__dirname + '/testdata/alice29.txt');
var data = fs.readFileSync(__dirname + '/testdata/alice30.txt');
var res = compress(data, { dictionary: dictionary });
var diff = fs.readFileSync(__dirname + '/testdata/alice30_diff_from_29.txt.sbr');
assert(res.length == diff.length);
// The first char of the output is different between our function and the CLI version.
// It presumably represents the window size difference when encoding. It has no impact
// on decoding outcomes.
assert.deepEqual(res.slice(1), diff.slice(1));
});
it('should compress short data', function() {
let res = compress(Buffer.from([255, 255, 255]));
assert(res.length > 3);
});
});
describe('decompress', function() {
fs.readdirSync(__dirname + '/testdata').forEach(function(file) {
if (!/\.compressed/.test(file)) return;
it(file, function() {
var compressed = fs.readFileSync(__dirname + '/testdata/' + file);
var expected = fs.readFileSync(__dirname + '/testdata/' + file.replace(/\.compressed.*/, ''));
var result = decompress(compressed);
assert.deepEqual(new Buffer(result), expected);
});
});
});
describe('roundtrip', function() {
var files = ['alice29.txt', 'asyoulik.txt', 'lcet10.txt', 'plrabn12.txt'];
files.forEach(function(file) {
it(file, function() {
this.timeout(10000);
var input = fs.readFileSync(__dirname + '/testdata/' + file);
var compressed = compress(input);
var decompressed = decompress(compressed);
assert.deepEqual(new Buffer(decompressed), input);
});
});
});
});