Tabulate value comparison tests

This commit is contained in:
kortschak 2014-07-07 12:01:16 +09:30
parent c88a24467e
commit 5bc1c438ef

View file

@ -15,6 +15,7 @@
package iterator package iterator
import ( import (
"reflect"
"testing" "testing"
"github.com/google/cayley/graph" "github.com/google/cayley/graph"
@ -30,78 +31,94 @@ func simpleFixedIterator() *Fixed {
return f return f
} }
func checkIteratorContains(ts graph.TripleStore, it graph.Iterator, expected []string, t *testing.T) { var comparisonTests = []struct {
var actual []string message string
actual = nil operand graph.Value
for { operator Operator
val, ok := it.Next() expect []string
if !ok { }{
break {
} message: "successful int64 less than comparison",
actual = append(actual, ts.NameOf(val)) operand: int64(3),
} operator: kCompareLT,
actualSet := actual[:] expect: []string{"0", "1", "2"},
for _, a := range expected { },
found := false {
for j, b := range actualSet { message: "empty int64 less than comparison",
if a == b { operand: int64(0),
actualSet = append(actualSet[:j], actualSet[j+1:]...) operator: kCompareLT,
found = true 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 break
} }
got = append(got, ts.NameOf(val))
} }
if !found { if !reflect.DeepEqual(got, test.expect) {
t.Error("Couldn't find", a, "in actual output.\nActual:", actual, "\nExpected: ", expected, "\nRemainder: ", actualSet) t.Errorf("Failed to show %s, got:%q expect:%q", test.message, got, test.expect)
return
} }
} }
if len(actualSet) != 0 {
t.Error("Actual output has more than expected.\nActual:", actual, "\nExpected: ", expected, "\nRemainder: ", actualSet)
}
} }
func TestWorkingIntValueComparison(t *testing.T) { var vciCheckTests = []struct {
ts := simpleStore message string
fixed := simpleFixedIterator() operator Operator
vc := NewComparison(fixed, kCompareLT, int64(3), ts) check graph.Value
checkIteratorContains(ts, vc, []string{"0", "1", "2"}, t) expect bool
} }{
{
func TestFailingIntValueComparison(t *testing.T) { message: "1 is less than 2",
ts := simpleStore operator: kCompareGTE,
fixed := simpleFixedIterator() check: 1,
vc := NewComparison(fixed, kCompareLT, int64(0), ts) expect: false,
checkIteratorContains(ts, vc, []string{}, t) },
} {
message: "2 is greater than or equal to 2",
func TestWorkingGT(t *testing.T) { operator: kCompareGTE,
ts := simpleStore check: 2,
fixed := simpleFixedIterator() expect: true,
vc := NewComparison(fixed, kCompareGT, int64(2), ts) },
checkIteratorContains(ts, vc, []string{"3", "4"}, t) {
} message: "3 is greater than or equal to 2",
operator: kCompareGTE,
func TestWorkingGTE(t *testing.T) { check: 3,
ts := simpleStore expect: true,
fixed := simpleFixedIterator() },
vc := NewComparison(fixed, kCompareGTE, int64(2), ts) {
checkIteratorContains(ts, vc, []string{"2", "3", "4"}, t) message: "5 is absent from iterator",
operator: kCompareGTE,
check: 5,
expect: false,
},
} }
func TestVCICheck(t *testing.T) { func TestVCICheck(t *testing.T) {
ts := simpleStore for _, test := range vciCheckTests {
fixed := simpleFixedIterator() vc := NewComparison(simpleFixedIterator(), test.operator, int64(2), simpleStore)
vc := NewComparison(fixed, kCompareGTE, int64(2), ts) if vc.Check(test.check) != test.expect {
if vc.Check(1) { t.Errorf("Failed to show %s", test.message)
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")
} }
} }