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.

ObjectIdOwnerMapTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 ObjectIdOwnerMapTest {
  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. ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
  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. ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
  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. ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
  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. ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
  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. int n = 16384;
  92. ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
  93. m.add(id_1);
  94. for (int i = 32; i < n; i++)
  95. m.add(new SubId(id(i)));
  96. assertEquals(n - 32 + 1, m.size());
  97. assertSame(id_1, m.get(id_1.copy()));
  98. for (int i = 32; i < n; i++)
  99. assertTrue(m.contains(id(i)));
  100. }
  101. @Test
  102. public void testAddIfAbsentGrowsWithObjects() {
  103. int n = 16384;
  104. ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
  105. m.add(id_1);
  106. for (int i = 32; i < n; i++)
  107. m.addIfAbsent(new SubId(id(i)));
  108. assertEquals(n - 32 + 1, m.size());
  109. assertSame(id_1, m.get(id_1.copy()));
  110. for (int i = 32; i < n; i++)
  111. assertTrue(m.contains(id(i)));
  112. }
  113. @Test
  114. public void testIterator() {
  115. ObjectIdOwnerMap<SubId> m = new ObjectIdOwnerMap<>();
  116. m.add(id_1);
  117. m.add(id_2);
  118. m.add(id_3);
  119. Iterator<SubId> i = m.iterator();
  120. assertTrue(i.hasNext());
  121. assertSame(id_1, i.next());
  122. assertTrue(i.hasNext());
  123. assertSame(id_2, i.next());
  124. assertTrue(i.hasNext());
  125. assertSame(id_3, i.next());
  126. assertFalse(i.hasNext());
  127. try {
  128. i.next();
  129. fail("did not fail on next with no next");
  130. } catch (NoSuchElementException expected) {
  131. // OK
  132. }
  133. i = m.iterator();
  134. assertSame(id_1, i.next());
  135. try {
  136. i.remove();
  137. fail("did not fail on remove");
  138. } catch (UnsupportedOperationException expected) {
  139. // OK
  140. }
  141. }
  142. private AnyObjectId id(int val) {
  143. idBuf.setByte(0, val & 0xff);
  144. idBuf.setByte(3, (val >>> 8) & 0xff);
  145. return idBuf;
  146. }
  147. private static class SubId extends ObjectIdOwnerMap.Entry {
  148. SubId(AnyObjectId id) {
  149. super(id);
  150. }
  151. }
  152. }