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 ObjectIdOwnerMapTest {
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 ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
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 ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
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 ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
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 ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
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 int n = 16384;
148 ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
149 m.add(id_1);
150 for (int i = 32; i < n; i++)
151 m.add(new SubId(id(i)));
152 assertEquals(n - 32 + 1, m.size());
153
154 assertSame(id_1, m.get(id_1.copy()));
155 for (int i = 32; i < n; i++)
156 assertTrue(m.contains(id(i)));
157 }
158
159 @Test
160 public void testAddIfAbsentGrowsWithObjects() {
161 int n = 16384;
162 ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
163 m.add(id_1);
164 for (int i = 32; i < n; i++)
165 m.addIfAbsent(new SubId(id(i)));
166 assertEquals(n - 32 + 1, m.size());
167
168 assertSame(id_1, m.get(id_1.copy()));
169 for (int i = 32; i < n; i++)
170 assertTrue(m.contains(id(i)));
171 }
172
173 @Test
174 public void testIterator() {
175 ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
176 m.add(id_1);
177 m.add(id_2);
178 m.add(id_3);
179
180 Iterator<SubId> i = m.iterator();
181 assertTrue(i.hasNext());
182 assertSame(id_1, i.next());
183 assertTrue(i.hasNext());
184 assertSame(id_2, i.next());
185 assertTrue(i.hasNext());
186 assertSame(id_3, i.next());
187
188 assertFalse(i.hasNext());
189 try {
190 i.next();
191 fail("did not fail on next with no next");
192 } catch (NoSuchElementException expected) {
193
194 }
195
196 i = m.iterator();
197 assertSame(id_1, i.next());
198 try {
199 i.remove();
200 fail("did not fail on remove");
201 } catch (UnsupportedOperationException expected) {
202
203 }
204 }
205
206 private AnyObjectId id(int val) {
207 idBuf.setByte(0, val & 0xff);
208 idBuf.setByte(3, (val >>> 8) & 0xff);
209 return idBuf;
210 }
211
212 private static class SubId extends ObjectIdOwnerMap.Entry {
213 SubId(AnyObjectId id) {
214 super(id);
215 }
216 }
217 }