-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteger.test.ts
More file actions
132 lines (117 loc) · 3 KB
/
integer.test.ts
File metadata and controls
132 lines (117 loc) · 3 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { assertEquals, assertThrows } from '@std/assert';
import { PLInteger, PLTYPE_INTEGER } from './integer.ts';
import { PLReal } from './real.ts';
Deno.test('initial value', () => {
assertEquals(new PLInteger().value, 0n);
assertEquals(new PLInteger(42n).value, 42n);
});
Deno.test('set value', () => {
const pl = new PLInteger();
pl.value = -42n;
assertEquals(pl.value, -42n);
assertEquals(pl.bits, 64);
pl.bits = 128;
assertEquals(pl.bits, 128);
assertEquals(pl.value, -42n);
pl.value = 0x18171615141312110807060504030201n;
assertEquals(pl.value, 0x18171615141312110807060504030201n);
pl.bits = 64;
assertEquals(pl.bits, 64);
assertEquals(pl.value, 0x0807060504030201n);
pl.bits = 32;
assertEquals(pl.bits, 32);
assertEquals(pl.value, 0x04030201n);
pl.bits = 16;
assertEquals(pl.bits, 16);
assertEquals(pl.value, 0x0201n);
pl.bits = 8;
assertEquals(pl.bits, 8);
assertEquals(pl.value, 0x01n);
});
Deno.test('round bits', () => {
for (const bits of [32.1, 32.5, 32.9]) {
assertEquals(new PLInteger(0n, bits as 32).bits, 32);
}
});
Deno.test('bad bits', () => {
for (const bits of [0, -1, NaN, Infinity, -Infinity, 33]) {
const tag = `bits: ${bits}`;
assertThrows(
() => {
new PLInteger(0n, bits as 32);
},
RangeError,
'Invalid bits',
tag,
);
const pl = new PLInteger(0x1122334455n);
assertThrows(
() => {
pl.bits = bits as 32;
},
RangeError,
'Invalid bits',
tag,
);
assertEquals(pl.bits, 64, tag);
assertEquals(pl.value, 0x1122334455n, tag);
}
});
Deno.test('value wrap', () => {
const pl = new PLInteger();
for (const bits of [8, 16, 32, 64, 128] as const) {
pl.bits = bits;
const max = (1n << BigInt(bits - 1)) - 1n;
const min = -(max + 1n);
for (
const [i, w] of [
[0n, 0n],
[1n, 1n],
[2n, 2n],
[max, max],
[max + 1n, min],
[max + 2n, min + 1n],
[min, min],
[min - 1n, max],
[min - 2n, max - 1n],
]
) {
assertEquals(new PLInteger(i, bits).value, w, `${i} -> ${w}`);
pl.value = i;
assertEquals(pl.value, w, `${i} -> ${w}`);
}
}
});
Deno.test('valueOf', () => {
const pl = new PLInteger();
assertEquals(pl.valueOf(), 0n);
pl.value = 42n;
assertEquals(pl.valueOf(), 42n);
pl.value = -42n;
assertEquals(pl.valueOf(), -42n);
});
Deno.test('toString', () => {
const pl = new PLInteger();
assertEquals(pl.valueOf(), 0n);
pl.value = 42n;
assertEquals(pl.toString(), '42');
pl.value = -42n;
assertEquals(pl.toString(), '-42');
});
Deno.test('is type', () => {
assertEquals(new PLInteger().type, PLTYPE_INTEGER);
assertEquals(new PLInteger()[Symbol.toStringTag], PLTYPE_INTEGER);
assertEquals(
Object.prototype.toString.call(new PLInteger()),
`[object ${PLTYPE_INTEGER}]`,
);
assertEquals(PLInteger.is(new PLInteger()), true);
assertEquals(PLInteger.is(new PLReal()), false);
assertEquals(PLInteger.is({}), false);
assertEquals(PLInteger.is(null), false);
for (const v of [new PLInteger(), new PLReal(), {}, null]) {
if (PLInteger.is(v)) {
assertEquals(v.value, 0n);
}
}
});