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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 2011, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.lib;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertSame;
  15. import static org.junit.Assert.assertTrue;
  16. import static org.junit.Assert.fail;
  17. import java.util.Iterator;
  18. import java.util.NoSuchElementException;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. public class ObjectIdSubclassMapTest {
  22. private MutableObjectId idBuf;
  23. private SubId id_1, id_2, id_3, id_a31, id_b31;
  24. @Before
  25. public void init() {
  26. idBuf = new MutableObjectId();
  27. id_1 = new SubId(id(1));
  28. id_2 = new SubId(id(2));
  29. id_3 = new SubId(id(3));
  30. id_a31 = new SubId(id(31));
  31. id_b31 = new SubId(id((1 << 8) + 31));
  32. }
  33. @Test
  34. public void testEmptyMap() {
  35. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  36. assertTrue(m.isEmpty());
  37. assertEquals(0, m.size());
  38. Iterator<SubId> i = m.iterator();
  39. assertNotNull(i);
  40. assertFalse(i.hasNext());
  41. assertFalse(m.contains(id(1)));
  42. }
  43. @Test
  44. public void testAddGetAndContains() {
  45. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  46. m.add(id_1);
  47. m.add(id_2);
  48. m.add(id_3);
  49. m.add(id_a31);
  50. m.add(id_b31);
  51. assertFalse(m.isEmpty());
  52. assertEquals(5, m.size());
  53. assertSame(id_1, m.get(id_1));
  54. assertSame(id_1, m.get(id(1)));
  55. assertSame(id_1, m.get(id(1).copy()));
  56. assertSame(id_2, m.get(id(2).copy()));
  57. assertSame(id_3, m.get(id(3).copy()));
  58. assertSame(id_a31, m.get(id(31).copy()));
  59. assertSame(id_b31, m.get(id_b31.copy()));
  60. assertTrue(m.contains(id_1));
  61. }
  62. @Test
  63. public void testClear() {
  64. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  65. m.add(id_1);
  66. assertSame(id_1, m.get(id_1));
  67. m.clear();
  68. assertTrue(m.isEmpty());
  69. assertEquals(0, m.size());
  70. Iterator<SubId> i = m.iterator();
  71. assertNotNull(i);
  72. assertFalse(i.hasNext());
  73. assertFalse(m.contains(id(1)));
  74. }
  75. @Test
  76. public void testAddIfAbsent() {
  77. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  78. m.add(id_1);
  79. assertSame(id_1, m.addIfAbsent(new SubId(id_1)));
  80. assertEquals(1, m.size());
  81. assertSame(id_2, m.addIfAbsent(id_2));
  82. assertEquals(2, m.size());
  83. assertSame(id_a31, m.addIfAbsent(id_a31));
  84. assertSame(id_b31, m.addIfAbsent(id_b31));
  85. assertSame(id_a31, m.addIfAbsent(new SubId(id_a31)));
  86. assertSame(id_b31, m.addIfAbsent(new SubId(id_b31)));
  87. assertEquals(4, m.size());
  88. }
  89. @Test
  90. public void testAddGrowsWithObjects() {
  91. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  92. m.add(id_1);
  93. for (int i = 32; i < 8000; i++)
  94. m.add(new SubId(id(i)));
  95. assertEquals(8000 - 32 + 1, m.size());
  96. assertSame(id_1, m.get(id_1.copy()));
  97. for (int i = 32; i < 8000; i++)
  98. assertTrue(m.contains(id(i)));
  99. }
  100. @Test
  101. public void testAddIfAbsentGrowsWithObjects() {
  102. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  103. m.add(id_1);
  104. for (int i = 32; i < 8000; i++)
  105. m.addIfAbsent(new SubId(id(i)));
  106. assertEquals(8000 - 32 + 1, m.size());
  107. assertSame(id_1, m.get(id_1.copy()));
  108. for (int i = 32; i < 8000; i++)
  109. assertTrue(m.contains(id(i)));
  110. }
  111. @Test
  112. public void testIterator() {
  113. ObjectIdSubclassMap<SubId> m = new ObjectIdSubclassMap<>();
  114. m.add(id_1);
  115. m.add(id_2);
  116. m.add(id_3);
  117. Iterator<SubId> i = m.iterator();
  118. assertTrue(i.hasNext());
  119. assertSame(id_1, i.next());
  120. assertTrue(i.hasNext());
  121. assertSame(id_2, i.next());
  122. assertTrue(i.hasNext());
  123. assertSame(id_3, i.next());
  124. assertFalse(i.hasNext());
  125. try {
  126. i.next();
  127. fail("did not fail on next with no next");
  128. } catch (NoSuchElementException expected) {
  129. // OK
  130. }
  131. i = m.iterator();
  132. assertSame(id_1, i.next());
  133. try {
  134. i.remove();
  135. fail("did not fail on remove");
  136. } catch (UnsupportedOperationException expected) {
  137. // OK
  138. }
  139. }
  140. private AnyObjectId id(int val) {
  141. // Using bytes 2 and 3 positions our value at the low end of idBuf.w1,
  142. // which is what ObjectIdSubclassMap uses for hashing. This makes
  143. // collisions likely, making collision testing easier.
  144. val <<= 1;
  145. idBuf.setByte(2, (val >>> 8) & 0xff);
  146. idBuf.setByte(3, val & 0xff);
  147. return idBuf;
  148. }
  149. private static class SubId extends ObjectId {
  150. SubId(AnyObjectId id) {
  151. super(id);
  152. }
  153. }
  154. }