Finished
55
66 """
77 def __init__(self, message: str):
8 pass
8 self.message = message
9 super().__init__(message)
910
1011
1112class BufferEmptyException(BufferError):
1516
1617 """
1718 def __init__(self, message: str):
18 pass
19 self.message = message
20 super().__init__(message)
1921
2022
2123class CircularBuffer:
2224 def __init__(self, capacity: int):
23 pass
24
25 self.capacity = capacity
26 self.buffer = [None] * capacity # Fixed-size array
27 self.head = 0 # Points to oldest element (to read)
28 self.tail = 0 # Points to next write position
29 self.size = 0 # Current number of elements
30
2531 def read(self) -> str:
26 pass
27
32 # Edge Case: Buffer is empty when trying to read
33 if self.size == 0:
34 raise BufferEmptyException("Circular buffer is empty")
35
36 data = self.buffer[self.head]
37 self.buffer[self.head] = None # Clear the position
38 self.head = (self.head + 1) % self.capacity # Move head circularly
39 self.size -= 1
40 return data
41
2842 def write(self, data: str) -> None:
29 pass
30
43 # Edge Case: Buffer is full when trying to write
44 if self.size >= self.capacity:
45 raise BufferFullException("Circular buffer is full")
46
47 self.buffer[self.tail] = data
48 self.tail = (self.tail + 1) % self.capacity # Move tail circularly
49 self.size += 1
50
3151 def overwrite(self, data: str) -> None:
32 pass
33
52 # If buffer is full, overwrite oldest element by moving head
53 if self.size >= self.capacity:
54 self.head = (self.head + 1) % self.capacity # Move head to skip oldest
55 self.size -= 1
56
57 self.buffer[self.tail] = data
58 self.tail = (self.tail + 1) % self.capacity # Move tail circularly
59 self.size += 1
60
3461 def clear(self) -> None:
35 pass
62 self.buffer = [None] * self.capacity
63 self.head = 0
64 self.tail = 0
65 self.size = 0
66
67# Handled Edge Cases: Buffer is empty when trying to read; Buffer is full when trying to write; Buffer is full and overwrite removes oldest element before adding new one
Test NameStatus
test_a_read_frees_up_capacity_for_another_write
Pass
test_can_read_an_item_just_written
Pass
test_clear_does_nothing_on_empty_buffer
Pass
test_clear_frees_up_capacity_for_another_write
Pass
test_each_item_may_only_be_read_once
Pass
test_full_buffer_can_t_be_written_to
Pass
test_initial_clear_does_not_affect_wrapping_around
Pass
test_items_are_read_in_the_order_they_are_written
Pass
test_items_cleared_out_of_buffer_can_t_be_read
Pass
test_overwrite_acts_like_write_on_non_full_buffer
Pass
test_overwrite_replaces_the_oldest_item_on_full_buffer
Pass
test_overwrite_replaces_the_oldest_item_remaining_in_buffer_following_a_read
Pass
test_read_position_is_maintained_even_across_multiple_writes
Pass
test_reading_empty_buffer_should_fail
Pass

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