Finished
44//
55
66class CircularBuffer {
7 constructor() {
8 throw new Error('Remove this line and implement the function');
7 constructor(size) {
8 // Edge Case: Invalid buffer size (non-positive or non-integer)
9 if (size <= 0 || !Number.isInteger(size)) {
10 throw new Error('Invalid buffer size');
11 }
12
13 this.buffer = new Array(size);
14 this.size = size;
15 this.readIndex = 0;
16 this.writeIndex = 0;
17 this.isFull = false;
918 }
1019
11 write() {
12 throw new Error('Remove this line and implement the function');
20 write(value) {
21 // Edge Case: Writing to a full buffer
22 if (this.isFull) {
23 throw new BufferFullError();
24 }
25
26 this.buffer[this.writeIndex] = value;
27 this.writeIndex = (this.writeIndex + 1) % this.size;
28
29 if (this.writeIndex === this.readIndex) {
30 this.isFull = true;
31 }
32
33 return value;
1334 }
1435
1536 read() {
16 throw new Error('Remove this line and implement the function');
37 // Edge Case: Reading from an empty buffer
38 if (this.readIndex === this.writeIndex && !this.isFull) {
39 throw new BufferEmptyError();
40 }
41
42 const value = this.buffer[this.readIndex];
43 this.buffer[this.readIndex] = undefined;
44 this.readIndex = (this.readIndex + 1) % this.size;
45 this.isFull = false;
46
47 return value;
1748 }
1849
19 forceWrite() {
20 throw new Error('Remove this line and implement the function');
50 forceWrite(value) {
51 // Edge Case: Force writing to a full buffer (overwrites oldest element)
52 if (this.isFull) {
53 this.buffer[this.writeIndex] = value;
54 this.writeIndex = (this.writeIndex + 1) % this.size;
55 this.readIndex = (this.readIndex + 1) % this.size;
56 } else {
57 this.write(value);
58 }
59
60 return value;
2161 }
2262
2363 clear() {
24 throw new Error('Remove this line and implement the function');
64 // Edge Case: Clearing an already empty buffer
65 this.buffer = new Array(this.size);
66 this.readIndex = 0;
67 this.writeIndex = 0;
68 this.isFull = false;
69 }
70
71 isEmpty() {
72 return this.readIndex === this.writeIndex && !this.isFull;
2573 }
2674}
2775
2977
3078export class BufferFullError extends Error {
3179 constructor() {
32 throw new Error('Remove this line and implement the function');
80 super('Buffer full');
81 this.name = 'BufferFullError';
3382 }
83
84 // Handled Edge Cases: Buffer overflow when full
3485}
3586
3687export class BufferEmptyError extends Error {
3788 constructor() {
38 throw new Error('Remove this line and implement the function');
89 super('Buffer empty');
90 this.name = 'BufferEmptyError';
3991 }
92
93 // Handled Edge Cases: Reading from empty buffer
4094}
95
96// Handled Edge Cases: Buffer initialization with invalid size, Buffer overflow when full, Reading from empty buffer, Wraparound indexing, Buffer clearing
Test NameStatus
reading empty buffer should fail
Pass
can read an item just written
Pass
each item may only be read once
Pass
items are read in the order they are written
Pass
full buffer can't be written to
Pass
a read frees up capacity for another write
Pass
read position is maintained even across multiple writes
Pass
items cleared out of buffer can't be read
Pass
clear frees up capacity for another write
Pass
clear does nothing on empty buffer
Pass
forceWrite acts like write on non-full buffer
Pass
forceWrite replaces the oldest item on full buffer
Pass
forceWrite replaces the oldest item remaining in buffer following a read
Pass
initial clear does not affect wrapping around
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.