GlueGen: src/junit/com/jogamp/common/util/TestFloatStack01.java Source File

GlueGen v2.6.0-rc-20250822 GlueGen, Native Binding Generator for Java™ (public API).

TestFloatStack01.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 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
31import java.io.IOException;
32import java.nio./*value2*/FloatBuffer/*value2*/;
33import java.util.Arrays;
34
35import org.junit.Assert;
36import org.junit.Test;
37
38import com.jogamp.junit.util.SingletonJunitCase;
39
40import org.junit.FixMethodOrder;
41import org.junit.runners.MethodSorters;
42
43@FixMethodOrder(MethodSorters.NAME_ASCENDING)
44public class /*testname*/ TestFloatStack01/*testname*/ extends SingletonJunitCase {
45
46 static final boolean equals(final /*value*/float/*value*/[] b, final int bOffset,
47 final /*value*/float/*value*/[] stack, final int stackOffset, final int length) {
48 for(int i=0; i<length; i++) {
49 if( b[bOffset+i] != stack[stackOffset+i]) {
50 return false;
51 }
52 }
53 return true;
54 }
55
56
57 @Test
58 public void test01PrimitiveArray_I32_G02() {
59 final int initialSizeElem = 32;
60 final int growSizeElem = 2;
61 testPrimitiveArrayImpl(initialSizeElem, growSizeElem);
62 }
63
64 @Test
65 public void test02PrimitiveArray_I00_G32() {
66 final int initialSizeElem = 0;
67 final int growSizeElem = 32;
68 testPrimitiveArrayImpl(initialSizeElem, growSizeElem);
69 }
70
71 static private final boolean VERBOSE = false;
72
73 private void testPrimitiveArrayImpl(final int initialSizeElem, final int growSizeElem) {
74 final int compNum = 3;
75 final /*value*/float/*value*/[] e0 =
76 new /*value*/float/*value*/[] { 0, 1, 2 };
77 final /*value*/float/*value*/[] e1 =
78 new /*value*/float/*value*/[] { 3, 4, 5 };
79
80 final int totalSizeElem = initialSizeElem+2*growSizeElem;
81
82 final int initialSizeComp = initialSizeElem*compNum;
83 final int growSizeComp = growSizeElem*compNum;
84 final int totalSizeComp = totalSizeElem*compNum;
85
86 final /*name*/FloatStack/*name*/ fs0 =
87 new /*name*/FloatStack/*name*/(initialSizeComp, growSizeComp);
88
89 //
90 // PUT
91 //
92 if(VERBOSE) {
93 System.err.println("0: "+fs0);
94 }
95 for(int i=0; i<totalSizeElem; i++) {
96 if(i < initialSizeElem) {
97 Assert.assertTrue("Error #"+i+", "+fs0, fs0.remaining() == (initialSizeElem-i)*compNum);
98 } else {
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);
102 }
103 Assert.assertTrue("Error "+fs0, fs0.position() == i*compNum);
104
105 String s;
106 if( 0 == i % 2) {
107 if(VERBOSE) {
108 s = Arrays.toString(e0);
109 }
110 fs0.putOnTop(e0, 0, compNum);
111 } else {
112 if(VERBOSE) {
113 s = Arrays.toString(e1);
114 }
115 fs0.putOnTop(e1, 0, compNum);
116 }
117 if(VERBOSE) {
118 System.err.println("#"+i+"/"+totalSizeElem+": "+fs0+" <- "+s);
119 }
120 }
121 if(VERBOSE) {
122 System.err.println("X: "+fs0);
123 }
124 Assert.assertTrue("Error "+fs0, fs0.remaining() == 0);
125 Assert.assertTrue("Error "+fs0, fs0.position() == totalSizeComp);
126
127 fs0.setGrowSize(0);
128 {
129 Exception expectedException = null;
130 try {
131 fs0.putOnTop(e1, 0, compNum);
132 } catch (final Exception e) {
133 expectedException = e;
134 }
135 if(null == expectedException || !(expectedException instanceof IndexOutOfBoundsException) ) {
136 Assert.assertTrue("Error "+fs0+", exception "+expectedException, false);
137 }
138 }
139
140 //
141 // GET
142 //
143
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);
147
148 final /*value*/float/*value*/[] buf =
149 new /*value*/float/*value*/[compNum];
150 fs0.getFromTop(buf, 0, compNum);
151 if( 0 == i % 2) {
152 Assert.assertTrue("Error "+fs0+", #"+i+": "+Arrays.toString(e1)+" != "+Arrays.toString(buf), Arrays.equals(e1, buf));
153 } else {
154 Assert.assertTrue("Error "+fs0+", #"+i+": "+Arrays.toString(e0)+" != "+Arrays.toString(buf), Arrays.equals(e0, buf));
155 }
156 }
157 Assert.assertTrue("Error "+fs0, fs0.remaining() == totalSizeComp);
158 Assert.assertTrue("Error "+fs0, fs0.position() == 0);
159
160 {
161 final /*value*/float/*value*/[] buf =
162 new /*value*/float/*value*/[compNum];
163 Exception expectedException = null;
164 try {
165 fs0.getFromTop(buf, 0, compNum);
166 } catch (final Exception e) {
167 expectedException = e;
168 }
169 if(null == expectedException || !(expectedException instanceof IndexOutOfBoundsException) ) {
170 Assert.assertTrue("Error "+fs0+", exception "+expectedException, false);
171 }
172 }
173 }
174
175 @Test
176 public void test11FloatBuffer_I32_G02() {
177 final int initialSizeElem = 32;
178 final int growSizeElem = 2;
179 testFloatBufferImpl(initialSizeElem, growSizeElem);
180 }
181
182 @Test
183 public void test12FloatBuffer_I00_G32() {
184 final int initialSizeElem = 0;
185 final int growSizeElem = 32;
186 testFloatBufferImpl(initialSizeElem, growSizeElem);
187 }
188
189 private void testFloatBufferImpl(final int initialSizeElem, final int growSizeElem) {
190 final int compNum = 3;
191 final /*value2*/FloatBuffer/*value2*/ fb0 =
192 /*value2*/FloatBuffer/*value2*/.allocate(3*compNum);
193
194 final /*value*/float/*value*/[] e0 =
195 new /*value*/float/*value*/[] { 0, 1, 2 };
196 final /*value*/float/*value*/[] e1 =
197 new /*value*/float/*value*/[] { 3, 4, 5 };
198 final /*value*/float/*value*/[] e2 =
199 new /*value*/float/*value*/[] { 6, 7, 8 }; // not put on stack!
200 fb0.put(e0);
201 fb0.put(e1);
202 fb0.put(e2);
203 fb0.position(0);
204
205 final int totalSizeElem = initialSizeElem+2*growSizeElem;
206
207 final int initialSizeComp = initialSizeElem*compNum;
208 final int growSizeComp = growSizeElem*compNum;
209 final int totalSizeComp = totalSizeElem*compNum;
210
211 final /*name*/FloatStack/*name*/ fs0 =
212 new /*name*/FloatStack/*name*/(initialSizeComp, growSizeComp);
213
214 //
215 // PUT
216 //
217
218 for(int i=0; i<totalSizeElem; i++) {
219 if( 0 == 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);
223 fb0.position(0);
224 } else {
225 Assert.assertTrue("Error #"+i+", "+fs0+", "+fb0, fb0.position() == compNum);
226 }
227 if(i < initialSizeElem) {
228 Assert.assertTrue("Error #"+i+", "+fs0, fs0.remaining() == (initialSizeElem-i)*compNum);
229 } else {
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);
233 }
234 Assert.assertTrue("Error "+fs0, fs0.position() == i*compNum);
235
236 final int fb0Pos0 = fb0.position();
237 fs0.putOnTop(fb0, compNum);
238 Assert.assertTrue("Error "+fs0+", "+fb0, fb0.position() == fb0Pos0 + compNum);
239 }
240 Assert.assertTrue("Error "+fs0, fs0.remaining() == 0);
241 Assert.assertTrue("Error "+fs0, fs0.position() == totalSizeComp);
242
243 fs0.setGrowSize(0);
244 {
245 fb0.position(0);
246 Exception expectedException = null;
247 try {
248 fs0.putOnTop(fb0, compNum);
249 } catch (final Exception e) {
250 expectedException = e;
251 }
252 if(null == expectedException || !(expectedException instanceof IndexOutOfBoundsException) ) {
253 Assert.assertTrue("Error "+fs0+", exception "+expectedException, false);
254 }
255 fb0.position(0);
256 }
257
258 //
259 // GET
260 //
261
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);
265
266 final /*value*/float/*value*/[] backing =
267 new /*value*/float/*value*/[compNum];
268 final /*value2*/FloatBuffer/*value2*/ buf =
269 /*value2*/FloatBuffer/*value2*/.wrap(backing);
270
271 fs0.getFromTop(buf, compNum);
272 if( 0 == i % 2) {
273 Assert.assertTrue("Error "+fs0+", #"+i+": "+Arrays.toString(e1)+" != "+Arrays.toString(backing), Arrays.equals(e1, backing));
274 } else {
275 Assert.assertTrue("Error "+fs0+", #"+i+": "+Arrays.toString(e0)+" != "+Arrays.toString(backing), Arrays.equals(e0, backing));
276 }
277 Assert.assertTrue("Error "+fs0+", "+buf, buf.position() == compNum);
278 buf.position(0);
279 }
280 Assert.assertTrue("Error "+fs0, fs0.remaining() == totalSizeComp);
281 Assert.assertTrue("Error "+fs0, fs0.position() == 0);
282
283 {
284 final /*value*/float/*value*/[] backing =
285 new /*value*/float/*value*/[compNum];
286 final /*value2*/FloatBuffer/*value2*/ buf
287 = /*value2*/FloatBuffer/*value2*/.wrap(backing);
288 Exception expectedException = null;
289 try {
290 fs0.getFromTop(buf, compNum);
291 } catch (final Exception e) {
292 expectedException = e;
293 }
294 if(null == expectedException || !(expectedException instanceof IndexOutOfBoundsException) ) {
295 Assert.assertTrue("Error "+fs0+", exception "+expectedException, false);
296 }
297 }
298
299 }
300
301 public static void main(final String args[]) throws IOException {
302 final String tstname = /*testname*/ TestFloatStack01/*testname*/.class.getName();
303 org.junit.runner.JUnitCore.main(tstname);
304 }
305
306}
com.jogamp.common.util.TestFloatStack01
Definition: TestFloatStack01.java:44
com.jogamp.common.util.TestFloatStack01.main
static void main(final String args[])
Definition: TestFloatStack01.java:301
com.jogamp.common.util.TestFloatStack01.test01PrimitiveArray_I32_G02
void test01PrimitiveArray_I32_G02()
Definition: TestFloatStack01.java:58
com.jogamp.common.util.TestFloatStack01.test11FloatBuffer_I32_G02
void test11FloatBuffer_I32_G02()
Definition: TestFloatStack01.java:176
com.jogamp.common.util.TestFloatStack01.test12FloatBuffer_I00_G32
void test12FloatBuffer_I00_G32()
Definition: TestFloatStack01.java:183
com.jogamp.common.util.TestFloatStack01.test02PrimitiveArray_I00_G32
void test02PrimitiveArray_I00_G32()
Definition: TestFloatStack01.java:65
com.jogamp.junit.util.SingletonJunitCase
Definition: SingletonJunitCase.java:39