30package com.jogamp.opengl.test.junit.util;
32import java.io.BufferedReader;
33import java.io.IOException;
34import java.io.InputStream;
35import java.io.InputStreamReader;
36import java.io.OutputStream;
37import java.lang.reflect.*;
38import java.nio.FloatBuffer;
39import java.util.Iterator;
42import com.jogamp.opengl.GLContext;
43import com.jogamp.common.os.Platform;
44import com.jogamp.common.util.InterruptSource;
45import com.jogamp.math.Matrix4f;
48 public static boolean atob(
final String str,
final boolean def) {
50 return Boolean.parseBoolean(str);
51 }
catch (
final Exception ex) {
57 public static int atoi(
final String str,
final int def) {
59 return Integer.parseInt(str);
60 }
catch (
final Exception ex) {
66 public static long atol(
final String str,
final long def) {
68 return Long.parseLong(str);
69 }
catch (
final Exception ex) {
75 public static float atof(
final String str,
final float def) {
77 return Float.parseFloat(str);
78 }
catch (
final Exception ex) {
85 return "0x" + Integer.toHexString( hex & 0x000000FF );
89 return "0x" + Integer.toHexString( hex & 0x0000FFFF );
93 return "0x" + Integer.toHexString( hex );
97 return "0x" + Long.toHexString( hex );
101 final BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
102 System.err.println(preMessage+
"> Press enter to continue");
104 System.err.println(stdin.readLine());
105 }
catch (
final IOException e) { e.printStackTrace(); }
108 public static void assertFloatBufferEquals(
final String errmsg,
final FloatBuffer expected,
final FloatBuffer actual,
final float delta) {
109 if(
null == expected &&
null == actual) {
112 final String msg =
null != errmsg ? errmsg +
" " :
"";
113 if(
null == expected) {
114 throw new AssertionError(msg+
"; Expected is null, but actual not: "+actual);
117 throw new AssertionError(msg+
"; Actual is null, but expected not: "+expected);
119 if(expected.remaining() != actual.remaining()) {
120 throw new AssertionError(msg+
"; Expected has "+expected.remaining()+
" remaining, but actual has "+actual.remaining());
122 final int a0 = expected.position();
123 final int b0 = actual.position();
124 for(
int i=0; i<expected.remaining(); i++) {
125 final float ai = expected.get(a0 + i);
126 final float bi = actual.get(b0 + i);
127 final float daibi = Math.abs(ai - bi);
128 if( daibi > delta ) {
129 throw new AssertionError(msg+
"; Expected @ ["+a0+
"+"+i+
"] has "+ai+
", but actual @ ["+b0+
"+"+i+
"] has "+bi+
", it's delta "+daibi+
" > "+delta);
133 public static void assertFloatBufferNotEqual(
final String errmsg,
final FloatBuffer expected,
final FloatBuffer actual,
final float delta) {
134 if(
null == expected ||
null == actual) {
137 if(expected.remaining() != actual.remaining()) {
140 final String msg =
null != errmsg ? errmsg +
" " :
"";
141 final int a0 = expected.position();
142 final int b0 = actual.position();
143 for(
int i=0; i<expected.remaining(); i++) {
144 final float ai = expected.get(a0 + i);
145 final float bi = actual.get(b0 + i);
146 final float daibi = Math.abs(ai - bi);
147 if( daibi > delta ) {
151 throw new AssertionError(msg+
"; Expected and actual are equal.");
158 if(
null == expected &&
null == actual) {
161 final String msg =
null != errmsg ? errmsg +
" " :
"";
162 if(
null == expected) {
163 throw new AssertionError(msg+
"; Expected is null, but actual not: "+actual);
166 throw new AssertionError(msg+
"; Actual is null, but expected not: "+expected);
168 for(
int i=0; i<16; i++) {
169 final float ai = expected.
get(i);
170 final float bi = actual.
get(i);
171 final float daibi = Math.abs(ai - bi);
172 if( daibi > delta ) {
173 throw new AssertionError(msg+
"; Expected @ ["+i+
"] has "+ai+
", but actual @ ["+i+
"] has "+bi+
", it's delta "+daibi+
" > "+delta);
178 if(
null == expected ||
null == actual) {
181 final String msg =
null != errmsg ? errmsg +
" " :
"";
182 for(
int i=0; i<16; i++) {
183 final float ai = expected.
get(i);
184 final float bi = actual.
get(i);
185 final float daibi = Math.abs(ai - bi);
186 if( daibi > delta ) {
190 throw new AssertionError(msg+
"; Expected and actual are equal.");
193 public static boolean setFieldIfExists(
final Object instance,
final String fieldName,
final Object value) {
195 final Field f = instance.getClass().getField(fieldName);
196 if(value instanceof Boolean || f.getType().isInstance(value)) {
197 f.set(instance, value);
200 System.out.println(instance.getClass()+
" '"+fieldName+
"' field not assignable with "+value.getClass()+
", it's a: "+f.getType());
202 }
catch (
final IllegalAccessException ex) {
203 throw new RuntimeException(ex);
204 }
catch (
final NoSuchFieldException nsfe) {
210 public static class StreamDump extends InterruptSource.Thread {
211 final InputStream is;
212 final StringBuilder outString;
213 final OutputStream outStream;
216 volatile boolean eos =
false;
218 public StreamDump(
final OutputStream out,
final String prefix,
final InputStream is,
final Object sync) {
220 this.outString =
null;
221 this.outStream = out;
222 this.prefix = prefix;
225 public StreamDump(
final StringBuilder sb,
final String prefix,
final InputStream is,
final Object sync) {
228 this.outStream =
null;
229 this.prefix = prefix;
232 public StreamDump(
final StringBuilder sb,
final InputStream is,
final Object sync) {
235 this.outStream =
null;
240 public final boolean eos() {
return eos; }
244 synchronized ( sync ) {
246 final BufferedReader in =
new BufferedReader(
new InputStreamReader(is) );
248 while ((line = in.readLine()) !=
null) {
249 if(
null != outString ) {
250 outString.append(line).append(Platform.getNewline());
251 }
else if(
null != outStream ) {
252 if(
null != prefix ) {
253 outStream.write(prefix.getBytes());
255 outStream.write(line.getBytes());
256 outStream.write(Platform.getNewline().getBytes());
260 }
catch (
final IOException ioe) {
261 System.err.println(
"Caught "+ioe.getClass().getName()+
": "+ioe.getMessage());
262 ioe.printStackTrace();
274 final int masterHash =
null != master ? master.hashCode() : 0;
275 System.err.println(prefix+
": hash 0x"+Integer.toHexString(
self.hashCode())+
", \t(isShared "+
self.isShared()+
", created "+
self.isCreated()+
", master 0x"+Integer.toHexString(masterHash)+
")");
278 for (
final Iterator<GLContext> iter =
set.iterator(); iter.hasNext(); ) {
280 System.err.println(
" Created Ctx #"+(i++)+
": hash 0x"+Integer.toHexString(c.hashCode())+
", \t(created "+c.
isCreated()+
")");
285 for (
final Iterator<GLContext> iter =
set.iterator(); iter.hasNext(); ) {
287 System.err.println(
" Destroyed Ctx #"+(j++)+
": hash 0x"+Integer.toHexString(c.hashCode())+
", \t(created "+c.
isCreated()+
")");
290 System.err.println(
"\t Total created "+i+
" + destroyed "+j+
" = "+(i+j));
291 System.err.println();
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
float get(final int i)
Gets the ith component, 0 <= i < 16.
Abstraction for an OpenGL rendering context.
final boolean isCreated()
Indicates whether the underlying native OpenGL context has been created.
final GLContext getSharedMaster()
Returns the shared master GLContext of this GLContext if shared, otherwise return null.
final List< GLContext > getDestroyedShares()
Returns a new list of destroyed GLContext shared with this GLContext.
final List< GLContext > getCreatedShares()
Returns a new list of created GLContext shared with this GLContext.
StreamDump(final StringBuilder sb, final InputStream is, final Object sync)
StreamDump(final OutputStream out, final String prefix, final InputStream is, final Object sync)
StreamDump(final StringBuilder sb, final String prefix, final InputStream is, final Object sync)
static void assertMatrix4fEquals(final Matrix4f expected, final Matrix4f actual, final float delta)
static String toHexString(final short hex)
static float atof(final String str, final float def)
static String toHexString(final long hex)
static boolean setFieldIfExists(final Object instance, final String fieldName, final Object value)
static void assertMatrix4fEquals(final String errmsg, final Matrix4f expected, final Matrix4f actual, final float delta)
static void waitForKey(final String preMessage)
static void assertMatrix4fNotEqual(final String errmsg, final Matrix4f expected, final Matrix4f actual, final float delta)
static void assertFloatBufferEquals(final String errmsg, final FloatBuffer expected, final FloatBuffer actual, final float delta)
static int atoi(final String str, final int def)
static long atol(final String str, final long def)
static String toHexString(final int hex)
static String toHexString(final byte hex)
static boolean atob(final String str, final boolean def)
static void assertFloatBufferNotEqual(final String errmsg, final FloatBuffer expected, final FloatBuffer actual, final float delta)
static void dumpSharedGLContext(final String prefix, final GLContext self)