initial commit
This commit is contained in:
commit
e309b478f5
5 changed files with 453 additions and 0 deletions
139
filter_test.go
Normal file
139
filter_test.go
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
// Copyright (C) 2017 Marius Schellenberger
|
||||
|
||||
package filter
|
||||
|
||||
import "testing"
|
||||
|
||||
type Example struct {
|
||||
String string
|
||||
Int int
|
||||
Bool bool
|
||||
Struct Struct1
|
||||
}
|
||||
|
||||
type Struct1 struct {
|
||||
String string
|
||||
Struct Struct2
|
||||
}
|
||||
|
||||
type Struct2 struct {
|
||||
Int int
|
||||
}
|
||||
|
||||
type filter []Example
|
||||
|
||||
func makefilter(v filter) (f filter) {
|
||||
f = make(filter, len(v))
|
||||
copy(f, v)
|
||||
return
|
||||
}
|
||||
|
||||
func (v filter) Len() int { return len(v) }
|
||||
func (v filter) Index(i int) interface{} { return v[i] }
|
||||
func (v *filter) Remove(i, j int) { *v = append((*v)[:i], (*v)[j:]...) }
|
||||
|
||||
var (
|
||||
data = filter{
|
||||
{"str1", 42, true, Struct1{"s1", Struct2{1}}},
|
||||
{"2str", 42, true, Struct1{"s2", Struct2{2}}},
|
||||
{"str3", 42, false, Struct1{"s3", Struct2{3}}},
|
||||
}
|
||||
benchData = filter{
|
||||
{"str1", 41, true, Struct1{"s1", Struct2{1}}},
|
||||
{"2str", 42, true, Struct1{"s2", Struct2{1}}},
|
||||
{"str3", 43, false, Struct1{"s3", Struct2{1}}},
|
||||
{"str4", 44, false, Struct1{"s4", Struct2{1}}},
|
||||
{"str5", 45, false, Struct1{"s5", Struct2{1}}},
|
||||
{"str6", 46, false, Struct1{"s6", Struct2{1}}},
|
||||
{"str7", 47, true, Struct1{"s7", Struct2{1}}},
|
||||
{"str8", 48, true, Struct1{"s8", Struct2{1}}},
|
||||
{"str9", 49, true, Struct1{"s9", Struct2{1}}},
|
||||
{"str10", 50, true, Struct1{"s10", Struct2{1}}},
|
||||
{"str11", 51, false, Struct1{"s11", Struct2{1}}},
|
||||
{"str12", 52, false, Struct1{"s12", Struct2{1}}},
|
||||
}
|
||||
)
|
||||
|
||||
func TestFilter(t *testing.T) {
|
||||
f1 := makefilter(data)
|
||||
NewQuery("String:*st* Bool:true").Filter(&f1)
|
||||
if len(f1) != 2 {
|
||||
t.Error("f1 len is:", len(f1))
|
||||
}
|
||||
if f1[0] != data[0] {
|
||||
t.Error("f1[0] != data[0]")
|
||||
}
|
||||
if f1[1] != data[1] {
|
||||
t.Error("f1[1] != data[1]")
|
||||
}
|
||||
f2 := makefilter(data)
|
||||
NewQuery("Struct.Struct.Int:3").Filter(&f2)
|
||||
if len(f2) != 1 {
|
||||
t.Error("f2 len is:", len(f2))
|
||||
}
|
||||
if f2[0] != data[2] {
|
||||
t.Error("f1[0] != data[2]")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenericFilter(t *testing.T) {
|
||||
i1 := NewQuery("String:*st* Bool:true").GenericFilter(data)
|
||||
if i1 == nil {
|
||||
t.Error("i1 is nil")
|
||||
}
|
||||
f1 := i1.(filter)
|
||||
if len(f1) != 2 {
|
||||
t.Error("f1 len is:", len(f1))
|
||||
}
|
||||
if f1[0] != data[0] {
|
||||
t.Error("f1[0] != data[0]")
|
||||
}
|
||||
if f1[1] != data[1] {
|
||||
t.Error("f1[1] != data[1]")
|
||||
}
|
||||
i2 := NewQuery("Struct.Struct.Int:3").GenericFilter(data)
|
||||
if i2 == nil {
|
||||
t.Error("i2 is nil")
|
||||
}
|
||||
f2 := i2.(filter)
|
||||
if len(f2) != 1 {
|
||||
t.Error("f2 len is:", len(f2))
|
||||
}
|
||||
if f2[0] != data[2] {
|
||||
t.Error("f1[0] != data[2]")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkFilterSimple(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
q := NewQuery("Bool:true")
|
||||
fd := makefilter(benchData)
|
||||
for i := 0; i < b.N; i++ {
|
||||
q.Filter(&fd)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGenericFilterSimple(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
q := NewQuery("Bool:true")
|
||||
for i := 0; i < b.N; i++ {
|
||||
q.GenericFilter(benchData)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkFilterComplex(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
q := NewQuery("String:*st* Bool:false Struct.Struct.Int:1")
|
||||
fd := makefilter(benchData)
|
||||
for i := 0; i < b.N; i++ {
|
||||
q.Filter(&fd)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGenericFilterComplex(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
q := NewQuery("String:*st* Bool:false Struct.Struct.Int:1")
|
||||
for i := 0; i < b.N; i++ {
|
||||
q.GenericFilter(benchData)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue