1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.lib;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertFalse;
48 import static org.junit.Assert.assertNotNull;
49 import static org.junit.Assert.assertSame;
50 import static org.junit.Assert.assertTrue;
51 import static org.junit.Assert.fail;
52
53 import java.util.Iterator;
54 import java.util.NoSuchElementException;
55
56 import org.junit.Before;
57 import org.junit.Test;
58
59 public class ObjectIdSubclassMapTest {
60 private MutableObjectId idBuf;
61
62 private SubId id_1, id_2, id_3, id_a31, id_b31;
63
64 @Before
65 public void init() {
66 idBuf = new MutableObjectId();
67 id_1 = new SubId(id(1));
68 id_2 = new SubId(id(2));
69 id_3 = new SubId(id(3));
70 id_a31 = new SubId(id(31));
71 id_b31 = new SubId(id((1 << 8) + 31));
72 }
73
74 @Test
75 public void testEmptyMap() {
76 ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
77 assertTrue(m.isEmpty());
78 assertEquals(0, m.size());
79
80 Iterator<SubId> i = m.iterator();
81 assertNotNull(i);
82 assertFalse(i.hasNext());
83
84 assertFalse(m.contains(id(1)));
85 }
86
87 @Test
88 public void testAddGetAndContains() {
89 ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
90 m.add(id_1);
91 m.add(id_2);
92 m.add(id_3);
93 m.add(id_a31);
94 m.add(id_b31);
95 assertFalse(m.isEmpty());
96 assertEquals(5, m.size());
97
98 assertSame(id_1, m.get(id_1));
99 assertSame(id_1, m.get(id(1)));
100 assertSame(id_1, m.get(id(1).copy()));
101 assertSame(id_2, m.get(id(2).copy()));
102 assertSame(id_3, m.get(id(3).copy()));
103 assertSame(id_a31, m.get(id(31).copy()));
104 assertSame(id_b31, m.get(id_b31.copy()));
105
106 assertTrue(m.contains(id_1));
107 }
108
109 @Test
110 public void testClear() {
111 ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
112
113 m.add(id_1);
114 assertSame(id_1, m.get(id_1));
115
116 m.clear();
117 assertTrue(m.isEmpty());
118 assertEquals(0, m.size());
119
120 Iterator<SubId> i = m.iterator();
121 assertNotNull(i);
122 assertFalse(i.hasNext());
123
124 assertFalse(m.contains(id(1)));
125 }
126
127 @Test
128 public void testAddIfAbsent() {
129 ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
130 m.add(id_1);
131
132 assertSame(id_1, m.addIfAbsent(new SubId(id_1)));
133 assertEquals(1, m.size());
134
135 assertSame(id_2, m.addIfAbsent(id_2));
136 assertEquals(2, m.size());
137 assertSame(id_a31, m.addIfAbsent(id_a31));
138 assertSame(id_b31, m.addIfAbsent(id_b31));
139
140 assertSame(id_a31, m.addIfAbsent(new SubId(id_a31)));
141 assertSame(id_b31, m.addIfAbsent(new SubId(id_b31)));
142 assertEquals(4, m.size());
143 }
144
145 @Test
146 public void testAddGrowsWithObjects() {
147 ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
148 m.add(id_1);
149 for (int i = 32; i < 8000; i++)
150 m.add(new SubId(id(i)));
151 assertEquals(8000 - 32 + 1, m.size());
152
153 assertSame(id_1, m.get(id_1.copy()));
154 for (int i = 32; i < 8000; i++)
155 assertTrue(m.contains(id(i)));
156 }
157
158 @Test
159 public void testAddIfAbsentGrowsWithObjects() {
160 ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
161 m.add(id_1);
162 for (int i = 32; i < 8000; i++)
163 m.addIfAbsent(new SubId(id(i)));
164 assertEquals(8000 - 32 + 1, m.size());
165
166 assertSame(id_1, m.get(id_1.copy()));
167 for (int i = 32; i < 8000; i++)
168 assertTrue(m.contains(id(i)));
169 }
170
171 @Test
172 public void testIterator() {
173 ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
174 m.add(id_1);
175 m.add(id_2);
176 m.add(id_3);
177
178 Iterator<SubId> i = m.iterator();
179 assertTrue(i.hasNext());
180 assertSame(id_1, i.next());
181 assertTrue(i.hasNext());
182 assertSame(id_2, i.next());
183 assertTrue(i.hasNext());
184 assertSame(id_3, i.next());
185
186 assertFalse(i.hasNext());
187 try {
188 i.next();
189 fail("did not fail on next with no next");
190 } catch (NoSuchElementException expected) {
191
192 }
193
194 i = m.iterator();
195 assertSame(id_1, i.next());
196 try {
197 i.remove();
198 fail("did not fail on remove");
199 } catch (UnsupportedOperationException expected) {
200
201 }
202 }
203
204 private AnyObjectId id(int val) {
205
206
207
208
209 val <<= 1;
210
211 idBuf.setByte(2, (val >>> 8) & 0xff);
212 idBuf.setByte(3, val & 0xff);
213 return idBuf;
214 }
215
216 private static class SubId extends ObjectId {
217 SubId(AnyObjectId id) {
218 super(id);
219 }
220 }
221 }