Spaces:
Sleeping
Sleeping
File size: 3,564 Bytes
90cbf22 |
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 |
import {
deltaDecode,
deltaEncode,
quantize,
runLengthDecode,
runLengthEncode,
unquantize,
} from './compression';
describe('compression', () => {
test('quantize (approximately) roundtrips', () => {
const precisions = [-1, 0, 1, 4, 8];
const datasets = [
// Random samples from [-2^32, 2^32]
[
-2331813745.435792, 4165391630.4586916, 2508162414.104561, -3815881222.355323,
3182227671.241928, -2091141304.634983, -3454731809.638463, 1539778764.4030657,
3723556916.971266, 4014694279.989772, 1165331218.5641785, -4209073662.9696226,
-3837962324.440032, 2145014827.7712336, -631662265.4694176, 4116219084.927844,
],
// [-2^16, 2^16]
[
-29109.399926296363, 24836.163035466132, 59528.43800645282, 5706.0239888604265,
61844.35496542655, -46030.9434605508, 10288.243500897894, -48623.38350764701,
-62182.09862667126, 20639.535833017246, -7691.974206406943, -44505.52704528734,
-28755.644095767944, 38244.45061335398, -14135.607864461621, -14792.956311113172,
],
// [-2^8, 2^8]
[
-67.02672070745166, -117.41024397385388, -243.41065459675673, 160.3825635900851,
191.79026087008378, 89.76668679513216, -10.719096486254784, 205.25021491717217,
-68.83096015839055, 44.321620651742364, -203.44266714551503, -19.734642986127426,
159.0214530150044, 72.07459707399431, -242.49909539291787, -246.50759645751867,
],
// [-2^4, 2^4]
[
14.993015665565746, -14.206729228453774, -1.503306544783097, -8.618521795982875,
15.14825900944064, -0.7561338814569538, -4.372631369200661, -14.296889398516797,
-0.7673738652041102, 5.880288329769968, -0.12246711347653516, 2.6074790469727773,
-1.0378494460674226, -5.395209965702431, -0.9218194118035932, -1.8677599340100492,
],
];
for (const values of datasets) {
for (const precision of precisions) {
const maxError = Math.max(1 / (1 << precision), 1e-8);
const roundTripped = unquantize(quantize(values, precision), precision);
expect(values.length).toEqual(roundTripped.length);
for (let i = 0; i < values.length; i++) {
const value = values[i];
const roundtrippedValue = roundTripped[i];
expect(Math.abs(value - roundtrippedValue)).toBeLessThanOrEqual(maxError);
}
}
}
});
test('delta encode roundtrips', () => {
const data = [
41476, -13450, -59451, -65102, -32493, -39078, -53884, 40784, 32081, -40422, 43421, 17184,
23042, 27548, -61705, -45215, -39037, 61611, -43945, 28001, -64417, -54192, -56325, 24401,
17735, 37464, -39842, 54964, 14469, -47248, -39450,
];
const roundtripped = deltaDecode(deltaEncode(data));
expect(data).toEqual(roundtripped);
});
test('run length encode roundtrips', () => {
const datasets = [
// No repetitions.
[
41476, -13450, -59451, -65102, -32493, -39078, -53884, 40784, 32081, -40422, 43421, 17184,
23042, 27548, -61705, -45215, -39037, 61611, -43945, 28001, -64417, -54192, -56325, 24401,
17735, 37464, -39842, 54964, 14469, -47248, -39450,
],
// All repetitions.
[10, 10, 10, 10, 10, 10],
// Just one value.
[11],
// Repetitions in the middle of unique values.
[1, 2, 3, 4, 4, 4, 4, 5, 6, 7],
];
for (const data of datasets) {
const roundtripped = runLengthDecode(runLengthEncode(data));
expect(data).toEqual(roundtripped);
}
});
});
|