暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.5KB

123456789101112131415161718192021222324252627282930313233
  1. var tape = require("tape");
  2. var pool = require("..");
  3. if (typeof Uint8Array !== "undefined")
  4. tape.test("pool", function(test) {
  5. var alloc = pool(function(size) { return new Uint8Array(size); }, Uint8Array.prototype.subarray);
  6. var buf1 = alloc(0);
  7. test.equal(buf1.length, 0, "should allocate a buffer of size 0");
  8. var buf2 = alloc(1);
  9. test.equal(buf2.length, 1, "should allocate a buffer of size 1 (initializes slab)");
  10. test.notEqual(buf2.buffer, buf1.buffer, "should not reference the same backing buffer if previous buffer had size 0");
  11. test.equal(buf2.byteOffset, 0, "should allocate at byteOffset 0 when using a new slab");
  12. buf1 = alloc(1);
  13. test.equal(buf1.buffer, buf2.buffer, "should reference the same backing buffer when allocating a chunk fitting into the slab");
  14. test.equal(buf1.byteOffset, 8, "should align slices to 32 bit and this allocate at byteOffset 8");
  15. var buf3 = alloc(4097);
  16. 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");
  17. buf2 = alloc(4096);
  18. 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");
  19. buf1 = alloc(4096);
  20. test.notEqual(buf1.buffer, buf2.buffer, "should not reference the same backing buffer when the slab is exhausted (initializes new slab)");
  21. test.end();
  22. });