Skip to content

Commit c846d0d

Browse files
authored
Merge pull request #3 from jasonpaulos/additional-int-testing
Additional int testing
2 parents 6039e48 + 4f73b37 commit c846d0d

File tree

3 files changed

+106
-333
lines changed

3 files changed

+106
-333
lines changed

test/bigint64.test.ts

-304
Original file line numberDiff line numberDiff line change
@@ -49,307 +49,3 @@ describe("useBigInt64: true", () => {
4949
assert.deepStrictEqual(decoded, value);
5050
});
5151
});
52-
53-
interface TestCase {
54-
input: bigint;
55-
expected: Map<IntMode, number | bigint | "error">;
56-
}
57-
58-
// declared as a function to delay referencing the BigInt constructor
59-
function BIGINTSPECS(): Record<string, TestCase> {
60-
return {
61-
ZERO: {
62-
input: BigInt(0),
63-
expected: new Map<IntMode, number | bigint>([
64-
[IntMode.UNSAFE_NUMBER, 0],
65-
[IntMode.SAFE_NUMBER, 0],
66-
[IntMode.MIXED, 0],
67-
[IntMode.BIGINT, BigInt(0)],
68-
]),
69-
},
70-
ONE: {
71-
input: BigInt(1),
72-
expected: new Map<IntMode, number | bigint>([
73-
[IntMode.UNSAFE_NUMBER, 1],
74-
[IntMode.SAFE_NUMBER, 1],
75-
[IntMode.MIXED, 1],
76-
[IntMode.BIGINT, BigInt(1)],
77-
]),
78-
},
79-
MINUS_ONE: {
80-
input: BigInt(-1),
81-
expected: new Map<IntMode, number | bigint>([
82-
[IntMode.UNSAFE_NUMBER, -1],
83-
[IntMode.SAFE_NUMBER, -1],
84-
[IntMode.MIXED, -1],
85-
[IntMode.BIGINT, BigInt(-1)],
86-
]),
87-
},
88-
X_FF: {
89-
input: BigInt(0xff),
90-
expected: new Map<IntMode, number | bigint>([
91-
[IntMode.UNSAFE_NUMBER, 0xff],
92-
[IntMode.SAFE_NUMBER, 0xff],
93-
[IntMode.MIXED, 0xff],
94-
[IntMode.BIGINT, BigInt(0xff)],
95-
]),
96-
},
97-
MINUS_X_FF: {
98-
input: BigInt(-0xff),
99-
expected: new Map<IntMode, number | bigint>([
100-
[IntMode.UNSAFE_NUMBER, -0xff],
101-
[IntMode.SAFE_NUMBER, -0xff],
102-
[IntMode.MIXED, -0xff],
103-
[IntMode.BIGINT, BigInt(-0xff)],
104-
]),
105-
},
106-
INT32_MAX: {
107-
input: BigInt(0x7fffffff),
108-
expected: new Map<IntMode, number | bigint>([
109-
[IntMode.UNSAFE_NUMBER, 0x7fffffff],
110-
[IntMode.SAFE_NUMBER, 0x7fffffff],
111-
[IntMode.MIXED, 0x7fffffff],
112-
[IntMode.BIGINT, BigInt(0x7fffffff)],
113-
]),
114-
},
115-
INT32_MIN: {
116-
input: BigInt(-0x7fffffff - 1),
117-
expected: new Map<IntMode, number | bigint>([
118-
[IntMode.UNSAFE_NUMBER, -0x7fffffff - 1],
119-
[IntMode.SAFE_NUMBER, -0x7fffffff - 1],
120-
[IntMode.MIXED, -0x7fffffff - 1],
121-
[IntMode.BIGINT, BigInt(-0x7fffffff - 1)],
122-
]),
123-
},
124-
MAX_SAFE_INTEGER: {
125-
input: BigInt(Number.MAX_SAFE_INTEGER),
126-
expected: new Map<IntMode, number | bigint>([
127-
[IntMode.UNSAFE_NUMBER, Number.MAX_SAFE_INTEGER],
128-
[IntMode.SAFE_NUMBER, Number.MAX_SAFE_INTEGER],
129-
[IntMode.MIXED, Number.MAX_SAFE_INTEGER],
130-
[IntMode.BIGINT, BigInt(Number.MAX_SAFE_INTEGER)],
131-
]),
132-
},
133-
MAX_SAFE_INTEGER_PLUS_ONE: {
134-
input: BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1),
135-
expected: new Map<IntMode, number | bigint | "error">([
136-
// exclude IntMode.UNSAFE_NUMBER, behavior will not be exact
137-
[IntMode.SAFE_NUMBER, "error"],
138-
[IntMode.MIXED, BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1)],
139-
[IntMode.BIGINT, BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1)],
140-
]),
141-
},
142-
MIN_SAFE_INTEGER: {
143-
input: BigInt(Number.MIN_SAFE_INTEGER),
144-
expected: new Map<IntMode, number | bigint>([
145-
[IntMode.UNSAFE_NUMBER, Number.MIN_SAFE_INTEGER],
146-
[IntMode.SAFE_NUMBER, Number.MIN_SAFE_INTEGER],
147-
[IntMode.MIXED, Number.MIN_SAFE_INTEGER],
148-
[IntMode.BIGINT, BigInt(Number.MIN_SAFE_INTEGER)],
149-
]),
150-
},
151-
MIN_SAFE_INTEGER_MINUS_ONE: {
152-
input: BigInt(Number.MIN_SAFE_INTEGER) - BigInt(1),
153-
expected: new Map<IntMode, number | bigint | "error">([
154-
// exclude IntMode.UNSAFE_NUMBER, behavior will not be exact
155-
[IntMode.SAFE_NUMBER, "error"],
156-
[IntMode.MIXED, BigInt(Number.MIN_SAFE_INTEGER) - BigInt(1)],
157-
[IntMode.BIGINT, BigInt(Number.MIN_SAFE_INTEGER) - BigInt(1)],
158-
]),
159-
},
160-
INT64_MAX: {
161-
input: BigInt("0x7fffffffffffffff"),
162-
expected: new Map<IntMode, number | bigint | "error">([
163-
// exclude IntMode.UNSAFE_NUMBER, behavior will not be exact
164-
[IntMode.SAFE_NUMBER, "error"],
165-
[IntMode.MIXED, BigInt("0x7fffffffffffffff")],
166-
[IntMode.BIGINT, BigInt("0x7fffffffffffffff")],
167-
]),
168-
},
169-
INT64_MIN: {
170-
input: BigInt(-1) * BigInt("0x8000000000000000"),
171-
expected: new Map<IntMode, number | bigint | "error">([
172-
// exclude IntMode.UNSAFE_NUMBER, behavior will not be exact
173-
[IntMode.SAFE_NUMBER, "error"],
174-
[IntMode.MIXED, BigInt(-1) * BigInt("0x8000000000000000")],
175-
[IntMode.BIGINT, BigInt(-1) * BigInt("0x8000000000000000")],
176-
]),
177-
},
178-
};
179-
}
180-
181-
// declared as a function to delay referencing the BigInt constructor
182-
function BIGUINTSPECS(): Record<string, TestCase> {
183-
return {
184-
ZERO: {
185-
input: BigInt(0),
186-
expected: new Map<IntMode, number | bigint>([
187-
[IntMode.UNSAFE_NUMBER, 0],
188-
[IntMode.SAFE_NUMBER, 0],
189-
[IntMode.MIXED, 0],
190-
[IntMode.BIGINT, BigInt(0)],
191-
]),
192-
},
193-
ONE: {
194-
input: BigInt(1),
195-
expected: new Map<IntMode, number | bigint>([
196-
[IntMode.UNSAFE_NUMBER, 1],
197-
[IntMode.SAFE_NUMBER, 1],
198-
[IntMode.MIXED, 1],
199-
[IntMode.BIGINT, BigInt(1)],
200-
]),
201-
},
202-
X_FF: {
203-
input: BigInt(0xff),
204-
expected: new Map<IntMode, number | bigint>([
205-
[IntMode.UNSAFE_NUMBER, 0xff],
206-
[IntMode.SAFE_NUMBER, 0xff],
207-
[IntMode.MIXED, 0xff],
208-
[IntMode.BIGINT, BigInt(0xff)],
209-
]),
210-
},
211-
UINT32_MAX: {
212-
input: BigInt(0xffffffff),
213-
expected: new Map<IntMode, number | bigint>([
214-
[IntMode.UNSAFE_NUMBER, 0xffffffff],
215-
[IntMode.SAFE_NUMBER, 0xffffffff],
216-
[IntMode.MIXED, 0xffffffff],
217-
[IntMode.BIGINT, BigInt(0xffffffff)],
218-
]),
219-
},
220-
MAX_SAFE_INTEGER: {
221-
input: BigInt(Number.MAX_SAFE_INTEGER),
222-
expected: new Map<IntMode, number | bigint>([
223-
[IntMode.UNSAFE_NUMBER, Number.MAX_SAFE_INTEGER],
224-
[IntMode.SAFE_NUMBER, Number.MAX_SAFE_INTEGER],
225-
[IntMode.MIXED, Number.MAX_SAFE_INTEGER],
226-
[IntMode.BIGINT, BigInt(Number.MAX_SAFE_INTEGER)],
227-
]),
228-
},
229-
MAX_SAFE_INTEGER_PLUS_ONE: {
230-
input: BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1),
231-
expected: new Map<IntMode, number | bigint | "error">([
232-
// exclude IntMode.UNSAFE_NUMBER, behavior will not be exact
233-
[IntMode.SAFE_NUMBER, "error"],
234-
[IntMode.MIXED, BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1)],
235-
[IntMode.BIGINT, BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1)],
236-
]),
237-
},
238-
UINT64_MAX: {
239-
input: BigInt("0xffffffffffffffff"),
240-
expected: new Map<IntMode, number | bigint | "error">([
241-
// exclude IntMode.UNSAFE_NUMBER, behavior will not be exact
242-
[IntMode.SAFE_NUMBER, "error"],
243-
[IntMode.MIXED, BigInt("0xffffffffffffffff")],
244-
[IntMode.BIGINT, BigInt("0xffffffffffffffff")],
245-
]),
246-
},
247-
};
248-
}
249-
250-
function abs(value: bigint): bigint {
251-
if (value < 0) {
252-
return BigInt(-1) * value;
253-
}
254-
return value;
255-
}
256-
257-
const extensionCodec = new ExtensionCodec();
258-
extensionCodec.register({
259-
type: 0,
260-
encode: (input: unknown) => {
261-
if (typeof input === "bigint") {
262-
if (input <= Number.MAX_SAFE_INTEGER && input >= Number.MIN_SAFE_INTEGER) {
263-
return encode(parseInt(input.toString(), 10));
264-
} else {
265-
return encode(input.toString());
266-
}
267-
} else {
268-
return null;
269-
}
270-
},
271-
decode: (data: Uint8Array) => {
272-
const val = decode(data);
273-
if (!(typeof val === "string" || typeof val === "number")) {
274-
throw new DecodeError(`unexpected BigInt source: ${val} (${typeof val})`);
275-
}
276-
return BigInt(val);
277-
},
278-
});
279-
context("extension", () => {
280-
it("encodes and decodes 0n", () => {
281-
const value = BigInt(0);
282-
const encoded = encode(value, { extensionCodec });
283-
assert.deepStrictEqual(decode(encoded, { extensionCodec }), value);
284-
});
285-
286-
it("encodes and decodes MAX_SAFE_INTEGER+1", () => {
287-
const value = BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1);
288-
const encoded = encode(value, { extensionCodec });
289-
assert.deepStrictEqual(decode(encoded, { extensionCodec }), value);
290-
});
291-
292-
it("encodes and decodes MIN_SAFE_INTEGER-1", () => {
293-
const value = BigInt(Number.MIN_SAFE_INTEGER) - BigInt(1);
294-
const encoded = encode(value, { extensionCodec });
295-
assert.deepStrictEqual(decode(encoded, { extensionCodec }), value);
296-
});
297-
});
298-
299-
context("native", () => {
300-
context("int 64", () => {
301-
const specs = BIGINTSPECS();
302-
303-
for (const name of Object.keys(specs)) {
304-
const testCase = specs[name]!;
305-
306-
it(`sets and gets ${testCase.input} (${testCase.input < 0 ? "-" : ""}0x${abs(testCase.input).toString(
307-
16,
308-
)})`, () => {
309-
const b = new Uint8Array(8);
310-
const view = new DataView(b.buffer);
311-
view.setBigInt64(0, testCase.input);
312-
for (const [mode, expected] of testCase.expected) {
313-
if (expected === "error") {
314-
assert.throws(
315-
() => getInt64(view, 0, mode),
316-
new RegExp(
317-
`Mode is IntMode\\.SAFE_NUMBER and value is not a safe integer: ${testCase.input < 0 ? "-" : ""}0x${abs(
318-
testCase.input,
319-
).toString(16)}$`,
320-
),
321-
);
322-
continue;
323-
}
324-
assert.deepStrictEqual(getInt64(view, 0, mode), expected);
325-
}
326-
});
327-
}
328-
});
329-
330-
context("uint 64", () => {
331-
const specs = BIGUINTSPECS();
332-
333-
for (const name of Object.keys(specs)) {
334-
const testCase = specs[name]!;
335-
336-
it(`sets and gets ${testCase.input} (0x${testCase.input.toString(16)})`, () => {
337-
const b = new Uint8Array(8);
338-
const view = new DataView(b.buffer);
339-
view.setBigUint64(0, testCase.input);
340-
for (const [mode, expected] of testCase.expected) {
341-
if (expected === "error") {
342-
assert.throws(
343-
() => getUint64(view, 0, mode),
344-
new RegExp(
345-
`Mode is IntMode\\.SAFE_NUMBER and value is not a safe integer: 0x${testCase.input.toString(16)}$`,
346-
),
347-
);
348-
continue;
349-
}
350-
assert.deepStrictEqual(getUint64(view, 0, mode), expected);
351-
}
352-
});
353-
}
354-
});
355-
});

0 commit comments

Comments
 (0)