Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestArrayHashMap01.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) 2015 Gothel Software e.K.
5 * Copyright (c) 2015 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;
31import java.util.Map;
32
33import org.jau.util.ArrayHashMap;
34import org.jau.util.ArrayHashSet;
35import org.junit.Assert;
36import org.junit.FixMethodOrder;
37import org.junit.Test;
38import org.junit.runners.MethodSorters;
39
40import jau.test.junit.util.JunitTracer;
41
42@FixMethodOrder(MethodSorters.NAME_ASCENDING)
43public class TestArrayHashMap01 extends JunitTracer {
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 @Override
55 public boolean equals(final Object o) {
56 if(o instanceof Dummy) {
57 final Dummy d = (Dummy)o;
58 return this.i1 == d.i1 &&
59 this.i2 == d.i2 &&
60 this.i3 == d.i3 ;
61 }
62 return false;
63 }
64
65 @Override
66 public final int hashCode() {
67 // 31 * x == (x << 5) - x
68 int hash = 31 + i1;
69 hash = ((hash << 5) - hash) + i2;
70 hash = ((hash << 5) - hash) + i3;
71 return hash;
72 }
73
74 @Override
75 public String toString() {
76 return "Dummy["+super.toString()+": "+i1+", "+i2+", "+i3+"]";
77 }
78 }
79
80 void populate(final Map<Integer, Dummy> l, final int start, final int len,
81 final int i2, final int i3, final int expectedPlusSize) {
82 final int oldSize = l.size();
83 for(int pos = start+len-1; pos>=start; pos--) {
84 l.put(pos, new Dummy(pos, i2, i3));
85 }
86 Assert.assertEquals(expectedPlusSize, l.size() - oldSize);
87 }
88 boolean checkOrder(final List<Dummy> l, final int startIdx, final int start, final int len) {
89 for(int i=0; i<len; i++) {
90 final Dummy d = l.get(startIdx+i);
91 final int i1 = start+len-1-i;
92 if( d.i1 != i1 ) {
93 return false;
94 }
95 }
96 return true;
97 }
98
99 @Test
101 testArrayHashMapImpl(true);
102 }
103 @Test
105 testArrayHashMapImpl(false);
106 }
107 void testArrayHashMapImpl(final boolean supportNullValue) {
108 final ArrayHashMap<Integer, Dummy> l =
109 new ArrayHashMap<Integer, Dummy>(supportNullValue,
110 ArrayHashSet.DEFAULT_INITIAL_CAPACITY,
111 ArrayHashSet.DEFAULT_LOAD_FACTOR);
112 Assert.assertEquals(supportNullValue, l.supportsNullValue());
113 final int p7_22_34_key, p7_22_34_idx;
114 final Dummy p7_22_34_orig;
115 final int p6_22_34_key, p6_22_34_idx;
116 final Dummy p6_22_34_orig;
117 {
118 populate(l, 10, 100, 22, 34, 100); // [109 .. 10]
119 Assert.assertTrue(checkOrder(l.getData(), 0, 10, 100));
120 populate(l, 10, 100, 22, 34, 0); // [109 .. 10]
121 Assert.assertTrue(checkOrder(l.getData(), 0, 10, 100));
122 populate(l, 6, 5, 22, 34, 4); // [ 9 .. 6], 10 already exists
123 Assert.assertTrue(checkOrder(l.getData(), 100, 6, 4));
124 p7_22_34_idx = l.size() - 2;
125 p7_22_34_key = 7;
126 p7_22_34_orig = l.get(p7_22_34_key);
127 p6_22_34_idx = l.size() - 1;
128 p6_22_34_key = 6;
129 p6_22_34_orig = l.get(p6_22_34_key);
130 }
131 Assert.assertNotNull(p7_22_34_orig);
132 Assert.assertEquals(7, p7_22_34_orig.i1);
133 Assert.assertEquals(l.getData().get(p7_22_34_idx), p7_22_34_orig);
134 Assert.assertNotNull(p6_22_34_orig);
135 Assert.assertEquals(6, p6_22_34_orig.i1);
136 Assert.assertEquals(l.getData().get(p6_22_34_idx), p6_22_34_orig);
137
138 final Dummy p7_22_34_other = new Dummy(7, 22, 34);
139 Assert.assertEquals(p7_22_34_other, p7_22_34_orig);
140 Assert.assertTrue(p7_22_34_other.hashCode() == p7_22_34_orig.hashCode());
141 Assert.assertTrue(p7_22_34_other != p7_22_34_orig); // diff reference
142 final Dummy p6_22_34_other = new Dummy(6, 22, 34);
143 Assert.assertEquals(p6_22_34_other, p6_22_34_orig);
144 Assert.assertTrue(p6_22_34_other.hashCode() == p6_22_34_orig.hashCode());
145 Assert.assertTrue(p6_22_34_other != p6_22_34_orig); // diff reference
146
147 // fast identity ..
148 Dummy q = l.get(p6_22_34_key);
149 Assert.assertNotNull(q);
150 Assert.assertEquals(p6_22_34_other, q);
151 Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
152 Assert.assertTrue(p6_22_34_other != q); // diff reference
153 Assert.assertTrue(p6_22_34_orig == q); // same reference
154
155 Assert.assertTrue(l.containsValue(q));
156 Assert.assertTrue(l.containsValue(p6_22_34_other)); // add equivalent
157
158 q = l.put(p6_22_34_key, p6_22_34_other); // override w/ diff hash-obj
159 Assert.assertNotNull(q);
160 Assert.assertEquals(p6_22_34_other, q);
161 Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
162 Assert.assertTrue(p6_22_34_other != q); // diff reference new != old (q)
163 Assert.assertTrue(p6_22_34_orig == q); // same reference orig == old (q)
164 Assert.assertTrue(checkOrder(l.getData(), 0, 10, 100));
165 Assert.assertTrue(checkOrder(l.getData(), 100, 6, 4));
166
167 final Dummy p1_2_3 = new Dummy(1, 2, 3); // a new one ..
168 q = l.put(1, p1_2_3); // added test
169 Assert.assertNull(q);
170
171 final Dummy pNull = null;
172 NullPointerException npe = null;
173 try {
174 q = l.put(0, pNull);
175 Assert.assertNull(q);
176 } catch (final NullPointerException _npe) { npe = _npe; }
177 if( l.supportsNullValue() ) {
178 Assert.assertNull(npe);
179 } else {
180 Assert.assertNotNull(npe);
181 }
182 }
183
184 public static void main(final String args[]) throws IOException {
185 final String tstname = TestArrayHashMap01.class.getName();
186 org.junit.runner.JUnitCore.main(tstname);
187 }
188
189}
Dummy(final int i1, final int i2, final int i3)
static void main(final String args[])