diff --git a/test/VexCRC.spec.ts b/test/VexCRC.spec.ts new file mode 100644 index 0000000..600046a --- /dev/null +++ b/test/VexCRC.spec.ts @@ -0,0 +1,26 @@ +import crc16ccitt from 'crc/calculators/crc16ccitt'; +import { CrcGenerator } from '../src'; + + +function getRandomInt(max: number) { + return Math.floor(Math.random() * max); +} + +function getRandomUInt8Array(size: number) { + let rtn: number[] = []; + for (let i = 0; i < size; i++) { + rtn.push(getRandomInt(256)); + } + return new Uint8Array(rtn); +} + +const crcgen = new CrcGenerator(); + +test("CRC 16 ccitt", async () => { + for (let i = 10; i < 100; i++) { + let test = getRandomUInt8Array(i); + if (crc16ccitt(test, 0) !== crcgen.crc16(test, 0)) { + fail(); + } + } +}); \ No newline at end of file diff --git a/test/VexEvent.spec.ts b/test/VexEvent.spec.ts new file mode 100644 index 0000000..3d9ae48 --- /dev/null +++ b/test/VexEvent.spec.ts @@ -0,0 +1,44 @@ +import { VexEventTarget } from "../src"; + +function getRandomInt(max: number) { + return Math.floor(Math.random() * max); +} + +test("VexEventEmitter and VexEventTarget", async () => { + let target = new VexEventTarget(); + + let fn1 = jest.fn(); + let fn2 = jest.fn(); + + target.remove("test1", fn1); + + target.emit("test1", undefined); + + + target.on("test", fn1); + + let r1 = getRandomInt(10) + 10; + for (let i = 0; i < r1; i++) target.emit("test", undefined); + + target.on("test", fn2); + + let r2 = getRandomInt(10) + 10; + for (let i = 0; i < r2; i++) target.emit("test", undefined); + + expect(fn1).toBeCalledTimes(r1 + r2); + expect(fn2).toBeCalledTimes(r2); + + target.remove("test", fn1); + + target.emit("test", undefined); + + expect(fn1).toBeCalledTimes(r1 + r2); + expect(fn2).toBeCalledTimes(r2 + 1); + + target.clearListeners(); + + target.emit("test", undefined); + + expect(fn1).toBeCalledTimes(r1 + r2); + expect(fn2).toBeCalledTimes(r2 + 1); +}); \ No newline at end of file diff --git a/test/VexFirmwareVersion.spec.ts b/test/VexFirmwareVersion.spec.ts new file mode 100644 index 0000000..49ab1de --- /dev/null +++ b/test/VexFirmwareVersion.spec.ts @@ -0,0 +1,40 @@ +import { VexFirmwareVersion } from "../src"; + +function getRandomInt(max: number) { + return Math.floor(Math.random() * max); +} + +test("Constructor, fromString, fromCatalogString and Uint8 related methods", async () => { + for (let i = 0; i < 1000; i++) { + let a = getRandomInt(256), b = getRandomInt(256), c = getRandomInt(256), d = getRandomInt(256); + + let subject = new VexFirmwareVersion(a, b, c, d); + let str = `${a}.${b}.${c}.b${d}`; + + expect(subject.toInternalString()).toBe(str); + expect(VexFirmwareVersion.fromString(str)).toEqual(subject); + expect(VexFirmwareVersion.fromCatalogString(`${a}_${b}_${c}_b${d}`)).toEqual(subject); + expect(VexFirmwareVersion.fromUint8Array(subject.toUint8Array(false)).toInternalString()).toBe(str); + expect(VexFirmwareVersion.fromUint8Array(subject.toUint8Array(true), 0, true).toInternalString()).toBe(str); + subject.beta = 0; + expect(VexFirmwareVersion.fromString(`${a}.${b}.${c}`)).toEqual(subject); + } +}); + +test("Compare", async () => { + let base = new VexFirmwareVersion(1, 2, 3, 4); + expect(base.compare(base)).toBe(0); + expect(new VexFirmwareVersion(1, 2, 3, 5).compare(base)).toBe(1); + expect(new VexFirmwareVersion(1, 2, 4, 5).compare(base)).toBe(1); + expect(new VexFirmwareVersion(1, 3, 4, 5).compare(base)).toBe(1); + expect(new VexFirmwareVersion(2, 3, 4, 5).compare(base)).toBe(1); +}); + +test("Is beta", async () => { + expect(VexFirmwareVersion.allZero().isBeta()).toBeFalsy(); + expect(new VexFirmwareVersion(0, 0, 0, 1).isBeta()).toBeTruthy(); +}); + +test("All zero", async () => { + expect(VexFirmwareVersion.allZero().toInternalString()).toBe("0.0.0.b0"); +});