-
Notifications
You must be signed in to change notification settings - Fork 12
/
query_test.go
154 lines (132 loc) · 2.3 KB
/
query_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
package main
import (
"testing"
)
func postingsList(n int) []int64 {
list := make([]int64, n)
for i := 0; i < n; i++ {
list[i] = int64(i) * 3
}
return list
}
func query(query Query) []int64 {
out := []int64{}
for query.Next() != NO_MORE {
out = append(out, query.GetDocId())
}
return out
}
func eq(t *testing.T, a, b []int64) {
if len(a) != len(b) {
t.Logf("len(a) != len(b) ; len(a) = %d, len(b) = %d", len(a), len(b))
t.FailNow()
}
for i, _ := range a {
if a[i] != b[i] {
t.Log("a[i] != b[i]")
t.FailNow()
}
}
}
func BenchmarkNext1000(b *testing.B) {
x := postingsList(1000)
for n := 0; n < b.N; n++ {
sum := int64(0)
q := NewTerm(x)
for q.Next() != NO_MORE {
sum += q.GetDocId()
}
}
}
func BenchmarkOr1000(b *testing.B) {
x := postingsList(1000)
y := postingsList(1000)
for n := 0; n < b.N; n++ {
sum := int64(0)
q := NewBoolOrQuery(
NewTerm(x),
NewTerm(y),
)
for q.Next() != NO_MORE {
sum += q.GetDocId()
}
}
}
func BenchmarkAnd1000(b *testing.B) {
x := postingsList(1000000)
y := postingsList(1000)
for n := 0; n < b.N; n++ {
sum := int64(0)
q := NewBoolAndQuery(
NewTerm(x),
NewTerm(y),
)
for q.Next() != NO_MORE {
sum += q.GetDocId()
}
}
}
func TestModify(t *testing.T) {
a := postingsList(100)
b := postingsList(1000)
c := postingsList(10000)
d := postingsList(100000)
e := postingsList(1000000)
eq(t, a, query(NewTerm(a)))
eq(t, b, query(NewTerm(b)))
eq(t, c, query(NewTerm(c)))
eq(t, d, query(NewTerm(d)))
eq(t, e, query(NewTerm(e)))
eq(t, b, query(NewBoolOrQuery(
NewTerm(a),
NewTerm(b),
)))
eq(t, c, query(NewBoolOrQuery(
NewTerm(a),
NewTerm(b),
NewTerm(c),
)))
eq(t, e, query(NewBoolOrQuery(
NewTerm(a),
NewTerm(b),
NewTerm(c),
NewTerm(d),
NewTerm(e),
)))
eq(t, a, query(NewBoolAndQuery(
NewTerm(a),
NewTerm(b),
NewTerm(c),
NewTerm(d),
NewTerm(e),
)))
eq(t, a, query(NewBoolAndQuery(
NewTerm(a),
NewTerm(b),
NewTerm(c),
NewTerm(d),
NewTerm(e),
)))
eq(t, b, query(NewBoolAndQuery(
NewBoolOrQuery(
NewTerm(a),
NewTerm(b),
),
NewTerm(b),
NewTerm(c),
NewTerm(d),
NewTerm(e),
)))
eq(t, c, query(NewBoolAndQuery(
NewBoolOrQuery(
NewTerm(a),
NewTerm(b),
NewBoolAndQuery(
NewTerm(c),
NewTerm(d),
),
),
NewTerm(d),
NewTerm(e),
)))
}