Skip to content

Commit e31104e

Browse files
committed
refactor: refine circular buffer API
Signed-off-by: Jérôme Benoit <[email protected]>
1 parent 54af2cd commit e31104e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/circular-buffer.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ export class CircularBuffer {
2828
this.items = new Float32Array(size)
2929
}
3030

31+
/**
32+
* Clears the buffer.
33+
*/
34+
public clear(): void {
35+
this.readIdx = 0
36+
this.writeIdx = 0
37+
this.size = 0
38+
}
39+
3140
/**
3241
* Checks whether the buffer is empty.
3342
*

tests/circular-buffer.test.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,20 @@ describe('Circular buffer test suite', () => {
165165
circularBuffer.put(6)
166166
expect(circularBuffer.toArray()).toStrictEqual([3, 4, 5, 6])
167167
})
168+
169+
it('Verify that circular buffer clear() works as intended', () => {
170+
const circularBuffer = new CircularBuffer(4)
171+
circularBuffer.put(1)
172+
circularBuffer.put(2)
173+
circularBuffer.put(3)
174+
circularBuffer.put(4)
175+
circularBuffer.put(5)
176+
expect(circularBuffer.readIdx).toBe(1)
177+
expect(circularBuffer.writeIdx).toBe(1)
178+
expect(circularBuffer.size).toBe(4)
179+
circularBuffer.clear()
180+
expect(circularBuffer.readIdx).toBe(0)
181+
expect(circularBuffer.writeIdx).toBe(0)
182+
expect(circularBuffer.size).toBe(0)
183+
})
168184
})

0 commit comments

Comments
 (0)