added sort support

This commit is contained in:
ston1th 2017-08-30 22:01:19 +02:00
commit 1674abae73
5 changed files with 377 additions and 66 deletions

View file

@ -64,6 +64,11 @@ func TestNewQuery(t *testing.T) {
if err == nil {
t.Error("invalid match")
}
_, err = NewQuery("String:1 sort:Bool:ASC", AND)
if err == nil {
t.Error("invalid sort")
}
}
const (
@ -118,6 +123,61 @@ func TestFilter(t *testing.T) {
}
}
const (
sort1 = "sort:Int:desc"
sort2 = "sort:Int:desc Bool:true"
)
func TestSortFilter(t *testing.T) {
f1 := makefilter(benchData)
q1, err := NewQuery(sort1, AND)
if err != nil {
t.Error(err)
}
q1.Filter(&f1)
if len(f1) != 12 {
t.Error("f1 len is:", len(f1))
}
if f1[0] != benchData[11] {
t.Error("f1[0] != benchData[11]")
}
if f1[11] != benchData[0] {
t.Error("f1[11] != benchData[0]")
}
f2 := makefilter(benchData)
q2, err := NewQuery(sort2, OR)
if err != nil {
t.Error(err)
}
q2.Filter(&f2)
if len(f2) != 6 {
t.Error("f2 len is:", len(f2))
}
if f2[0] != benchData[9] {
t.Error("f2[0] != benchData[9]")
}
if f2[5] != benchData[0] {
t.Error("f2[5] != benchData[0]")
}
f3 := makefilter(benchData)
q3, err := NewQuery(sort2, NOT)
if err != nil {
t.Error(err)
}
q3.Filter(&f3)
if len(f3) != 6 {
t.Error("f3 len is:", len(f3))
}
if f3[0] != benchData[11] {
t.Error("f3[0] != benchData[11]")
}
if f3[5] != benchData[2] {
t.Error("f3[5] != benchData[2]")
}
}
func TestGenericFilter(t *testing.T) {
q1, err := NewQuery(filter1, AND)
if err != nil {
@ -175,9 +235,13 @@ func TestGenericFilter(t *testing.T) {
}
const (
simple = "Bool:true"
comp = "String:*st* Bool:false Struct.Struct.Int:1"
compOr = "Int:47 Bool:false Struct.String:s1*"
simple = "Bool:true"
comp = "String:*st* Bool:false Struct.Struct.Int:1"
compOr = "Int:47 Bool:false Struct.String:s1*"
sortOnly = "sort:Int:desc"
simpleSort = "sort:Int:desc Bool:true"
compSort = "sort:Int:desc String:*st* Bool:false Struct.Struct.Int:1"
compOrSort = "sort:Int:desc Int:47 Bool:false Struct.String:s1*"
)
func BenchmarkNewQuerySimple(b *testing.B) {
@ -201,6 +265,34 @@ func BenchmarkNewQueryComplexOr(b *testing.B) {
}
}
func BenchmarkNewQuerySort(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
NewQuery(sortOnly, OR)
}
}
func BenchmarkNewQuerySortSimple(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
NewQuery(simpleSort, OR)
}
}
func BenchmarkNewQuerySortComplex(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
NewQuery(compSort, AND)
}
}
func BenchmarkNewQuerySortComplexOr(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
NewQuery(compOrSort, OR)
}
}
func BenchmarkFilterSimple(b *testing.B) {
b.ReportAllocs()
q, err := NewQuery(simple, OR)
@ -237,6 +329,54 @@ func BenchmarkFilterComplexOr(b *testing.B) {
}
}
func BenchmarkSort(b *testing.B) {
b.ReportAllocs()
q, err := NewQuery(sortOnly, OR)
if err != nil {
b.Error(err)
}
fd := makefilter(benchData)
for i := 0; i < b.N; i++ {
q.Filter(&fd)
}
}
func BenchmarkSortFilterSimple(b *testing.B) {
b.ReportAllocs()
q, err := NewQuery(simpleSort, OR)
if err != nil {
b.Error(err)
}
fd := makefilter(benchData)
for i := 0; i < b.N; i++ {
q.Filter(&fd)
}
}
func BenchmarkSortFilterComplex(b *testing.B) {
b.ReportAllocs()
q, err := NewQuery(compSort, AND)
if err != nil {
b.Error(err)
}
fd := makefilter(benchData)
for i := 0; i < b.N; i++ {
q.Filter(&fd)
}
}
func BenchmarkSortFilterComplexOr(b *testing.B) {
b.ReportAllocs()
q, err := NewQuery(compOrSort, OR)
if err != nil {
b.Error(err)
}
fd := makefilter(benchData)
for i := 0; i < b.N; i++ {
q.Filter(&fd)
}
}
func BenchmarkGenericFilterSimple(b *testing.B) {
b.ReportAllocs()
q, err := NewQuery(simple, OR)