-
Notifications
You must be signed in to change notification settings - Fork 0
/
16to32.test.js
41 lines (36 loc) · 1.23 KB
/
16to32.test.js
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
import test from "ava";
import { getFloat16, setFloat16 } from "../lib/index.js";
const float16Buffer = new ArrayBuffer(2);
const float16View = new DataView(float16Buffer);
function test16to32(t, littleEndian) {
for (let i = 0; i < 0xffff; i++) {
float16View.setUint16(0, i, littleEndian);
const f = getFloat16(float16View, 0, littleEndian);
// The only values that don't round-trip are NaN, which always get serialized as 0x7e00
// A value is NaN iff exponent === 0x7c00 AND mantissa !== 0
const exponent = i & 0x7c00;
const mantissa = i & 0x03ff;
if (exponent === 0x7c00 && mantissa !== 0) {
t.true(isNaN(f), "decode NaN value");
setFloat16(float16View, 0, f, littleEndian);
t.is(
float16View.getUint16(0, littleEndian),
0x7e00,
"encode all NaN values as 0x7e00",
);
} else {
setFloat16(float16View, 0, f, littleEndian);
t.is(
float16View.getUint16(0, littleEndian),
i,
"recover original uint16",
);
}
}
}
test("round-trip every 16-bit value to float64 and back (big-endian)", (t) => {
test16to32(t, false);
});
test("round-trip every 16-bit value to float64 and back (little-endian)", (t) => {
test16to32(t, true);
});