View Javadoc
1   /*
2    * Copyright (C) 2011, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 			// OK
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 			// OK
201 		}
202 	}
203 
204 	private AnyObjectId id(int val) {
205 		// Using bytes 2 and 3 positions our value at the low end of idBuf.w1,
206 		// which is what ObjectIdSubclassMap uses for hashing. This makes
207 		// collisions likely, making collision testing easier.
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 }