aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/packfile/index_test.go
blob: 67147046fb990532d92692123f3431f3f165204b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package packfile

import (
	"strconv"
	"strings"

	"gopkg.in/src-d/go-git.v4/plumbing"

	. "gopkg.in/check.v1"
)

type IndexSuite struct{}

var _ = Suite(&IndexSuite{})

func (s *IndexSuite) TestLookupOffset(c *C) {
	idx := NewIndex(0)

	for o1 := 0; o1 < 10000; o1 += 100 {
		for o2 := 0; o2 < 10000; o2 += 100 {
			if o2 >= o1 {
				e, ok := idx.LookupOffset(uint64(o2))
				c.Assert(ok, Equals, false)
				c.Assert(e, IsNil)
			} else {
				e, ok := idx.LookupOffset(uint64(o2))
				c.Assert(ok, Equals, true)
				c.Assert(e, NotNil)
				c.Assert(e.Hash, Equals, s.toHash(o2))
				c.Assert(e.Offset, Equals, uint64(o2))
			}
		}

		h1 := s.toHash(o1)
		idx.Add(h1, uint64(o1), 0)

		for o2 := 0; o2 < 10000; o2 += 100 {
			if o2 > o1 {
				e, ok := idx.LookupOffset(uint64(o2))
				c.Assert(ok, Equals, false)
				c.Assert(e, IsNil)
			} else {
				e, ok := idx.LookupOffset(uint64(o2))
				c.Assert(ok, Equals, true)
				c.Assert(e, NotNil)
				c.Assert(e.Hash, Equals, s.toHash(o2))
				c.Assert(e.Offset, Equals, uint64(o2))
			}
		}
	}
}

func (s *IndexSuite) TestLookupHash(c *C) {
	idx := NewIndex(0)

	for o1 := 0; o1 < 10000; o1 += 100 {
		for o2 := 0; o2 < 10000; o2 += 100 {
			if o2 >= o1 {
				e, ok := idx.LookupHash(s.toHash(o2))
				c.Assert(ok, Equals, false)
				c.Assert(e, IsNil)
			} else {
				e, ok := idx.LookupHash(s.toHash(o2))
				c.Assert(ok, Equals, true)
				c.Assert(e, NotNil)
				c.Assert(e.Hash, Equals, s.toHash(o2))
				c.Assert(e.Offset, Equals, uint64(o2))
			}
		}

		h1 := s.toHash(o1)
		idx.Add(h1, uint64(o1), 0)

		for o2 := 0; o2 < 10000; o2 += 100 {
			if o2 > o1 {
				e, ok := idx.LookupHash(s.toHash(o2))
				c.Assert(ok, Equals, false)
				c.Assert(e, IsNil)
			} else {
				e, ok := idx.LookupHash(s.toHash(o2))
				c.Assert(ok, Equals, true)
				c.Assert(e, NotNil)
				c.Assert(e.Hash, Equals, s.toHash(o2))
				c.Assert(e.Offset, Equals, uint64(o2))
			}
		}
	}
}

func (s *IndexSuite) TestSize(c *C) {
	idx := NewIndex(0)

	for o1 := 0; o1 < 1000; o1++ {
		c.Assert(idx.Size(), Equals, o1)
		h1 := s.toHash(o1)
		idx.Add(h1, uint64(o1), 0)
	}
}

func (s *IndexSuite) TestIdxFileEmpty(c *C) {
	idx := NewIndex(0)
	idxf := idx.ToIdxFile()
	idx2 := NewIndexFromIdxFile(idxf)
	c.Assert(idx, DeepEquals, idx2)
}

func (s *IndexSuite) TestIdxFile(c *C) {
	idx := NewIndex(0)
	for o1 := 0; o1 < 1000; o1++ {
		h1 := s.toHash(o1)
		idx.Add(h1, uint64(o1), 0)
	}

	idx2 := NewIndexFromIdxFile(idx.ToIdxFile())
	c.Assert(idx, DeepEquals, idx2)
}

func (s *IndexSuite) toHash(i int) plumbing.Hash {
	is := strconv.Itoa(i)
	padding := strings.Repeat("a", 40-len(is))
	return plumbing.NewHash(padding + is)
}