Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestArrayHashSet01.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2010 Gothel Software e.K.
5 * Copyright (c) 2010 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27package jau.test.util;
28
29import java.io.IOException;
30import java.util.List;
31
32import org.jau.util.ArrayHashSet;
33import org.junit.Assert;
34import org.junit.FixMethodOrder;
35import org.junit.Test;
36import org.junit.runners.MethodSorters;
37
38import jau.test.junit.util.JunitTracer;
39
40@FixMethodOrder(MethodSorters.NAME_ASCENDING)
41public class TestArrayHashSet01 extends JunitTracer {
42
43 public static class Dummy {
44 int i1, i2, i3;
45
46 public Dummy(final int i1, final int i2, final int i3) {
47 this.i1 = i1;
48 this.i2 = i2;
49 this.i3 = i3;
50 }
51
52 @Override
53 public boolean equals(final Object o) {
54 if(o instanceof Dummy) {
55 final Dummy d = (Dummy)o;
56 return this.i1 == d.i1 &&
57 this.i2 == d.i2 &&
58 this.i3 == d.i3 ;
59 }
60 return false;
61 }
62
63 @Override
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 @Override
73 public String toString() {
74 return "Dummy["+super.toString()+": "+i1+", "+i2+", "+i3+"]";
75 }
76 }
77
78 void populate(final List<Dummy> l, final int start, final int len,
79 final int i2, final int i3, final int expectedPlusSize) {
80 final int oldSize = l.size();
81 for(int pos = start+len-1; pos>=start; pos--) {
82 l.add(new Dummy(pos, i2, i3));
83 }
84 Assert.assertEquals(expectedPlusSize, l.size() - oldSize);
85 }
86 boolean checkOrder(final List<Dummy> l, final int startIdx, final int start, final int len) {
87 for(int i=0; i<len; i++) {
88 final Dummy d = l.get(startIdx+i);
89 final int i1 = start+len-1-i;
90 if( d.i1 != i1 ) {
91 return false;
92 }
93 }
94 return true;
95 }
96
97 @Test
99 testArrayHashSetImpl(true);
100 }
101 @Test
103 testArrayHashSetImpl(false);
104 }
105 void testArrayHashSetImpl(final boolean supportNullValue) {
106 final ArrayHashSet<Dummy> l =
107 new ArrayHashSet<Dummy>(supportNullValue,
108 ArrayHashSet.DEFAULT_INITIAL_CAPACITY,
109 ArrayHashSet.DEFAULT_LOAD_FACTOR);
110 Assert.assertEquals(supportNullValue, l.supportsNullValue());
111 final int p7_22_34_idx;
112 final Dummy p7_22_34_orig;
113 final int p6_22_34_idx;
114 final Dummy p6_22_34_orig;
115 {
116 populate(l, 10, 100, 22, 34, 100); // [109 .. 10]
117 Assert.assertTrue(checkOrder(l, 0, 10, 100));
118 populate(l, 10, 100, 22, 34, 0); // [109 .. 10]
119 Assert.assertTrue(checkOrder(l, 0, 10, 100));
120 populate(l, 6, 5, 22, 34, 4); // [ 9 .. 6], 10 already exists
121 Assert.assertTrue(checkOrder(l, 100, 6, 4));
122 p7_22_34_idx = l.size() - 2;
123 p7_22_34_orig = l.get(p7_22_34_idx);
124 p6_22_34_idx = l.size() - 1;
125 p6_22_34_orig = l.get(p6_22_34_idx);
126 }
127 Assert.assertNotNull(p7_22_34_orig);
128 Assert.assertEquals(7, p7_22_34_orig.i1);
129 Assert.assertEquals(l.getData().get(p7_22_34_idx), p7_22_34_orig);
130 Assert.assertNotNull(p6_22_34_orig);
131 Assert.assertEquals(6, p6_22_34_orig.i1);
132 Assert.assertEquals(l.getData().get(p6_22_34_idx), p6_22_34_orig);
133
134 final Dummy p7_22_34_other = new Dummy(7, 22, 34);
135 Assert.assertEquals(p7_22_34_other, p7_22_34_orig);
136 Assert.assertTrue(p7_22_34_other.hashCode() == p7_22_34_orig.hashCode());
137 Assert.assertTrue(p7_22_34_other != p7_22_34_orig); // diff reference
138 final Dummy p6_22_34_other = new Dummy(6, 22, 34);
139 Assert.assertEquals(p6_22_34_other, p6_22_34_orig);
140 Assert.assertTrue(p6_22_34_other.hashCode() == p6_22_34_orig.hashCode());
141 Assert.assertTrue(p6_22_34_other != p6_22_34_orig); // diff reference
142
143 // slow get on position ..
144 final int i = l.indexOf(p6_22_34_other);
145 Dummy q = l.get(i);
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 // fast identity ..
153 q = l.get(p6_22_34_other);
154 Assert.assertNotNull(q);
155 Assert.assertEquals(p6_22_34_other, q);
156 Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
157 Assert.assertTrue(p6_22_34_other != q); // diff reference
158 Assert.assertTrue(p6_22_34_orig == q); // same reference
159
160 Assert.assertTrue(!l.add(q)); // add same
161 Assert.assertTrue(!l.add(p6_22_34_other)); // add equivalent
162
163 q = l.getOrAdd(p6_22_34_other); // not added test w/ diff hash-obj
164 Assert.assertNotNull(q);
165 Assert.assertEquals(p6_22_34_other, q);
166 Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
167 Assert.assertTrue(p6_22_34_other != q); // diff reference
168 Assert.assertTrue(p6_22_34_orig == q); // same reference
169 Assert.assertTrue(checkOrder(l, 0, 10, 100));
170 Assert.assertTrue(checkOrder(l, 100, 6, 4));
171
172 final Dummy p1_2_3 = new Dummy(1, 2, 3); // a new one ..
173 q = l.getOrAdd(p1_2_3); // added test
174 Assert.assertNotNull(q);
175 Assert.assertEquals(p1_2_3, q);
176 Assert.assertTrue(p1_2_3.hashCode() == q.hashCode());
177 Assert.assertTrue(p1_2_3 == q); // _same_ reference, since getOrAdd added it
178 Assert.assertTrue(checkOrder(l, 0, 10, 100));
179 Assert.assertTrue(checkOrder(l, 100, 6, 4));
180
181 final Dummy pNull = null;
182 NullPointerException npe = null;
183 try {
184 q = l.getOrAdd(pNull);
185 Assert.assertNull(q);
186 } catch (final NullPointerException _npe) { npe = _npe; }
187 if( l.supportsNullValue() ) {
188 Assert.assertNull(npe);
189 } else {
190 Assert.assertNotNull(npe);
191 }
192
193 try {
194 Assert.assertTrue(l.remove(pNull));
195 } catch (final NullPointerException _npe) { npe = _npe; }
196 if( l.supportsNullValue() ) {
197 Assert.assertNull(npe);
198 } else {
199 Assert.assertNotNull(npe);
200 }
201 }
202
203 public static void main(final String args[]) throws IOException {
204 final String tstname = TestArrayHashSet01.class.getName();
205 org.junit.runner.JUnitCore.main(tstname);
206 }
207
208}
Dummy(final int i1, final int i2, final int i3)
static void main(final String args[])