GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
RingBuffer01Base.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.common.util;
30
31
32import org.junit.Assert;
33import org.junit.Test;
34
35import com.jogamp.common.util.Ringbuffer;
36
37public abstract class RingBuffer01Base {
38 private static boolean DEBUG = false;
39
40 public abstract Ringbuffer<Integer> createEmpty(int initialCapacity);
41 public abstract Ringbuffer<Integer> createFull(Integer[] source);
42
43 public Integer[] createIntArray(final int capacity, final int startValue) {
44 final Integer[] array = new Integer[capacity];
45 for(int i=0; i<capacity; i++) {
46 array[i] = Integer.valueOf(startValue+i);
47 }
48 return array;
49 }
50
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());
57
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());
62 }
63
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());
67 }
68
69 private void writeTestImpl(final Ringbuffer<Integer> rb, final int capacity, final int len, final int startValue) {
70 final int preSize = rb.size();
71
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());
76
77 for(int i=0; i<len; i++) {
78 Assert.assertTrue("Buffer is full at put #"+i+": "+rb, rb.put( Integer.valueOf(startValue+i) ));
79 }
80
81 Assert.assertEquals("Invalid size "+rb, preSize+len, rb.size());
82 Assert.assertTrue("Is empty "+rb, !rb.isEmpty());
83 }
84
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));
90 }
91 }
92
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());
98 }
99 }
100
101 @Test
102 public void test01_FullRead() {
103 final int capacity = 11;
104 final Integer[] source = createIntArray(capacity, 0);
105 final Ringbuffer<Integer> rb = createFull(source);
106 Assert.assertEquals("Not full size "+rb, capacity, rb.size());
107 Assert.assertTrue("Not full "+rb, rb.isFull());
108
109 readTestImpl(rb, true, capacity, capacity, 0);
110 Assert.assertTrue("Not empty "+rb, rb.isEmpty());
111 }
112
113 @Test
114 public void test02_EmptyWrite() {
115 final int capacity = 11;
116 final Ringbuffer<Integer> rb = createEmpty(capacity);
117 Assert.assertEquals("Not zero size "+rb, 0, rb.size());
118 Assert.assertTrue("Not empty "+rb, rb.isEmpty());
119
120 writeTestImpl(rb, capacity, capacity, 0);
121 Assert.assertEquals("Not full size "+rb, capacity, rb.size());
122 Assert.assertTrue("Not full "+rb, rb.isFull());
123
124 readTestImpl(rb, true, capacity, capacity, 0);
125 Assert.assertTrue("Not empty "+rb, rb.isEmpty());
126 }
127
128 @Test
129 public void test03_FullReadReset() {
130 final int capacity = 11;
131 final Integer[] source = createIntArray(capacity, 0);
132 final Ringbuffer<Integer> rb = createFull(source);
133 Assert.assertTrue("Not full "+rb, rb.isFull());
134
135 rb.resetFull(source);
136 Assert.assertTrue("Not full "+rb, rb.isFull());
137
138 readTestImpl(rb, false, capacity, capacity, 0);
139 Assert.assertTrue("Not empty "+rb, rb.isEmpty());
140
141 rb.resetFull(source);
142 Assert.assertTrue("Not full "+rb, rb.isFull());
143
144 readTestImpl(rb, false, capacity, capacity, 0);
145 Assert.assertTrue("Not empty "+rb, rb.isEmpty());
146 }
147
148 @Test
150 final int capacity = 11;
151 final Ringbuffer<Integer> rb = createEmpty(capacity);
152 Assert.assertTrue("Not empty "+rb, rb.isEmpty());
153
154 rb.clear();
155 Assert.assertTrue("Not empty "+rb, rb.isEmpty());
156
157 writeTestImpl(rb, capacity, capacity, 0);
158 Assert.assertTrue("Not full "+rb, rb.isFull());
159
160 readTestImpl(rb, false, capacity, capacity, 0);
161 Assert.assertTrue("Not empty "+rb, rb.isEmpty());
162
163 rb.clear();
164 Assert.assertTrue("Not empty "+rb, rb.isEmpty());
165
166 writeTestImpl(rb, capacity, capacity, 0);
167 Assert.assertTrue("Not full "+rb, rb.isFull());
168
169 readTestImpl(rb, false, capacity, capacity, 0);
170 Assert.assertTrue("Not empty "+rb, rb.isEmpty());
171 }
172
173 @Test
174 public void test05_ReadResetMid01() {
175 final int capacity = 11;
176 final Integer[] source = createIntArray(capacity, 0);
177 final Ringbuffer<Integer> rb = createFull(source);
178 Assert.assertTrue("Not full "+rb, rb.isFull());
179
180 rb.resetFull(source);
181 Assert.assertTrue("Not full "+rb, rb.isFull());
182
183 readTestImpl(rb, false, capacity, 5, 0);
184 Assert.assertTrue("Is empty "+rb, !rb.isEmpty());
185 Assert.assertTrue("Is Full "+rb, !rb.isFull());
186
187 if( DEBUG ) {
188 rb.dump(System.err, "ReadReset01["+5+"].pre0");
189 }
190 rb.resetFull(source);
191 Assert.assertTrue("Not full "+rb, rb.isFull());
192 if( DEBUG ) {
193 rb.dump(System.err, "ReadReset01["+5+"].post");
194 }
195
196 readTestImpl(rb, false, capacity, capacity, 0);
197 Assert.assertTrue("Not empty "+rb, rb.isEmpty());
198 }
199
200 @Test
201 public void test06_ReadResetMid02() {
202 final int capacity = 11;
203 final Integer[] source = createIntArray(capacity, 0);
204 final Ringbuffer<Integer> rb = createFull(source);
205 Assert.assertTrue("Not full "+rb, rb.isFull());
206
207 rb.resetFull(source);
208 Assert.assertTrue("Not full "+rb, rb.isFull());
209
210 moveGetPutImpl(rb, 5);
211 // readTestImpl(rb, false, capacity, 5, 0);
212 // Assert.assertTrue("Is empty "+rb, !rb.isEmpty());
213 // Assert.assertTrue("Is Full "+rb, !rb.isFull());
214
215 if( DEBUG ) {
216 rb.dump(System.err, "ReadReset02["+5+"].pre0");
217 }
218 rb.resetFull(source);
219 Assert.assertTrue("Not full "+rb, rb.isFull());
220 if( DEBUG ) {
221 rb.dump(System.err, "ReadReset02["+5+"].post");
222 }
223
224 readTestImpl(rb, false, capacity, capacity, 0);
225 Assert.assertTrue("Not empty "+rb, rb.isEmpty());
226 }
227
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);
234 }
235 final Ringbuffer<Integer> rb = createEmpty(initCapacity);
236
237 if( DEBUG ) {
238 rb.dump(System.err, "GrowEmpty["+pos+"].pre0");
239 }
240 movePutGetImpl(rb, pos);
241 if( DEBUG ) {
242 rb.dump(System.err, "GrowEmpty["+pos+"].pre1");
243 }
244 rb.growEmptyBuffer(growArray);
245 if( DEBUG ) {
246 rb.dump(System.err, "GrowEmpty["+pos+"].post");
247 }
248
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());
253
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());
258 }
259
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());
263 }
264 @Test
266 test_GrowEmptyImpl(11, 0);
267 }
268 @Test
270 test_GrowEmptyImpl(11, 0+2);
271 }
272 @Test
274 test_GrowEmptyImpl(11, 11-1);
275 }
276 @Test
278 test_GrowEmptyImpl(11, 11-1-2);
279 }
280
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;
284 final Integer[] source = createIntArray(initCapacity, 0);
285 final Ringbuffer<Integer> rb = createFull(source);
286
287 if( DEBUG || debug ) {
288 rb.dump(System.err, "GrowFull["+pos+"].pre0");
289 }
290 moveGetPutImpl(rb, pos);
291 if( DEBUG || debug ) {
292 rb.dump(System.err, "GrowFull["+pos+"].pre1");
293 }
294 rb.growFullBuffer(growAmount);
295 if( DEBUG || debug ) {
296 rb.dump(System.err, "GrowFull["+pos+"].post");
297 }
298
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());
303
304 for(int i=0; i<growAmount; i++) {
305 Assert.assertTrue("Buffer is full at put #"+i+": "+rb, rb.put( Integer.valueOf(100+i) ));
306 }
307 Assert.assertEquals("Not new size "+rb, grownCapacity, rb.size());
308 Assert.assertTrue("Not full "+rb, rb.isFull());
309
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());
314 }
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());
319 }
320
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());
324 }
325 @Test
327 test_GrowFullImpl(11, 0, false);
328 }
329 @Test
331 test_GrowFullImpl(11, 0+1, false);
332 }
333 @Test
335 test_GrowFullImpl(11, 0+2, false);
336 }
337 @Test
339 test_GrowFullImpl(11, 0+3, false);
340 }
341 @Test
342 public void test24_GrowFull05_End() {
343 test_GrowFullImpl(11, 11-1, false);
344 }
345 @Test
347 test_GrowFullImpl(11, 11-1-1, false);
348 }
349 @Test
351 test_GrowFullImpl(11, 11-1-2, false);
352 }
353 @Test
355 test_GrowFullImpl(11, 11-1-3, false);
356 }
357}
abstract Ringbuffer< Integer > createEmpty(int initialCapacity)
Integer[] createIntArray(final int capacity, final int startValue)
abstract Ringbuffer< Integer > createFull(Integer[] source)
Ring buffer interface, a.k.a circular buffer.
Definition: Ringbuffer.java:45
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.