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.

PackReverseIndexTest.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2008, Imran M Yousuf <imyousuf@smartitengineering.com>
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com> and others
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v. 1.0 which is available at
  7. * https://www.eclipse.org/org/documents/edl-v10.php.
  8. *
  9. * SPDX-License-Identifier: BSD-3-Clause
  10. */
  11. package org.eclipse.jgit.internal.storage.file;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertNull;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import org.eclipse.jgit.errors.CorruptObjectException;
  17. import org.eclipse.jgit.internal.storage.file.PackIndex.MutableEntry;
  18. import org.eclipse.jgit.junit.JGitTestUtil;
  19. import org.eclipse.jgit.junit.RepositoryTestCase;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. public class PackReverseIndexTest extends RepositoryTestCase {
  23. private PackIndex idx;
  24. private PackReverseIndex reverseIdx;
  25. /**
  26. * Set up tested class instance, test constructor by the way.
  27. */
  28. @Override
  29. @Before
  30. public void setUp() throws Exception {
  31. super.setUp();
  32. // index with both small (< 2^31) and big offsets
  33. idx = PackIndex.open(JGitTestUtil.getTestResourceFile(
  34. "pack-huge.idx"));
  35. reverseIdx = new PackReverseIndex(idx);
  36. }
  37. /**
  38. * Test findObject() for all index entries.
  39. */
  40. @Test
  41. public void testFindObject() {
  42. for (MutableEntry me : idx)
  43. assertEquals(me.toObjectId(), reverseIdx.findObject(me.getOffset()));
  44. }
  45. /**
  46. * Test findObject() with illegal argument.
  47. */
  48. @Test
  49. public void testFindObjectWrongOffset() {
  50. assertNull(reverseIdx.findObject(0));
  51. }
  52. /**
  53. * Test findNextOffset() for all index entries.
  54. *
  55. * @throws CorruptObjectException
  56. */
  57. @Test
  58. public void testFindNextOffset() throws CorruptObjectException {
  59. long offset = findFirstOffset();
  60. assertTrue(offset > 0);
  61. for (int i = 0; i < idx.getObjectCount(); i++) {
  62. long newOffset = reverseIdx.findNextOffset(offset, Long.MAX_VALUE);
  63. assertTrue(newOffset > offset);
  64. if (i == idx.getObjectCount() - 1)
  65. assertEquals(newOffset, Long.MAX_VALUE);
  66. else
  67. assertEquals(newOffset, idx.findOffset(reverseIdx
  68. .findObject(newOffset)));
  69. offset = newOffset;
  70. }
  71. }
  72. /**
  73. * Test findNextOffset() with wrong illegal argument as offset.
  74. */
  75. @Test
  76. public void testFindNextOffsetWrongOffset() {
  77. try {
  78. reverseIdx.findNextOffset(0, Long.MAX_VALUE);
  79. fail("findNextOffset() should throw exception");
  80. } catch (CorruptObjectException x) {
  81. // expected
  82. }
  83. }
  84. private long findFirstOffset() {
  85. long min = Long.MAX_VALUE;
  86. for (MutableEntry me : idx)
  87. min = Math.min(min, me.getOffset());
  88. return min;
  89. }
  90. }