Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Unified Diff: impl/memory/gkvlite_iter_test.go

Issue 1302813003: impl/memory: Implement Queries (Closed) Base URL: https://github.com/luci/gae.git@add_multi_iterator
Patch Set: Baby's first query! Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: impl/memory/gkvlite_iter_test.go
diff --git a/impl/memory/gkvlite_iter_test.go b/impl/memory/gkvlite_iter_test.go
index cb22c6d8c32bdb8950b37cdb6fdc663383cc603b..bab9a4718bd1a06e57cee9b6867f6125070125d6 100644
--- a/impl/memory/gkvlite_iter_test.go
+++ b/impl/memory/gkvlite_iter_test.go
@@ -6,7 +6,6 @@ package memory
import (
"bytes"
- "fmt"
"testing"
"github.com/luci/gkvlite"
@@ -17,17 +16,15 @@ import (
func mkNum(n int64) []byte {
buf := &bytes.Buffer{}
_, err := cmpbin.WriteInt(buf, n)
- if err != nil {
- panic(fmt.Errorf("your RAM is busted: %s", err))
- }
+ memoryCorruption(err)
+
return buf.Bytes()
}
func readNum(data []byte) int64 {
ret, _, err := cmpbin.ReadInt(bytes.NewBuffer(data))
- if err != nil {
- panic(fmt.Errorf("your RAM is (probably) busted: %s", err))
- }
+ memoryCorruption(err)
+
return ret
}
@@ -43,47 +40,49 @@ func TestIterator(t *testing.T) {
prev = data
}
- get := func(c C, t *iterator) int64 {
- ret := int64(0)
+ get := func(c C, t *iterator) interface{} {
+ ret := interface{}(nil)
t.next(nil, func(i *gkvlite.Item) {
- c.So(i, ShouldNotBeNil)
- ret = readNum(i.Key)
+ if i != nil {
+ ret = readNum(i.Key)
+ }
})
return ret
}
- skipGet := func(c C, t *iterator, skipTo int64) int64 {
- ret := int64(0)
+ skipGet := func(c C, t *iterator, skipTo int64) interface{} {
+ ret := interface{}(nil)
t.next(mkNum(skipTo), func(i *gkvlite.Item) {
- c.So(i, ShouldNotBeNil)
- ret = readNum(i.Key)
+ if i != nil {
+ ret = readNum(i.Key)
+ }
})
return ret
}
Convey("Test iterator", t, func() {
Convey("start at nil", func(ctx C) {
- t := newIterator(c, nil, nil)
+ t := newIterator(&iterDefinition{c: c})
defer t.stop()
So(get(ctx, t), ShouldEqual, 5)
So(get(ctx, t), ShouldEqual, 6)
So(get(ctx, t), ShouldEqual, 7)
- Convey("And can skip", func() {
+ Convey("And can skip", func(ctx C) {
So(skipGet(ctx, t, 10), ShouldEqual, 10)
So(get(ctx, t), ShouldEqual, 11)
- Convey("But not forever", func(c C) {
+ Convey("But not forever", func(ctx C) {
t.next(mkNum(200), func(i *gkvlite.Item) {
- c.So(i, ShouldBeNil)
+ ctx.So(i, ShouldBeNil)
})
t.next(nil, func(i *gkvlite.Item) {
- c.So(i, ShouldBeNil)
+ ctx.So(i, ShouldBeNil)
})
})
})
- Convey("Can iterate explicitly", func() {
+ Convey("Can iterate explicitly", func(ctx C) {
So(skipGet(ctx, t, 7), ShouldEqual, 8)
So(skipGet(ctx, t, 8), ShouldEqual, 9)
@@ -91,27 +90,37 @@ func TestIterator(t *testing.T) {
So(skipGet(ctx, t, 10), ShouldEqual, 10)
})
- Convey("Can stop", func(c C) {
+ Convey("Can stop", func(ctx C) {
t.stop()
t.next(mkNum(200), func(i *gkvlite.Item) {
- c.So(i, ShouldBeNil)
+ ctx.So(i, ShouldBeNil)
})
t.next(nil, func(i *gkvlite.Item) {
- c.So(i, ShouldBeNil)
+ ctx.So(i, ShouldBeNil)
})
So(t.stop, ShouldNotPanic)
})
- Convey("Going backwards is ignored", func(c C) {
+ Convey("Going backwards is ignored", func(ctx C) {
So(skipGet(ctx, t, 3), ShouldEqual, 8)
So(get(ctx, t), ShouldEqual, 9)
So(skipGet(ctx, t, 20), ShouldEqual, 20)
So(get(ctx, t), ShouldEqual, 21)
})
+
+ Convey("will stop at the end of the list", func(ctx C) {
+ So(skipGet(ctx, t, 95), ShouldEqual, 95)
+ So(get(ctx, t), ShouldEqual, 96)
+ So(get(ctx, t), ShouldEqual, 97)
+ So(get(ctx, t), ShouldEqual, 98)
+ So(get(ctx, t), ShouldEqual, 99)
+ So(get(ctx, t), ShouldBeNil)
+ So(get(ctx, t), ShouldBeNil)
+ })
})
Convey("can have caps on both sides", func(ctx C) {
- t := newIterator(c, mkNum(20), mkNum(25))
+ t := newIterator(&iterDefinition{c: c, start: mkNum(20), end: mkNum(25)})
So(get(ctx, t), ShouldEqual, 20)
So(get(ctx, t), ShouldEqual, 21)
So(get(ctx, t), ShouldEqual, 22)
@@ -123,7 +132,7 @@ func TestIterator(t *testing.T) {
})
Convey("can skip over starting cap", func(ctx C) {
- t := newIterator(c, mkNum(20), mkNum(25))
+ t := newIterator(&iterDefinition{c: c, start: mkNum(20), end: mkNum(25)})
So(skipGet(ctx, t, 22), ShouldEqual, 22)
So(get(ctx, t), ShouldEqual, 23)
So(get(ctx, t), ShouldEqual, 24)
@@ -152,8 +161,8 @@ func TestMultiIteratorSimple(t *testing.T) {
valBytes := make([][]byte, len(vals))
for i, nms := range vals {
numbs := make([][]byte, len(nms))
- for i, n := range nms {
- numbs[i] = mkNum(n)
+ for j, n := range nms {
+ numbs[j] = mkNum(n)
}
valBytes[i] = bjoin(numbs...)
}
@@ -194,8 +203,8 @@ func TestMultiIteratorSimple(t *testing.T) {
// starting at (1, 2) (i.e. >= 2)
// ending at (1, 4) (i.e. < 7)
defs := []*iterDefinition{
- {c, mkNum(1), mkNum(2), mkNum(7)},
- {c, mkNum(1), mkNum(2), mkNum(7)},
+ {c: c, prefix: mkNum(1), prefixLen: len(mkNum(1)), start: mkNum(2), end: mkNum(7)},
+ {c: c, prefix: mkNum(1), prefixLen: len(mkNum(1)), start: mkNum(2), end: mkNum(7)},
}
i := 1
@@ -211,8 +220,8 @@ func TestMultiIteratorSimple(t *testing.T) {
Convey("can make empty iteration", func() {
// get just the (20, *) (doesn't exist)
defs := []*iterDefinition{
- {c, mkNum(20), nil, nil},
- {c, mkNum(20), nil, nil},
+ {c: c, prefix: mkNum(20)},
+ {c: c, prefix: mkNum(20)},
}
i := 0
@@ -227,9 +236,9 @@ func TestMultiIteratorSimple(t *testing.T) {
// 'other' must start with 20, 'vals' must start with 1
// no range constraints
defs := []*iterDefinition{
- {c2, mkNum(20), nil, nil},
- {c, mkNum(1), nil, nil},
- {c, mkNum(1), nil, nil},
+ {c: c2, prefix: mkNum(20)},
+ {c: c, prefix: mkNum(1)},
+ {c: c, prefix: mkNum(1)},
}
expect := []int64{2, 4}
@@ -243,8 +252,8 @@ func TestMultiIteratorSimple(t *testing.T) {
Convey("Can stop early", func() {
defs := []*iterDefinition{
- {c, mkNum(1), nil, nil},
- {c, mkNum(1), nil, nil},
+ {c: c, prefix: mkNum(1), prefixLen: len(mkNum(1))},
+ {c: c, prefix: mkNum(1), prefixLen: len(mkNum(1))},
}
i := 0

Powered by Google App Engine
This is Rietveld 408576698