-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathcodec-float.test.ts
72 lines (59 loc) · 2.25 KB
/
codec-float.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import assert from "assert";
import * as ieee754 from "ieee754";
import { decode } from "../src/index";
const FLOAT32_TYPE = 0xca;
const FLOAT64_TYPE = 0xcb;
const SPECS = {
POSITIVE_ZERO: +0.0,
NEGATIVE_ZERO: -0.0,
POSITIVE_INFINITY: Number.POSITIVE_INFINITY,
NEGATIVE_INFINITY: Number.NEGATIVE_INFINITY,
POSITIVE_VALUE_1: +0.1,
POSITIVE_VALUE_2: +42,
POSITIVE_VALUE_3: +Math.PI,
POSITIVE_VALUE_4: +Math.E,
NEGATIVE_VALUE_1: -0.1,
NEGATIVE_VALUE_2: -42,
NEGATIVE_VALUE_3: -Math.PI,
NEGATIVE_VALUE_4: -Math.E,
MAX_SAFE_INTEGER: Number.MAX_SAFE_INTEGER,
MIN_SAFE_INTEGER: Number.MIN_SAFE_INTEGER,
MAX_VALUE: Number.MAX_VALUE,
MIN_VALUE: Number.MIN_VALUE,
} as Record<string, number>;
describe("codec: float 32/64", () => {
context("float 32", () => {
for (const [name, value] of Object.entries(SPECS)) {
it(`decodes ${name} (${value})`, () => {
const buf = new Uint8Array(4);
ieee754.write(buf, value, 0, false, 23, 4);
const expected = ieee754.read(buf, 0, false, 23, 4);
assert.deepStrictEqual(decode([FLOAT32_TYPE, ...buf]), expected, "matched sign");
assert.notDeepStrictEqual(decode([FLOAT32_TYPE, ...buf]), -expected, "unmatched sign");
});
}
it(`decodes NaN`, () => {
const buf = new Uint8Array(4);
ieee754.write(buf, NaN, 0, false, 23, 4);
const expected = ieee754.read(buf, 0, false, 23, 4);
assert.deepStrictEqual(decode([FLOAT32_TYPE, ...buf]), expected, "matched sign");
});
});
context("float 64", () => {
for (const [name, value] of Object.entries(SPECS)) {
it(`decodes ${name} (${value})`, () => {
const buf = new Uint8Array(8);
ieee754.write(buf, value, 0, false, 52, 8);
const expected = ieee754.read(buf, 0, false, 52, 8);
assert.deepStrictEqual(decode([FLOAT64_TYPE, ...buf]), expected, "matched sign");
assert.notDeepStrictEqual(decode([FLOAT64_TYPE, ...buf]), -expected, "unmatched sign");
});
}
it(`decodes NaN`, () => {
const buf = new Uint8Array(8);
ieee754.write(buf, NaN, 0, false, 52, 8);
const expected = ieee754.read(buf, 0, false, 52, 8);
assert.deepStrictEqual(decode([FLOAT64_TYPE, ...buf]), expected, "matched sign");
});
});
});