|
| 1 | +import { Buffers } from '../../src/common/connection/buffers'; |
| 2 | + |
| 3 | +describe('Buffers', () => { |
| 4 | + it('can append and slice', () => { |
| 5 | + const list = new Buffers(); |
| 6 | + list.push(new Uint8Array([1, 2, 3])); |
| 7 | + list.push(new Uint8Array([4, 5, 6])); |
| 8 | + |
| 9 | + expect(list.slice(0, 0)).toEqual(new Uint8Array(0)); |
| 10 | + expect(list.slice(0, 2)).toEqual(new Uint8Array([1, 2])); |
| 11 | + expect(list.slice(0, 7)).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6])); |
| 12 | + |
| 13 | + expect(list.slice(1, 1)).toEqual(new Uint8Array(0)); |
| 14 | + expect(list.slice(1, 5)).toEqual(new Uint8Array([2, 3, 4, 5])); |
| 15 | + expect(list.slice(1, 7)).toEqual(new Uint8Array([2, 3, 4, 5, 6])); |
| 16 | + |
| 17 | + expect(list.slice(2, 2)).toEqual(new Uint8Array(0)); |
| 18 | + expect(list.slice(2, 6)).toEqual(new Uint8Array([3, 4, 5, 6])); |
| 19 | + }); |
| 20 | + |
| 21 | + it('can splice', () => { |
| 22 | + const list = new Buffers(); |
| 23 | + list.push(new Uint8Array([1, 2, 3])); |
| 24 | + list.push(new Uint8Array([4, 5, 6])); |
| 25 | + |
| 26 | + expect(list.splice(0, 0)).toEqual({ buffers: [], size: 0 }); |
| 27 | + expect(list.byteLength).toEqual(6); |
| 28 | + expect(list.splice(0, 1)).toEqual({ buffers: [new Uint8Array([1])], size: 1 }); |
| 29 | + expect(list.byteLength).toEqual(5); |
| 30 | + expect(list.splice(0, 2)).toEqual({ buffers: [new Uint8Array([2, 3])], size: 2 }); |
| 31 | + expect(list.byteLength).toEqual(3); |
| 32 | + |
| 33 | + expect(list.splice(0, 0, new Uint8Array([1]))).toEqual({ buffers: [], size: 0 }); |
| 34 | + expect(list.byteLength).toEqual(4); |
| 35 | + expect(list.splice(0, 0, new Uint8Array([2, 3]))).toEqual({ buffers: [], size: 0 }); |
| 36 | + expect(list.byteLength).toEqual(6); |
| 37 | + expect(list.splice(0, 0, new Uint8Array([4, 5, 6]))).toEqual({ buffers: [], size: 0 }); |
| 38 | + expect(list.byteLength).toEqual(9); |
| 39 | + |
| 40 | + expect(list.buffers).toEqual([ |
| 41 | + new Uint8Array([4, 5, 6]), |
| 42 | + new Uint8Array([2, 3]), |
| 43 | + new Uint8Array([1]), |
| 44 | + new Uint8Array([4, 5, 6]), |
| 45 | + ]); |
| 46 | + }); |
| 47 | + it('can copy', () => { |
| 48 | + const list = new Buffers(); |
| 49 | + list.push(new Uint8Array([1, 2, 3])); |
| 50 | + |
| 51 | + list.push(new Uint8Array([4, 5, 6])); |
| 52 | + |
| 53 | + const target = new Uint8Array(7); |
| 54 | + |
| 55 | + list.copy(target, 0); |
| 56 | + |
| 57 | + expect(target).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6, 0])); |
| 58 | + }); |
| 59 | + |
| 60 | + it('other', () => { |
| 61 | + const list = new Buffers(); |
| 62 | + list.push(new Uint8Array([1, 2, 3])); |
| 63 | + list.push(new Uint8Array([4, 5, 6])); |
| 64 | + expect(list.byteLength).toEqual(6); |
| 65 | + expect(list.pos(0)).toEqual({ buf: 0, offset: 0 }); |
| 66 | + expect(list.pos(1)).toEqual({ buf: 0, offset: 1 }); |
| 67 | + expect(list.pos(2)).toEqual({ buf: 0, offset: 2 }); |
| 68 | + expect(list.get(0)).toEqual(1); |
| 69 | + expect(list.get(1)).toEqual(2); |
| 70 | + |
| 71 | + list.set(0, 9); |
| 72 | + list.set(1, 10); |
| 73 | + expect(list.get(0)).toEqual(9); |
| 74 | + expect(list.get(1)).toEqual(10); |
| 75 | + }); |
| 76 | +}); |
0 commit comments