-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathpb-long.spec.ts
313 lines (240 loc) · 9.87 KB
/
pb-long.spec.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import {PbLong, PbULong} from "../src";
import {int64toString} from "../src/goog-varint";
describe('PbULong', function () {
it('can be constructed with bits', function () {
let bi = new PbULong(0, 1);
expect(bi.lo).toBe(0);
expect(bi.hi).toBe(1);
});
it('should from() max unsigned int64 string', function () {
let str = '18446744073709551615';
let uLong = PbULong.from(str);
expect(uLong.lo).toBe(-1);
expect(uLong.hi).toBe(-1);
});
it('should from() max unsigned int64 native bigint', function () {
if (globalThis.BigInt === undefined)
pending('No BigInt support on current platform');
// @ts-ignore
let uLong = PbULong.from(18446744073709551615n);
expect(uLong.lo).toBe(-1);
expect(uLong.hi).toBe(-1);
});
it('should from() max safe integer number', function () {
let uLong = PbULong.from(Number.MAX_SAFE_INTEGER);
expect(uLong.lo).toBe(-1);
expect(uLong.hi).toBe(2097151);
expect(uLong.toNumber()).toBe(Number.MAX_SAFE_INTEGER);
});
it('should toString() max unsigned int64', function () {
let uLong = new PbULong(-1, -1);
expect(uLong.toString()).toBe('18446744073709551615');
});
it('should toBigInt()', function () {
if (globalThis.BigInt === undefined)
pending('No BigInt support on current platform');
let uLong = new PbULong(-1, -1);
// @ts-ignore
expect(uLong.toBigInt()).toBe(18446744073709551615n);
});
it('should fail from() max unsigned int64 native bigint', function () {
if (globalThis.BigInt === undefined)
pending('No BigInt support on current platform');
// @ts-ignore
expect(() => PbULong.from(18446744073709551615n + 10n)).toThrowError("ulong too large");
});
it('should fail invalid from() value', function () {
expect(() => PbULong.from("")).toThrowError();
expect(() => PbULong.from("0.75")).toThrowError();
expect(() => PbULong.from("1,000")).toThrowError();
expect(() => PbULong.from("1-000")).toThrowError();
expect(() => PbULong.from("-1")).toThrowError();
expect(() => PbULong.from(-1)).toThrowError();
expect(() => PbULong.from(Number.NaN)).toThrowError();
expect(() => PbULong.from(Number.POSITIVE_INFINITY)).toThrowError();
});
it('0 has lo = 0, hi = 0', function () {
let ulong = PbULong.from(0);
expect(ulong.hi).toBe(0);
expect(ulong.lo).toBe(0);
});
it('int64toString should serialize the same was as BigInt.toString()', function () {
let ulong = PbULong.from(1661324400000);
expect(ulong.hi).toBe(386);
expect(ulong.lo).toBe(-827943552);
expect(ulong.toString()).toBe('1661324400000')
expect(int64toString(ulong.lo, ulong.hi)).toBe('1661324400000')
});
});
describe('PbLong', function () {
it('can be constructed with bits', function () {
let bi = new PbLong(0, 1);
expect(bi.lo).toBe(0);
expect(bi.hi).toBe(1);
});
it('should from() max signed int64 string', function () {
let str = '9223372036854775807';
let uLong = PbLong.from(str);
expect(uLong.lo).toBe(-1);
expect(uLong.hi).toBe(2147483647);
expect(uLong.toString()).toBe("9223372036854775807");
});
it('should from() min signed int64 string', function () {
let str = '-9223372036854775808';
let uLong = PbLong.from(str);
expect(uLong.lo).toBe(0);
expect(uLong.hi).toBe(-2147483648);
});
it('should toString() min signed int64', function () {
let uLong = new PbLong(0, -2147483648);
expect(uLong.toString()).toBe('-9223372036854775808');
});
it('should from() max safe integer number', function () {
let uLong = PbLong.from(Number.MAX_SAFE_INTEGER);
expect(uLong.lo).toBe(-1);
expect(uLong.hi).toBe(2097151);
expect(uLong.toNumber()).toBe(Number.MAX_SAFE_INTEGER);
});
it('should from() min safe integer number', function () {
let uLong = PbLong.from(Number.MIN_SAFE_INTEGER);
expect(uLong.lo).toBe(1);
expect(uLong.hi).toBe(-2097152);
expect(uLong.toNumber()).toBe(Number.MIN_SAFE_INTEGER);
});
it('should fail invalid from() value', function () {
expect(() => PbLong.from("")).toThrowError();
expect(() => PbLong.from("0.75")).toThrowError();
expect(() => PbLong.from("1,000")).toThrowError();
expect(() => PbLong.from("1-000")).toThrowError();
let maxSignedPlusOneStr = "9223372036854775808"
expect(() => PbLong.from(maxSignedPlusOneStr)).toThrowError();
expect(() => PbLong.from(Number.NaN)).toThrowError();
expect(() => PbLong.from(Number.POSITIVE_INFINITY)).toThrowError();
});
it('should toBigInt()', function () {
if (globalThis.BigInt === undefined)
pending('No BigInt support on current platform');
let bi = new PbLong(-620756991, 53471156);
// @ts-ignore
expect(bi.toBigInt()).toBe(229656869973524481n);
bi = new PbLong(1234567890, 0);
// @ts-ignore
expect(bi.toBigInt()).toBe(1234567890n);
// signed max value
bi = new PbLong(-1, 2147483647);
// @ts-ignore
expect(bi.toBigInt()).toBe(9223372036854775807n);
});
it('should isNegative()', function () {
let long = PbLong.from(-9223372)
expect(long.isNegative()).toBeTrue();
long = PbLong.from("-9223372")
expect(long.isNegative()).toBeTrue();
long = PbLong.from(9223372)
expect(long.isNegative()).toBeFalse();
long = PbLong.from("9223372")
expect(long.isNegative()).toBeFalse();
})
it('should isNegative() with negative native bigint', function () {
if (globalThis.BigInt === undefined)
pending('No BigInt support on current platform');
// @ts-ignore
let long = PbLong.from(-9223372036854775808n)
expect(long.isNegative()).toBeTrue();
// @ts-ignore
long = PbLong.from(9223372036854775807n)
expect(long.isNegative()).toBeFalse();
})
it('from(bigint) set expected bits', function () {
if (globalThis.BigInt === undefined)
pending('No BigInt support on current platform');
// @ts-ignore
let bi = PbLong.from(9223372036854775807n);
expect(bi.lo).toBe(0xFFFFFFFF | 0);
expect(bi.hi).toBe(0x7FFFFFFF | 0);
// @ts-ignore
expect(bi.toBigInt()).toBe(9223372036854775807n);
});
it('0 has lo = 0, hi = 0', function () {
let ulong = PbLong.from(0);
expect(ulong.hi).toBe(0);
expect(ulong.lo).toBe(0);
});
});
describe('testing native bigint', function () {
it('max uint64 value should survive string conversion', function () {
if (globalThis.BigInt === undefined)
pending('No BigInt support on current platform');
// @ts-ignore
let m = 18446744073709551615n;
let s = "18446744073709551615"
expect(m.toString()).toBe(s);
expect(globalThis.BigInt(s)).toBe(m);
});
it('min int64 value should survive string conversion', function () {
if (globalThis.BigInt === undefined)
pending('No BigInt support on current platform');
// @ts-ignore
let m = -9223372036854775808n;
let s = "-9223372036854775808"
expect(m.toString()).toBe(s);
expect(globalThis.BigInt(s)).toBe(m);
});
it('max int64 value should survive string conversion', function () {
if (globalThis.BigInt === undefined)
pending('No BigInt support on current platform');
// @ts-ignore
let m = 9223372036854775807n;
let s = "9223372036854775807"
expect(m.toString()).toBe(s);
expect(globalThis.BigInt(s)).toBe(m);
});
it('max uint64 value should survive DataView round trip', function () {
if (globalThis.BigInt === undefined)
pending('No BigInt support on current platform');
// @ts-ignore
let expected = 18446744073709551615n;
// write
let view64 = new DataView(new ArrayBuffer(8));
view64.setBigUint64(0, expected, true);
let lo = view64.getInt32(0, true) | 0;
let hi = view64.getInt32(4, true) | 0;
// read
view64.setInt32(0, lo, true);
view64.setInt32(4, hi, true);
let bigint = view64.getBigUint64(0, true);
expect(bigint === expected).toBeTrue();
});
it('max int64 value should survive DataView round trip', function () {
if (globalThis.BigInt === undefined)
pending('No BigInt support on current platform');
// @ts-ignore
let expected = 9223372036854775807n;
// write
let view64 = new DataView(new ArrayBuffer(8));
view64.setBigInt64(0, expected, true);
let lo = view64.getInt32(0, true) | 0;
let hi = view64.getInt32(4, true) | 0;
// read
view64.setInt32(0, lo, true);
view64.setInt32(4, hi, true);
let actual = view64.getBigInt64(0, true);
expect(actual.toString()).toBe(expected.toString());
});
it('min int64 value should survive DataView round trip', function () {
if (globalThis.BigInt === undefined)
pending('No BigInt support on current platform');
// @ts-ignore
let expected = -9223372036854775808n;
// write
let view64 = new DataView(new ArrayBuffer(8));
view64.setBigInt64(0, expected, true);
let lo = view64.getInt32(0, true) | 0;
let hi = view64.getInt32(4, true) | 0;
// read
view64.setInt32(0, lo, true);
view64.setInt32(4, hi, true);
let actual = view64.getBigInt64(0, true);
expect(actual.toString()).toBe(expected.toString());
});
});