forked from google/zoekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bits_test.go
198 lines (174 loc) · 4.29 KB
/
bits_test.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zoekt
import (
"encoding/binary"
"log"
"math/rand"
"reflect"
"sort"
"testing"
"testing/quick"
"github.com/google/go-cmp/cmp"
)
var _ = log.Println
func TestNgram(t *testing.T) {
in := "abc"
n := stringToNGram(in)
if n.String() != "abc" {
t.Errorf("got %q, want %q", n, "abc")
}
f := func(b ngramRunes) bool {
n := runesToNGram(b)
got := ngramRunes(ngramToRunes(n))
if !reflect.DeepEqual(b, got) {
t.Log(cmp.Diff(b, got))
return false
}
return true
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
type ngramRunes [ngramSize]rune
func (ngramRunes) Generate(rand *rand.Rand, size int) reflect.Value {
// Same implementation used by testing/quick to generate strings. But we
// force it to ngramSize runes.
var b ngramRunes
for i := range b {
b[i] = rune(rand.Intn(0x10ffff))
}
return reflect.ValueOf(b)
}
func TestDocSection(t *testing.T) {
in := []DocumentSection{{1, 2}, {3, 4}}
serialized := marshalDocSections(in)
roundtrip := unmarshalDocSections(serialized, nil)
if !reflect.DeepEqual(in, roundtrip) {
t.Errorf("got %v, want %v", roundtrip, in)
}
}
func TestGenerateCaseNgrams(t *testing.T) {
ng := stringToNGram("aB1")
gotNG := generateCaseNgrams(ng)
got := map[string]bool{}
for _, n := range gotNG {
got[string(ngramToBytes(n))] = true
}
want := map[string]bool{
"aB1": true,
"AB1": true,
"ab1": true,
"Ab1": true,
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}
func TestNextFileIndex(t *testing.T) {
for _, tc := range []struct {
off, curFile uint32
ends []uint32
want uint32
}{
{maxUInt32, 0, []uint32{34}, 1},
{9, 0, []uint32{34}, 0},
{450, 0, []uint32{100, 200, 300, 400, 500, 600}, 4},
} {
got := nextFileIndex(tc.off, tc.curFile, tc.ends)
if got != tc.want {
t.Errorf("%v: got %d, want %d", tc, got, tc.want)
}
}
}
func TestSizedDeltas(t *testing.T) {
encode := func(nums []uint32) []byte {
return toSizedDeltas(nums)
}
decode := func(data []byte) []uint32 {
if len(data) == 0 {
return nil
}
return fromSizedDeltas(data, nil)
}
testIncreasingIntCoder(t, encode, decode)
}
func TestFromDeltas(t *testing.T) {
decode := func(data []byte) []uint32 {
if len(data) == 0 {
return nil
}
return fromDeltas(data, nil)
}
testIncreasingIntCoder(t, toDeltas, decode)
}
func TestCompressedPostingIterator(t *testing.T) {
decode := func(data []byte) []uint32 {
if len(data) == 0 {
return nil
}
var nums []uint32
i := newCompressedPostingIterator(data, stringToNGram("abc"))
for i.first() != maxUInt32 {
nums = append(nums, i.first())
i.next(i.first())
}
return nums
}
testIncreasingIntCoder(t, toDeltas, decode)
}
func toDeltas(offsets []uint32) []byte {
var enc [8]byte
deltas := make([]byte, 0, len(offsets)*2)
var last uint32
for _, p := range offsets {
delta := p - last
last = p
m := binary.PutUvarint(enc[:], uint64(delta))
deltas = append(deltas, enc[:m]...)
}
return deltas
}
func testIncreasingIntCoder(t *testing.T, encode func([]uint32) []byte, decode func([]byte) []uint32) {
f := func(nums []uint32) bool {
nums = sortedUnique(nums)
b := encode(nums)
got := decode(b)
if len(nums) == len(got) && len(nums) == 0 {
return true
}
if !reflect.DeepEqual(got, nums) {
t.Log(cmp.Diff(nums, got))
return false
}
return true
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func sortedUnique(nums []uint32) []uint32 {
if len(nums) == 0 {
return nums
}
sort.Slice(nums, func(i, j int) bool { return nums[i] < nums[j] })
filtered := nums[:1]
for _, n := range nums[1:] {
if filtered[len(filtered)-1] != n {
filtered = append(filtered, n)
}
}
return filtered
}