This repository was archived by the owner on Jan 22, 2025. It is now read-only.
forked from msgpack/msgpack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodec-float.test.ts
More file actions
77 lines (61 loc) · 2.28 KB
/
codec-float.test.ts
File metadata and controls
77 lines (61 loc) · 2.28 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import assert from "assert";
import { decode } from "../src";
const ieee754 = require("ieee754");
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 of Object.keys(SPECS)) {
const value = SPECS[name];
it(`decodes ${name} (${value})`, () => {
const buf: Array<number> = [];
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: Array<number> = [];
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 of Object.keys(SPECS)) {
const value = SPECS[name];
it(`decodes ${name} (${value})`, () => {
const buf: Array<number> = [];
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: Array<number> = [];
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");
});
});
});