forked from exercism/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularBufferTest.java
More file actions
192 lines (158 loc) · 6.36 KB
/
CircularBufferTest.java
File metadata and controls
192 lines (158 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CircularBufferTest {
@Test
@DisplayName("reading empty buffer should fail")
public void readingFromEmptyBufferShouldThrowException() {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
assertThatExceptionOfType(BufferIOException.class)
.isThrownBy(buffer::read)
.withMessage("Tried to read from empty buffer");
}
@Disabled("Remove to run test")
@Test
@DisplayName("can read an item just written")
public void canReadItemJustWritten() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
buffer.write(1);
assertThat(buffer.read()).isEqualTo(1);
}
@Disabled("Remove to run test")
@Test
@DisplayName("each item may only be read once")
public void canReadItemOnlyOnce() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
buffer.write(1);
assertThat(buffer.read()).isEqualTo(1);
assertThatExceptionOfType(BufferIOException.class)
.isThrownBy(buffer::read)
.withMessage("Tried to read from empty buffer");
}
@Disabled("Remove to run test")
@Test
@DisplayName("items are read in the order they are written")
public void readsItemsInOrderWritten() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(2);
buffer.write(1);
buffer.write(2);
assertThat(buffer.read()).isEqualTo(1);
assertThat(buffer.read()).isEqualTo(2);
}
@Disabled("Remove to run test")
@Test
@DisplayName("full buffer can't be written to")
public void fullBufferCantBeWrittenTo() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
buffer.write(1);
assertThatExceptionOfType(BufferIOException.class)
.isThrownBy(() -> buffer.write(2))
.withMessage("Tried to write to full buffer");
}
@Disabled("Remove to run test")
@Test
@DisplayName("a read frees up capacity for another write")
public void readFreesUpSpaceForWrite() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
buffer.write(1);
assertThat(buffer.read()).isEqualTo(1);
buffer.write(2);
assertThat(buffer.read()).isEqualTo(2);
}
@Disabled("Remove to run test")
@Test
@DisplayName("read position is maintained even across multiple writes")
public void maintainsReadPositionAcrossWrites() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(3);
buffer.write(1);
buffer.write(2);
assertThat(buffer.read()).isEqualTo(1);
buffer.write(3);
assertThat(buffer.read()).isEqualTo(2);
assertThat(buffer.read()).isEqualTo(3);
}
@Disabled("Remove to run test")
@Test
@DisplayName("items cleared out of buffer can't be read")
public void cantReadClearedItems() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
buffer.write(1);
buffer.clear();
assertThatExceptionOfType(BufferIOException.class)
.isThrownBy(buffer::read)
.withMessage("Tried to read from empty buffer");
}
@Disabled("Remove to run test")
@Test
@DisplayName("clear frees up capacity for another write")
public void clearFreesUpCapacity() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
buffer.write(1);
buffer.clear();
buffer.write(2);
assertThat(buffer.read()).isEqualTo(2);
}
@Disabled("Remove to run test")
@Test
@DisplayName("clear does nothing on empty buffer")
public void clearDoesNothingOnEmptyBuffer() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
buffer.clear();
buffer.write(1);
assertThat(buffer.read()).isEqualTo(1);
}
@Disabled("Remove to run test")
@Test
@DisplayName("overwrite acts like write on non-full buffer")
public void overwriteActsLikeWriteOnNonFullBuffer() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(2);
buffer.write(1);
buffer.overwrite(2);
assertThat(buffer.read()).isEqualTo(1);
assertThat(buffer.read()).isEqualTo(2);
}
@Disabled("Remove to run test")
@Test
@DisplayName("overwrite replaces the oldest item on full buffer")
public void overwriteRemovesOldestElementOnFullBuffer() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(2);
buffer.write(1);
buffer.write(2);
buffer.overwrite(3);
assertThat(buffer.read()).isEqualTo(2);
assertThat(buffer.read()).isEqualTo(3);
}
@Disabled("Remove to run test")
@Test
@DisplayName("overwrite replaces the oldest item remaining in buffer following a read")
public void overwriteDoesntRemoveAnAlreadyReadElement() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(3);
buffer.write(1);
buffer.write(2);
buffer.write(3);
assertThat(buffer.read()).isEqualTo(1);
buffer.write(4);
buffer.overwrite(5);
assertThat(buffer.read()).isEqualTo(3);
assertThat(buffer.read()).isEqualTo(4);
assertThat(buffer.read()).isEqualTo(5);
}
@Disabled("Remove to run test")
@Test
@DisplayName("initial clear does not affect wrapping around")
public void initialClearDoesNotAffectWrappingAround() throws BufferIOException {
CircularBuffer<Integer> buffer = new CircularBuffer<>(2);
buffer.clear();
buffer.write(1);
buffer.write(2);
buffer.overwrite(3);
buffer.overwrite(4);
assertThat(buffer.read()).isEqualTo(3);
assertThat(buffer.read()).isEqualTo(4);
assertThatExceptionOfType(BufferIOException.class)
.isThrownBy(buffer::read)
.withMessage("Tried to read from empty buffer");
}
}