Skip to content

Commit 9af9e64

Browse files
committed
Add 64/128 bigint support (fixes #101)
1 parent 095873b commit 9af9e64

File tree

2 files changed

+346
-104
lines changed

2 files changed

+346
-104
lines changed

typescript/src/bufferfish.test.ts

Lines changed: 118 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,12 @@ test("should fail to peek too many bytes", () => {
3939
)
4040
})
4141

42-
test("should push another bufferfish", () => {
43-
const bf = new Bufferfish()
44-
bf.writeUint8(0)
45-
46-
const buf2 = new Bufferfish()
47-
buf2.writeUint8(1)
48-
49-
bf.push(buf2)
50-
51-
expect(bf.view()).toEqual(new Uint8Array([0, 1]))
52-
})
53-
54-
test("should push array-likes", () => {
55-
const bf = new Bufferfish()
56-
bf.writeUint8(0)
57-
58-
bf.push([1])
59-
bf.push(new Uint8Array([2]))
60-
61-
expect(bf.view()).toEqual(new Uint8Array([0, 1, 2]))
62-
})
63-
6442
test("should write u8", () => {
6543
const bf = new Bufferfish()
6644
bf.writeUint8(0)
6745
bf.writeUint8(255)
6846

69-
expect(bf.view()).toEqual(new Uint8Array([0, 255]))
47+
expect(bf.bytes()).toEqual(new Uint8Array([0, 255]))
7048
})
7149

7250
test("should write u16", () => {
@@ -75,7 +53,7 @@ test("should write u16", () => {
7553
bf.writeUint16(12345)
7654
bf.writeUint16(65535)
7755

78-
expect(bf.view()).toEqual(new Uint8Array([0, 0, 48, 57, 255, 255]))
56+
expect(bf.bytes()).toEqual(new Uint8Array([0, 0, 48, 57, 255, 255]))
7957
})
8058

8159
test("should write u32", () => {
@@ -84,11 +62,40 @@ test("should write u32", () => {
8462
bf.writeUint32(1234567890)
8563
bf.writeUint32(4294967295)
8664

87-
expect(bf.view()).toEqual(
65+
expect(bf.bytes()).toEqual(
8866
new Uint8Array([0, 0, 0, 0, 73, 150, 2, 210, 255, 255, 255, 255]),
8967
)
9068
})
9169

70+
test("should write u64", () => {
71+
const bf = new Bufferfish()
72+
bf.writeUint64(0n)
73+
bf.writeUint64(1234567890123456789n)
74+
bf.writeUint64(18446744073709551615n)
75+
76+
expect(bf.bytes()).toEqual(
77+
new Uint8Array([
78+
0, 0, 0, 0, 0, 0, 0, 0, 17, 34, 16, 244, 125, 233, 129, 21, 255,
79+
255, 255, 255, 255, 255, 255, 255,
80+
]),
81+
)
82+
})
83+
84+
test("should write u128", () => {
85+
const bf = new Bufferfish()
86+
bf.writeUint128(0n)
87+
bf.writeUint128(170141183460469231731687303715884105728n)
88+
bf.writeUint128(340282366920938463463374607431768211455n)
89+
90+
expect(bf.bytes()).toEqual(
91+
new Uint8Array([
92+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0,
93+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255,
94+
255, 255, 255, 255, 255, 255, 255, 255, 255,
95+
]),
96+
)
97+
})
98+
9299
test("should read u8", () => {
93100
const bf = new Bufferfish()
94101
bf.writeUint8(0)
@@ -120,13 +127,33 @@ test("should read u32", () => {
120127
expect(bf.readUint32()).toEqual(4294967295)
121128
})
122129

130+
test("should read u64", () => {
131+
const bf = new Bufferfish()
132+
bf.writeUint64(0n)
133+
bf.writeUint64(1234567890123456789n)
134+
bf.writeUint64(18446744073709551615n)
135+
136+
expect(bf.readUint64()).toEqual(0n)
137+
expect(bf.readUint64()).toEqual(1234567890123456789n)
138+
expect(bf.readUint64()).toEqual(18446744073709551615n)
139+
})
140+
141+
test("should read u128", () => {
142+
const bf = new Bufferfish()
143+
bf.writeUint128(0n)
144+
bf.writeUint128(340282366920938463463374607431768211455n)
145+
146+
expect(bf.readUint128()).toEqual(0n)
147+
expect(bf.readUint128()).toEqual(340282366920938463463374607431768211455n)
148+
})
149+
123150
test("should write i8", () => {
124151
const bf = new Bufferfish()
125152
bf.writeInt8(0)
126153
bf.writeInt8(127)
127154
bf.writeInt8(-128)
128155

129-
expect(bf.view()).toEqual(new Uint8Array([0, 127, 128]))
156+
expect(bf.bytes()).toEqual(new Uint8Array([0, 127, 128]))
130157
})
131158

132159
test("should write i16", () => {
@@ -136,7 +163,7 @@ test("should write i16", () => {
136163
bf.writeInt16(32767)
137164
bf.writeInt16(-32768)
138165

139-
expect(bf.view()).toEqual(new Uint8Array([0, 0, 48, 57, 127, 255, 128, 0]))
166+
expect(bf.bytes()).toEqual(new Uint8Array([0, 0, 48, 57, 127, 255, 128, 0]))
140167
})
141168

142169
test("should write i32", () => {
@@ -146,13 +173,43 @@ test("should write i32", () => {
146173
bf.writeInt32(2147483647)
147174
bf.writeInt32(-2147483648)
148175

149-
expect(bf.view()).toEqual(
176+
expect(bf.bytes()).toEqual(
150177
new Uint8Array([
151178
0, 0, 0, 0, 73, 150, 2, 210, 127, 255, 255, 255, 128, 0, 0, 0,
152179
]),
153180
)
154181
})
155182

183+
test("should write i64", () => {
184+
const bf = new Bufferfish()
185+
bf.writeInt64(0n)
186+
bf.writeInt64(1234567890123456789n)
187+
bf.writeInt64(9223372036854775807n)
188+
bf.writeInt64(-9223372036854775808n)
189+
190+
expect(bf.bytes()).toEqual(
191+
new Uint8Array([
192+
0, 0, 0, 0, 0, 0, 0, 0, 17, 34, 16, 244, 125, 233, 129, 21, 127,
193+
255, 255, 255, 255, 255, 255, 255, 128, 0, 0, 0, 0, 0, 0, 0,
194+
]),
195+
)
196+
})
197+
198+
test("should write i128", () => {
199+
const bf = new Bufferfish()
200+
bf.writeInt128(0n)
201+
bf.writeInt128(-170141183460469231731687303715884105728n)
202+
bf.writeInt128(170141183460469231731687303715884105727n)
203+
204+
expect(bf.bytes()).toEqual(
205+
new Uint8Array([
206+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0,
207+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 255, 255, 255, 255, 255, 255,
208+
255, 255, 255, 255, 255, 255, 255, 255, 255,
209+
]),
210+
)
211+
})
212+
156213
test("should read i8", () => {
157214
const bf = new Bufferfish()
158215
bf.writeInt8(0)
@@ -192,6 +249,30 @@ test("should read i32", () => {
192249
expect(bf.readInt32()).toEqual(-1)
193250
})
194251

252+
test("should read i64", () => {
253+
const bf = new Bufferfish()
254+
bf.writeInt64(0n)
255+
bf.writeInt64(1234567890123456789n)
256+
bf.writeInt64(9223372036854775807n)
257+
bf.writeInt64(-9223372036854775808n)
258+
259+
expect(bf.readInt64()).toEqual(0n)
260+
expect(bf.readInt64()).toEqual(1234567890123456789n)
261+
expect(bf.readInt64()).toEqual(9223372036854775807n)
262+
expect(bf.readInt64()).toEqual(-9223372036854775808n)
263+
})
264+
265+
test("should read i128", () => {
266+
const bf = new Bufferfish()
267+
bf.writeInt128(0n)
268+
bf.writeInt128(-170141183460469231731687303715884105728n)
269+
bf.writeInt128(170141183460469231731687303715884105727n)
270+
271+
expect(bf.readInt128()).toEqual(0n)
272+
expect(bf.readInt128()).toEqual(-170141183460469231731687303715884105728n)
273+
expect(bf.readInt128()).toEqual(170141183460469231731687303715884105727n)
274+
})
275+
195276
test("should read from reset position", () => {
196277
const bf = new Bufferfish()
197278
bf.writeUint8(0)
@@ -227,7 +308,7 @@ test("should write string", () => {
227308
const bf = new Bufferfish()
228309
bf.writeString("Bufferfish")
229310

230-
expect(bf.view()).toEqual(
311+
expect(bf.bytes()).toEqual(
231312
new Uint8Array([
232313
0, 10, 66, 117, 102, 102, 101, 114, 102, 105, 115, 104,
233314
]),
@@ -238,7 +319,7 @@ test("should write string big chars", () => {
238319
const bf = new Bufferfish()
239320
bf.writeString("안녕하세요")
240321

241-
expect(bf.view()).toEqual(
322+
expect(bf.bytes()).toEqual(
242323
new Uint8Array([
243324
0, 15, 236, 149, 136, 235, 133, 149, 237, 149, 152, 236, 132, 184,
244325
236, 154, 148,
@@ -251,7 +332,7 @@ test("should write multiple strings", () => {
251332
bf.writeString("Bufferfish")
252333
bf.writeString("안녕하세요")
253334

254-
expect(bf.view()).toEqual(
335+
expect(bf.bytes()).toEqual(
255336
new Uint8Array([
256337
0, 10, 66, 117, 102, 102, 101, 114, 102, 105, 115, 104, 0, 15, 236,
257338
149, 136, 235, 133, 149, 237, 149, 152, 236, 132, 184, 236, 154,
@@ -272,21 +353,21 @@ test("should write bool", () => {
272353
bf.writeBool(true)
273354
bf.writeBool(false)
274355

275-
expect(bf.view()).toEqual(new Uint8Array([1, 0]))
356+
expect(bf.bytes()).toEqual(new Uint8Array([1, 0]))
276357
})
277358

278359
test("should write full packed bools", () => {
279360
const bf = new Bufferfish()
280361
bf.writePackedBools([true, false, true, true, false, false, true, false])
281362

282-
expect(bf.view()).toEqual(new Uint8Array([0b10110010]))
363+
expect(bf.bytes()).toEqual(new Uint8Array([0b10110010]))
283364
})
284365

285366
test("should write partial packed bools", () => {
286367
const bf = new Bufferfish()
287368
bf.writePackedBools([true, false])
288369

289-
expect(bf.view()).toEqual(new Uint8Array([0b10000000]))
370+
expect(bf.bytes()).toEqual(new Uint8Array([0b10000000]))
290371
})
291372

292373
test("should read bool", () => {
@@ -328,7 +409,7 @@ test("should write raw bytes", () => {
328409
const buf2 = new Bufferfish()
329410
buf2.writeString("안녕하세요")
330411

331-
bf.writeRawBytes(buf2.view())
412+
bf.writeRawBytes(buf2.bytes())
332413

333414
expect(bf.readString()).toEqual("Bufferfish")
334415
expect(bf.readString()).toEqual("안녕하세요")
@@ -400,7 +481,7 @@ test("should write and read arrays of u8", () => {
400481
const numbers = [1, 2, 3, 4, 5]
401482
bf.writeArray(numbers, (n) => bf.writeUint8(n))
402483

403-
expect(bf.view()).toEqual(new Uint8Array([0, 5, 1, 2, 3, 4, 5]))
484+
expect(bf.bytes()).toEqual(new Uint8Array([0, 5, 1, 2, 3, 4, 5]))
404485

405486
const readArray = bf.readArray(() => {
406487
const val = bf.readUint8()
@@ -436,7 +517,7 @@ test("should handle empty arrays", () => {
436517
const emptyArray: number[] = []
437518
bf.writeArray(emptyArray, (n) => bf.writeUint8(n))
438519

439-
expect(bf.view()).toEqual(new Uint8Array([0, 0]))
520+
expect(bf.bytes()).toEqual(new Uint8Array([0, 0]))
440521

441522
const readArray = bf.readArray(() => {
442523
const val = bf.readUint8()

0 commit comments

Comments
 (0)