forked from Jerrylum/v5-serial-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}); |