From 1edddbca29ca66ff698c8f62782f6a3473bc04c5 Mon Sep 17 00:00:00 2001 From: Martin Zmitko Date: Jul 15 2023 19:47:53 +0000 Subject: Add unit tests for JS and WASM CRC16 consistency --- diff --git a/tests/unit_tests/config/global.json b/tests/unit_tests/config/global.json index 9ac8ab9..c112433 100644 --- a/tests/unit_tests/config/global.json +++ b/tests/unit_tests/config/global.json @@ -318,7 +318,7 @@ { "from": "./crc16.js", "objects": [ - "CRC16" + "crc16" ] }, { diff --git a/tests/unit_tests/tests/crc16_tests.js b/tests/unit_tests/tests/crc16_tests.js new file mode 100644 index 0000000..c733931 --- /dev/null +++ b/tests/unit_tests/tests/crc16_tests.js @@ -0,0 +1,85 @@ +describe("Function CRC16", function() { + function checkEqualInt(data) { + wasm.set(data); + const crcWASM = wasm.crc16(data.byteLength); + + const crc = new CRC16(); + crc.next(data); + const crcJS = crc.crc; + + expect(crcWASM).toEqual(crcJS); + } + + function checkEqualFloat(data) { + expect(wasm.grow(data.byteLength)).toBe(true); + wasm.set(data, 0, true); + const crcWASM = wasm.crc16Float(data.byteLength); + + const crc = new CRC16(); + const MAXUINT32 = 4294967295; + for (let i = 0; i < data.length; i++) { + crc.single(data[i] * MAXUINT32); + } + const crcJS = crc.crc; + + expect(crcWASM).toEqual(crcJS); + } + + var wasm = {ready: false}; + eval(crc16); + + beforeAll(function initWASM(done) { // init WASM module before tests + let code = insert_wasm_code("// WASM_CODE //"); + // replace debug message with done() call to signal that the module is ready + code = code.replace('console.debug("WASM farbling module initialized");', 'done();'); + eval(code); + }); + + it("should provide consistent results for both JS and WASM implementations for empty byte data.",function() { + expect(wasm.ready).toBe(true); + const data = new Uint8Array(40000); + checkEqualInt(data); + }); + it("should provide consistent results for both JS and WASM implementations for constant byte data.",function() { + expect(wasm.ready).toBe(true); + const data = new Uint8Array(40000); + for (let i = 0; i < data.length; i++) { + data[i] = i % 256; + } + checkEqualInt(data); + }); + it("should provide consistent results for both JS and WASM implemenations for random byte data.", function() { + expect(wasm.ready).toBe(true); + for (let i = 0; i < 1; i++) { + const randomData = new Uint8Array(40000); + for (let i = 0; i < randomData.length; i++) { + randomData[i] = Math.random() * 256; + } + checkEqualInt(randomData); + } + }); + + it("should provide consistent results for both JS and WASM implementations for empty float data.",function() { + expect(wasm.ready).toBe(true); + const data = new Float32Array(40000); + checkEqualFloat(data); + }); + it("should provide consistent results for both JS and WASM implementations for constant float data.",function() { + expect(wasm.ready).toBe(true); + const data = new Float32Array(40000); + for (let i = 0; i < data.length; i++) { + data[i] = (i % 256) / 256; + } + checkEqualFloat(data); + }); + it("should provide consistent results for both JS and WASM implemenations for random float data.", function() { + expect(wasm.ready).toBe(true); + for (let i = 0; i < 5; i++) { + const randomData = new Float32Array(10000); + for (let i = 0; i < randomData.length; i++) { + randomData[i] = Math.random() * 2 - 1; + } + checkEqualFloat(randomData); + } + }); +});