You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectIdSubclassMapTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. package org.eclipse.jgit.lib;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertSame;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.junit.Assert.fail;
  50. import java.util.Iterator;
  51. import java.util.NoSuchElementException;
  52. import org.junit.Before;
  53. import org.junit.Test;
  54. public class ObjectIdSubclassMapTest {
  55. private MutableObjectId idBuf;
  56. private SubId id_1, id_2, id_3, id_a31, id_b31;
  57. @Before
  58. public void init() {
  59. idBuf = new MutableObjectId();
  60. id_1 = new SubId(id(1));
  61. id_2 = new SubId(id(2));
  62. id_3 = new SubId(id(3));
  63. id_a31 = new SubId(id(31));
  64. id_b31 = new SubId(id((1 << 8) + 31));
  65. }
  66. @Test
  67. public void testEmptyMap() {
  68. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  69. assertTrue(m.isEmpty());
  70. assertEquals(0, m.size());
  71. Iterator<SubId> i = m.iterator();
  72. assertNotNull(i);
  73. assertFalse(i.hasNext());
  74. assertFalse(m.contains(id(1)));
  75. }
  76. @Test
  77. public void testAddGetAndContains() {
  78. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  79. m.add(id_1);
  80. m.add(id_2);
  81. m.add(id_3);
  82. m.add(id_a31);
  83. m.add(id_b31);
  84. assertFalse(m.isEmpty());
  85. assertEquals(5, m.size());
  86. assertSame(id_1, m.get(id_1));
  87. assertSame(id_1, m.get(id(1)));
  88. assertSame(id_1, m.get(id(1).copy()));
  89. assertSame(id_2, m.get(id(2).copy()));
  90. assertSame(id_3, m.get(id(3).copy()));
  91. assertSame(id_a31, m.get(id(31).copy()));
  92. assertSame(id_b31, m.get(id_b31.copy()));
  93. assertTrue(m.contains(id_1));
  94. }
  95. @Test
  96. public void testClear() {
  97. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  98. m.add(id_1);
  99. assertSame(id_1, m.get(id_1));
  100. m.clear();
  101. assertTrue(m.isEmpty());
  102. assertEquals(0, m.size());
  103. Iterator<SubId> i = m.iterator();
  104. assertNotNull(i);
  105. assertFalse(i.hasNext());
  106. assertFalse(m.contains(id(1)));
  107. }
  108. @Test
  109. public void testAddIfAbsent() {
  110. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  111. m.add(id_1);
  112. assertSame(id_1, m.addIfAbsent(new SubId(id_1)));
  113. assertEquals(1, m.size());
  114. assertSame(id_2, m.addIfAbsent(id_2));
  115. assertEquals(2, m.size());
  116. assertSame(id_a31, m.addIfAbsent(id_a31));
  117. assertSame(id_b31, m.addIfAbsent(id_b31));
  118. assertSame(id_a31, m.addIfAbsent(new SubId(id_a31)));
  119. assertSame(id_b31, m.addIfAbsent(new SubId(id_b31)));
  120. assertEquals(4, m.size());
  121. }
  122. @Test
  123. public void testAddGrowsWithObjects() {
  124. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  125. m.add(id_1);
  126. for (int i = 32; i < 8000; i++)
  127. m.add(new SubId(id(i)));
  128. assertEquals(8000 - 32 + 1, m.size());
  129. assertSame(id_1, m.get(id_1.copy()));
  130. for (int i = 32; i < 8000; i++)
  131. assertTrue(m.contains(id(i)));
  132. }
  133. @Test
  134. public void testAddIfAbsentGrowsWithObjects() {
  135. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  136. m.add(id_1);
  137. for (int i = 32; i < 8000; i++)
  138. m.addIfAbsent(new SubId(id(i)));
  139. assertEquals(8000 - 32 + 1, m.size());
  140. assertSame(id_1, m.get(id_1.copy()));
  141. for (int i = 32; i < 8000; i++)
  142. assertTrue(m.contains(id(i)));
  143. }
  144. @Test
  145. public void testIterator() {
  146. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  147. m.add(id_1);
  148. m.add(id_2);
  149. m.add(id_3);
  150. Iterator<SubId> i = m.iterator();
  151. assertTrue(i.hasNext());
  152. assertSame(id_1, i.next());
  153. assertTrue(i.hasNext());
  154. assertSame(id_2, i.next());
  155. assertTrue(i.hasNext());
  156. assertSame(id_3, i.next());
  157. assertFalse(i.hasNext());
  158. try {
  159. i.next();
  160. fail("did not fail on next with no next");
  161. } catch (NoSuchElementException expected) {
  162. // OK
  163. }
  164. i = m.iterator();
  165. assertSame(id_1, i.next());
  166. try {
  167. i.remove();
  168. fail("did not fail on remove");
  169. } catch (UnsupportedOperationException expected) {
  170. // OK
  171. }
  172. }
  173. private AnyObjectId id(int val) {
  174. // Using bytes 2 and 3 positions our value at the low end of idBuf.w1,
  175. // which is what ObjectIdSubclassMap uses for hashing. This makes
  176. // collisions likely, making collision testing easier.
  177. val <<= 1;
  178. idBuf.setByte(2, (val >>> 8) & 0xff);
  179. idBuf.setByte(3, val & 0xff);
  180. return idBuf;
  181. }
  182. private static class SubId extends ObjectId {
  183. SubId(AnyObjectId id) {
  184. super(id);
  185. }
  186. }
  187. }