RevWalk: Fix RevSort.REVERSE combined with RevSort.TOPO
[egit/fonseca.git] / org.spearce.jgit.test / tst / org / spearce / jgit / lib / PackReverseIndexTest.java
blob52d128243c0c64c739275a9622baeb2761d379e4
1 /*
2 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.lib;
40 import java.io.File;
42 import org.spearce.jgit.errors.CorruptObjectException;
43 import org.spearce.jgit.lib.PackIndex.MutableEntry;
45 public class PackReverseIndexTest extends RepositoryTestCase {
47 private PackIndex idx;
49 private PackReverseIndex reverseIdx;
51 /**
52 * Set up tested class instance, test constructor by the way.
54 public void setUp() throws Exception {
55 super.setUp();
56 // index with both small (< 2^31) and big offsets
57 idx = PackIndex.open(new File(new File("tst"),
58 "pack-huge.idx"));
59 reverseIdx = new PackReverseIndex(idx);
62 /**
63 * Test findObject() for all index entries.
65 public void testFindObject() {
66 for (MutableEntry me : idx)
67 assertEquals(me.toObjectId(), reverseIdx.findObject(me.getOffset()));
70 /**
71 * Test findObject() with illegal argument.
73 public void testFindObjectWrongOffset() {
74 assertNull(reverseIdx.findObject(0));
77 /**
78 * Test findNextOffset() for all index entries.
80 * @throws CorruptObjectException
82 public void testFindNextOffset() throws CorruptObjectException {
83 long offset = findFirstOffset();
84 assertTrue(offset > 0);
85 for (int i = 0; i < idx.getObjectCount(); i++) {
86 long newOffset = reverseIdx.findNextOffset(offset, Long.MAX_VALUE);
87 assertTrue(newOffset > offset);
88 if (i == idx.getObjectCount() - 1)
89 assertEquals(newOffset, Long.MAX_VALUE);
90 else
91 assertEquals(newOffset, idx.findOffset(reverseIdx
92 .findObject(newOffset)));
93 offset = newOffset;
97 /**
98 * Test findNextOffset() with wrong illegal argument as offset.
100 public void testFindNextOffsetWrongOffset() {
101 try {
102 reverseIdx.findNextOffset(0, Long.MAX_VALUE);
103 fail("findNextOffset() should throw exception");
104 } catch (CorruptObjectException x) {
105 // expected
109 private long findFirstOffset() {
110 long min = Long.MAX_VALUE;
111 for (MutableEntry me : idx)
112 min = Math.min(min, me.getOffset());
113 return min;