]> Sergey Matveev's repositories - btrtrc.git/blob - segments/segments_test.go
Abstract out segments mapping and use it in mmap storage
[btrtrc.git] / segments / segments_test.go
1 package segments
2
3 import (
4         "testing"
5
6         "github.com/stretchr/testify/assert"
7 )
8
9 func LengthIterFromSlice(ls []Length) LengthIter {
10         return func() (Length, bool) {
11                 switch len(ls) {
12                 case 0:
13                         return -1, false
14                 default:
15                         l := ls[0]
16                         ls = ls[1:]
17                         return l, true
18                 }
19         }
20 }
21
22 type ScanCallbackValue struct {
23         Index int
24         Extent
25 }
26
27 type collectExtents []ScanCallbackValue
28
29 func (me *collectExtents) scanCallback(i int, e Extent) bool {
30         *me = append(*me, ScanCallbackValue{
31                 Index:  i,
32                 Extent: e,
33         })
34         return true
35 }
36
37 type newLocater func(LengthIter) Locater
38
39 func assertLocate(t *testing.T, nl newLocater, ls []Length, needle Extent, firstExpectedIndex int, expectedExtents []Extent) {
40         var actual collectExtents
41         var expected collectExtents
42         for i, e := range expectedExtents {
43                 expected.scanCallback(firstExpectedIndex+i, e)
44         }
45         nl(LengthIterFromSlice(ls))(needle, actual.scanCallback)
46         assert.EqualValues(t, expected, actual)
47 }
48
49 func testLocater(t *testing.T, newLocater newLocater) {
50         assertLocate(t, newLocater,
51                 []Length{1, 0, 2, 0, 3},
52                 Extent{2, 2},
53                 2,
54                 []Extent{{1, 1}, {0, 0}, {0, 1}})
55         assertLocate(t, newLocater,
56                 []Length{1, 0, 2, 0, 3},
57                 Extent{6, 2},
58                 2,
59                 []Extent{})
60 }
61
62 func TestScan(t *testing.T) {
63         testLocater(t, LocaterFromLengthIter)
64 }
65
66 func TestIndex(t *testing.T) {
67         testLocater(t, func(li LengthIter) Locater {
68                 return NewIndex(li).Locate
69         })
70 }