GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestArrayHashMap01.java
Go to the documentation of this file.
1/**
2 * Copyright 2015 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)
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 Map<Integer, 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.put(pos, 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
98 testArrayHashMapImpl(true);
99 }
100 @Test
102 testArrayHashMapImpl(false);
103 }
104 void testArrayHashMapImpl(final boolean supportNullValue) {
106 new ArrayHashMap<Integer, Dummy>(supportNullValue,
109 Assert.assertEquals(supportNullValue, l.supportsNullValue());
110 final int p7_22_34_key, p7_22_34_idx;
111 final Dummy p7_22_34_orig;
112 final int p6_22_34_key, 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.getData(), 0, 10, 100));
117 populate(l, 10, 100, 22, 34, 0); // [109 .. 10]
118 Assert.assertTrue(checkOrder(l.getData(), 0, 10, 100));
119 populate(l, 6, 5, 22, 34, 4); // [ 9 .. 6], 10 already exists
120 Assert.assertTrue(checkOrder(l.getData(), 100, 6, 4));
121 p7_22_34_idx = l.size() - 2;
122 p7_22_34_key = 7;
123 p7_22_34_orig = l.get(p7_22_34_key);
124 p6_22_34_idx = l.size() - 1;
125 p6_22_34_key = 6;
126 p6_22_34_orig = l.get(p6_22_34_key);
127 }
128 Assert.assertNotNull(p7_22_34_orig);
129 Assert.assertEquals(7, p7_22_34_orig.i1);
130 Assert.assertEquals(l.getData().get(p7_22_34_idx), p7_22_34_orig);
131 Assert.assertNotNull(p6_22_34_orig);
132 Assert.assertEquals(6, p6_22_34_orig.i1);
133 Assert.assertEquals(l.getData().get(p6_22_34_idx), p6_22_34_orig);
134
135 final Dummy p7_22_34_other = new Dummy(7, 22, 34);
136 Assert.assertEquals(p7_22_34_other, p7_22_34_orig);
137 Assert.assertTrue(p7_22_34_other.hashCode() == p7_22_34_orig.hashCode());
138 Assert.assertTrue(p7_22_34_other != p7_22_34_orig); // diff reference
139 final Dummy p6_22_34_other = new Dummy(6, 22, 34);
140 Assert.assertEquals(p6_22_34_other, p6_22_34_orig);
141 Assert.assertTrue(p6_22_34_other.hashCode() == p6_22_34_orig.hashCode());
142 Assert.assertTrue(p6_22_34_other != p6_22_34_orig); // diff reference
143
144 // fast identity ..
145 Dummy q = l.get(p6_22_34_key);
146 Assert.assertNotNull(q);
147 Assert.assertEquals(p6_22_34_other, q);
148 Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
149 Assert.assertTrue(p6_22_34_other != q); // diff reference
150 Assert.assertTrue(p6_22_34_orig == q); // same reference
151
152 Assert.assertTrue(l.containsValue(q));
153 Assert.assertTrue(l.containsValue(p6_22_34_other)); // add equivalent
154
155 q = l.put(p6_22_34_key, p6_22_34_other); // override w/ diff hash-obj
156 Assert.assertNotNull(q);
157 Assert.assertEquals(p6_22_34_other, q);
158 Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
159 Assert.assertTrue(p6_22_34_other != q); // diff reference new != old (q)
160 Assert.assertTrue(p6_22_34_orig == q); // same reference orig == old (q)
161 Assert.assertTrue(checkOrder(l.getData(), 0, 10, 100));
162 Assert.assertTrue(checkOrder(l.getData(), 100, 6, 4));
163
164 final Dummy p1_2_3 = new Dummy(1, 2, 3); // a new one ..
165 q = l.put(1, p1_2_3); // added test
166 Assert.assertNull(q);
167
168 final Dummy pNull = null;
169 NullPointerException npe = null;
170 try {
171 q = l.put(0, pNull);
172 Assert.assertNull(q);
173 } catch (final NullPointerException _npe) { npe = _npe; }
174 if( l.supportsNullValue() ) {
175 Assert.assertNull(npe);
176 } else {
177 Assert.assertNotNull(npe);
178 }
179 }
180
181 public static void main(final String args[]) throws IOException {
182 final String tstname = TestArrayHashMap01.class.getName();
183 org.junit.runner.JUnitCore.main(tstname);
184 }
185
186}
HashMap implementation backed by an ArrayList to preserve order of values.
final boolean supportsNullValue()
Returns true for default behavior, i.e.
boolean containsValue(final Object value)
final ArrayList< V > getData()
Returns this object ordered ArrayList.
final V get(final Object key)
final V put(final K key, final V value)
Hashed ArrayList implementation of the List and Collection interface.
static final float DEFAULT_LOAD_FACTOR
Default load factor: {@value}.
static final int DEFAULT_INITIAL_CAPACITY
The default initial capacity: {@value}.
Dummy(final int i1, final int i2, final int i3)
static void main(final String args[])