diff options
Diffstat (limited to 'src/junit/com/jogamp/common/util')
19 files changed, 1085 insertions, 57 deletions
diff --git a/src/junit/com/jogamp/common/util/BitstreamData.java b/src/junit/com/jogamp/common/util/BitDemoData.java index a5a0bd9..9d605fc 100644 --- a/src/junit/com/jogamp/common/util/BitstreamData.java +++ b/src/junit/com/jogamp/common/util/BitDemoData.java @@ -30,11 +30,49 @@ package com.jogamp.common.util; import java.nio.ByteBuffer; -public class BitstreamData { +public class BitDemoData { + public static final long UNSIGNED_INT_MAX_VALUE = 0xffffffffL; + + public static final String[] pyramid32bit_one = { + "00000000000000000000000000000001", + "00000000000000000000000000000010", + "00000000000000000000000000000100", + "00000000000000000000000000001000", + "00000000000000000000000000010000", + "00000000000000000000000000100000", + "00000000000000000000000001000000", + "00000000000000000000000010000000", + "00000000000000000000000100000000", + "00000000000000000000001000000000", + "00000000000000000000010000000000", + "00000000000000000000100000000000", + "00000000000000000001000000000000", + "00000000000000000010000000000000", + "00000000000000000100000000000000", + "00000000000000001000000000000000", + "00000000000000010000000000000000", + "00000000000000100000000000000000", + "00000000000001000000000000000000", + "00000000000010000000000000000000", + "00000000000100000000000000000000", + "00000000001000000000000000000000", + "00000000010000000000000000000000", + "00000000100000000000000000000000", + "00000001000000000000000000000000", + "00000010000000000000000000000000", + "00000100000000000000000000000000", + "00001000000000000000000000000000", + "00010000000000000000000000000000", + "00100000000000000000000000000000", + "01000000000000000000000000000000", + "10000000000000000000000000000000" + }; + // // MSB -> LSB over whole data // public static final byte[] testBytesMSB = new byte[] { (byte)0xde, (byte)0xaf, (byte)0xca, (byte)0xfe }; + public static final int testIntMSB = 0xdeafcafe; // 11011110 10101111 11001010 11111110 public static final String[] testStringsMSB = new String[] { "11011110", "10101111", "11001010", "11111110" }; public static final String testStringMSB = testStringsMSB[0]+testStringsMSB[1]+testStringsMSB[2]+testStringsMSB[3]; @@ -42,6 +80,7 @@ public class BitstreamData { // MSB -> LSB, reverse bit-order over each byte of testBytesLSB // public static final byte[] testBytesMSB_rev = new byte[] { (byte)0xfe, (byte)0xca, (byte)0xaf, (byte)0xde }; + public static final int testIntMSB_rev = 0xfecaafde; public static final String[] testStringsMSB_rev = new String[] { "11111110", "11001010", "10101111", "11011110" }; public static final String testStringMSB_rev = testStringsMSB_rev[0]+testStringsMSB_rev[1]+testStringsMSB_rev[2]+testStringsMSB_rev[3]; @@ -49,6 +88,7 @@ public class BitstreamData { // LSB -> MSB over whole data // public static final byte[] testBytesLSB = new byte[] { (byte)0x7f, (byte)0x53, (byte)0xf5, (byte)0x7b }; + public static final int testIntLSB = 0x7f53f57b; public static final String[] testStringsLSB = new String[] { "01111111", "01010011", "11110101", "01111011" }; public static final String testStringLSB = testStringsLSB[0]+testStringsLSB[1]+testStringsLSB[2]+testStringsLSB[3]; @@ -56,6 +96,7 @@ public class BitstreamData { // LSB -> MSB, reverse bit-order over each byte of testBytesMSB // public static final byte[] testBytesLSB_revByte = new byte[] { (byte)0x7b, (byte)0xf5, (byte)0x53, (byte)0x7f }; + public static final int testIntLSB_revByte = 0x7bf5537f; public static final String[] testStringsLSB_revByte = new String[] { "01111011", "11110101", "01010011", "01111111" }; public static final String testStringLSB_revByte = testStringsLSB_revByte[0]+testStringsLSB_revByte[1]+testStringsLSB_revByte[2]+testStringsLSB_revByte[3]; @@ -80,6 +121,26 @@ public class BitstreamData { } } + public static int getOneBitCount(final String pattern) { + int c=0; + for(int i=0; i<pattern.length(); i++) { + if( '1' == pattern.charAt(i) ) { + c++; + } + } + return c; + } + public static long toLong(final String bitPattern) { + return Long.valueOf(bitPattern, 2).longValue(); + } + public static int toInteger(final String bitPattern) { + final long res = Long.valueOf(bitPattern, 2).longValue(); + if( res > UNSIGNED_INT_MAX_VALUE ) { + throw new NumberFormatException("Exceeds "+toHexString(UNSIGNED_INT_MAX_VALUE)+": "+toHexString(res)+" - source "+bitPattern); + } + return (int)res; + } + public static String toHexString(final int v) { return "0x"+Integer.toHexString(v); } diff --git a/src/junit/com/jogamp/common/util/CustomDeflate.java b/src/junit/com/jogamp/common/util/CustomDeflate.java new file mode 100644 index 0000000..d4682e8 --- /dev/null +++ b/src/junit/com/jogamp/common/util/CustomDeflate.java @@ -0,0 +1,115 @@ +/** + * Copyright 2015 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package com.jogamp.common.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class CustomDeflate { + public static void main(final String[] args) { + if (args.length != 2) { + System.err.println("Usage: java "+CustomDeflate.class.getName()+" file-in file-out"); + } else { + final File fileIn = new File(args[0]); + final File fileOut = new File(args[1]); + final int inSize; + { + final long _inSize = fileIn.length(); + if( 0 >= _inSize || _inSize > Integer.MAX_VALUE ) { + throw new IllegalArgumentException(""); + } + inSize = (int) _inSize; + } + final byte[] input = new byte[inSize]; + InputStream in = null; + OutputStream out = null; + try { + in = new FileInputStream(fileIn); + int numBytes = 0; + try { + while (true) { + final int remBytes = inSize - numBytes; + int count; + if ( 0 >= remBytes || (count = in.read(input, numBytes, remBytes)) == -1 ) { + break; + } + numBytes += count; + } + } finally { + in.close(); + } + if( inSize != numBytes ) { + throw new IOException("Got "+numBytes+" bytes != expected "+inSize); + } + out = new FileOutputStream(fileOut); + CustomCompress.deflateToStream(input, 0, inSize, 9, out); + } catch (final IOException ioe) { + ioe.printStackTrace(); + } finally { + if( null != in ) { + try { in.close(); } catch (final IOException e) { } + } + if( null != out ) { + try { out.close(); } catch (final IOException e) { } + } + } + + // + // Test + // + in = null; + out = null; + try { + in = new FileInputStream(fileOut); + final byte[] compare = CustomCompress.inflateFromStream(in); + if( compare.length != inSize ) { + throw new InternalError("Inflated Size Mismatch: Has "+compare.length+", expected "+inSize); + } + for(int i=0; i<inSize; i++) { + if( input[i] != compare[i] ) { + throw new InternalError("Inflated Bytes Mismatch at "+i+"/"+inSize+": Has "+Integer.toHexString(compare[i])+", expected "+Integer.toHexString(input[i])); + } + } + } catch (final IOException ioe) { + ioe.printStackTrace(); + } finally { + if( null != in ) { + try { in.close(); } catch (final IOException e) { } + } + if( null != out ) { + try { out.close(); } catch (final IOException e) { } + } + } + } + } + +} diff --git a/src/junit/com/jogamp/common/util/CustomInflate.java b/src/junit/com/jogamp/common/util/CustomInflate.java new file mode 100644 index 0000000..477439e --- /dev/null +++ b/src/junit/com/jogamp/common/util/CustomInflate.java @@ -0,0 +1,68 @@ +/** + * Copyright 2015 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package com.jogamp.common.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class CustomInflate { + public static final int magic = 0xDEF1A7E0; + + public static void main(final String[] args) { + if (args.length != 2) { + System.err.println("Usage: java "+CustomCompress.class.getName()+" file-in file-out"); + } else { + InputStream in = null; + OutputStream out = null; + try { + final File fileIn = new File(args[0]); + in = new FileInputStream(fileIn); + + final byte[] output = CustomCompress.inflateFromStream(in); + + final File fileOut = new File(args[1]); + out = new FileOutputStream(fileOut); + out.write(output, 0, output.length); + out.flush(); + } catch (final IOException ioe) { + ioe.printStackTrace(); + } finally { + if( null != in ) { + try { in.close(); } catch (final IOException e) { } + } + if( null != out ) { + try { out.close(); } catch (final IOException e) { } + } + } + } + } +} diff --git a/src/junit/com/jogamp/common/util/TestArrayHashMap01.java b/src/junit/com/jogamp/common/util/TestArrayHashMap01.java new file mode 100644 index 0000000..529612c --- /dev/null +++ b/src/junit/com/jogamp/common/util/TestArrayHashMap01.java @@ -0,0 +1,186 @@ +/** + * Copyright 2015 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.common.util; + +import java.util.*; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Test; + +import com.jogamp.junit.util.SingletonJunitCase; + +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestArrayHashMap01 extends SingletonJunitCase { + + public static class Dummy { + int i1, i2, i3; + + public Dummy(final int i1, final int i2, final int i3) { + this.i1 = i1; + this.i2 = i2; + this.i3 = i3; + } + + public boolean equals(final Object o) { + if(o instanceof Dummy) { + final Dummy d = (Dummy)o; + return this.i1 == d.i1 && + this.i2 == d.i2 && + this.i3 == d.i3 ; + } + return false; + } + + public final int hashCode() { + // 31 * x == (x << 5) - x + int hash = 31 + i1; + hash = ((hash << 5) - hash) + i2; + hash = ((hash << 5) - hash) + i3; + return hash; + } + + public String toString() { + return "Dummy["+super.toString()+": "+i1+", "+i2+", "+i3+"]"; + } + } + + void populate(final Map<Integer, Dummy> l, final int start, final int len, + final int i2, final int i3, final int expectedPlusSize) { + final int oldSize = l.size(); + for(int pos = start+len-1; pos>=start; pos--) { + l.put(pos, new Dummy(pos, i2, i3)); + } + Assert.assertEquals(expectedPlusSize, l.size() - oldSize); + } + boolean checkOrder(final List<Dummy> l, final int startIdx, final int start, final int len) { + for(int i=0; i<len; i++) { + final Dummy d = l.get(startIdx+i); + final int i1 = start+len-1-i; + if( d.i1 != i1 ) { + return false; + } + } + return true; + } + + @Test + public void test01ArrayHashMapWithNullValue() { + testArrayHashMapImpl(true); + } + @Test + public void test02ArrayHashSetWithoutNullValue() { + testArrayHashMapImpl(false); + } + void testArrayHashMapImpl(final boolean supportNullValue) { + final ArrayHashMap<Integer, Dummy> l = + new ArrayHashMap<Integer, Dummy>(supportNullValue, + ArrayHashSet.DEFAULT_INITIAL_CAPACITY, + ArrayHashSet.DEFAULT_LOAD_FACTOR); + Assert.assertEquals(supportNullValue, l.supportsNullValue()); + final int p7_22_34_key, p7_22_34_idx; + final Dummy p7_22_34_orig; + final int p6_22_34_key, p6_22_34_idx; + final Dummy p6_22_34_orig; + { + populate(l, 10, 100, 22, 34, 100); // [109 .. 10] + Assert.assertTrue(checkOrder(l.getData(), 0, 10, 100)); + populate(l, 10, 100, 22, 34, 0); // [109 .. 10] + Assert.assertTrue(checkOrder(l.getData(), 0, 10, 100)); + populate(l, 6, 5, 22, 34, 4); // [ 9 .. 6], 10 already exists + Assert.assertTrue(checkOrder(l.getData(), 100, 6, 4)); + p7_22_34_idx = l.size() - 2; + p7_22_34_key = 7; + p7_22_34_orig = l.get(p7_22_34_key); + p6_22_34_idx = l.size() - 1; + p6_22_34_key = 6; + p6_22_34_orig = l.get(p6_22_34_key); + } + Assert.assertNotNull(p7_22_34_orig); + Assert.assertEquals(7, p7_22_34_orig.i1); + Assert.assertEquals(l.getData().get(p7_22_34_idx), p7_22_34_orig); + Assert.assertNotNull(p6_22_34_orig); + Assert.assertEquals(6, p6_22_34_orig.i1); + Assert.assertEquals(l.getData().get(p6_22_34_idx), p6_22_34_orig); + + final Dummy p7_22_34_other = new Dummy(7, 22, 34); + Assert.assertEquals(p7_22_34_other, p7_22_34_orig); + Assert.assertTrue(p7_22_34_other.hashCode() == p7_22_34_orig.hashCode()); + Assert.assertTrue(p7_22_34_other != p7_22_34_orig); // diff reference + final Dummy p6_22_34_other = new Dummy(6, 22, 34); + Assert.assertEquals(p6_22_34_other, p6_22_34_orig); + Assert.assertTrue(p6_22_34_other.hashCode() == p6_22_34_orig.hashCode()); + Assert.assertTrue(p6_22_34_other != p6_22_34_orig); // diff reference + + // fast identity .. + Dummy q = l.get(p6_22_34_key); + Assert.assertNotNull(q); + Assert.assertEquals(p6_22_34_other, q); + Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode()); + Assert.assertTrue(p6_22_34_other != q); // diff reference + Assert.assertTrue(p6_22_34_orig == q); // same reference + + Assert.assertTrue(l.containsValue(q)); + Assert.assertTrue(l.containsValue(p6_22_34_other)); // add equivalent + + q = l.put(p6_22_34_key, p6_22_34_other); // override w/ diff hash-obj + Assert.assertNotNull(q); + Assert.assertEquals(p6_22_34_other, q); + Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode()); + Assert.assertTrue(p6_22_34_other != q); // diff reference new != old (q) + Assert.assertTrue(p6_22_34_orig == q); // same reference orig == old (q) + Assert.assertTrue(checkOrder(l.getData(), 0, 10, 100)); + Assert.assertTrue(checkOrder(l.getData(), 100, 6, 4)); + + final Dummy p1_2_3 = new Dummy(1, 2, 3); // a new one .. + q = l.put(1, p1_2_3); // added test + Assert.assertNull(q); + + final Dummy pNull = null; + NullPointerException npe = null; + try { + q = l.put(0, pNull); + Assert.assertNull(q); + } catch (final NullPointerException _npe) { npe = _npe; } + if( l.supportsNullValue() ) { + Assert.assertNull(npe); + } else { + Assert.assertNotNull(npe); + } + } + + public static void main(final String args[]) throws IOException { + final String tstname = TestArrayHashMap01.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } + +} diff --git a/src/junit/com/jogamp/common/util/TestArrayHashSet01.java b/src/junit/com/jogamp/common/util/TestArrayHashSet01.java index 51db2c7..3e8fbc0 100644 --- a/src/junit/com/jogamp/common/util/TestArrayHashSet01.java +++ b/src/junit/com/jogamp/common/util/TestArrayHashSet01.java @@ -74,47 +74,99 @@ public class TestArrayHashSet01 extends SingletonJunitCase { } } - public void populate(final List<Dummy> l, final int start, final int len, final int i2, final int i3, final int expectedPlusSize) { + void populate(final List<Dummy> l, final int start, final int len, + final int i2, final int i3, final int expectedPlusSize) { final int oldSize = l.size(); - int pos = start+len-1; - while(pos>=start) { - l.add(new Dummy(pos--, i2, i3)); + for(int pos = start+len-1; pos>=start; pos--) { + l.add(new Dummy(pos, i2, i3)); } Assert.assertEquals(expectedPlusSize, l.size() - oldSize); } + boolean checkOrder(final List<Dummy> l, final int startIdx, final int start, final int len) { + for(int i=0; i<len; i++) { + final Dummy d = l.get(startIdx+i); + final int i1 = start+len-1-i; + if( d.i1 != i1 ) { + return false; + } + } + return true; + } @Test - public void test01ArrayHashSet() { - final ArrayHashSet<Dummy> l = new ArrayHashSet<Dummy>(); - populate(l, 10, 100, 22, 34, 100); // [10 .. 109] - populate(l, 10, 100, 22, 34, 0); // [10 .. 109] - populate(l, 6, 5, 22, 34, 4); // [ 6 .. 9], 10 already exists - - final Dummy p6_22_34 = new Dummy(6, 22, 34); + public void test01ArrayHashSetWithNullValue() { + testArrayHashSetImpl(true); + } + @Test + public void test02ArrayHashSetWithoutNullValue() { + testArrayHashSetImpl(false); + } + void testArrayHashSetImpl(final boolean supportNullValue) { + final ArrayHashSet<Dummy> l = + new ArrayHashSet<Dummy>(supportNullValue, + ArrayHashSet.DEFAULT_INITIAL_CAPACITY, + ArrayHashSet.DEFAULT_LOAD_FACTOR); + Assert.assertEquals(supportNullValue, l.supportsNullValue()); + final int p7_22_34_idx; + final Dummy p7_22_34_orig; + final int p6_22_34_idx; + final Dummy p6_22_34_orig; + { + populate(l, 10, 100, 22, 34, 100); // [109 .. 10] + Assert.assertTrue(checkOrder(l, 0, 10, 100)); + populate(l, 10, 100, 22, 34, 0); // [109 .. 10] + Assert.assertTrue(checkOrder(l, 0, 10, 100)); + populate(l, 6, 5, 22, 34, 4); // [ 9 .. 6], 10 already exists + Assert.assertTrue(checkOrder(l, 100, 6, 4)); + p7_22_34_idx = l.size() - 2; + p7_22_34_orig = l.get(p7_22_34_idx); + p6_22_34_idx = l.size() - 1; + p6_22_34_orig = l.get(p6_22_34_idx); + } + Assert.assertNotNull(p7_22_34_orig); + Assert.assertEquals(7, p7_22_34_orig.i1); + Assert.assertEquals(l.getData().get(p7_22_34_idx), p7_22_34_orig); + Assert.assertNotNull(p6_22_34_orig); + Assert.assertEquals(6, p6_22_34_orig.i1); + Assert.assertEquals(l.getData().get(p6_22_34_idx), p6_22_34_orig); + + final Dummy p7_22_34_other = new Dummy(7, 22, 34); + Assert.assertEquals(p7_22_34_other, p7_22_34_orig); + Assert.assertTrue(p7_22_34_other.hashCode() == p7_22_34_orig.hashCode()); + Assert.assertTrue(p7_22_34_other != p7_22_34_orig); // diff reference + final Dummy p6_22_34_other = new Dummy(6, 22, 34); + Assert.assertEquals(p6_22_34_other, p6_22_34_orig); + Assert.assertTrue(p6_22_34_other.hashCode() == p6_22_34_orig.hashCode()); + Assert.assertTrue(p6_22_34_other != p6_22_34_orig); // diff reference // slow get on position .. - final int i = l.indexOf(p6_22_34); + final int i = l.indexOf(p6_22_34_other); Dummy q = l.get(i); Assert.assertNotNull(q); - Assert.assertEquals(p6_22_34, q); - Assert.assertTrue(p6_22_34.hashCode() == q.hashCode()); - Assert.assertTrue(p6_22_34 != q); // diff reference + Assert.assertEquals(p6_22_34_other, q); + Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode()); + Assert.assertTrue(p6_22_34_other != q); // diff reference + Assert.assertTrue(p6_22_34_orig == q); // same reference // fast identity .. - q = l.get(p6_22_34); + q = l.get(p6_22_34_other); Assert.assertNotNull(q); - Assert.assertEquals(p6_22_34, q); - Assert.assertTrue(p6_22_34.hashCode() == q.hashCode()); - Assert.assertTrue(p6_22_34 != q); // diff reference + Assert.assertEquals(p6_22_34_other, q); + Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode()); + Assert.assertTrue(p6_22_34_other != q); // diff reference + Assert.assertTrue(p6_22_34_orig == q); // same reference Assert.assertTrue(!l.add(q)); // add same - Assert.assertTrue(!l.add(p6_22_34)); // add equivalent + Assert.assertTrue(!l.add(p6_22_34_other)); // add equivalent - q = l.getOrAdd(p6_22_34); // not added test + q = l.getOrAdd(p6_22_34_other); // not added test w/ diff hash-obj Assert.assertNotNull(q); - Assert.assertEquals(p6_22_34, q); - Assert.assertTrue(p6_22_34.hashCode() == q.hashCode()); - Assert.assertTrue(p6_22_34 != q); // diff reference + Assert.assertEquals(p6_22_34_other, q); + Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode()); + Assert.assertTrue(p6_22_34_other != q); // diff reference + Assert.assertTrue(p6_22_34_orig == q); // same reference + Assert.assertTrue(checkOrder(l, 0, 10, 100)); + Assert.assertTrue(checkOrder(l, 100, 6, 4)); final Dummy p1_2_3 = new Dummy(1, 2, 3); // a new one .. q = l.getOrAdd(p1_2_3); // added test @@ -122,6 +174,29 @@ public class TestArrayHashSet01 extends SingletonJunitCase { Assert.assertEquals(p1_2_3, q); Assert.assertTrue(p1_2_3.hashCode() == q.hashCode()); Assert.assertTrue(p1_2_3 == q); // _same_ reference, since getOrAdd added it + Assert.assertTrue(checkOrder(l, 0, 10, 100)); + Assert.assertTrue(checkOrder(l, 100, 6, 4)); + + final Dummy pNull = null; + NullPointerException npe = null; + try { + q = l.getOrAdd(pNull); + Assert.assertNull(q); + } catch (final NullPointerException _npe) { npe = _npe; } + if( l.supportsNullValue() ) { + Assert.assertNull(npe); + } else { + Assert.assertNotNull(npe); + } + + try { + Assert.assertTrue(l.remove(pNull)); + } catch (final NullPointerException _npe) { npe = _npe; } + if( l.supportsNullValue() ) { + Assert.assertNull(npe); + } else { + Assert.assertNotNull(npe); + } } public static void main(final String args[]) throws IOException { diff --git a/src/junit/com/jogamp/common/util/TestBitfield00.java b/src/junit/com/jogamp/common/util/TestBitfield00.java new file mode 100644 index 0000000..9ace743 --- /dev/null +++ b/src/junit/com/jogamp/common/util/TestBitfield00.java @@ -0,0 +1,431 @@ +/** + * Copyright 2015 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.common.util; + +import java.io.IOException; + +import org.junit.Test; +import org.junit.Assert; + +import com.jogamp.junit.util.SingletonJunitCase; + +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +/** + * Test basic bitfield operations for {@link Bitfield} + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestBitfield00 extends SingletonJunitCase { + + @Test + public void test01_BitCount32_One() { + final String[] pyramid32bit_one = BitDemoData.pyramid32bit_one; + for(int i=0; i<pyramid32bit_one.length; i++) { + final int val0 = 1 << i; + final int oneBitCountI = Integer.bitCount(val0); + final String pattern0 = pyramid32bit_one[i]; + final int val1 = BitDemoData.toInteger(pattern0); + final String pattern1 = BitDemoData.toBinaryString(val0, 32); + final int oneBitCount0 = BitDemoData.getOneBitCount(pattern0); + final int oneBitCount1 = Bitfield.Util.bitCount(val0); + final String msg = String.format("Round %02d: 0x%08x %s, c %d / %d%n : 0x%08x %s, c %d%n", + i, val0, pattern0, oneBitCount0, oneBitCountI, val1, pattern1, oneBitCount1); + + Assert.assertEquals(msg, val0, val1); + Assert.assertEquals(msg, pattern0, pattern1); + + Assert.assertEquals(msg, oneBitCount0, oneBitCountI); + Assert.assertEquals(msg, oneBitCount0, oneBitCount1); + } + } + + @SuppressWarnings("unused") + @Test + public void test02_BitCount32_Samples() { + final long MAX = BitDemoData.UNSIGNED_INT_MAX_VALUE; + final long MAX_minus = MAX-0x1FF; + final long MAX_half = MAX/2; + final long MAX_half_minus = MAX_half-0x1FF; + final long MAX_half_plus = MAX_half+0x1FF; + + if( false ) { + for(long l=0; l<=MAX; l++) { + test_BitCount32_Samples(l); + } + } + for(long l=0; l>=0x1FF; l++) { + test_BitCount32_Samples(l); + } + for(long l=MAX_half_minus; l<=MAX_half_plus; l++) { + test_BitCount32_Samples(l); + } + for(long l=MAX_minus; l<=MAX; l++) { + test_BitCount32_Samples(l); + } + } + static void test_BitCount32_Samples(final long l) { + final int oneBitCountL = Long.bitCount(l); + final int val0 = (int)l; + final int oneBitCountI = Integer.bitCount(val0); + final int oneBitCount1 = Bitfield.Util.bitCount(val0); + final String msg = String.format("Round 0x%08x, c %d / %d / %d", val0, + oneBitCountL, oneBitCountI, oneBitCount1); + Assert.assertEquals(msg, oneBitCountI, oneBitCountL); + Assert.assertEquals(msg, oneBitCountI, oneBitCount1); + } + + static int[] testDataOneBit = new int[] { + 0,0, 1,1, 2,1, 3,2, 4,1, 5,2, 6,2, 7,3, + 8,1, 9,2, 10,2, 11,3, 12,2, 13,3, 14,3, 15,4, 16,1, 17,2, + 0x3F,6, 0x40,1, 0x41,2, 0x7f,7, 0x80,1, 0x81,2, 0xfe,7, 0xff,8, + 0x4000,1, 0x4001,2, 0x7000,3, 0x7fff,15, + 0x0FFFFFF0,24, + 0x55555555,16, + 0x7F53F57B,23, 0xFEA7EAF6,23, /* << 1 */ + 0x80000000, 1, + 0xAAAAAAAA,16, + 0xC0C0C0C0, 8, + 0xFF000000, 8, + 0xFFFFFFFF,32 + }; + @Test + public void test03_BitCount32_Data() { + for(int i = 0; i<testDataOneBit.length; i+=2) { + test_BitCount32_Data(testDataOneBit[i], testDataOneBit[i+1]); + } + } + static void test_BitCount32_Data(final int i, final int expOneBits) { + final int oneBitCountI = Integer.bitCount(i); + final int oneBitCount1 = Bitfield.Util.bitCount(i); + final String msg = String.format("Round 0x%08x, c %d / %d", i, + oneBitCountI, oneBitCount1); + Assert.assertEquals(msg, oneBitCount1, expOneBits); + Assert.assertEquals(msg, oneBitCountI, oneBitCount1); + } + + @Test + public void test10_Setup() { + final int bitSize32 = 32; + final int bitSize128 = 128; + final Bitfield bf1 = Bitfield.Factory.create(bitSize32); + Assert.assertEquals(bitSize32, bf1.size()); + Assert.assertTrue(bf1 instanceof jogamp.common.util.Int32Bitfield); + final Bitfield bf2 = Bitfield.Factory.create(bitSize128); + Assert.assertEquals(bitSize128, bf2.size()); + Assert.assertTrue(bf2 instanceof jogamp.common.util.Int32ArrayBitfield); + + // verify no bit is set + Assert.assertEquals(0, bf1.bitCount()); + Assert.assertEquals(0, bf2.bitCount()); + + bf1.clearField(true); + bf2.clearField(true); + Assert.assertEquals(bf1.size(), bf1.bitCount()); + Assert.assertEquals(bf2.size(), bf2.bitCount()); + + bf1.clearField(false); + bf2.clearField(false); + Assert.assertEquals(0, bf1.bitCount()); + Assert.assertEquals(0, bf2.bitCount()); + } + static class TestDataBF { + final int bitSize; + final int val; + final String pattern; + public TestDataBF(final int bitSize, final int value, final String pattern) { + this.bitSize = bitSize; + this.val = value; + this.pattern = pattern; + } + } + static TestDataBF[] testDataBF32Bit = { + new TestDataBF(32, BitDemoData.testIntMSB, BitDemoData.testStringMSB), + new TestDataBF(32, BitDemoData.testIntMSB_rev, BitDemoData.testStringMSB_rev), + new TestDataBF(32, BitDemoData.testIntLSB, BitDemoData.testStringLSB), + new TestDataBF(32, BitDemoData.testIntLSB_revByte, BitDemoData.testStringLSB_revByte), + + // H->L : 0x04030201: 00000100 00000011 00000010 00000001 + new TestDataBF(32, 0x04030201, "00000100000000110000001000000001"), + + // H->L : 0xAFFECAFE: 10101111 11111110 11001010 11111110 + new TestDataBF(32, 0xAFFECAFE, "10101111111111101100101011111110"), + // H->L : 0xDEADBEEF: 11011110 10101101 10111110 11101111 + new TestDataBF(32, 0xDEADBEEF, "11011110101011011011111011101111") + }; + static TestDataBF[] testDataBF16Bit = { + // H->L : 0x0201: 00000100 00000011 00000010 00000001 + new TestDataBF(16, 0x0201, "0000001000000001"), + // H->L : 0x0403: 00000100 00000011 + new TestDataBF(16, 0x0403, "0000010000000011"), + + // H->L : 0xAFFE: 10101111 11111110 + new TestDataBF(16, 0xAFFE, "1010111111111110"), + // H->L : 0xCAFE: 11001010 11111110 + new TestDataBF(16, 0xCAFE, "1100101011111110"), + + // H->L : 0xDEADBEEF: 11011110 10101101 10111110 11101111 + new TestDataBF(16, 0xDEAD, "1101111010101101"), + new TestDataBF(16, 0xBEEF, "1011111011101111") + }; + static TestDataBF[] testDataBF3Bit = { + new TestDataBF(3, 0x01, "001"), + new TestDataBF(3, 0x02, "010"), + new TestDataBF(3, 0x05, "101") + }; + + @Test + public void test20_ValidateTestData() { + for(int i=0; i<testDataBF32Bit.length; i++) { + test_ValidateTestData( testDataBF32Bit[i] ); + } + for(int i=0; i<testDataBF16Bit.length; i++) { + test_ValidateTestData( testDataBF16Bit[i] ); + } + for(int i=0; i<testDataBF3Bit.length; i++) { + test_ValidateTestData( testDataBF3Bit[i] ); + } + } + static void test_ValidateTestData(final TestDataBF d) { + final int oneBitCount0 = Bitfield.Util.bitCount(d.val); + final int oneBitCount1 = BitDemoData.getOneBitCount(d.pattern); + Assert.assertEquals(oneBitCount1, oneBitCount0); + final String pattern0 = BitDemoData.toBinaryString(d.val, d.bitSize); + Assert.assertEquals(d.pattern, pattern0); + final int val1 = BitDemoData.toInteger(d.pattern); + Assert.assertEquals(d.val, val1); + Assert.assertEquals(d.bitSize, pattern0.length()); + } + + static void assertEquals(final Bitfield bf, final int bf_off, final int v, final String pattern, final int oneBitCount) { + final int len = pattern.length(); + for(int i=0; i<len; i++) { + final boolean exp0 = 0 != ( v & ( 1 << i ) ); + final boolean exp1 = '1' == pattern.charAt(len-1-i); + final boolean has = bf.get(i+bf_off); + final String msg = String.format("Pos %04d: Value 0x%08x / %s, c %d", i, v, pattern, oneBitCount); + Assert.assertEquals(msg, exp0, has); + Assert.assertEquals(msg, exp1, has); + } + } + + @Test + public void test21_Aligned32bit() { + for(int i=0; i<testDataBF32Bit.length; i++) { + test_Aligned32bit( testDataBF32Bit[i] ); + } + for(int i=0; i<testDataBF16Bit.length; i++) { + test_Aligned32bit( testDataBF16Bit[i] ); + } + for(int i=0; i<testDataBF3Bit.length; i++) { + test_Aligned32bit( testDataBF3Bit[i] ); + } + } + static int get32BitStorageSize(final int bits) { + final int units = Math.max(1, ( bits + 31 ) >>> 5); + return units << 5; + } + static void test_Aligned32bit(final TestDataBF d) { + final int oneBitCount = Bitfield.Util.bitCount(d.val); + + final Bitfield bf1 = Bitfield.Factory.create(d.bitSize); + Assert.assertEquals(get32BitStorageSize(d.bitSize), bf1.size()); + final Bitfield bf2 = Bitfield.Factory.create(d.bitSize+128); + Assert.assertEquals(get32BitStorageSize(d.bitSize+128), bf2.size()); + + bf1.put32( 0, d.bitSize, d.val); + Assert.assertEquals(d.val, bf1.get32( 0, d.bitSize)); + Assert.assertEquals(oneBitCount, bf1.bitCount()); + assertEquals(bf1, 0, d.val, d.pattern, oneBitCount); + + bf2.put32( 0, d.bitSize, d.val); + Assert.assertEquals(d.val, bf2.get32( 0, d.bitSize)); + Assert.assertEquals(oneBitCount*1, bf2.bitCount()); + assertEquals(bf2, 0, d.val, d.pattern, oneBitCount); + bf2.put32(64, d.bitSize, d.val); + Assert.assertEquals(d.val, bf2.get32(64, d.bitSize)); + Assert.assertEquals(oneBitCount*2, bf2.bitCount()); + assertEquals(bf2, 64, d.val, d.pattern, oneBitCount); + + Assert.assertEquals(d.val, bf2.copy32(0, 96, d.bitSize)); + Assert.assertEquals(d.val, bf2.get32(96, d.bitSize)); + Assert.assertEquals(oneBitCount*3, bf2.bitCount()); + assertEquals(bf2, 96, d.val, d.pattern, oneBitCount); + } + + @Test + public void test21_Unaligned() { + for(int i=0; i<testDataBF32Bit.length; i++) { + test_Unaligned(testDataBF32Bit[i]); + } + for(int i=0; i<testDataBF16Bit.length; i++) { + test_Unaligned(testDataBF16Bit[i]); + } + for(int i=0; i<testDataBF3Bit.length; i++) { + test_Unaligned( testDataBF3Bit[i] ); + } + } + static void test_Unaligned(final TestDataBF d) { + final Bitfield bf1 = Bitfield.Factory.create(d.bitSize); + final Bitfield bf2 = Bitfield.Factory.create(d.bitSize+128); + Assert.assertEquals(get32BitStorageSize(d.bitSize), bf1.size()); + Assert.assertEquals(get32BitStorageSize(d.bitSize+128), bf2.size()); + test_Unaligned( d, bf1 ); + test_Unaligned( d, bf2 ); + } + static void test_Unaligned(final TestDataBF d, final Bitfield bf) { + final int maxBitpos = bf.size()-d.bitSize; + for(int i=0; i<=maxBitpos; i++) { + bf.clearField(false); + test_Unaligned(d, bf, i); + } + } + static void test_Unaligned(final TestDataBF d, final Bitfield bf, final int lowBitnum) { + final int maxBitpos = bf.size()-d.bitSize; + final int oneBitCount = Bitfield.Util.bitCount(d.val); + + final String msg = String.format("Value 0x%08x / %s, l %d/%d, c %d, lbPos %d -> %d", + d.val, d.pattern, d.bitSize, bf.size(), oneBitCount, lowBitnum, maxBitpos); + + // + // via put32 + // + bf.put32( lowBitnum, d.bitSize, d.val); + for(int i=0; i<d.bitSize; i++) { + Assert.assertEquals(msg+", bitpos "+i, 0 != ( d.val & ( 1 << i ) ), bf.get(lowBitnum+i)); + } + Assert.assertEquals(msg, d.val, bf.get32( lowBitnum, d.bitSize)); + Assert.assertEquals(msg, oneBitCount, bf.bitCount()); + assertEquals(bf, lowBitnum, d.val, d.pattern, oneBitCount); + + // + // via copy32 + // + if( lowBitnum < maxBitpos ) { + // copy bits 1 forward + // clear trailing orig bit + Assert.assertEquals(msg, d.val, bf.copy32(lowBitnum, lowBitnum+1, d.bitSize)); + bf.clear(lowBitnum); + Assert.assertEquals(msg, d.val, bf.get32( lowBitnum+1, d.bitSize)); + Assert.assertEquals(msg, oneBitCount, bf.bitCount()); + assertEquals(bf, lowBitnum+1, d.val, d.pattern, oneBitCount); + } + + // test put() return value (previous value) + bf.clearField(false); + Assert.assertEquals(msg+", bitpos "+0, false, bf.put(lowBitnum+0, true)); + Assert.assertEquals(msg+", bitpos "+0, true, bf.put(lowBitnum+0, false)); + + // + // via put + // + for(int i=0; i<d.bitSize; i++) { + Assert.assertEquals(msg+", bitpos "+i, false, bf.put(lowBitnum+i, 0 != ( d.val & ( 1 << i ) ))); + } + Assert.assertEquals(msg, d.val, bf.get32(lowBitnum, d.bitSize)); + for(int i=0; i<d.bitSize; i++) { + Assert.assertEquals(msg+", bitpos "+i, 0 != ( d.val & ( 1 << i ) ), bf.get(lowBitnum+i)); + } + Assert.assertEquals(msg, oneBitCount, bf.bitCount()); + assertEquals(bf, lowBitnum, d.val, d.pattern, oneBitCount); + + // + // via copy + // + if( lowBitnum < maxBitpos ) { + // copy bits 1 forward + // clear trailing orig bit + for(int i=d.bitSize-1; i>=0; i--) { + Assert.assertEquals(msg+", bitpos "+i, 0 != ( d.val & ( 1 << i ) ), + bf.copy(lowBitnum+i, lowBitnum+1+i) ); + } + bf.clear(lowBitnum); + Assert.assertEquals(msg, d.val, bf.get32( lowBitnum+1, d.bitSize)); + for(int i=0; i<d.bitSize; i++) { + Assert.assertEquals(msg+", bitpos "+i, 0 != ( d.val & ( 1 << i ) ), bf.get(lowBitnum+1+i)); + } + Assert.assertEquals(msg, oneBitCount, bf.bitCount()); + assertEquals(bf, lowBitnum+1, d.val, d.pattern, oneBitCount); + } + + // + // via set/clear + // + bf.clearField(false); + for(int i=0; i<d.bitSize; i++) { + if( 0 != ( d.val & ( 1 << i ) ) ) { + bf.set(lowBitnum+i); + } else { + bf.clear(lowBitnum+i); + } + } + Assert.assertEquals(msg, d.val, bf.get32(lowBitnum, d.bitSize)); + for(int i=0; i<d.bitSize; i++) { + Assert.assertEquals(msg+", bitpos "+i, 0 != ( d.val & ( 1 << i ) ), bf.get(lowBitnum+i)); + } + Assert.assertEquals(msg, oneBitCount, bf.bitCount()); + assertEquals(bf, lowBitnum, d.val, d.pattern, oneBitCount); + + // + // Validate 'other bits' put32/get32 + // + bf.clearField(false); + bf.put32( lowBitnum, d.bitSize, d.val); + checkOtherBits(d, bf, lowBitnum, msg, 0); + + bf.clearField(true); + bf.put32( lowBitnum, d.bitSize, d.val); + checkOtherBits(d, bf, lowBitnum, msg, Bitfield.UNSIGNED_INT_MAX_VALUE); + } + + static void checkOtherBits(final TestDataBF d, final Bitfield bf, final int lowBitnum, final String msg, final int expBits) { + final int highBitnum = lowBitnum + d.bitSize - 1; + // System.err.println(msg+": [0"+".."+"("+lowBitnum+".."+highBitnum+").."+(bf.size()-1)+"]"); + for(int i=0; i<lowBitnum; i+=32) { + final int len = Math.min(32, lowBitnum-i); + final int val = bf.get32(i, len); + final int exp = expBits & Bitfield.Util.getBitMask(len); + // System.err.println(" <"+i+".."+(i+len-1)+">, exp "+BitDemoData.toHexString(exp)); + Assert.assertEquals(msg+", bitpos "+i, exp, val); + } + for(int i=highBitnum+1; i<bf.size(); i+=32) { + final int len = Math.min(32, bf.size() - i); + final int val = bf.get32(i, len); + final int exp = expBits & Bitfield.Util.getBitMask(len); + // System.err.println(" <"+i+".."+(i+len-1)+">, exp "+BitDemoData.toHexString(exp)); + Assert.assertEquals(msg+", bitpos "+i, exp, val); + } + } + + public static void main(final String args[]) throws IOException { + final String tstname = TestBitfield00.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } + +} diff --git a/src/junit/com/jogamp/common/util/TestBitstream00.java b/src/junit/com/jogamp/common/util/TestBitstream00.java index 920363b..a2fd095 100644 --- a/src/junit/com/jogamp/common/util/TestBitstream00.java +++ b/src/junit/com/jogamp/common/util/TestBitstream00.java @@ -40,7 +40,7 @@ import org.junit.Assert; import com.jogamp.common.nio.Buffers; import com.jogamp.common.os.Platform; -import static com.jogamp.common.util.BitstreamData.*; +import static com.jogamp.common.util.BitDemoData.*; import com.jogamp.junit.util.SingletonJunitCase; diff --git a/src/junit/com/jogamp/common/util/TestBitstream01.java b/src/junit/com/jogamp/common/util/TestBitstream01.java index 49931a3..bff37e3 100644 --- a/src/junit/com/jogamp/common/util/TestBitstream01.java +++ b/src/junit/com/jogamp/common/util/TestBitstream01.java @@ -35,7 +35,7 @@ import org.junit.Assert; import org.junit.Test; import com.jogamp.junit.util.SingletonJunitCase; -import static com.jogamp.common.util.BitstreamData.*; +import static com.jogamp.common.util.BitDemoData.*; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; diff --git a/src/junit/com/jogamp/common/util/TestBitstream02.java b/src/junit/com/jogamp/common/util/TestBitstream02.java index 16904a2..5e6da1c 100644 --- a/src/junit/com/jogamp/common/util/TestBitstream02.java +++ b/src/junit/com/jogamp/common/util/TestBitstream02.java @@ -37,7 +37,7 @@ import org.junit.Test; import com.jogamp.common.nio.Buffers; import com.jogamp.junit.util.SingletonJunitCase; -import static com.jogamp.common.util.BitstreamData.*; +import static com.jogamp.common.util.BitDemoData.*; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; diff --git a/src/junit/com/jogamp/common/util/TestBitstream03.java b/src/junit/com/jogamp/common/util/TestBitstream03.java index a6129d8..bd09d4a 100644 --- a/src/junit/com/jogamp/common/util/TestBitstream03.java +++ b/src/junit/com/jogamp/common/util/TestBitstream03.java @@ -38,7 +38,7 @@ import org.junit.Test; import com.jogamp.common.nio.Buffers; import com.jogamp.junit.util.SingletonJunitCase; -import static com.jogamp.common.util.BitstreamData.*; +import static com.jogamp.common.util.BitDemoData.*; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; diff --git a/src/junit/com/jogamp/common/util/TestBitstream04.java b/src/junit/com/jogamp/common/util/TestBitstream04.java index 47be38d..2302e2e 100644 --- a/src/junit/com/jogamp/common/util/TestBitstream04.java +++ b/src/junit/com/jogamp/common/util/TestBitstream04.java @@ -38,7 +38,7 @@ import org.junit.Test; import com.jogamp.common.nio.Buffers; import com.jogamp.junit.util.SingletonJunitCase; -import static com.jogamp.common.util.BitstreamData.*; +import static com.jogamp.common.util.BitDemoData.*; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; diff --git a/src/junit/com/jogamp/common/util/TestIOUtil01.java b/src/junit/com/jogamp/common/util/TestIOUtil01.java index e85aa37..19bc8b0 100644 --- a/src/junit/com/jogamp/common/util/TestIOUtil01.java +++ b/src/junit/com/jogamp/common/util/TestIOUtil01.java @@ -35,6 +35,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.net.URISyntaxException; import java.net.URLConnection; import java.nio.ByteBuffer; @@ -43,6 +44,9 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import com.jogamp.common.ExceptionUtils; +import com.jogamp.common.net.URIDumpUtil; +import com.jogamp.common.net.Uri; import com.jogamp.common.os.MachineDataInfo; import com.jogamp.common.os.Platform; import com.jogamp.junit.util.SingletonJunitCase; @@ -78,8 +82,86 @@ public class TestIOUtil01 extends SingletonJunitCase { } @Test - public void testCopyStream01Array() throws IOException { - final URLConnection urlConn = IOUtil.getResource(this.getClass(), tfilename); + public void test01CleanPathString() throws IOException, URISyntaxException { + { + final String input = "./dummy/nop/../a.txt"; + final String expected = "dummy/a.txt"; + Assert.assertEquals(expected, IOUtil.cleanPathString(input)); + } + { + final String input = "../dummy/nop/../a.txt"; + final String expected = "../dummy/a.txt"; + Assert.assertEquals(expected, IOUtil.cleanPathString(input)); + } + { + final String input = ".././dummy/nop/../a.txt"; + final String expected = "../dummy/a.txt"; + Assert.assertEquals(expected, IOUtil.cleanPathString(input)); + } + { + final String input = "./../dummy/nop/../a.txt"; + final String expected = "../dummy/a.txt"; + Assert.assertEquals(expected, IOUtil.cleanPathString(input)); + } + { + final String input = "../dummy/./nop/../a.txt"; + final String expected = "../dummy/a.txt"; + Assert.assertEquals(expected, IOUtil.cleanPathString(input)); + } + { + final String input = "/dummy/nop/./../a.txt"; + final String expected = "/dummy/a.txt"; + Assert.assertEquals(expected, IOUtil.cleanPathString(input)); + } + { + final String input = "dummy/../nop/./.././aaa/bbb/../../a.txt"; + final String expected = "a.txt"; + Assert.assertEquals(expected, IOUtil.cleanPathString(input)); + } + { + final String input = "/dummy/../nop/./.././aaa/bbb/././ccc/../../../a.txt"; + final String expected = "/a.txt"; + Assert.assertEquals(expected, IOUtil.cleanPathString(input)); + } + { + URISyntaxException use = null; + try { + // Error case! + final String input = "../../error.txt"; + final String expected = "error.txt"; + final String result = IOUtil.cleanPathString(input); // URISyntaxException + System.err.println("input : "+input); + System.err.println("expected: "+expected); + System.err.println("result : "+result); + Assert.assertEquals(expected, result); + } catch (final URISyntaxException _use) { + use = _use; + ExceptionUtils.dumpThrowable("", _use, 0, 3); + } + Assert.assertNotNull("URISyntaxException expected", use); + } + { + URISyntaxException use = null; + try { + // Error case! + final String input = ".././a/../../error.txt"; + final String expected = "error.txt"; + final String result = IOUtil.cleanPathString(input); // URISyntaxException + System.err.println("input : "+input); + System.err.println("expected: "+expected); + System.err.println("result : "+result); + Assert.assertEquals(expected, result); + } catch (final URISyntaxException _use) { + use = _use; + ExceptionUtils.dumpThrowable("", _use, 0, 3); + } + Assert.assertNotNull("URISyntaxException expected", use); + } + } + + @Test + public void test11CopyStream01Array() throws IOException { + final URLConnection urlConn = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass()); Assert.assertNotNull(urlConn); final BufferedInputStream bis = new BufferedInputStream( urlConn.getInputStream() ); final byte[] bb; @@ -94,8 +176,8 @@ public class TestIOUtil01 extends SingletonJunitCase { } @Test - public void testCopyStream02Buffer() throws IOException { - final URLConnection urlConn = IOUtil.getResource(this.getClass(), tfilename); + public void test12CopyStream02Buffer() throws IOException { + final URLConnection urlConn = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass()); Assert.assertNotNull(urlConn); final BufferedInputStream bis = new BufferedInputStream( urlConn.getInputStream() ); final ByteBuffer bb; @@ -111,16 +193,16 @@ public class TestIOUtil01 extends SingletonJunitCase { } @Test - public void testCopyStream03Buffer() throws IOException { + public void test13CopyStream03Buffer() throws IOException { final String tfilename2 = "./test2.bin" ; - final URLConnection urlConn1 = IOUtil.getResource(this.getClass(), tfilename); + final URLConnection urlConn1 = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass()); Assert.assertNotNull(urlConn1); final File file2 = new File(tfilename2); file2.deleteOnExit(); try { IOUtil.copyURLConn2File(urlConn1, file2); - final URLConnection urlConn2 = IOUtil.getResource(this.getClass(), tfilename2); + final URLConnection urlConn2 = IOUtil.getResource(tfilename2, this.getClass().getClassLoader(), this.getClass()); Assert.assertNotNull(urlConn2); final BufferedInputStream bis = new BufferedInputStream( urlConn2.getInputStream() ); diff --git a/src/junit/com/jogamp/common/util/TestPlatform01.java b/src/junit/com/jogamp/common/util/TestPlatform01.java index 6f1fe0e..bf3e79d 100644 --- a/src/junit/com/jogamp/common/util/TestPlatform01.java +++ b/src/junit/com/jogamp/common/util/TestPlatform01.java @@ -44,7 +44,7 @@ public class TestPlatform01 extends SingletonJunitCase { @Test public void testInfo00() { System.err.println(); - System.err.println(); + System.err.print(Platform.getNewline()); System.err.println("OS name/type: "+Platform.getOSName()+", "+Platform.getOSType()); System.err.println("OS version: "+Platform.getOSVersion()+", "+Platform.getOSVersionNumber()); System.err.println(); diff --git a/src/junit/com/jogamp/common/util/TestRunnableTask01.java b/src/junit/com/jogamp/common/util/TestRunnableTask01.java index 76c2d2a..6fd8f19 100644 --- a/src/junit/com/jogamp/common/util/TestRunnableTask01.java +++ b/src/junit/com/jogamp/common/util/TestRunnableTask01.java @@ -60,7 +60,7 @@ public class TestRunnableTask01 extends SingletonJunitCase { System.err.println("BB.0: "+syncObject); synchronized (syncObject) { System.err.println("BB.1: "+syncObject); - new Thread(clientAction, Thread.currentThread().getName()+"-clientAction").start(); + new InterruptSource.Thread(null, clientAction, Thread.currentThread().getName()+"-clientAction").start(); try { System.err.println("BB.2"); syncObject.wait(); @@ -88,7 +88,7 @@ public class TestRunnableTask01 extends SingletonJunitCase { System.err.println("BB.0: "+rTask.getSyncObject()); synchronized (rTask.getSyncObject()) { System.err.println("BB.1: "+rTask.getSyncObject()); - new Thread(rTask, Thread.currentThread().getName()+"-clientAction").start(); + new InterruptSource.Thread(null, rTask, Thread.currentThread().getName()+"-clientAction").start(); try { System.err.println("BB.2"); rTask.getSyncObject().wait(); diff --git a/src/junit/com/jogamp/common/util/TestVersionSemantics.java b/src/junit/com/jogamp/common/util/TestVersionSemantics.java index 4fe4bcd..8e36684 100644 --- a/src/junit/com/jogamp/common/util/TestVersionSemantics.java +++ b/src/junit/com/jogamp/common/util/TestVersionSemantics.java @@ -89,6 +89,11 @@ public class TestVersionSemantics extends SingletonJunitCase { testVersions(diffCriteria, Delta.CompatibilityType.NON_BACKWARD_COMPATIBLE, "2.2.1", "2.3.0", excludesDefault); } + @Test + public void testVersionV230V232() throws IllegalArgumentException, IOException, URISyntaxException { + testVersions(diffCriteria, Delta.CompatibilityType.BACKWARD_COMPATIBLE_BINARY, "2.3.0", "2.3.2", excludesDefault); + } + void testVersions(final DiffCriteria diffCriteria, final Delta.CompatibilityType expectedCompatibilityType, final String v1, final String v2, final Set<String> excludes) throws IllegalArgumentException, IOException, URISyntaxException { @@ -100,16 +105,16 @@ public class TestVersionSemantics extends SingletonJunitCase { VersionSemanticsUtil.testVersion(diffCriteria, expectedCompatibilityType, previousJar, preVersionNumber, - currentJar, curVersionNumber, - excludes); + currentJar, curVersionNumber, excludes); } @Test - public void testVersionV230V23x() throws IllegalArgumentException, IOException, URISyntaxException { - // final Delta.CompatibilityType expectedCompatibilityType = Delta.CompatibilityType.NON_BACKWARD_COMPATIBLE; - final Delta.CompatibilityType expectedCompatibilityType = Delta.CompatibilityType.BACKWARD_COMPATIBLE_USER; + public void testVersionV232V24x() throws IllegalArgumentException, IOException, URISyntaxException { + final Delta.CompatibilityType expectedCompatibilityType = Delta.CompatibilityType.NON_BACKWARD_COMPATIBLE; + // final Delta.CompatibilityType expectedCompatibilityType = Delta.CompatibilityType.BACKWARD_COMPATIBLE_USER; + // final Delta.CompatibilityType expectedCompatibilityType = Delta.CompatibilityType.BACKWARD_COMPATIBLE_BINARY; - final VersionNumberString preVersionNumber = new VersionNumberString("2.3.0"); + final VersionNumberString preVersionNumber = new VersionNumberString("2.3.2"); final File previousJar = new File("lib/v"+preVersionNumber.getVersionString()+"/"+jarFile); final ClassLoader currentCL = TestVersionSemantics.class.getClassLoader(); diff --git a/src/junit/com/jogamp/common/util/locks/TestRecursiveLock01.java b/src/junit/com/jogamp/common/util/locks/TestRecursiveLock01.java index 4508f94..7455618 100644 --- a/src/junit/com/jogamp/common/util/locks/TestRecursiveLock01.java +++ b/src/junit/com/jogamp/common/util/locks/TestRecursiveLock01.java @@ -38,6 +38,7 @@ import org.junit.Assert; import org.junit.Test; import com.jogamp.common.os.Platform; +import com.jogamp.common.util.InterruptSource; import com.jogamp.junit.util.SingletonJunitCase; import org.junit.FixMethodOrder; @@ -170,7 +171,7 @@ public class TestRecursiveLock01 extends SingletonJunitCase { public final void action2Deferred(final int l, final YieldMode yieldMode) { final Action2 action2 = new Action2(l, yieldMode); - new Thread(action2, Thread.currentThread().getName()+"-deferred").start(); + new InterruptSource.Thread(null, action2, Thread.currentThread().getName()+"-deferred").start(); } public final void lock() { @@ -296,14 +297,14 @@ public class TestRecursiveLock01 extends SingletonJunitCase { final long t0 = System.currentTimeMillis(); final LockedObject lo = new LockedObject(implType, fair); final LockedObjectRunner[] runners = new LockedObjectRunner[threadNum]; - final Thread[] threads = new Thread[threadNum]; + final InterruptSource.Thread[] threads = new InterruptSource.Thread[threadNum]; int i; for(i=0; i<threadNum; i++) { runners[i] = new LockedObjectRunner1(lo, loops, iloops, yieldMode); // String name = Thread.currentThread().getName()+"-ActionThread-"+i+"_of_"+threadNum; final String name = "ActionThread-"+i+"_of_"+threadNum; - threads[i] = new Thread( runners[i], name ); + threads[i] = new InterruptSource.Thread( null, runners[i], name ); threads[i].start(); } diff --git a/src/junit/com/jogamp/common/util/locks/TestRecursiveThreadGroupLock01.java b/src/junit/com/jogamp/common/util/locks/TestRecursiveThreadGroupLock01.java index e35d146..30a0274 100644 --- a/src/junit/com/jogamp/common/util/locks/TestRecursiveThreadGroupLock01.java +++ b/src/junit/com/jogamp/common/util/locks/TestRecursiveThreadGroupLock01.java @@ -34,6 +34,7 @@ import org.junit.Assert; import org.junit.Test; import com.jogamp.common.os.Platform; +import com.jogamp.common.util.InterruptSource; import com.jogamp.junit.util.SingletonJunitCase; import org.junit.FixMethodOrder; @@ -239,15 +240,15 @@ public class TestRecursiveThreadGroupLock01 extends SingletonJunitCase { final LockedObject lo = new LockedObject(); final LockedObjectRunner[] concurrentRunners = new LockedObjectRunner[concurrentThreadNum]; final LockedObjectRunner[] slaveRunners = new LockedObjectRunner[slaveThreadNum]; - final Thread[] concurrentThreads = new Thread[concurrentThreadNum]; - final Thread[] slaveThreads = new Thread[slaveThreadNum]; - final Thread[] noCoOwnerThreads = new Thread[0]; + final InterruptSource.Thread[] concurrentThreads = new InterruptSource.Thread[concurrentThreadNum]; + final InterruptSource.Thread[] slaveThreads = new InterruptSource.Thread[slaveThreadNum]; + final InterruptSource.Thread[] noCoOwnerThreads = new InterruptSource.Thread[0]; int i; for(i=0; i<slaveThreadNum; i++) { slaveRunners[i] = new LockedObjectRunner1(" ", "s"+i, lo, loops, mark, yieldMode); final String name = "ActionThread-Slaves-"+i+"_of_"+slaveThreadNum; - slaveThreads[i] = new Thread( slaveRunners[i], name ); + slaveThreads[i] = new InterruptSource.Thread( null, slaveRunners[i], name ); } for(i=0; i<concurrentThreadNum; i++) { String name; @@ -258,7 +259,7 @@ public class TestRecursiveThreadGroupLock01 extends SingletonJunitCase { concurrentRunners[i] = new LockedObjectRunner1(" ", "O"+i, lo, noCoOwnerThreads, loops, mark, yieldMode); name = "ActionThread-Others-"+i+"_of_"+concurrentThreadNum; } - concurrentThreads[i] = new Thread( concurrentRunners[i], name ); + concurrentThreads[i] = new InterruptSource.Thread( null, concurrentRunners[i], name ); concurrentThreads[i].start(); if(i==0) { // master thread w/ slaves shall start first diff --git a/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket00.java b/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket00.java index b018a79..ce0087a 100644 --- a/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket00.java +++ b/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket00.java @@ -35,10 +35,12 @@ import org.junit.Test; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; +import com.jogamp.common.util.InterruptSource; +import com.jogamp.junit.util.JunitTracer; import com.jogamp.junit.util.SingletonJunitCase; @FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class TestSingletonServerSocket00 { +public class TestSingletonServerSocket00 extends JunitTracer { public static final long SINGLE_INSTANCE_LOCK_TO = SingletonJunitCase.SINGLE_INSTANCE_LOCK_TO; public static final long SINGLE_INSTANCE_LOCK_POLL = 100; // poll every 100ms @@ -69,7 +71,7 @@ public class TestSingletonServerSocket00 { } private Thread startLockUnlockOffThread(final int i) { - final Thread t = new Thread(new Runnable() { + final Thread t = new InterruptSource.Thread(null, new Runnable() { public void run() { final SingletonInstance myLock = SingletonInstance.createServerSocket(10, SingletonJunitCase.SINGLE_INSTANCE_LOCK_PORT); System.err.println(Thread.currentThread().getName()+" LOCK try .."); diff --git a/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket01.java b/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket01.java index b37e600..c637865 100644 --- a/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket01.java +++ b/src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket01.java @@ -35,10 +35,11 @@ import org.junit.Test; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; +import com.jogamp.junit.util.JunitTracer; import com.jogamp.junit.util.SingletonJunitCase; @FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class TestSingletonServerSocket01 { +public class TestSingletonServerSocket01 extends JunitTracer { private static volatile SingletonInstance singletonInstance; @BeforeClass |