Skip to content

Commit 3605615

Browse files
committed
implement circular-buffer & make requested fixes
1 parent 33cb53f commit 3605615

7 files changed

Lines changed: 243 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,15 @@
468468

469469
]
470470
},
471+
{
472+
"slug": "circular-buffer",
473+
"difficulty": 8,
474+
"topics": [
475+
"Queues",
476+
"Classes",
477+
"Exception handling"
478+
]
479+
},
471480
{
472481
"slug": "rectangles",
473482
"difficulty": 8,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class BufferIOException extends Exception {
2+
3+
BufferIOException(String message) {
4+
super(message);
5+
}
6+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
public class CircularBuffer<T> {
5+
6+
private Queue<T> buffer;
7+
private int capacity;
8+
9+
public CircularBuffer(int size) {
10+
this.buffer = new LinkedList<>();
11+
this.capacity = size;
12+
}
13+
14+
public T read() throws BufferIOException {
15+
if (this.buffer.size() == 0) {
16+
throw new BufferIOException("Tried to read from empty buffer");
17+
}
18+
19+
return this.buffer.remove();
20+
}
21+
22+
public void write(T data) throws BufferIOException {
23+
if (this.buffer.size() == this.capacity) {
24+
throw new BufferIOException("Tried to write to full buffer");
25+
}
26+
27+
this.buffer.add(data);
28+
}
29+
public void overwrite(T data) {
30+
if (this.buffer.size() == this.capacity) {
31+
this.buffer.remove();
32+
}
33+
34+
this.buffer.add(data);
35+
}
36+
37+
public void clear() {
38+
this.buffer.clear();
39+
}
40+
}
41+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class BufferIOException extends Exception {
2+
3+
BufferIOException(String message) {
4+
super(message);
5+
}
6+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import static org.junit.Assert.assertEquals;
2+
3+
import org.junit.Ignore;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.junit.rules.ExpectedException;
7+
8+
public class CircularBufferTest {
9+
10+
@Rule
11+
public ExpectedException expectedException = ExpectedException.none();
12+
13+
@Test
14+
public void readingFromEmptyBufferShouldThrowException() throws BufferIOException {
15+
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
16+
17+
expectedException.expect(BufferIOException.class);
18+
expectedException.expectMessage("Tried to read from empty buffer");
19+
buffer.read();
20+
}
21+
22+
@Ignore("Remove to run test")
23+
@Test
24+
public void canReadItemJustWritten() throws BufferIOException {
25+
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
26+
27+
buffer.write(1);
28+
assertEquals((int) buffer.read(), 1);
29+
}
30+
31+
@Ignore("Remove to run test")
32+
@Test
33+
public void canReadItemOnlyOnce() throws BufferIOException {
34+
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
35+
36+
buffer.write(1);
37+
assertEquals((int) buffer.read(), 1);
38+
39+
expectedException.expect(BufferIOException.class);
40+
expectedException.expectMessage("Tried to read from empty buffer");
41+
buffer.read();
42+
}
43+
44+
@Ignore("Remove to run test")
45+
@Test
46+
public void readsItemsInOrderWritten() throws BufferIOException {
47+
CircularBuffer<Integer> buffer = new CircularBuffer<>(2);
48+
49+
buffer.write(1);
50+
buffer.write(2);
51+
assertEquals((int) buffer.read(), 1);
52+
assertEquals((int) buffer.read(), 2);
53+
}
54+
55+
@Ignore("Remove to run test")
56+
@Test
57+
public void fullBufferCantBeWrittenTo() throws BufferIOException {
58+
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
59+
60+
buffer.write(1);
61+
expectedException.expect(BufferIOException.class);
62+
expectedException.expectMessage("Tried to write to full buffer");
63+
buffer.write(2);
64+
}
65+
66+
@Ignore("Remove to run test")
67+
@Test
68+
public void readFreesUpSpaceForWrite() throws BufferIOException {
69+
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
70+
71+
buffer.write(1);
72+
assertEquals((int) buffer.read(), 1);
73+
buffer.write(2);
74+
assertEquals((int) buffer.read(), 2);
75+
}
76+
77+
@Ignore("Remove to run test")
78+
@Test
79+
public void maintainsReadPositionAcrossWrites() throws BufferIOException {
80+
CircularBuffer<Integer> buffer = new CircularBuffer<>(3);
81+
82+
buffer.write(1);
83+
buffer.write(2);
84+
assertEquals((int) buffer.read(), 1);
85+
buffer.write(3);
86+
assertEquals((int) buffer.read(), 2);
87+
assertEquals((int) buffer.read(), 3);
88+
}
89+
90+
@Ignore("Remove to run test")
91+
@Test
92+
public void cantReadClearedItems() throws BufferIOException {
93+
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
94+
95+
buffer.write(1);
96+
buffer.clear();
97+
expectedException.expect(BufferIOException.class);
98+
expectedException.expectMessage("Tried to read from empty buffer");
99+
buffer.read();
100+
}
101+
102+
@Ignore("Remove to run test")
103+
@Test
104+
public void clearFreesUpCapacity() throws BufferIOException {
105+
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
106+
107+
buffer.write(1);
108+
buffer.clear();
109+
buffer.write(2);
110+
assertEquals((int) buffer.read(), 2);
111+
}
112+
113+
@Ignore("Remove to run test")
114+
@Test
115+
public void clearDoesNothingOnEmptyBuffer() throws BufferIOException {
116+
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
117+
118+
buffer.clear();
119+
buffer.write(1);
120+
assertEquals((int) buffer.read(), 1);
121+
}
122+
123+
@Ignore("Remove to run test")
124+
@Test
125+
public void overwriteActsLikeWriteOnNonFullBuffer() throws BufferIOException {
126+
CircularBuffer<Integer> buffer = new CircularBuffer<>(2);
127+
128+
buffer.write(1);
129+
buffer.overwrite(2);
130+
assertEquals((int) buffer.read(), 1);
131+
assertEquals((int) buffer.read(), 2);
132+
}
133+
134+
@Ignore("Remove to run test")
135+
@Test
136+
public void overwriteRemovesOldestElementOnFullBuffer() throws BufferIOException {
137+
CircularBuffer<Integer> buffer = new CircularBuffer<>(2);
138+
139+
buffer.write(1);
140+
buffer.write(2);
141+
buffer.overwrite(3);
142+
assertEquals((int) buffer.read(), 2);
143+
assertEquals((int) buffer.read(), 3);
144+
}
145+
146+
@Ignore("Remove to run test")
147+
@Test
148+
public void overwriteDoesntRemoveAnAlreadyReadElement() throws BufferIOException {
149+
CircularBuffer<Integer> buffer = new CircularBuffer<>(3);
150+
151+
buffer.write(1);
152+
buffer.write(2);
153+
buffer.write(3);
154+
assertEquals((int) buffer.read(), 1);
155+
buffer.write(4);
156+
buffer.overwrite(5);
157+
assertEquals((int) buffer.read(), 3);
158+
assertEquals((int) buffer.read(), 4);
159+
assertEquals((int) buffer.read(), 5);
160+
}
161+
}
162+

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ include 'book-store'
1414
include 'bowling'
1515
include 'bracket-push'
1616
include 'change'
17+
include 'circular-buffer'
1718
include 'clock'
1819
include 'collatz-conjecture'
1920
include 'complex-numbers'

0 commit comments

Comments
 (0)