forked from algorand/msgpack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbigint64.test.ts
More file actions
51 lines (45 loc) · 2.12 KB
/
bigint64.test.ts
File metadata and controls
51 lines (45 loc) · 2.12 KB
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
45
46
47
48
49
50
51
import assert from "assert";
import { encode, decode, IntMode, ExtensionCodec, DecodeError } from "../src";
import { getInt64, getUint64 } from "../src/utils/int";
describe("useBigInt64: true", () => {
before(function () {
if (typeof BigInt === "undefined") {
this.skip();
}
});
it("encodes and decodes 0n", () => {
const value = BigInt(0);
const encoded = encode(value, { forceBigIntToInt64: true });
assert.deepStrictEqual(decode(encoded, { useBigInt64: true }), value);
});
it("encodes and decodes MAX_SAFE_INTEGER+1", () => {
const value = BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1);
const encoded = encode(value, { forceBigIntToInt64: true });
assert.deepStrictEqual(decode(encoded, { useBigInt64: true }), value);
});
it("encodes and decodes MIN_SAFE_INTEGER-1", () => {
const value = BigInt(Number.MIN_SAFE_INTEGER) - BigInt(1);
const encoded = encode(value, { forceBigIntToInt64: true });
assert.deepStrictEqual(decode(encoded, { useBigInt64: true }), value);
});
it("encodes and decodes values with numbers and bigints - MIXED", () => {
const value = {
ints: [0, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER],
nums: [Number.NaN, Math.PI, Math.E, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
bigints: [BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1), BigInt(Number.MIN_SAFE_INTEGER) - BigInt(1)],
};
const encoded = encode(value, { forceBigIntToInt64: true });
const decoded = decode(encoded, { intMode: IntMode.MIXED });
assert.deepStrictEqual(decoded, value);
});
it("encodes and decodes values with numbers and bigints - AS_ENCODED", () => {
const value = {
ints: [0, Math.pow(2, 32) - 1, -1 * Math.pow(2, 31)],
nums: [Number.NaN, Math.PI, Math.E, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
bigints: [BigInt(0), BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1), BigInt(Number.MIN_SAFE_INTEGER) - BigInt(1)],
};
const encoded = encode(value, { forceBigIntToInt64: true });
const decoded = decode(encoded, { intMode: IntMode.AS_ENCODED });
assert.deepStrictEqual(decoded, value);
});
});