Commit | Line | Data |
---|---|---|
f12182ad JB |
1 | import { expect } from 'expect' |
2 | ||
3 | import { CircularBuffer, defaultBufferSize } from '../lib/circular-buffer.cjs' | |
4 | ||
5 | describe('Circular buffer test suite', t => { | |
6 | it('Verify that circular buffer can be instantiated', () => { | |
7 | const circularBuffer = new CircularBuffer() | |
8 | expect(circularBuffer).toBeInstanceOf(CircularBuffer) | |
9 | expect(circularBuffer.readIdx).toBe(0) | |
10 | expect(circularBuffer.writeIdx).toBe(0) | |
11 | expect(circularBuffer.maxArrayIdx).toBe(defaultBufferSize - 1) | |
12 | expect(circularBuffer.items).toBeInstanceOf(Array) | |
13 | expect(circularBuffer.items.length).toBe(defaultBufferSize) | |
14 | }) | |
15 | ||
16 | it('Verify that circular buffer size can be set at instance creation', () => { | |
17 | const circularBuffer = new CircularBuffer(1000) | |
18 | expect(circularBuffer.maxArrayIdx).toBe(999) | |
19 | expect(circularBuffer.items).toBeInstanceOf(Array) | |
20 | expect(circularBuffer.items.length).toBe(1000) | |
21 | }) | |
22 | ||
23 | it('Verify that circular buffer size is valid at instance creation', () => { | |
24 | expect(() => new CircularBuffer(0.25)).toThrow( | |
25 | new TypeError('Invalid circular buffer size: 0.25 is not an integer') | |
26 | ) | |
27 | expect(() => new CircularBuffer(-1)).toThrow( | |
28 | new RangeError('Invalid circular buffer size: -1 < 0') | |
29 | ) | |
30 | expect(() => new CircularBuffer(Number.MAX_SAFE_INTEGER + 1)).toThrow( | |
31 | new TypeError( | |
32 | `Invalid circular buffer size: ${ | |
33 | Number.MAX_SAFE_INTEGER + 1 | |
34 | } is not an integer` | |
35 | ) | |
36 | ) | |
37 | }) | |
38 | ||
39 | it('Verify that circular buffer put() works as intended', () => { | |
40 | const circularBuffer = new CircularBuffer(4) | |
41 | circularBuffer.put(1) | |
42 | expect(circularBuffer.items).toMatchObject([1]) | |
43 | expect(circularBuffer.writeIdx).toBe(1) | |
44 | circularBuffer.put(2) | |
45 | expect(circularBuffer.items).toMatchObject([1, 2]) | |
46 | expect(circularBuffer.writeIdx).toBe(2) | |
47 | circularBuffer.put(3) | |
48 | expect(circularBuffer.items).toMatchObject([1, 2, 3]) | |
49 | expect(circularBuffer.writeIdx).toBe(3) | |
50 | circularBuffer.put(4) | |
51 | expect(circularBuffer.items).toMatchObject([1, 2, 3, 4]) | |
52 | expect(circularBuffer.writeIdx).toBe(0) | |
53 | circularBuffer.put(5) | |
54 | expect(circularBuffer.items).toMatchObject([5, 2, 3, 4]) | |
55 | expect(circularBuffer.writeIdx).toBe(1) | |
56 | circularBuffer.put(6) | |
57 | expect(circularBuffer.items).toMatchObject([5, 6, 3, 4]) | |
58 | expect(circularBuffer.writeIdx).toBe(2) | |
59 | }) | |
bad5c705 JB |
60 | |
61 | it('Verify that circular buffer toArray() works as intended', () => { | |
62 | const circularBuffer = new CircularBuffer(4) | |
63 | circularBuffer.put(1) | |
64 | circularBuffer.put(2) | |
65 | circularBuffer.put(3) | |
66 | circularBuffer.put(4) | |
67 | circularBuffer.put(5) | |
68 | circularBuffer.put(6) | |
69 | expect(circularBuffer.toArray()).toStrictEqual([5, 6, 3, 4]) | |
70 | }) | |
f12182ad | 71 | }) |