Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/node_modules/@protobufjs/pool/tests/index.js
2593 views
1
var tape = require("tape");
2
3
var pool = require("..");
4
5
if (typeof Uint8Array !== "undefined")
6
tape.test("pool", function(test) {
7
8
var alloc = pool(function(size) { return new Uint8Array(size); }, Uint8Array.prototype.subarray);
9
10
var buf1 = alloc(0);
11
test.equal(buf1.length, 0, "should allocate a buffer of size 0");
12
13
var buf2 = alloc(1);
14
test.equal(buf2.length, 1, "should allocate a buffer of size 1 (initializes slab)");
15
16
test.notEqual(buf2.buffer, buf1.buffer, "should not reference the same backing buffer if previous buffer had size 0");
17
test.equal(buf2.byteOffset, 0, "should allocate at byteOffset 0 when using a new slab");
18
19
buf1 = alloc(1);
20
test.equal(buf1.buffer, buf2.buffer, "should reference the same backing buffer when allocating a chunk fitting into the slab");
21
test.equal(buf1.byteOffset, 8, "should align slices to 32 bit and this allocate at byteOffset 8");
22
23
var buf3 = alloc(4097);
24
test.notEqual(buf3.buffer, buf2.buffer, "should not reference the same backing buffer when allocating a buffer larger than half the backing buffer's size");
25
26
buf2 = alloc(4096);
27
test.equal(buf2.buffer, buf1.buffer, "should reference the same backing buffer when allocating a buffer smaller or equal than half the backing buffer's size");
28
29
buf1 = alloc(4096);
30
test.notEqual(buf1.buffer, buf2.buffer, "should not reference the same backing buffer when the slab is exhausted (initializes new slab)");
31
32
test.end();
33
});
34