Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,15 @@

]
},
{
"slug": "circular-buffer",
"difficulty": 8,
"topics": [
"Queues",
"Classes",
"Exception handling"
]
},
{
"slug": "rectangles",
"difficulty": 8,
Expand Down
18 changes: 18 additions & 0 deletions exercises/circular-buffer/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

repositories {
mavenCentral()
}

dependencies {
testCompile "junit:junit:4.12"
}

test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class BufferIOException extends Exception {

BufferIOException(String message) {
super(message);
}
}
41 changes: 41 additions & 0 deletions exercises/circular-buffer/src/example/java/CircularBuffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.LinkedList;
import java.util.Queue;

public class CircularBuffer<T> {

private Queue<T> buffer;
private int capacity;

public CircularBuffer(int size) {
this.buffer = new LinkedList<>();
this.capacity = size;
}

public T read() throws BufferIOException {
if (this.buffer.size() == 0) {
throw new BufferIOException("Tried to read from empty buffer");
}

return this.buffer.remove();
}

public void write(T data) throws BufferIOException {
if (this.buffer.size() == this.capacity) {
throw new BufferIOException("Tried to write to full buffer");
}

this.buffer.add(data);
}
public void overwrite(T data) {
if (this.buffer.size() == this.capacity) {
this.buffer.remove();
}

this.buffer.add(data);
}

public void clear() {
this.buffer.clear();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class BufferIOException extends Exception {

BufferIOException(String message) {
super(message);
}
}
163 changes: 163 additions & 0 deletions exercises/circular-buffer/src/test/java/CircularBufferTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class CircularBufferTest {

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test
public void readingFromEmptyBufferShouldThrowException() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);

expectedException.expect(BufferIOException.class);
expectedException.expectMessage("Tried to read from empty buffer");
buffer.read();
}

@Ignore("Remove to run test")
@Test
public void canReadItemJustWritten() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);

buffer.write(1);
assertThat(buffer.read(), is(1));
}

@Ignore("Remove to run test")
@Test
public void canReadItemOnlyOnce() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);

buffer.write(1);
assertThat(buffer.read(), is(1));

expectedException.expect(BufferIOException.class);
expectedException.expectMessage("Tried to read from empty buffer");
buffer.read();
}

@Ignore("Remove to run test")
@Test
public void readsItemsInOrderWritten() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(2);

buffer.write(1);
buffer.write(2);
assertThat(buffer.read(), is(1));
assertThat(buffer.read(), is(2));
}

@Ignore("Remove to run test")
@Test
public void fullBufferCantBeWrittenTo() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);

buffer.write(1);
expectedException.expect(BufferIOException.class);
expectedException.expectMessage("Tried to write to full buffer");
buffer.write(2);
}

@Ignore("Remove to run test")
@Test
public void readFreesUpSpaceForWrite() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);

buffer.write(1);
assertThat(buffer.read(), is(1));
buffer.write(2);
assertThat(buffer.read(), is(2));
}

@Ignore("Remove to run test")
@Test
public void maintainsReadPositionAcrossWrites() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(3);

buffer.write(1);
buffer.write(2);
assertThat(buffer.read(), is(1));
buffer.write(3);
assertThat(buffer.read(), is(2));
assertThat(buffer.read(), is(3));
}

@Ignore("Remove to run test")
@Test
public void cantReadClearedItems() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);

buffer.write(1);
buffer.clear();
expectedException.expect(BufferIOException.class);
expectedException.expectMessage("Tried to read from empty buffer");
buffer.read();
}

@Ignore("Remove to run test")
@Test
public void clearFreesUpCapacity() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);

buffer.write(1);
buffer.clear();
buffer.write(2);
assertThat(buffer.read(), is(2));
}

@Ignore("Remove to run test")
@Test
public void clearDoesNothingOnEmptyBuffer() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);

buffer.clear();
buffer.write(1);
assertThat(buffer.read(), is(1));
}

@Ignore("Remove to run test")
@Test
public void overwriteActsLikeWriteOnNonFullBuffer() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(2);

buffer.write(1);
buffer.overwrite(2);
assertThat(buffer.read(), is(1));
assertThat(buffer.read(), is(2));
}

@Ignore("Remove to run test")
@Test
public void overwriteRemovesOldestElementOnFullBuffer() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(2);

buffer.write(1);
buffer.write(2);
buffer.overwrite(3);
assertThat(buffer.read(), is(2));
assertThat(buffer.read(), is(3));
}

@Ignore("Remove to run test")
@Test
public void overwriteDoesntRemoveAnAlreadyReadElement() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(3);

buffer.write(1);
buffer.write(2);
buffer.write(3);
assertThat(buffer.read(), is(1));
buffer.write(4);
buffer.overwrite(5);
assertThat(buffer.read(), is(3));
assertThat(buffer.read(), is(4));
assertThat(buffer.read(), is(5));
}
}

1 change: 1 addition & 0 deletions exercises/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ include 'book-store'
include 'bowling'
include 'bracket-push'
include 'change'
include 'circular-buffer'
include 'clock'
include 'collatz-conjecture'
include 'complex-numbers'
Expand Down