30import org.jau.util.Ringbuffer;
31import org.junit.Assert;
35 private static boolean DEBUG =
false;
37 public abstract Ringbuffer<Integer>
createEmpty(
int initialCapacity);
38 public abstract Ringbuffer<Integer>
createFull(Integer[] source);
41 final Integer[] array =
new Integer[capacity];
42 for(
int i=0; i<capacity; i++) {
43 array[i] = Integer.valueOf(startValue+i);
48 private void readTestImpl(
final Ringbuffer<Integer> rb,
final boolean clearRef,
final int capacity,
final int len,
final int startValue) {
49 final int preSize = rb.size();
50 Assert.assertEquals(
"Wrong capacity "+rb, capacity, rb.capacity());
51 Assert.assertTrue(
"Too low capacity to read "+len+
" elems: "+rb, capacity-len >= 0);
52 Assert.assertTrue(
"Too low size to read "+len+
" elems: "+rb, preSize >= len);
53 Assert.assertTrue(
"Is empty "+rb, !rb.isEmpty());
55 for(
int i=0; i<len; i++) {
56 final Integer vI = rb.get();
57 Assert.assertNotNull(
"Empty at read #"+(i+1)+
": "+rb, vI);
58 Assert.assertEquals(
"Wrong value at read #"+(i+1)+
": "+rb, startValue+i, vI.intValue());
61 Assert.assertEquals(
"Invalid size "+rb, preSize-len, rb.size());
62 Assert.assertTrue(
"Invalid free slots after reading "+len+
": "+rb, rb.getFreeSlots()>= len);
63 Assert.assertTrue(
"Is full "+rb, !rb.isFull());
66 private void writeTestImpl(
final Ringbuffer<Integer> rb,
final int capacity,
final int len,
final int startValue) {
67 final int preSize = rb.size();
69 Assert.assertEquals(
"Wrong capacity "+rb, capacity, rb.capacity());
70 Assert.assertTrue(
"Too low capacity to write "+len+
" elems: "+rb, capacity-len >= 0);
71 Assert.assertTrue(
"Too low size to write "+len+
" elems: "+rb, preSize+len <= capacity);
72 Assert.assertTrue(
"Is full "+rb, !rb.isFull());
74 for(
int i=0; i<len; i++) {
75 Assert.assertTrue(
"Buffer is full at put #"+i+
": "+rb, rb.put( Integer.valueOf(startValue+i) ));
78 Assert.assertEquals(
"Invalid size "+rb, preSize+len, rb.size());
79 Assert.assertTrue(
"Is empty "+rb, !rb.isEmpty());
82 private void moveGetPutImpl(
final Ringbuffer<Integer> rb,
final int pos) {
83 Assert.assertTrue(
"RB is empty "+rb, !rb.isEmpty());
84 for(
int i=0; i<pos; i++) {
85 Assert.assertEquals(
"MoveFull.get failed "+rb, i, rb.get().intValue());
86 Assert.assertTrue(
"MoveFull.put failed "+rb, rb.put(i));
90 private void movePutGetImpl(
final Ringbuffer<Integer> rb,
final int pos) {
91 Assert.assertTrue(
"RB is full "+rb, !rb.isFull());
92 for(
int i=0; i<pos; i++) {
93 Assert.assertTrue(
"MoveEmpty.put failed "+rb, rb.put(600+i));
94 Assert.assertEquals(
"MoveEmpty.get failed "+rb, 600+i, rb.get().intValue());
100 final int capacity = 11;
102 final Ringbuffer<Integer> rb =
createFull(source);
103 Assert.assertEquals(
"Not full size "+rb, capacity, rb.size());
104 Assert.assertTrue(
"Not full "+rb, rb.isFull());
106 readTestImpl(rb,
true, capacity, capacity, 0);
107 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
112 final int capacity = 11;
113 final Ringbuffer<Integer> rb =
createEmpty(capacity);
114 Assert.assertEquals(
"Not zero size "+rb, 0, rb.size());
115 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
117 writeTestImpl(rb, capacity, capacity, 0);
118 Assert.assertEquals(
"Not full size "+rb, capacity, rb.size());
119 Assert.assertTrue(
"Not full "+rb, rb.isFull());
121 readTestImpl(rb,
true, capacity, capacity, 0);
122 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
127 final int capacity = 11;
129 final Ringbuffer<Integer> rb =
createFull(source);
130 Assert.assertTrue(
"Not full "+rb, rb.isFull());
132 rb.resetFull(source);
133 Assert.assertTrue(
"Not full "+rb, rb.isFull());
135 readTestImpl(rb,
false, capacity, capacity, 0);
136 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
138 rb.resetFull(source);
139 Assert.assertTrue(
"Not full "+rb, rb.isFull());
141 readTestImpl(rb,
false, capacity, capacity, 0);
142 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
147 final int capacity = 11;
148 final Ringbuffer<Integer> rb =
createEmpty(capacity);
149 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
152 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
154 writeTestImpl(rb, capacity, capacity, 0);
155 Assert.assertTrue(
"Not full "+rb, rb.isFull());
157 readTestImpl(rb,
false, capacity, capacity, 0);
158 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
161 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
163 writeTestImpl(rb, capacity, capacity, 0);
164 Assert.assertTrue(
"Not full "+rb, rb.isFull());
166 readTestImpl(rb,
false, capacity, capacity, 0);
167 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
172 final int capacity = 11;
174 final Ringbuffer<Integer> rb =
createFull(source);
175 Assert.assertTrue(
"Not full "+rb, rb.isFull());
177 rb.resetFull(source);
178 Assert.assertTrue(
"Not full "+rb, rb.isFull());
180 readTestImpl(rb,
false, capacity, 5, 0);
181 Assert.assertTrue(
"Is empty "+rb, !rb.isEmpty());
182 Assert.assertTrue(
"Is Full "+rb, !rb.isFull());
185 rb.dump(System.err,
"ReadReset01["+5+
"].pre0");
187 rb.resetFull(source);
188 Assert.assertTrue(
"Not full "+rb, rb.isFull());
190 rb.dump(System.err,
"ReadReset01["+5+
"].post");
193 readTestImpl(rb,
false, capacity, capacity, 0);
194 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
199 final int capacity = 11;
201 final Ringbuffer<Integer> rb =
createFull(source);
202 Assert.assertTrue(
"Not full "+rb, rb.isFull());
204 rb.resetFull(source);
205 Assert.assertTrue(
"Not full "+rb, rb.isFull());
207 moveGetPutImpl(rb, 5);
213 rb.dump(System.err,
"ReadReset02["+5+
"].pre0");
215 rb.resetFull(source);
216 Assert.assertTrue(
"Not full "+rb, rb.isFull());
218 rb.dump(System.err,
"ReadReset02["+5+
"].post");
221 readTestImpl(rb,
false, capacity, capacity, 0);
222 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
225 private void test_GrowEmptyImpl(
final int initCapacity,
final int pos) {
226 final int growAmount = 5;
227 final int grownCapacity = initCapacity+growAmount;
228 final Integer[] growArray =
new Integer[growAmount];
229 for(
int i=0; i<growAmount; i++) {
230 growArray[i] = Integer.valueOf(100+i);
232 final Ringbuffer<Integer> rb =
createEmpty(initCapacity);
235 rb.dump(System.err,
"GrowEmpty["+pos+
"].pre0");
237 movePutGetImpl(rb, pos);
239 rb.dump(System.err,
"GrowEmpty["+pos+
"].pre1");
241 rb.growEmptyBuffer(growArray);
243 rb.dump(System.err,
"GrowEmpty["+pos+
"].post");
246 Assert.assertEquals(
"Wrong capacity "+rb, grownCapacity, rb.capacity());
247 Assert.assertEquals(
"Not growAmount size "+rb, growAmount, rb.size());
248 Assert.assertTrue(
"Is full "+rb, !rb.isFull());
249 Assert.assertTrue(
"Is empty "+rb, !rb.isEmpty());
251 for(
int i=0; i<growAmount; i++) {
252 final Integer vI = rb.get();
253 Assert.assertNotNull(
"Empty at read #"+(i+1)+
": "+rb, vI);
254 Assert.assertEquals(
"Wrong value at read #"+(i+1)+
": "+rb, 100+i, vI.intValue());
257 Assert.assertEquals(
"Not zero size "+rb, 0, rb.size());
258 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
259 Assert.assertTrue(
"Is full "+rb, !rb.isFull());
263 test_GrowEmptyImpl(11, 0);
267 test_GrowEmptyImpl(11, 0+2);
271 test_GrowEmptyImpl(11, 11-1);
275 test_GrowEmptyImpl(11, 11-1-2);
278 private void test_GrowFullImpl(
final int initCapacity,
final int pos,
final boolean debug) {
279 final int growAmount = 5;
280 final int grownCapacity = initCapacity+growAmount;
282 final Ringbuffer<Integer> rb =
createFull(source);
284 if( DEBUG || debug ) {
285 rb.dump(System.err,
"GrowFull["+pos+
"].pre0");
287 moveGetPutImpl(rb, pos);
288 if( DEBUG || debug ) {
289 rb.dump(System.err,
"GrowFull["+pos+
"].pre1");
291 rb.growFullBuffer(growAmount);
292 if( DEBUG || debug ) {
293 rb.dump(System.err,
"GrowFull["+pos+
"].post");
296 Assert.assertEquals(
"Wrong capacity "+rb, grownCapacity, rb.capacity());
297 Assert.assertEquals(
"Not orig size "+rb, initCapacity, rb.size());
298 Assert.assertTrue(
"Is full "+rb, !rb.isFull());
299 Assert.assertTrue(
"Is empty "+rb, !rb.isEmpty());
301 for(
int i=0; i<growAmount; i++) {
302 Assert.assertTrue(
"Buffer is full at put #"+i+
": "+rb, rb.put( Integer.valueOf(100+i) ));
304 Assert.assertEquals(
"Not new size "+rb, grownCapacity, rb.size());
305 Assert.assertTrue(
"Not full "+rb, rb.isFull());
307 for(
int i=0; i<initCapacity; i++) {
308 final Integer vI = rb.get();
309 Assert.assertNotNull(
"Empty at read #"+(i+1)+
": "+rb, vI);
310 Assert.assertEquals(
"Wrong value at read #"+(i+1)+
": "+rb, (pos+i)%initCapacity, vI.intValue());
312 for(
int i=0; i<growAmount; i++) {
313 final Integer vI = rb.get();
314 Assert.assertNotNull(
"Empty at read #"+(i+1)+
": "+rb, vI);
315 Assert.assertEquals(
"Wrong value at read #"+(i+1)+
": "+rb, 100+i, vI.intValue());
318 Assert.assertEquals(
"Not zero size "+rb, 0, rb.size());
319 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
320 Assert.assertTrue(
"Is full "+rb, !rb.isFull());
324 test_GrowFullImpl(11, 0,
false);
328 test_GrowFullImpl(11, 0+1,
false);
332 test_GrowFullImpl(11, 0+2,
false);
336 test_GrowFullImpl(11, 0+3,
false);
340 test_GrowFullImpl(11, 11-1,
false);
344 test_GrowFullImpl(11, 11-1-1,
false);
348 test_GrowFullImpl(11, 11-1-2,
false);
352 test_GrowFullImpl(11, 11-1-3,
false);
void test21_GrowFull02_Begin1()
void test13_GrowEmpty04_End2()
void test06_ReadResetMid02()
abstract Ringbuffer< Integer > createFull(Integer[] source)
void test10_GrowEmpty01_Begin()
void test26_GrowFull12_End2()
void test25_GrowFull11_End1()
abstract Ringbuffer< Integer > createEmpty(int initialCapacity)
void test03_FullReadReset()
void test11_GrowEmpty02_Begin2()
void test05_ReadResetMid01()
void test12_GrowEmpty03_End()
void test04_EmptyWriteClear()
Integer[] createIntArray(final int capacity, final int startValue)
void test22_GrowFull03_Begin2()
void test27_GrowFull13_End3()
void test20_GrowFull01_Begin()
void test24_GrowFull05_End()
void test23_GrowFull04_Begin3()