29import java.io.IOException;
32import org.jau.util.ArrayHashSet;
33import org.junit.Assert;
34import org.junit.FixMethodOrder;
36import org.junit.runners.MethodSorters;
38import jau.test.junit.util.JunitTracer;
40@FixMethodOrder(MethodSorters.NAME_ASCENDING)
46 public Dummy(
final int i1,
final int i2,
final int i3) {
53 public boolean equals(
final Object o) {
54 if(o instanceof
Dummy) {
56 return this.i1 == d.i1 &&
67 hash = ((hash << 5) - hash) + i2;
68 hash = ((hash << 5) - hash) + i3;
74 return "Dummy["+super.toString()+
": "+i1+
", "+i2+
", "+i3+
"]";
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));
84 Assert.assertEquals(expectedPlusSize, l.size() - oldSize);
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;
99 testArrayHashSetImpl(
true);
103 testArrayHashSetImpl(
false);
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;
116 populate(l, 10, 100, 22, 34, 100);
117 Assert.assertTrue(checkOrder(l, 0, 10, 100));
118 populate(l, 10, 100, 22, 34, 0);
119 Assert.assertTrue(checkOrder(l, 0, 10, 100));
120 populate(l, 6, 5, 22, 34, 4);
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);
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);
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);
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);
144 final int i = l.indexOf(p6_22_34_other);
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);
150 Assert.assertTrue(p6_22_34_orig == q);
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);
158 Assert.assertTrue(p6_22_34_orig == q);
160 Assert.assertTrue(!l.add(q));
161 Assert.assertTrue(!l.add(p6_22_34_other));
163 q = l.getOrAdd(p6_22_34_other);
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);
168 Assert.assertTrue(p6_22_34_orig == q);
169 Assert.assertTrue(checkOrder(l, 0, 10, 100));
170 Assert.assertTrue(checkOrder(l, 100, 6, 4));
172 final Dummy p1_2_3 =
new Dummy(1, 2, 3);
173 q = l.getOrAdd(p1_2_3);
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);
178 Assert.assertTrue(checkOrder(l, 0, 10, 100));
179 Assert.assertTrue(checkOrder(l, 100, 6, 4));
181 final Dummy pNull =
null;
182 NullPointerException npe =
null;
184 q = l.getOrAdd(pNull);
185 Assert.assertNull(q);
186 }
catch (
final NullPointerException _npe) { npe = _npe; }
187 if( l.supportsNullValue() ) {
188 Assert.assertNull(npe);
190 Assert.assertNotNull(npe);
194 Assert.assertTrue(l.remove(pNull));
195 }
catch (
final NullPointerException _npe) { npe = _npe; }
196 if( l.supportsNullValue() ) {
197 Assert.assertNull(npe);
199 Assert.assertNotNull(npe);
203 public static void main(
final String args[])
throws IOException {
205 org.junit.runner.JUnitCore.
main(tstname);
boolean equals(final Object o)
Dummy(final int i1, final int i2, final int i3)
static void main(final String args[])
void test01ArrayHashSetWithNullValue()
void test02ArrayHashSetWithoutNullValue()