-
Notifications
You must be signed in to change notification settings - Fork 20
/
test.js
215 lines (183 loc) · 5.51 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
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
var assert = require('assert');
var RangeCoder = require('./rangeCoder');
var BitEncoder = require('./bitEncoder');
var Encoder = require('./encoder');
var LzmaDecompress = require('./lzma');
var BinTree = require('./binTree');
var min = function(a, b) {
return a < b ? a : b;
};
var compareArray = function(a1, a2) {
var i;
if (a1.length !== a2.length) {
throw 'lengths not equal';
}
for (i = 0; i < a1.length; i++) {
if (a1[i] !== a2[i]) {
throw 'not equal at ' + i + ' a1[i]=' + a1[i] + ', a2[i]=' + a2[i];
}
}
};
var createInStream = function(data) {
var inStream = {
data: data,
offset: 0,
readByte: function(){
return this.data[this.offset++];
}
};
return inStream;
};
var createEncoderStream = function(data) {
var stream = {
data: data,
offset: 0,
read: function(buffer, bufOffset, length) {
var bytesRead = 0;
while (bytesRead < length && this.offset < data.length) {
buffer[bufOffset++] = this.data[this.offset++];
bytesRead++;
}
return bytesRead;
}
};
return stream;
}
var testRangeCoder = function() {
var i;
var repeats = 3;
var encoder = new RangeCoder.Encoder();
for (i = 0; i < repeats; i++) {
encoder.encode(0,6,20);
encoder.encode(0,6,20);
encoder.encode(6,2,20);
encoder.encode(0,6,20);
}
encoder.encode(8,2,20);
encoder.finish();
var decoder = new RangeCoder.Decoder(encoder.bytes);
var inside = function(l, r, v) {
assert.ok(v >= l && v < r, v + ' not in ' + l + ',' + r);
};
for (i = 0; i < repeats; i++) {
inside(0, 6, decoder.getThreshold(20));
decoder.decode(0,6,20);
inside(0, 6, decoder.getThreshold(20));
decoder.decode(0,6,20);
inside(6, 8, decoder.getThreshold(20));
decoder.decode(6,2,20);
inside(0, 6, decoder.getThreshold(20));
decoder.decode(0,6,20);
}
inside(8, 10, decoder.getThreshold(20));
};
var testBitEncoder = function() {
// Simple test for the bit encoder
var testSequence = [5, 1, 9, 8, 10, 15];
var i;
var bitEncoder = new BitEncoder.BitEncoder();
var rangeEncoder = new RangeCoder.Encoder();
bitEncoder.init();
for (i = 0; i < testSequence.length; i++) {
bitEncoder.encode(rangeEncoder, testSequence[i]);
}
rangeEncoder.finish();
assert.deepEqual(rangeEncoder.bytes, [ 0, 249, 223, 15, 188 ]);
};
var testCRC = function() {
assert.equal(BinTree.CRC.table.length, 256);
};
var testBitTreeEncoder = function(testSequence) {
// Test the BitTreeEncoder, using LZMA.js decompression for verification
var i;
var rangeEncoder = new RangeCoder.Encoder();
var bitTreeEncoder = new BitEncoder.BitTreeEncoder(8);
bitTreeEncoder.init();
for (i = 0; i < testSequence.length; i++) {
bitTreeEncoder.encode(rangeEncoder, testSequence[i]);
}
rangeEncoder.finish();
var bitTreeDecoder = new LzmaDecompress.LZMA.BitTreeDecoder(8);
bitTreeDecoder.init();
var rangeDecoder = new LzmaDecompress.LZMA.RangeDecoder();
rangeDecoder.setStream(createInStream(rangeEncoder.bytes));
rangeDecoder.init();
var result = [];
for (i = 0; i < testSequence.length; i++) {
result[result.length] = bitTreeDecoder.decode(rangeDecoder);
}
compareArray(result, testSequence);
};
var buildSequence = function(length, maxVal) {
var sequence = [];
var seed = 0xDEADBEEF;
var i;
for (i = 0; i < length; i++) {
seed = ((seed * 73) + 0x1234567) % 0xFFFFFFFF;
sequence[i] = seed % maxVal;
}
return sequence;
};
var testEncoder = function() {
var rangeEncoder = new RangeCoder.Encoder();
var encoder = new Encoder.Encoder();
encoder.create();
encoder.init();
var literalEncoder = new encoder.LiteralEncoder();
literalEncoder.create(2, 3);
literalEncoder.init();
var subCoder = literalEncoder.getSubCoder(5, 11);
assert.ok(subCoder !== null);
var lenEncoder = new encoder.LenEncoder();
lenEncoder.init(5);
lenEncoder.encode(rangeEncoder, 1, 0);
lenEncoder.encode(rangeEncoder, 20, 0);
lenEncoder.encode(rangeEncoder, 199, 0);
rangeEncoder.finish();
var lenPriceTableEncoder = new encoder.LenPriceTableEncoder();
lenPriceTableEncoder.init();
};
var testBinTree = function(sequence) {
var stream = createEncoderStream(sequence);
var blockSize = (1 << 12) + 0x20 + 275;
var inWindow = new BinTree.InWindow();
inWindow.createBase(1 << 12, 0x20, 275);
inWindow.setStream(stream);
inWindow.initBase();
// Test basics
var remaining = min(sequence.length, blockSize);
assert.equal(inWindow.getNumAvailableBytes(), remaining);
assert.equal(inWindow.getIndexByte(0), sequence[0]);
assert.equal(inWindow.getIndexByte(1), sequence[1]);
inWindow.movePosBase();
assert.equal(inWindow.getNumAvailableBytes(), remaining - 1);
assert.equal(inWindow.getIndexByte(0), sequence[1]);
// Test sequence matching
var testSequenceRepeats = [0, 1, 2, 3, 5, 0, 1, 2, 3, 4];
inWindow.setStream(createEncoderStream(testSequenceRepeats));
inWindow.initBase();
assert.equal(inWindow.getMatch(5, 4, 8), 4);
// Test BinTree
stream = createEncoderStream(sequence);
var binTree = new BinTree.BinTree();
binTree.setType(4);
binTree.create(1 << 22, 1 << 12, 0x20, 275);
binTree.setStream(stream);
binTree.init();
assert.equal(binTree.getNumAvailableBytes(), sequence.length);
};
var runAllTests = function() {
testRangeCoder();
testBitEncoder();
testCRC();
var testSequenceSmall = [5, 112, 90, 8, 10, 153, 255, 0, 0, 15];
var testSequenceLarge = buildSequence(10000, 255);
testBitTreeEncoder(testSequenceSmall);
testBitTreeEncoder(testSequenceLarge);
testBinTree(testSequenceSmall);
testBinTree(testSequenceLarge);
testEncoder();
};
var start = new Date();
runAllTests();
console.log('Testing finished in', new Date() - start, 'ms');