Finished
88 * @param {number} capacity
99 */
1010 constructor(capacity) {
11 throw new Error('Remove this line and implement the function');
11 // Edge Case: Buffer initialization with zero or negative size
12 if (capacity <= 0) {
13 throw new Error('Capacity must be a positive number');
14 }
15
16 this.buffer = new Array(capacity);
17 this.capacity = capacity;
18 this.size = 0;
19 this.readIndex = 0;
20 this.writeIndex = 0;
1221 }
1322
1423 /**
1524 * @param {*} value
1625 */
1726 write(value) {
18 throw new Error('Remove this line and implement the function');
27 // Edge Case: Writing to a full buffer
28 if (this.size === this.capacity) {
29 throw new BufferFullError();
30 }
31
32 this.buffer[this.writeIndex] = value;
33 this.writeIndex = (this.writeIndex + 1) % this.capacity;
34 this.size++;
1935 }
2036
2137 /**
2238 * @returns {*}
2339 */
2440 read() {
25 throw new Error('Remove this line and implement the function');
41 // Edge Case: Reading from an empty buffer
42 if (this.size === 0) {
43 throw new BufferEmptyError();
44 }
45
46 const value = this.buffer[this.readIndex];
47 this.readIndex = (this.readIndex + 1) % this.capacity;
48 this.size--;
49 return value;
2650 }
2751
2852 /**
2953 * @param {*} value
3054 */
3155 forceWrite(value) {
32 throw new Error('Remove this line and implement the function');
56 // Edge Case: Force writing to a full buffer (overwrites oldest element)
57 if (this.size === this.capacity) {
58 // Overwrite the oldest element (at readIndex)
59 this.buffer[this.readIndex] = value;
60 this.readIndex = (this.readIndex + 1) % this.capacity;
61 this.writeIndex = (this.writeIndex + 1) % this.capacity;
62 } else {
63 // If buffer is not full, just write normally
64 this.write(value);
65 }
3366 }
3467
3568 clear() {
36 throw new Error('Remove this line and implement the function');
69 // Edge Case: Clearing an already empty buffer (no effect needed)
70 this.size = 0;
71 this.readIndex = 0;
72 this.writeIndex = 0;
3773 }
3874}
3975
4177
4278export class BufferFullError extends Error {
4379 constructor() {
44 throw new Error('Remove this line and implement the function');
80 super('Buffer full');
81 this.name = 'BufferFullError';
4582 }
83
84 // Handled Edge Cases: None (this is an error class)
4685}
4786
4887export class BufferEmptyError extends Error {
4988 constructor() {
50 throw new Error('Remove this line and implement the function');
89 super('Buffer empty');
90 this.name = 'BufferEmptyError';
5191 }
92
93 // Handled Edge Cases: None (this is an error class)
5294}
95
96// Handled Edge Cases: Buffer initialization with zero or negative size, Writing to a full buffer, Reading from an empty buffer, Force writing to a full buffer (overwrites oldest element), Clearing an already empty buffer
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.