Skip to content

Commit f32db1a

Browse files
committed
tests: more tests
* added tests about the int's and float's bound * unified tests about str and bin
1 parent dec3b0f commit f32db1a

File tree

1 file changed

+58
-11
lines changed

1 file changed

+58
-11
lines changed

tests/primitives.test.ts

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,70 @@ describe("bool", () => {
2020
test("false", primitive(false));
2121
});
2222

23+
function maxIntOf(signed: boolean, bits: 8 | 16 | 24 | 32): number;
24+
function maxIntOf(signed: boolean, bits: number): number | bigint;
25+
26+
function maxIntOf(signed: boolean, bits: number) {
27+
const ibits = signed ? bits - 1 : bits;
28+
29+
if (ibits >= 52) {
30+
return 2n ** BigInt(ibits) - 1n;
31+
} else {
32+
return 2 ** ibits - 1;
33+
}
34+
}
35+
36+
function minIntOf(signed: boolean, bits: number) {
37+
if (!signed) {
38+
return 0;
39+
}
40+
41+
const ibits = signed ? bits - 1 : bits;
42+
43+
if (ibits >= 52) {
44+
return -(2n ** BigInt(ibits));
45+
} else {
46+
return -(2 ** ibits);
47+
}
48+
}
49+
2350
describe("int", () => {
2451
test("0", primitive(0));
2552
test("1", primitive(1));
2653
test("-1", primitive(-1));
2754

28-
test("i64max", primitive(2n ** 63n - 1n));
29-
test("i64min", primitive(-(2n ** 63n)));
55+
for (const [signed, bits] of [8, 16, 32, 64].flatMap((x) => [
56+
[true, x] as const,
57+
[false, x] as const,
58+
])) {
59+
test(`${signed ? "i" : "u"}${bits}max`, primitive(maxIntOf(signed, bits)));
60+
if (signed) {
61+
// No need to test 0 - they will be rewrited as fixint 0
62+
test(`i${bits}min`, primitive(minIntOf(true, bits)));
63+
}
64+
}
3065

3166
test("f64maxint", primitive(Number.MAX_SAFE_INTEGER));
3267
test("f64minint", primitive(Number.MIN_SAFE_INTEGER));
3368
});
3469

3570
describe("float", () => {
3671
test("0.5", primitive(0.5));
37-
test("1.5", primitive(1.0));
72+
test("-0.5", primitive(-0.5));
73+
test("f32maxint - 0.5", primitive(maxIntOf(true, 24) - 0.5));
74+
test("f32minint + 0.5", primitive(maxIntOf(true, 24) + 0.5));
3875
test("f64max", primitive(Number.MAX_VALUE));
3976
test("f64min", primitive(Number.MIN_VALUE));
4077
});
4178

79+
const TEST_NBYTES = [
80+
0,
81+
12,
82+
maxIntOf(false, 8),
83+
maxIntOf(false, 16),
84+
maxIntOf(false, 16) * 2,
85+
]; // We could not test too large binary...
86+
4287
function makeStr(bytes: number) {
4388
const strs = [] as string[];
4489
for (let i = 0; i < bytes; i++) {
@@ -47,20 +92,22 @@ function makeStr(bytes: number) {
4792
return strs.join();
4893
}
4994

95+
const LARGEST_STR = makeStr(Math.max(...TEST_NBYTES));
96+
5097
describe("str", () => {
51-
test("0c", primitive(makeStr(0)));
52-
test("12c", primitive("Hello World!"));
53-
test("16384c", primitive(makeStr(16384)));
54-
test("32698c", primitive(makeStr(16384)));
98+
for (const nbytes of TEST_NBYTES) {
99+
test(`${nbytes}c`, primitive(LARGEST_STR.slice(0, nbytes)));
100+
}
55101
});
56102

57103
function makeBin(bytes: number) {
58104
return crypto.getRandomValues(new Uint8Array(bytes));
59105
}
60106

107+
const LARGEST_BIN = makeBin(Math.max(...TEST_NBYTES));
108+
61109
describe("bin", () => {
62-
test("0b", primitive(makeBin(0)));
63-
test("12b", primitive(makeBin(12)));
64-
test("16384b", primitive(makeBin(16384)));
65-
test("32698b", primitive(makeBin(32698)));
110+
for (const nbytes of TEST_NBYTES) {
111+
test(`${nbytes}c`, primitive(LARGEST_BIN.slice(0, nbytes)));
112+
}
66113
});

0 commit comments

Comments
 (0)