forked from kripken/zee.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
91 lines (76 loc) · 2.4 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
80
81
82
83
84
85
86
87
88
89
90
// js -m -n -e "load('zee.fast.js')" test.js
load('zee.js');
/*
var data = [100, 200, 200, 200, 200, 200, 200, 100, 100, 200, 200, 200, 200, 0, 1, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 1];
print('original: ' + data + ' : ' + data.length);
var compressed = Zee.compress(data);
print('compressed: ' + compressed.length);
var uncompressed = Zee.uncompress(compressed);
print('decompressed: ' + uncompressed.length);
*/
function assertEq(a, b) {
if (a !== b) {
throw 'Should have been equal: ' + a + ' : ' + b;
}
return false;
}
function assertNeq(a, b) {
try {
assertEq(a, b);
} catch(e) {
return;
}
throw 'Should have not been equal: ' + a + ' : ' + b;
}
function byteCompare(a, b) {
assertEq(a.length, b.length);
for (var i = 0; i < a.length; i++) {
assertEq(a[i]&255, b[i]&255);
}
}
function testSimple() {
print('testing simple..');
var data = [100, 200, 200, 200, 200, 200, 200, 100, 100, 200, 200, 200, 200, 0, 1];
var compressed = Zee.compress(data);
var decompressed = Zee.decompress(compressed);
byteCompare(data, decompressed);
assertNeq(data.length, compressed.length);
}
function testBig() {
print('testing big..');
var seed1 = 100;
var seed2 = 200;
var last = 255;
function fakeRandom() {
// numbers from http://triptico.com/docs/sp_random.html
seed1 = ((seed1 * 58321) + 11113) | 0;
var ret = (seed1 >> 16) & 255;
seed2 = ((seed2 * 58321) + 11113) | 0;
if (seed2 % 5) {
return last;
}
last = ret;
return last;
}
print(' ..generating data..');
var size = 1*1024*1024;
var data = new Array(size);
for (var i = 0; i < size; i++) {
data[i] = fakeRandom();
}
print(' ..compressing ' + data.length + ' bytes..');
var t = Date.now();
var compressed = Zee.compress(data);
print(' ..took ' + ((Date.now() - t)/1000).toFixed(2) + ' secs');
print(' ..decompressing ' + compressed.length + ' bytes..');
t = Date.now();
var decompressed = Zee.decompress(compressed);
print(' ..took ' + ((Date.now() - t)/1000).toFixed(2) + ' secs');
print(' ..got ' + decompressed.length + ' bytes..');
byteCompare(data, decompressed);
assertNeq(data.length, compressed.length);
print(' ..decompressed == original');
}
testSimple();
testBig();
print('ok.');