]> Sergey Matveev's repositories - btrtrc.git/blob - segments/segments_test.go
go1.19 compatibility
[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         assertLocate(t, newLocater,
61                 []Length{1652, 1514, 1554, 1618, 1546, 129241752, 1537}, // 128737588
62                 Extent{0, 16384},
63                 0,
64                 []Extent{
65                         {0, 1652},
66                         {0, 1514},
67                         {0, 1554},
68                         {0, 1618},
69                         {0, 1546},
70                         {0, 8500},
71                 })
72         assertLocate(t, newLocater,
73                 []Length{1652, 1514, 1554, 1618, 1546, 129241752, 1537, 1536, 1551}, // 128737588
74                 Extent{129236992, 16384},
75                 5,
76                 []Extent{
77                         {129229108, 12644},
78                         {0, 1537},
79                         {0, 1536},
80                         {0, 667},
81                 })
82 }
83
84 func TestScan(t *testing.T) {
85         testLocater(t, LocaterFromLengthIter)
86 }
87
88 func TestIndex(t *testing.T) {
89         testLocater(t, func(li LengthIter) Locater {
90                 return NewIndex(li).Locate
91         })
92 }