-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathcodec-bigint.test.ts
44 lines (39 loc) · 1.25 KB
/
codec-bigint.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import assert from "assert";
import { encode, decode, ExtensionCodec } from "../src";
const extensionCodec = new ExtensionCodec();
extensionCodec.register({
type: 0,
encode: (input: unknown) => {
// eslint-disable-next-line valid-typeof
if (typeof input === "bigint") {
return encode(input.toString());
} else {
return null;
}
},
decode: (data: Uint8Array) => {
return BigInt(decode(data));
},
});
describe("codec BigInt", () => {
before(function () {
if (typeof BigInt === "undefined") {
this.skip();
}
});
it("encodes and decodes 0n", () => {
const value = BigInt(0);
const encoded = encode(value, { extensionCodec });
assert.deepStrictEqual(decode(encoded, { extensionCodec }), value);
});
it("encodes and decodes MAX_SAFE_INTEGER+1", () => {
const value = BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1);
const encoded = encode(value, { extensionCodec });
assert.deepStrictEqual(decode(encoded, { extensionCodec }), value);
});
it("encodes and decodes MIN_SAFE_INTEGER-1", () => {
const value = BigInt(Number.MIN_SAFE_INTEGER) - BigInt(1);
const encoded = encode(value, { extensionCodec });
assert.deepStrictEqual(decode(encoded, { extensionCodec }), value);
});
});