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