29package com.jogamp.common.util;
32import org.junit.Assert;
35import com.jogamp.common.util.Ringbuffer;
38 private static boolean DEBUG =
false;
44 final Integer[] array =
new Integer[capacity];
45 for(
int i=0; i<capacity; i++) {
46 array[i] = Integer.valueOf(startValue+i);
51 private void readTestImpl(
final Ringbuffer<Integer> rb,
final boolean clearRef,
final int capacity,
final int len,
final int startValue) {
52 final int preSize = rb.
size();
53 Assert.assertEquals(
"Wrong capacity "+rb, capacity, rb.
capacity());
54 Assert.assertTrue(
"Too low capacity to read "+len+
" elems: "+rb, capacity-len >= 0);
55 Assert.assertTrue(
"Too low size to read "+len+
" elems: "+rb, preSize >= len);
56 Assert.assertTrue(
"Is empty "+rb, !rb.
isEmpty());
58 for(
int i=0; i<len; i++) {
59 final Integer vI = rb.
get();
60 Assert.assertNotNull(
"Empty at read #"+(i+1)+
": "+rb, vI);
61 Assert.assertEquals(
"Wrong value at read #"+(i+1)+
": "+rb, startValue+i, vI.intValue());
64 Assert.assertEquals(
"Invalid size "+rb, preSize-len, rb.
size());
65 Assert.assertTrue(
"Invalid free slots after reading "+len+
": "+rb, rb.
getFreeSlots()>= len);
66 Assert.assertTrue(
"Is full "+rb, !rb.
isFull());
69 private void writeTestImpl(
final Ringbuffer<Integer> rb,
final int capacity,
final int len,
final int startValue) {
70 final int preSize = rb.size();
72 Assert.assertEquals(
"Wrong capacity "+rb, capacity, rb.capacity());
73 Assert.assertTrue(
"Too low capacity to write "+len+
" elems: "+rb, capacity-len >= 0);
74 Assert.assertTrue(
"Too low size to write "+len+
" elems: "+rb, preSize+len <= capacity);
75 Assert.assertTrue(
"Is full "+rb, !rb.isFull());
77 for(
int i=0; i<len; i++) {
78 Assert.assertTrue(
"Buffer is full at put #"+i+
": "+rb, rb.put( Integer.valueOf(startValue+i) ));
81 Assert.assertEquals(
"Invalid size "+rb, preSize+len, rb.size());
82 Assert.assertTrue(
"Is empty "+rb, !rb.isEmpty());
85 private void moveGetPutImpl(
final Ringbuffer<Integer> rb,
final int pos) {
86 Assert.assertTrue(
"RB is empty "+rb, !rb.isEmpty());
87 for(
int i=0; i<pos; i++) {
88 Assert.assertEquals(
"MoveFull.get failed "+rb, i, rb.get().intValue());
89 Assert.assertTrue(
"MoveFull.put failed "+rb, rb.put(i));
93 private void movePutGetImpl(
final Ringbuffer<Integer> rb,
final int pos) {
94 Assert.assertTrue(
"RB is full "+rb, !rb.isFull());
95 for(
int i=0; i<pos; i++) {
96 Assert.assertTrue(
"MoveEmpty.put failed "+rb, rb.put(600+i));
97 Assert.assertEquals(
"MoveEmpty.get failed "+rb, 600+i, rb.get().intValue());
103 final int capacity = 11;
106 Assert.assertEquals(
"Not full size "+rb, capacity, rb.
size());
107 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
109 readTestImpl(rb,
true, capacity, capacity, 0);
110 Assert.assertTrue(
"Not empty "+rb, rb.
isEmpty());
115 final int capacity = 11;
117 Assert.assertEquals(
"Not zero size "+rb, 0, rb.
size());
118 Assert.assertTrue(
"Not empty "+rb, rb.
isEmpty());
120 writeTestImpl(rb, capacity, capacity, 0);
121 Assert.assertEquals(
"Not full size "+rb, capacity, rb.
size());
122 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
124 readTestImpl(rb,
true, capacity, capacity, 0);
125 Assert.assertTrue(
"Not empty "+rb, rb.
isEmpty());
130 final int capacity = 11;
133 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
136 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
138 readTestImpl(rb,
false, capacity, capacity, 0);
139 Assert.assertTrue(
"Not empty "+rb, rb.
isEmpty());
142 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
144 readTestImpl(rb,
false, capacity, capacity, 0);
145 Assert.assertTrue(
"Not empty "+rb, rb.
isEmpty());
150 final int capacity = 11;
152 Assert.assertTrue(
"Not empty "+rb, rb.
isEmpty());
155 Assert.assertTrue(
"Not empty "+rb, rb.
isEmpty());
157 writeTestImpl(rb, capacity, capacity, 0);
158 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
160 readTestImpl(rb,
false, capacity, capacity, 0);
161 Assert.assertTrue(
"Not empty "+rb, rb.
isEmpty());
164 Assert.assertTrue(
"Not empty "+rb, rb.
isEmpty());
166 writeTestImpl(rb, capacity, capacity, 0);
167 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
169 readTestImpl(rb,
false, capacity, capacity, 0);
170 Assert.assertTrue(
"Not empty "+rb, rb.
isEmpty());
175 final int capacity = 11;
178 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
181 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
183 readTestImpl(rb,
false, capacity, 5, 0);
184 Assert.assertTrue(
"Is empty "+rb, !rb.
isEmpty());
185 Assert.assertTrue(
"Is Full "+rb, !rb.
isFull());
188 rb.
dump(System.err,
"ReadReset01["+5+
"].pre0");
191 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
193 rb.
dump(System.err,
"ReadReset01["+5+
"].post");
196 readTestImpl(rb,
false, capacity, capacity, 0);
197 Assert.assertTrue(
"Not empty "+rb, rb.
isEmpty());
202 final int capacity = 11;
205 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
208 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
210 moveGetPutImpl(rb, 5);
216 rb.
dump(System.err,
"ReadReset02["+5+
"].pre0");
219 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
221 rb.
dump(System.err,
"ReadReset02["+5+
"].post");
224 readTestImpl(rb,
false, capacity, capacity, 0);
225 Assert.assertTrue(
"Not empty "+rb, rb.
isEmpty());
228 private void test_GrowEmptyImpl(
final int initCapacity,
final int pos) {
229 final int growAmount = 5;
230 final int grownCapacity = initCapacity+growAmount;
231 final Integer[] growArray =
new Integer[growAmount];
232 for(
int i=0; i<growAmount; i++) {
233 growArray[i] = Integer.valueOf(100+i);
235 final Ringbuffer<Integer> rb =
createEmpty(initCapacity);
238 rb.dump(System.err,
"GrowEmpty["+pos+
"].pre0");
240 movePutGetImpl(rb, pos);
242 rb.dump(System.err,
"GrowEmpty["+pos+
"].pre1");
244 rb.growEmptyBuffer(growArray);
246 rb.dump(System.err,
"GrowEmpty["+pos+
"].post");
249 Assert.assertEquals(
"Wrong capacity "+rb, grownCapacity, rb.capacity());
250 Assert.assertEquals(
"Not growAmount size "+rb, growAmount, rb.size());
251 Assert.assertTrue(
"Is full "+rb, !rb.isFull());
252 Assert.assertTrue(
"Is empty "+rb, !rb.isEmpty());
254 for(
int i=0; i<growAmount; i++) {
255 final Integer vI = rb.get();
256 Assert.assertNotNull(
"Empty at read #"+(i+1)+
": "+rb, vI);
257 Assert.assertEquals(
"Wrong value at read #"+(i+1)+
": "+rb, 100+i, vI.intValue());
260 Assert.assertEquals(
"Not zero size "+rb, 0, rb.size());
261 Assert.assertTrue(
"Not empty "+rb, rb.isEmpty());
262 Assert.assertTrue(
"Is full "+rb, !rb.isFull());
266 test_GrowEmptyImpl(11, 0);
270 test_GrowEmptyImpl(11, 0+2);
274 test_GrowEmptyImpl(11, 11-1);
278 test_GrowEmptyImpl(11, 11-1-2);
281 private void test_GrowFullImpl(
final int initCapacity,
final int pos,
final boolean debug) {
282 final int growAmount = 5;
283 final int grownCapacity = initCapacity+growAmount;
287 if( DEBUG || debug ) {
288 rb.
dump(System.err,
"GrowFull["+pos+
"].pre0");
290 moveGetPutImpl(rb, pos);
291 if( DEBUG || debug ) {
292 rb.
dump(System.err,
"GrowFull["+pos+
"].pre1");
295 if( DEBUG || debug ) {
296 rb.
dump(System.err,
"GrowFull["+pos+
"].post");
299 Assert.assertEquals(
"Wrong capacity "+rb, grownCapacity, rb.
capacity());
300 Assert.assertEquals(
"Not orig size "+rb, initCapacity, rb.
size());
301 Assert.assertTrue(
"Is full "+rb, !rb.
isFull());
302 Assert.assertTrue(
"Is empty "+rb, !rb.
isEmpty());
304 for(
int i=0; i<growAmount; i++) {
305 Assert.assertTrue(
"Buffer is full at put #"+i+
": "+rb, rb.
put( Integer.valueOf(100+i) ));
307 Assert.assertEquals(
"Not new size "+rb, grownCapacity, rb.
size());
308 Assert.assertTrue(
"Not full "+rb, rb.
isFull());
310 for(
int i=0; i<initCapacity; i++) {
311 final Integer vI = rb.
get();
312 Assert.assertNotNull(
"Empty at read #"+(i+1)+
": "+rb, vI);
313 Assert.assertEquals(
"Wrong value at read #"+(i+1)+
": "+rb, (pos+i)%initCapacity, vI.intValue());
315 for(
int i=0; i<growAmount; i++) {
316 final Integer vI = rb.
get();
317 Assert.assertNotNull(
"Empty at read #"+(i+1)+
": "+rb, vI);
318 Assert.assertEquals(
"Wrong value at read #"+(i+1)+
": "+rb, 100+i, vI.intValue());
321 Assert.assertEquals(
"Not zero size "+rb, 0, rb.
size());
322 Assert.assertTrue(
"Not empty "+rb, rb.
isEmpty());
323 Assert.assertTrue(
"Is full "+rb, !rb.
isFull());
327 test_GrowFullImpl(11, 0,
false);
331 test_GrowFullImpl(11, 0+1,
false);
335 test_GrowFullImpl(11, 0+2,
false);
339 test_GrowFullImpl(11, 0+3,
false);
343 test_GrowFullImpl(11, 11-1,
false);
347 test_GrowFullImpl(11, 11-1-1,
false);
351 test_GrowFullImpl(11, 11-1-2,
false);
355 test_GrowFullImpl(11, 11-1-3,
false);
void test06_ReadResetMid02()
void test11_GrowEmpty02_Begin2()
void test13_GrowEmpty04_End2()
void test25_GrowFull11_End1()
void test03_FullReadReset()
void test23_GrowFull04_Begin3()
abstract Ringbuffer< Integer > createEmpty(int initialCapacity)
void test24_GrowFull05_End()
void test27_GrowFull13_End3()
void test12_GrowEmpty03_End()
Integer[] createIntArray(final int capacity, final int startValue)
abstract Ringbuffer< Integer > createFull(Integer[] source)
void test20_GrowFull01_Begin()
void test26_GrowFull12_End2()
void test21_GrowFull02_Begin1()
void test04_EmptyWriteClear()
void test22_GrowFull03_Begin2()
void test05_ReadResetMid01()
void test10_GrowEmpty01_Begin()
Ring buffer interface, a.k.a circular buffer.
void clear()
Resets the read and write position according to an empty ring buffer and set all ring buffer slots to...
T get()
Dequeues the oldest enqueued element if available, otherwise null.
int size()
Returns the number of elements in this ring buffer.
boolean isFull()
Returns true if this ring buffer is full, otherwise false.
boolean isEmpty()
Returns true if this ring buffer is empty, otherwise false.
void resetFull(T[] copyFrom)
Resets the read and write position according to a full ring buffer and fill all slots w/ elements of ...
int getFreeSlots()
Returns the number of free slots available to put.
int capacity()
Returns the net capacity of this ring buffer.
void growFullBuffer(int amount)
Grows a full ring buffer, increasing it's capacity about the amount.
boolean put(T e)
Enqueues the given element.
void dump(PrintStream stream, String prefix)
Debug functionality - Dumps the contents of the internal array.