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

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

TestArrayHashSet01.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.util.*;
32import java.io.IOException;
33
34import org.junit.Assert;
35import org.junit.Test;
36
37import com.jogamp.junit.util.SingletonJunitCase;
38
39import org.junit.FixMethodOrder;
40import org.junit.runners.MethodSorters;
41
42@FixMethodOrder(MethodSorters.NAME_ASCENDING)
43public class TestArrayHashSet01 extends SingletonJunitCase {
44
45 public static class Dummy {
46 int i1, i2, i3;
47
48 public Dummy(final int i1, final int i2, final int i3) {
49 this.i1 = i1;
50 this.i2 = i2;
51 this.i3 = i3;
52 }
53
54 public boolean equals(final Object o) {
55 if(o instanceof Dummy) {
56 final Dummy d = (Dummy)o;
57 return this.i1 == d.i1 &&
58 this.i2 == d.i2 &&
59 this.i3 == d.i3 ;
60 }
61 return false;
62 }
63
64 public final int hashCode() {
65 // 31 * x == (x << 5) - x
66 int hash = 31 + i1;
67 hash = ((hash << 5) - hash) + i2;
68 hash = ((hash << 5) - hash) + i3;
69 return hash;
70 }
71
72 public String toString() {
73 return "Dummy["+super.toString()+": "+i1+", "+i2+", "+i3+"]";
74 }
75 }
76
77 void populate(final List<Dummy> l, final int start, final int len,
78 final int i2, final int i3, final int expectedPlusSize) {
79 final int oldSize = l.size();
80 for(int pos = start+len-1; pos>=start; pos--) {
81 l.add(new Dummy(pos, i2, i3));
82 }
83 Assert.assertEquals(expectedPlusSize, l.size() - oldSize);
84 }
85 boolean checkOrder(final List<Dummy> l, final int startIdx, final int start, final int len) {
86 for(int i=0; i<len; i++) {
87 final Dummy d = l.get(startIdx+i);
88 final int i1 = start+len-1-i;
89 if( d.i1 != i1 ) {
90 return false;
91 }
92 }
93 return true;
94 }
95
96 @Test
97 public void test01ArrayHashSetWithNullValue() {
98 testArrayHashSetImpl(true);
99 }
100 @Test
101 public void test02ArrayHashSetWithoutNullValue() {
102 testArrayHashSetImpl(false);
103 }
104 void testArrayHashSetImpl(final boolean supportNullValue) {
105 final ArrayHashSet<Dummy> l =
106 new ArrayHashSet<Dummy>(supportNullValue,
107 ArrayHashSet.DEFAULT_INITIAL_CAPACITY,
108 ArrayHashSet.DEFAULT_LOAD_FACTOR);
109 Assert.assertEquals(supportNullValue, l.supportsNullValue());
110 final int p7_22_34_idx;
111 final Dummy p7_22_34_orig;
112 final int p6_22_34_idx;
113 final Dummy p6_22_34_orig;
114 {
115 populate(l, 10, 100, 22, 34, 100); // [109 .. 10]
116 Assert.assertTrue(checkOrder(l, 0, 10, 100));
117 populate(l, 10, 100, 22, 34, 0); // [109 .. 10]
118 Assert.assertTrue(checkOrder(l, 0, 10, 100));
119 populate(l, 6, 5, 22, 34, 4); // [ 9 .. 6], 10 already exists
120 Assert.assertTrue(checkOrder(l, 100, 6, 4));
121 p7_22_34_idx = l.size() - 2;
122 p7_22_34_orig = l.get(p7_22_34_idx);
123 p6_22_34_idx = l.size() - 1;
124 p6_22_34_orig = l.get(p6_22_34_idx);
125 }
126 Assert.assertNotNull(p7_22_34_orig);
127 Assert.assertEquals(7, p7_22_34_orig.i1);
128 Assert.assertEquals(l.getData().get(p7_22_34_idx), p7_22_34_orig);
129 Assert.assertNotNull(p6_22_34_orig);
130 Assert.assertEquals(6, p6_22_34_orig.i1);
131 Assert.assertEquals(l.getData().get(p6_22_34_idx), p6_22_34_orig);
132
133 final Dummy p7_22_34_other = new Dummy(7, 22, 34);
134 Assert.assertEquals(p7_22_34_other, p7_22_34_orig);
135 Assert.assertTrue(p7_22_34_other.hashCode() == p7_22_34_orig.hashCode());
136 Assert.assertTrue(p7_22_34_other != p7_22_34_orig); // diff reference
137 final Dummy p6_22_34_other = new Dummy(6, 22, 34);
138 Assert.assertEquals(p6_22_34_other, p6_22_34_orig);
139 Assert.assertTrue(p6_22_34_other.hashCode() == p6_22_34_orig.hashCode());
140 Assert.assertTrue(p6_22_34_other != p6_22_34_orig); // diff reference
141
142 // slow get on position ..
143 final int i = l.indexOf(p6_22_34_other);
144 Dummy q = l.get(i);
145 Assert.assertNotNull(q);
146 Assert.assertEquals(p6_22_34_other, q);
147 Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
148 Assert.assertTrue(p6_22_34_other != q); // diff reference
149 Assert.assertTrue(p6_22_34_orig == q); // same reference
150
151 // fast identity ..
152 q = l.get(p6_22_34_other);
153 Assert.assertNotNull(q);
154 Assert.assertEquals(p6_22_34_other, q);
155 Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
156 Assert.assertTrue(p6_22_34_other != q); // diff reference
157 Assert.assertTrue(p6_22_34_orig == q); // same reference
158
159 Assert.assertTrue(!l.add(q)); // add same
160 Assert.assertTrue(!l.add(p6_22_34_other)); // add equivalent
161
162 q = l.getOrAdd(p6_22_34_other); // not added test w/ diff hash-obj
163 Assert.assertNotNull(q);
164 Assert.assertEquals(p6_22_34_other, q);
165 Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
166 Assert.assertTrue(p6_22_34_other != q); // diff reference
167 Assert.assertTrue(p6_22_34_orig == q); // same reference
168 Assert.assertTrue(checkOrder(l, 0, 10, 100));
169 Assert.assertTrue(checkOrder(l, 100, 6, 4));
170
171 final Dummy p1_2_3 = new Dummy(1, 2, 3); // a new one ..
172 q = l.getOrAdd(p1_2_3); // added test
173 Assert.assertNotNull(q);
174 Assert.assertEquals(p1_2_3, q);
175 Assert.assertTrue(p1_2_3.hashCode() == q.hashCode());
176 Assert.assertTrue(p1_2_3 == q); // _same_ reference, since getOrAdd added it
177 Assert.assertTrue(checkOrder(l, 0, 10, 100));
178 Assert.assertTrue(checkOrder(l, 100, 6, 4));
179
180 final Dummy pNull = null;
181 NullPointerException npe = null;
182 try {
183 q = l.getOrAdd(pNull);
184 Assert.assertNull(q);
185 } catch (final NullPointerException _npe) { npe = _npe; }
186 if( l.supportsNullValue() ) {
187 Assert.assertNull(npe);
188 } else {
189 Assert.assertNotNull(npe);
190 }
191
192 try {
193 Assert.assertTrue(l.remove(pNull));
194 } catch (final NullPointerException _npe) { npe = _npe; }
195 if( l.supportsNullValue() ) {
196 Assert.assertNull(npe);
197 } else {
198 Assert.assertNotNull(npe);
199 }
200 }
201
202 public static void main(final String args[]) throws IOException {
203 final String tstname = TestArrayHashSet01.class.getName();
204 org.junit.runner.JUnitCore.main(tstname);
205 }
206
207}
com.jogamp.common.util.ArrayHashSet
Hashed ArrayList implementation of the List and Collection interface.
Definition: ArrayHashSet.java:70
com.jogamp.common.util.ArrayHashSet.getData
final ArrayList< E > getData()
Returns this object ordered ArrayList.
Definition: ArrayHashSet.java:131
com.jogamp.common.util.ArrayHashSet.getOrAdd
final E getOrAdd(final E element)
Identity method allowing to get the identical object, using the internal hash map.
Definition: ArrayHashSet.java:522
com.jogamp.common.util.ArrayHashSet.size
final int size()
Definition: ArrayHashSet.java:358
com.jogamp.common.util.ArrayHashSet.DEFAULT_LOAD_FACTOR
static final float DEFAULT_LOAD_FACTOR
Default load factor: {@value}.
Definition: ArrayHashSet.java:74
com.jogamp.common.util.ArrayHashSet.supportsNullValue
final boolean supportsNullValue()
Returns true for default behavior, i.e.
Definition: ArrayHashSet.java:116
com.jogamp.common.util.ArrayHashSet.indexOf
final int indexOf(final Object element)
Definition: ArrayHashSet.java:382
com.jogamp.common.util.ArrayHashSet.remove
final boolean remove(final Object element)
Remove element from this list.
Definition: ArrayHashSet.java:193
com.jogamp.common.util.ArrayHashSet.add
final boolean add(final E element)
Add element at the end of this list, if it is not contained yet.
Definition: ArrayHashSet.java:161
com.jogamp.common.util.ArrayHashSet.get
final E get(final int index)
Definition: ArrayHashSet.java:377
com.jogamp.common.util.ArrayHashSet.DEFAULT_INITIAL_CAPACITY
static final int DEFAULT_INITIAL_CAPACITY
The default initial capacity: {@value}.
Definition: ArrayHashSet.java:78
com.jogamp.common.util.TestArrayHashSet01.Dummy
Definition: TestArrayHashSet01.java:45
com.jogamp.common.util.TestArrayHashSet01.Dummy.equals
boolean equals(final Object o)
Definition: TestArrayHashSet01.java:54
com.jogamp.common.util.TestArrayHashSet01.Dummy.hashCode
final int hashCode()
Definition: TestArrayHashSet01.java:64
com.jogamp.common.util.TestArrayHashSet01.Dummy.toString
String toString()
Definition: TestArrayHashSet01.java:72
com.jogamp.common.util.TestArrayHashSet01.Dummy.Dummy
Dummy(final int i1, final int i2, final int i3)
Definition: TestArrayHashSet01.java:48
com.jogamp.common.util.TestArrayHashSet01
Definition: TestArrayHashSet01.java:43
com.jogamp.common.util.TestArrayHashSet01.main
static void main(final String args[])
Definition: TestArrayHashSet01.java:202
com.jogamp.common.util.TestArrayHashSet01.test02ArrayHashSetWithoutNullValue
void test02ArrayHashSetWithoutNullValue()
Definition: TestArrayHashSet01.java:101
com.jogamp.common.util.TestArrayHashSet01.test01ArrayHashSetWithNullValue
void test01ArrayHashSetWithNullValue()
Definition: TestArrayHashSet01.java:97
com.jogamp.junit.util.SingletonJunitCase
Definition: SingletonJunitCase.java:39