PHP 8.2.30
Preview: LazyArray.js Size: 1.87 KB
/home/byroehnu/.trash/node_modules11/restructure/test/LazyArray.js

import assert from 'assert';
import {LazyArray, Pointer, uint8, uint16, DecodeStream, EncodeStream} from 'restructure';

describe('LazyArray', function() {
  describe('decode', function() {
    it('should decode items lazily', function() {
      const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
      const array = new LazyArray(uint8, 4);

      const arr = array.decode(stream);
      assert(!(arr instanceof Array));
      assert.equal(arr.length, 4);
      assert.equal(stream.pos, 4);

      assert.equal(arr.get(0), 1);
      assert.equal(arr.get(1), 2);
      assert.equal(arr.get(2), 3);
      assert.equal(arr.get(3), 4);

      assert.equal(arr.get(-1), null);
      assert.equal(arr.get(5), null);
    });

    it('should be able to convert to an array', function() {
      const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
      const array = new LazyArray(uint8, 4);

      const arr = array.decode(stream);
      assert.deepEqual(arr.toArray(), [1, 2, 3, 4]);
    });

    it('should decode length as number before array', function() {
      const stream = new DecodeStream(new Uint8Array([4, 1, 2, 3, 4, 5]));
      const array = new LazyArray(uint8, uint8);
      const arr = array.decode(stream);

      assert.deepEqual(arr.toArray(), [1, 2, 3, 4]);
    });
  });

  describe('size', () =>
    it('should work with LazyArrays', function() {
      const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
      const array = new LazyArray(uint8, 4);
      const arr = array.decode(stream);

      assert.equal(array.size(arr), 4);
    })
  );

  describe('encode', () =>
    it('should work with LazyArrays', function() {
      const array = new LazyArray(uint8, 4);
      const arr = array.fromBuffer(new Uint8Array([1, 2, 3, 4, 5]));
      const buffer = array.toBuffer(arr);
      assert.deepEqual(buffer, new Uint8Array([1, 2, 3, 4]));
    })
  );
});

Directory Contents

Dirs: 0 × Files: 15

Name Size Perms Modified Actions
3.97 KB lrw-r--r-- 2026-02-14 19:19:14
Edit Download
1.04 KB lrw-r--r-- 2026-02-14 19:19:14
Edit Download
1.15 KB lrw-r--r-- 2026-02-14 19:19:16
Edit Download
1.53 KB lrw-r--r-- 2026-02-14 19:19:16
Edit Download
3.53 KB lrw-r--r-- 2026-02-14 19:19:16
Edit Download
3.74 KB lrw-r--r-- 2026-02-14 19:19:16
Edit Download
831 B lrw-r--r-- 2026-02-14 19:19:16
Edit Download
1.87 KB lrw-r--r-- 2026-02-14 19:19:18
Edit Download
11.03 KB lrw-r--r-- 2026-02-14 19:19:18
Edit Download
3.83 KB lrw-r--r-- 2026-02-14 19:19:18
Edit Download
9.48 KB lrw-r--r-- 2026-02-14 19:19:18
Edit Download
820 B lrw-r--r-- 2026-02-14 19:19:18
Edit Download
5.61 KB lrw-r--r-- 2026-02-14 19:19:18
Edit Download
3.45 KB lrw-r--r-- 2026-02-14 19:19:18
Edit Download
11.37 KB lrw-r--r-- 2026-02-14 19:19:18
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).