-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbuffer-backed-object.test.js
414 lines (381 loc) · 11.3 KB
/
buffer-backed-object.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import { expect, test } from "vitest";
import * as BBO from "./buffer-backed-object.ts";
test("structSize calculates the size of a struct correctly", function () {
{
const size = BBO.structSize({
id: BBO.Uint8(),
});
expect(size).toBe(1);
}
{
const size = BBO.structSize({
id: BBO.Uint16(),
});
expect(size).toBe(2);
}
{
const size = BBO.structSize({
id: BBO.Uint16(),
id2: BBO.Uint8(),
});
expect(size).toBe(4);
}
{
const size = BBO.structSize({
id: BBO.Uint8(),
id2: BBO.Uint16(),
});
expect(size).toBe(4);
}
{
const size = BBO.structSize({
id: BBO.Uint8(),
id2: BBO.Uint32(),
id3: BBO.Uint32(),
});
expect(size).toBe(12);
}
{
const size = BBO.structSize({
id: BBO.Uint8(), // 0...7
x: BBO.Float64({ endianness: "big" }), // 8...15
y: BBO.Float64({ endianness: "little" }), // 16...23
texture: BBO.Int32(), // 24...28
_: BBO.reserved(1), // 28...29
});
expect(size).toBe(32);
}
});
test("ArrayOfBufferBackedObjects calculates length correctly", function () {
{
const buffer = new ArrayBuffer(7);
const aosv = BBO.ArrayOfBufferBackedObjects(buffer, {
id: BBO.Uint8(),
_: BBO.reserved(1),
});
expect(aosv.length).toBe(3);
}
{
const buffer = new ArrayBuffer(47);
const aosv = BBO.ArrayOfBufferBackedObjects(buffer, {
id: BBO.Uint8(),
i2: BBO.BigInt64(),
});
expect(aosv.length).toBe(2);
}
});
test("ArrayOfBufferBackedObjects decodes items correctly", function () {
const descriptor = {
id: BBO.Uint8(), // 0...7
x: BBO.Float64({ endianness: "big" }), // 8...15
y: BBO.Float64({ endianness: "little" }), // 16...23
texture: BBO.Int32(), // 24...27
_: BBO.reserved(1), // 28...29
};
console.log(BBO.structSize(descriptor), BBO.structAlign(descriptor));
const buffer = new ArrayBuffer(BBO.structSize(descriptor) * 2);
const dataView = new DataView(buffer);
dataView.setUint8(0 + 0, 1);
dataView.setUint8(32 + 0, 2);
dataView.setFloat64(0 + 8, 20, false);
dataView.setFloat64(0 + 16, 30, true);
dataView.setFloat64(32 + 8, 40, false);
dataView.setFloat64(32 + 16, 50, true);
dataView.setInt32(0 + 24, 9, true);
dataView.setInt32(32 + 24, 10, true);
const aosv = BBO.ArrayOfBufferBackedObjects(buffer, descriptor);
expect(aosv[0].id).toBe(1);
expect(aosv[1].id).toBe(2);
expect(aosv[0].x).toBe(20);
expect(aosv[1].x).toBe(40);
expect(aosv[0].y).toBe(30);
expect(aosv[1].y).toBe(50);
expect(aosv[0].texture).toBe(9);
expect(aosv[1].texture).toBe(10);
});
test("ArrayOfBufferBackedObjects can have an offset", function () {
const descriptor = {
id: BBO.Uint8(),
_: BBO.reserved(1),
};
const buffer = new ArrayBuffer(BBO.structSize(descriptor) * 4 + 1);
const dataView = new DataView(buffer);
const aosv = BBO.ArrayOfBufferBackedObjects(buffer, descriptor, {
byteOffset: 1,
length: 2,
});
expect(aosv.length).toBe(2);
aosv[0].id = 1;
aosv[1].id = 1;
expect(dataView.getUint8(1)).toBe(1);
});
test("ArrayOfBufferBackedObjects handles nested Array of BBOs", function () {
const descriptors = {
id: BBO.Uint8(),
vertices: BBO.NestedArrayOfBufferBackedObjects(3, {
x: BBO.Float64(),
y: BBO.Float64(),
}),
};
const buffer = new ArrayBuffer(BBO.structSize(descriptors) * 3);
const aosv = BBO.ArrayOfBufferBackedObjects(buffer, descriptors);
expect(aosv.length).toBe(3);
aosv[2].id = 1;
aosv[2].vertices[0].x = 0;
aosv[2].vertices[0].y = 1;
aosv[2].vertices[1].x = 2;
aosv[2].vertices[1].y = 3;
aosv[2].vertices[2].x = 4;
aosv[2].vertices[2].y = 5;
expect(aosv.length).toBe(3);
expect(JSON.stringify(aosv[2])).toBe(
JSON.stringify({
id: 1,
vertices: [
{ x: 0, y: 1 },
{ x: 2, y: 3 },
{ x: 4, y: 5 },
],
})
);
});
test("ArrayOfBufferBackedObjects handles nested BBO", function () {
const descriptors = {
id: BBO.Uint8(),
pos: BBO.NestedBufferBackedObject({
x: BBO.Float64(),
y: BBO.Float64(),
}),
};
const buffer = new ArrayBuffer(BBO.structSize(descriptors) * 3);
const aosv = BBO.ArrayOfBufferBackedObjects(buffer, descriptors);
expect(aosv.length).toBe(3);
aosv[2].id = 1;
aosv[2].pos.x = 3;
aosv[2].pos.y = 2;
expect(aosv.length).toBe(3);
expect(JSON.stringify(aosv[2])).toBe(
JSON.stringify({ id: 1, pos: { x: 3, y: 2 } })
);
expect(JSON.stringify(aosv)).toBe(
JSON.stringify([
{ id: 0, pos: { x: 0, y: 0 } },
{ id: 0, pos: { x: 0, y: 0 } },
{ id: 1, pos: { x: 3, y: 2 } },
])
);
});
test("ArrayOfBufferBackedObjects handles nested BBO with nested arrays", function () {
const PathNodeDescription = {
type: BBO.Uint8(),
x: BBO.Uint32(),
y: BBO.Uint32(),
};
const EnemyDescription = {
type: BBO.Uint8(),
x: BBO.Float32(),
y: BBO.Float32(),
path: BBO.NestedArrayOfBufferBackedObjects(1, PathNodeDescription),
};
const GameStateDescription = {
gametime: BBO.Uint32(),
season: BBO.Uint8(),
enemies: BBO.NestedArrayOfBufferBackedObjects(1, EnemyDescription),
};
const gameStateBuffer = new ArrayBuffer(BBO.structSize(GameStateDescription));
const gameState = BBO.BufferBackedObject(
gameStateBuffer,
GameStateDescription
);
gameState.gametime = 12345;
gameState.season = 3;
expect(gameState.enemies.length).toBe(1);
gameState.enemies[0].type = 1;
gameState.enemies[0].x = 512;
gameState.enemies[0].y = 0;
expect(gameState.enemies.length).toBe(1);
expect(JSON.stringify(gameState.enemies[0])).toBe(
JSON.stringify({ type: 1, x: 512, y: 0, path: [{ type: 0, x: 0, y: 0 }] })
);
expect(JSON.stringify(gameState)).toBe(
JSON.stringify({
gametime: 12345,
season: 3,
enemies: [{ type: 1, x: 512, y: 0, path: [{ type: 0, x: 0, y: 0 }] }],
})
);
});
test("ArrayOfBufferBackedObjects can return the buffer", function () {
const buffer = new ArrayBuffer(22);
const aosv = BBO.ArrayOfBufferBackedObjects(buffer, {
x: BBO.Uint8(),
});
expect(aosv.buffer).toBe(buffer);
});
test("ArrayOfBufferBackedObjects encodes to JSON", function () {
const { buffer } = new Uint8Array([0, 0, 1, 0, 2, 0, 1]);
const aosv = BBO.ArrayOfBufferBackedObjects(buffer, {
id: BBO.Uint8(),
_: BBO.reserved(1),
});
expect(JSON.stringify(aosv)).toBe(
JSON.stringify([{ id: 0 }, { id: 1 }, { id: 2 }])
);
});
test("ArrayOfBufferBackedObjects can write items", function () {
const descriptor = {
id: BBO.Uint8(),
x: BBO.Float64(),
_: BBO.reserved(1),
};
const buffer = new ArrayBuffer(BBO.structSize(descriptor) * 2);
const dataView = new DataView(buffer);
const aosv = BBO.ArrayOfBufferBackedObjects(buffer, descriptor);
aosv[0].x = 10;
aosv[1].x = 20;
expect(dataView.getFloat64(8, true)).toBe(10);
expect(dataView.getFloat64(32, true)).toBe(20);
});
test("ArrayOfBufferBackedObjects handles filter()", function () {
const descriptor = {
id: BBO.Uint8(),
data: BBO.Uint8(),
};
const { buffer } = new Uint8Array([0, 10, 1, 11, 2, 12, 3, 13]);
const aosv = BBO.ArrayOfBufferBackedObjects(buffer, descriptor);
const even = aosv.filter(({ id }) => id % 2 == 0);
expect(even.length).toBe(2);
even[1].data = 99;
expect(aosv[2].data).toBe(99);
});
test("ArrayOfBufferBackedObjects rejects new properties", function () {
const descriptor = {
id: BBO.Uint8(),
data: BBO.Uint8(),
};
const { buffer } = new Uint8Array([0, 10, 1, 11, 2, 12, 3, 13]);
const aosv = BBO.ArrayOfBufferBackedObjects(buffer, descriptor);
expect(() => {
aosv[0].lol = 4;
}).toThrow();
});
test("ArrayOfBufferBackedObjects can handle UTF8 strings", function () {
const descriptor = {
name: BBO.UTF8String(32),
id: BBO.Uint8(),
};
const buffer = new ArrayBuffer(BBO.structSize(descriptor) * 2);
const aosv = BBO.ArrayOfBufferBackedObjects(buffer, descriptor);
aosv[0].name = "Surma";
aosv[1].name = "Jason";
const name1 = new TextDecoder().decode(new Uint8Array(buffer, 0, 5));
const name2 = new TextDecoder().decode(new Uint8Array(buffer, 33, 5));
expect(name1).toBe("Surma");
expect(name2).toBe("Jason");
});
test("BufferBackedObject decodes items correctly", function () {
const descriptor = {
id: BBO.Uint8(),
x: BBO.Float64({ endianness: "big" }),
y: BBO.Float64({ endianness: "little" }),
texture: BBO.Int32(),
_: BBO.reserved(1),
};
const buffer = new ArrayBuffer(BBO.structSize(descriptor));
const dataView = new DataView(buffer);
dataView.setUint8(0 + 0, 1);
dataView.setFloat64(0 + 8, 20, false);
dataView.setFloat64(0 + 16, 30, true);
dataView.setInt32(0 + 24, 9, true);
const sdv = BBO.BufferBackedObject(buffer, descriptor);
expect(sdv.id).toBe(1);
expect(sdv.x).toBe(20);
expect(sdv.y).toBe(30);
expect(sdv.texture).toBe(9);
});
test("BufferBackedObject decodes items correctly with custom align", function () {
const descriptor = {
id: BBO.Uint8(),
x: BBO.Float64({ endianness: "big", align: 1 }),
y: BBO.Float64({ endianness: "little", align: 1 }),
texture: BBO.Int32({ align: 1 }),
_: BBO.reserved(1),
};
const buffer = new ArrayBuffer(BBO.structSize(descriptor));
const dataView = new DataView(buffer);
dataView.setUint8(0 + 0, 1);
dataView.setFloat64(0 + 1, 20, false);
dataView.setFloat64(0 + 9, 30, true);
dataView.setInt32(0 + 17, 9, true);
const sdv = BBO.BufferBackedObject(buffer, descriptor);
expect(sdv.id).toBe(1);
expect(sdv.x).toBe(20);
expect(sdv.y).toBe(30);
expect(sdv.texture).toBe(9);
});
test("NestedArrayOfBufferBackedObjects test raw bufferview", function () {
const BBOVec3 = BBO.NestedBufferBackedObject({
x: BBO.Float32(),
y: BBO.Float32(),
z: BBO.Float32(),
});
const structDesc = {
ambient: BBOVec3,
lightCount: BBO.Float32(),
lights: BBO.NestedArrayOfBufferBackedObjects(1, {
position: BBOVec3,
range: BBO.Float32(),
color: BBOVec3,
intensity: BBO.Float32(),
}),
};
const buffer = new ArrayBuffer(BBO.structSize(structDesc));
const view = BBO.BufferBackedObject(buffer, structDesc);
view.ambient.x = 1;
view.ambient.y = 2;
view.ambient.z = 3;
view.lightCount = 4;
view.lights.forEach(light => {
light.position.x = 5;
light.position.y = 6;
light.position.z = 7;
light.range = 8;
light.color.x = 9;
light.color.y = 10;
light.color.z = 11;
light.intensity = 12;
});
const f32 = new Float32Array(buffer);
expect(f32[0]).toBe(1);
expect(f32[1]).toBe(2);
expect(f32[2]).toBe(3);
expect(f32[3]).toBe(4);
expect(f32[4]).toBe(5);
expect(f32[5]).toBe(6);
expect(f32[6]).toBe(7);
expect(f32[7]).toBe(8);
expect(f32[8]).toBe(9);
expect(f32[9]).toBe(10);
expect(f32[10]).toBe(11);
expect(f32[11]).toBe(12);
});
test("Nested NestedBufferBackedObjects", function () {
const structDesc = {
somethingElse: BBO.Float32(),
nest1: BBO.NestedBufferBackedObject({
somethingElse: BBO.Float32(),
nest2: BBO.NestedBufferBackedObject( {
value: BBO.Float32()
})
}),
};
const buffer = new ArrayBuffer(BBO.structSize(structDesc));
const view = BBO.BufferBackedObject(buffer, structDesc);
view.somethingElse = 1;
view.nest1.somethingElse = 2;
view.nest1.nest2.value = 3;
const f32 = new Float32Array(buffer);
expect(f32[0]).toBe(1);
expect(f32[1]).toBe(2);
expect(f32[2]).toBe(3);
});