Merge pull request #56 from kortschak/testing
Tabulate value comparison tests
This commit is contained in:
commit
9ca38d1f10
5 changed files with 93 additions and 76 deletions
|
|
@ -15,6 +15,7 @@
|
|||
package iterator
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
|
|
@ -30,78 +31,94 @@ func simpleFixedIterator() *Fixed {
|
|||
return f
|
||||
}
|
||||
|
||||
func checkIteratorContains(ts graph.TripleStore, it graph.Iterator, expected []string, t *testing.T) {
|
||||
var actual []string
|
||||
actual = nil
|
||||
for {
|
||||
val, ok := it.Next()
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
actual = append(actual, ts.NameOf(val))
|
||||
}
|
||||
actualSet := actual[:]
|
||||
for _, a := range expected {
|
||||
found := false
|
||||
for j, b := range actualSet {
|
||||
if a == b {
|
||||
actualSet = append(actualSet[:j], actualSet[j+1:]...)
|
||||
found = true
|
||||
var comparisonTests = []struct {
|
||||
message string
|
||||
operand graph.Value
|
||||
operator Operator
|
||||
expect []string
|
||||
}{
|
||||
{
|
||||
message: "successful int64 less than comparison",
|
||||
operand: int64(3),
|
||||
operator: kCompareLT,
|
||||
expect: []string{"0", "1", "2"},
|
||||
},
|
||||
{
|
||||
message: "empty int64 less than comparison",
|
||||
operand: int64(0),
|
||||
operator: kCompareLT,
|
||||
expect: nil,
|
||||
},
|
||||
{
|
||||
message: "successful int64 greater than comparison",
|
||||
operand: int64(2),
|
||||
operator: kCompareGT,
|
||||
expect: []string{"3", "4"},
|
||||
},
|
||||
{
|
||||
message: "successful int64 greater than or equal comparison",
|
||||
operand: int64(2),
|
||||
operator: kCompareGTE,
|
||||
expect: []string{"2", "3", "4"},
|
||||
},
|
||||
}
|
||||
|
||||
func TestValueComparison(t *testing.T) {
|
||||
for _, test := range comparisonTests {
|
||||
ts := simpleStore
|
||||
vc := NewComparison(simpleFixedIterator(), test.operator, test.operand, ts)
|
||||
|
||||
var got []string
|
||||
for {
|
||||
val, ok := vc.Next()
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
got = append(got, ts.NameOf(val))
|
||||
}
|
||||
if !found {
|
||||
t.Error("Couldn't find", a, "in actual output.\nActual:", actual, "\nExpected: ", expected, "\nRemainder: ", actualSet)
|
||||
return
|
||||
if !reflect.DeepEqual(got, test.expect) {
|
||||
t.Errorf("Failed to show %s, got:%q expect:%q", test.message, got, test.expect)
|
||||
}
|
||||
}
|
||||
if len(actualSet) != 0 {
|
||||
t.Error("Actual output has more than expected.\nActual:", actual, "\nExpected: ", expected, "\nRemainder: ", actualSet)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkingIntValueComparison(t *testing.T) {
|
||||
ts := simpleStore
|
||||
fixed := simpleFixedIterator()
|
||||
vc := NewComparison(fixed, kCompareLT, int64(3), ts)
|
||||
checkIteratorContains(ts, vc, []string{"0", "1", "2"}, t)
|
||||
}
|
||||
|
||||
func TestFailingIntValueComparison(t *testing.T) {
|
||||
ts := simpleStore
|
||||
fixed := simpleFixedIterator()
|
||||
vc := NewComparison(fixed, kCompareLT, int64(0), ts)
|
||||
checkIteratorContains(ts, vc, []string{}, t)
|
||||
}
|
||||
|
||||
func TestWorkingGT(t *testing.T) {
|
||||
ts := simpleStore
|
||||
fixed := simpleFixedIterator()
|
||||
vc := NewComparison(fixed, kCompareGT, int64(2), ts)
|
||||
checkIteratorContains(ts, vc, []string{"3", "4"}, t)
|
||||
}
|
||||
|
||||
func TestWorkingGTE(t *testing.T) {
|
||||
ts := simpleStore
|
||||
fixed := simpleFixedIterator()
|
||||
vc := NewComparison(fixed, kCompareGTE, int64(2), ts)
|
||||
checkIteratorContains(ts, vc, []string{"2", "3", "4"}, t)
|
||||
var vciCheckTests = []struct {
|
||||
message string
|
||||
operator Operator
|
||||
check graph.Value
|
||||
expect bool
|
||||
}{
|
||||
{
|
||||
message: "1 is less than 2",
|
||||
operator: kCompareGTE,
|
||||
check: 1,
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
message: "2 is greater than or equal to 2",
|
||||
operator: kCompareGTE,
|
||||
check: 2,
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
message: "3 is greater than or equal to 2",
|
||||
operator: kCompareGTE,
|
||||
check: 3,
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
message: "5 is absent from iterator",
|
||||
operator: kCompareGTE,
|
||||
check: 5,
|
||||
expect: false,
|
||||
},
|
||||
}
|
||||
|
||||
func TestVCICheck(t *testing.T) {
|
||||
ts := simpleStore
|
||||
fixed := simpleFixedIterator()
|
||||
vc := NewComparison(fixed, kCompareGTE, int64(2), ts)
|
||||
if vc.Check(1) {
|
||||
t.Error("1 is less than 2, should be GTE")
|
||||
}
|
||||
if !vc.Check(2) {
|
||||
t.Error("2 is GTE 2")
|
||||
}
|
||||
if !vc.Check(3) {
|
||||
t.Error("3 is GTE 2")
|
||||
}
|
||||
if vc.Check(5) {
|
||||
t.Error("5 is not in the underlying iterator")
|
||||
for _, test := range vciCheckTests {
|
||||
vc := NewComparison(simpleFixedIterator(), test.operator, int64(2), simpleStore)
|
||||
if vc.Check(test.check) != test.expect {
|
||||
t.Errorf("Failed to show %s", test.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -105,7 +105,7 @@ func (it *AllIterator) Close() {
|
|||
}
|
||||
|
||||
func (it *AllIterator) Size() (int64, bool) {
|
||||
size, err := it.ts.GetApproximateSizeForPrefix(it.prefix)
|
||||
size, err := it.ts.SizeOfPrefix(it.prefix)
|
||||
if err == nil {
|
||||
return size, false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ func (it *Iterator) Next() (graph.Value, bool) {
|
|||
return nil, false
|
||||
}
|
||||
|
||||
func GetPositionFromPrefix(prefix []byte, d graph.Direction, ts *TripleStore) int {
|
||||
func PositionOf(prefix []byte, d graph.Direction, ts *TripleStore) int {
|
||||
if bytes.Equal(prefix, []byte("sp")) {
|
||||
switch d {
|
||||
case graph.Subject:
|
||||
|
|
@ -171,7 +171,7 @@ func (it *Iterator) Check(v graph.Value) bool {
|
|||
if val[0] == 'z' {
|
||||
return false
|
||||
}
|
||||
offset := GetPositionFromPrefix(val[0:2], it.dir, it.ts)
|
||||
offset := PositionOf(val[0:2], it.dir, it.ts)
|
||||
if offset != -1 {
|
||||
if bytes.HasPrefix(val[offset:], it.checkId[1:]) {
|
||||
return true
|
||||
|
|
@ -187,7 +187,7 @@ func (it *Iterator) Check(v graph.Value) bool {
|
|||
}
|
||||
|
||||
func (it *Iterator) Size() (int64, bool) {
|
||||
return it.ts.GetSizeFor(it.checkId), true
|
||||
return it.ts.SizeOf(it.checkId), true
|
||||
}
|
||||
|
||||
func (it *Iterator) DebugString(indent int) string {
|
||||
|
|
|
|||
|
|
@ -176,7 +176,7 @@ func TestLoadDatabase(t *testing.T) {
|
|||
if s := ts.Size(); s != 11 {
|
||||
t.Errorf("Unexpected triplestore size, got:%d expect:11", s)
|
||||
}
|
||||
if s := ts.GetSizeFor(ts.ValueOf("B")); s != 5 {
|
||||
if s := ts.SizeOf(ts.ValueOf("B")); s != 5 {
|
||||
t.Errorf("Unexpected triplestore size, got:%d expect:5", s)
|
||||
}
|
||||
|
||||
|
|
@ -184,7 +184,7 @@ func TestLoadDatabase(t *testing.T) {
|
|||
if s := ts.Size(); s != 10 {
|
||||
t.Errorf("Unexpected triplestore size after RemoveTriple, got:%d expect:10", s)
|
||||
}
|
||||
if s := ts.GetSizeFor(ts.ValueOf("B")); s != 4 {
|
||||
if s := ts.SizeOf(ts.ValueOf("B")); s != 4 {
|
||||
t.Errorf("Unexpected triplestore size, got:%d expect:4", s)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -332,7 +332,7 @@ func (ts *TripleStore) ValueOf(s string) graph.Value {
|
|||
return ts.createValueKeyFor(s)
|
||||
}
|
||||
|
||||
func (ts *TripleStore) getValueData(value_key []byte) ValueData {
|
||||
func (ts *TripleStore) valueData(value_key []byte) ValueData {
|
||||
var out ValueData
|
||||
if glog.V(3) {
|
||||
glog.V(3).Infof("%s %v\n", string(value_key[0]), value_key)
|
||||
|
|
@ -357,14 +357,14 @@ func (ts *TripleStore) NameOf(k graph.Value) string {
|
|||
glog.V(2).Infoln("k was nil")
|
||||
return ""
|
||||
}
|
||||
return ts.getValueData(k.([]byte)).Name
|
||||
return ts.valueData(k.([]byte)).Name
|
||||
}
|
||||
|
||||
func (ts *TripleStore) GetSizeFor(k graph.Value) int64 {
|
||||
func (ts *TripleStore) SizeOf(k graph.Value) int64 {
|
||||
if k == nil {
|
||||
return 0
|
||||
}
|
||||
return int64(ts.getValueData(k.([]byte)).Size)
|
||||
return int64(ts.valueData(k.([]byte)).Size)
|
||||
}
|
||||
|
||||
func (ts *TripleStore) getSize() {
|
||||
|
|
@ -386,7 +386,7 @@ func (ts *TripleStore) getSize() {
|
|||
ts.size = size
|
||||
}
|
||||
|
||||
func (ts *TripleStore) GetApproximateSizeForPrefix(pre []byte) (int64, error) {
|
||||
func (ts *TripleStore) SizeOfPrefix(pre []byte) (int64, error) {
|
||||
limit := make([]byte, len(pre))
|
||||
copy(limit, pre)
|
||||
end := len(limit) - 1
|
||||
|
|
@ -394,7 +394,7 @@ func (ts *TripleStore) GetApproximateSizeForPrefix(pre []byte) (int64, error) {
|
|||
ranges := make([]util.Range, 1)
|
||||
ranges[0].Start = pre
|
||||
ranges[0].Limit = limit
|
||||
sizes, err := ts.db.GetApproximateSizes(ranges)
|
||||
sizes, err := ts.db.SizeOf(ranges)
|
||||
if err == nil {
|
||||
return (int64(sizes[0]) >> 6) + 1, nil
|
||||
}
|
||||
|
|
@ -428,7 +428,7 @@ func (ts *TripleStore) TriplesAllIterator() graph.Iterator {
|
|||
|
||||
func (ts *TripleStore) TripleDirection(val graph.Value, d graph.Direction) graph.Value {
|
||||
v := val.([]uint8)
|
||||
offset := GetPositionFromPrefix(v[0:2], d, ts)
|
||||
offset := PositionOf(v[0:2], d, ts)
|
||||
if offset != -1 {
|
||||
return append([]byte("z"), v[offset:offset+ts.hasher.Size()]...)
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue