Skip to content

Commit

Permalink
Add tests for TS brotli decoder
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 527326003
  • Loading branch information
eustas committed Jul 4, 2023
1 parent efe140a commit 11abde4
Show file tree
Hide file tree
Showing 6 changed files with 2,472 additions and 51 deletions.
6 changes: 3 additions & 3 deletions js/bundle_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const CRC_64_POLY = new Uint32Array([0xD7870F42, 0xC96C5795]);
* @return {string} footprint
*/
function calculateCrc64(data) {
let crc = new Uint32Array([0xFFFFFFFF, 0xFFFFFFFF]);
let c = new Uint32Array(2);
const crc = new Uint32Array([0xFFFFFFFF, 0xFFFFFFFF]);
const c = new Uint32Array(2);
for (let i = 0; i < data.length; ++i) {
c[1] = 0;
c[0] = (crc[0] ^ data[i]) & 0xFF;
Expand Down Expand Up @@ -58,7 +58,7 @@ function checkEntry(entry, data) {

describe("BundleTest", () => {
const testData = makeTestData();
for (let entry in testData) {
for (const entry in testData) {
if (!testData.hasOwnProperty(entry)) {
continue;
}
Expand Down
63 changes: 63 additions & 0 deletions js/bundle_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Copyright 2023 Google Inc. All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/
import {brotliDecode} from "./decode";
import {makeTestData} from "./test_data";

const CRC_64_POLY = new Uint32Array([0xD7870F42, 0xC96C5795]);

/**
* Calculates binary data footprint.
*/
function calculateCrc64(data: Int8Array): string {
const crc = new Uint32Array([0xFFFFFFFF, 0xFFFFFFFF]);
const c = new Uint32Array(2);
for (let i = 0; i < data.length; ++i) {
c[1] = 0;
c[0] = (crc[0] ^ data[i]) & 0xFF;
for (let k = 0; k < 8; ++k) {
const isOdd = c[0] & 1;
c[0] = (c[0] >>> 1) | ((c[1] & 1) << 31);
c[1] = c[1] >>> 1;
if (isOdd) {
c[0] = c[0] ^ CRC_64_POLY[0];
c[1] = c[1] ^ CRC_64_POLY[1];
}
}
crc[0] = ((crc[0] >>> 8) | ((crc[1] & 0xFF) << 24)) ^ c[0];
crc[1] = (crc[1] >>> 8) ^ c[1];
}
crc[0] = ~crc[0];
crc[1] = ~crc[1];

let lo = crc[0].toString(16);
lo = "0".repeat(8 - lo.length) + lo;
let hi = crc[1].toString(16);
hi = "0".repeat(8 - hi.length) + hi;

return hi + lo;
}

/**
* Decompresses data and checks that output footprint is correct.
*/
function checkEntry(entry: string, data: Int8Array) {
const expectedCrc = entry.substring(0, 16);
const decompressed = brotliDecode(data);
const crc = calculateCrc64(decompressed);
expect(expectedCrc).toEqual(crc);
}

describe("BundleTest", () => {
const testData = makeTestData();
for (const entry in testData) {
if (!testData.hasOwnProperty(entry)) {
continue;
}
const name = entry.substring(17);
const data = testData[entry];
it('test_' + name, checkEntry.bind(null, entry, data));
}
});
Loading

0 comments on commit 11abde4

Please sign in to comment.