29package com.jogamp.common.util;
31import java.io.IOException;
32import java.nio.FloatBuffer;
33import java.util.Arrays;
35import org.junit.Assert;
38import com.jogamp.junit.util.SingletonJunitCase;
40import org.junit.FixMethodOrder;
41import org.junit.runners.MethodSorters;
43@FixMethodOrder(MethodSorters.NAME_ASCENDING)
46 static final boolean equals(
final float[] b,
final int bOffset,
47 final float[] stack,
final int stackOffset,
final int length) {
48 for(
int i=0; i<length; i++) {
49 if( b[bOffset+i] != stack[stackOffset+i]) {
59 final int initialSizeElem = 32;
60 final int growSizeElem = 2;
61 testPrimitiveArrayImpl(initialSizeElem, growSizeElem);
66 final int initialSizeElem = 0;
67 final int growSizeElem = 32;
68 testPrimitiveArrayImpl(initialSizeElem, growSizeElem);
71 static private final boolean VERBOSE =
false;
73 private void testPrimitiveArrayImpl(
final int initialSizeElem,
final int growSizeElem) {
74 final int compNum = 3;
76 new float[] { 0, 1, 2 };
78 new float[] { 3, 4, 5 };
80 final int totalSizeElem = initialSizeElem+2*growSizeElem;
82 final int initialSizeComp = initialSizeElem*compNum;
83 final int growSizeComp = growSizeElem*compNum;
84 final int totalSizeComp = totalSizeElem*compNum;
86 final FloatStack fs0 =
87 new FloatStack(initialSizeComp, growSizeComp);
93 System.err.println(
"0: "+fs0);
95 for(
int i=0; i<totalSizeElem; i++) {
96 if(i < initialSizeElem) {
97 Assert.assertTrue(
"Error #"+i+
", "+fs0, fs0.remaining() == (initialSizeElem-i)*compNum);
99 final int j = ( i - initialSizeElem ) % growSizeElem ;
100 final int k = ( 0 < j && j < growSizeElem ) ? growSizeElem - j : 0;
101 Assert.assertTrue(
"Error #"+i+
"("+j+
", "+k+
"), "+fs0, fs0.remaining() == k*compNum);
103 Assert.assertTrue(
"Error "+fs0, fs0.position() == i*compNum);
108 s = Arrays.toString(e0);
110 fs0.putOnTop(e0, 0, compNum);
113 s = Arrays.toString(e1);
115 fs0.putOnTop(e1, 0, compNum);
118 System.err.println(
"#"+i+
"/"+totalSizeElem+
": "+fs0+
" <- "+s);
122 System.err.println(
"X: "+fs0);
124 Assert.assertTrue(
"Error "+fs0, fs0.remaining() == 0);
125 Assert.assertTrue(
"Error "+fs0, fs0.position() == totalSizeComp);
129 Exception expectedException =
null;
131 fs0.putOnTop(e1, 0, compNum);
132 }
catch (
final Exception e) {
133 expectedException = e;
135 if(
null == expectedException || !(expectedException instanceof IndexOutOfBoundsException) ) {
136 Assert.assertTrue(
"Error "+fs0+
", exception "+expectedException,
false);
144 for(
int i=0; i<totalSizeElem; i++) {
145 Assert.assertTrue(
"Error "+fs0, fs0.remaining() == i*compNum);
146 Assert.assertTrue(
"Error "+fs0, fs0.position() == (totalSizeElem-i)*compNum);
150 fs0.getFromTop(buf, 0, compNum);
152 Assert.assertTrue(
"Error "+fs0+
", #"+i+
": "+Arrays.toString(e1)+
" != "+Arrays.toString(buf), Arrays.equals(e1, buf));
154 Assert.assertTrue(
"Error "+fs0+
", #"+i+
": "+Arrays.toString(e0)+
" != "+Arrays.toString(buf), Arrays.equals(e0, buf));
157 Assert.assertTrue(
"Error "+fs0, fs0.remaining() == totalSizeComp);
158 Assert.assertTrue(
"Error "+fs0, fs0.position() == 0);
163 Exception expectedException =
null;
165 fs0.getFromTop(buf, 0, compNum);
166 }
catch (
final Exception e) {
167 expectedException = e;
169 if(
null == expectedException || !(expectedException instanceof IndexOutOfBoundsException) ) {
170 Assert.assertTrue(
"Error "+fs0+
", exception "+expectedException,
false);
177 final int initialSizeElem = 32;
178 final int growSizeElem = 2;
179 testFloatBufferImpl(initialSizeElem, growSizeElem);
184 final int initialSizeElem = 0;
185 final int growSizeElem = 32;
186 testFloatBufferImpl(initialSizeElem, growSizeElem);
189 private void testFloatBufferImpl(
final int initialSizeElem,
final int growSizeElem) {
190 final int compNum = 3;
191 final FloatBuffer fb0 =
192 FloatBuffer.allocate(3*compNum);
195 new float[] { 0, 1, 2 };
197 new float[] { 3, 4, 5 };
199 new float[] { 6, 7, 8 };
205 final int totalSizeElem = initialSizeElem+2*growSizeElem;
207 final int initialSizeComp = initialSizeElem*compNum;
208 final int growSizeComp = growSizeElem*compNum;
209 final int totalSizeComp = totalSizeElem*compNum;
211 final FloatStack fs0 =
212 new FloatStack(initialSizeComp, growSizeComp);
218 for(
int i=0; i<totalSizeElem; i++) {
220 Assert.assertTrue(
"Error #"+i+
", "+fs0+
", "+fb0, fb0.position() == 0);
221 }
else if( 0 == i % 2) {
222 Assert.assertTrue(
"Error #"+i+
", "+fs0+
", "+fb0, fb0.position() == 2*compNum);
225 Assert.assertTrue(
"Error #"+i+
", "+fs0+
", "+fb0, fb0.position() == compNum);
227 if(i < initialSizeElem) {
228 Assert.assertTrue(
"Error #"+i+
", "+fs0, fs0.remaining() == (initialSizeElem-i)*compNum);
230 final int j = ( i - initialSizeElem ) % growSizeElem ;
231 final int k = ( 0 < j && j < growSizeElem ) ? growSizeElem - j : 0;
232 Assert.assertTrue(
"Error #"+i+
"("+j+
", "+k+
"), "+fs0, fs0.remaining() == k*compNum);
234 Assert.assertTrue(
"Error "+fs0, fs0.position() == i*compNum);
236 final int fb0Pos0 = fb0.position();
237 fs0.putOnTop(fb0, compNum);
238 Assert.assertTrue(
"Error "+fs0+
", "+fb0, fb0.position() == fb0Pos0 + compNum);
240 Assert.assertTrue(
"Error "+fs0, fs0.remaining() == 0);
241 Assert.assertTrue(
"Error "+fs0, fs0.position() == totalSizeComp);
246 Exception expectedException =
null;
248 fs0.putOnTop(fb0, compNum);
249 }
catch (
final Exception e) {
250 expectedException = e;
252 if(
null == expectedException || !(expectedException instanceof IndexOutOfBoundsException) ) {
253 Assert.assertTrue(
"Error "+fs0+
", exception "+expectedException,
false);
262 for(
int i=0; i<totalSizeElem; i++) {
263 Assert.assertTrue(
"Error "+fs0, fs0.remaining() == i*compNum);
264 Assert.assertTrue(
"Error "+fs0, fs0.position() == (totalSizeElem-i)*compNum);
266 final float[] backing =
268 final FloatBuffer buf =
269 FloatBuffer.wrap(backing);
271 fs0.getFromTop(buf, compNum);
273 Assert.assertTrue(
"Error "+fs0+
", #"+i+
": "+Arrays.toString(e1)+
" != "+Arrays.toString(backing), Arrays.equals(e1, backing));
275 Assert.assertTrue(
"Error "+fs0+
", #"+i+
": "+Arrays.toString(e0)+
" != "+Arrays.toString(backing), Arrays.equals(e0, backing));
277 Assert.assertTrue(
"Error "+fs0+
", "+buf, buf.position() == compNum);
280 Assert.assertTrue(
"Error "+fs0, fs0.remaining() == totalSizeComp);
281 Assert.assertTrue(
"Error "+fs0, fs0.position() == 0);
284 final float[] backing =
286 final FloatBuffer buf
287 = FloatBuffer.wrap(backing);
288 Exception expectedException =
null;
290 fs0.getFromTop(buf, compNum);
291 }
catch (
final Exception e) {
292 expectedException = e;
294 if(
null == expectedException || !(expectedException instanceof IndexOutOfBoundsException) ) {
295 Assert.assertTrue(
"Error "+fs0+
", exception "+expectedException,
false);
301 public static void main(
final String args[])
throws IOException {
303 org.junit.runner.JUnitCore.
main(tstname);
static void main(final String args[])
void test01PrimitiveArray_I32_G02()
void test11FloatBuffer_I32_G02()
void test12FloatBuffer_I00_G32()
void test02PrimitiveArray_I00_G32()