Fixed Stats method for Not iterator.
Added unit tests for Not iterator.
This commit is contained in:
parent
a5fd1905d0
commit
305815e663
2 changed files with 49 additions and 8 deletions
|
|
@ -154,15 +154,12 @@ func (it *Not) Optimize() (graph.Iterator, bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *Not) Stats() graph.IteratorStats {
|
func (it *Not) Stats() graph.IteratorStats {
|
||||||
subitStats := it.primaryIt.Stats()
|
primaryStats := it.primaryIt.Stats()
|
||||||
// TODO(barakmich): These should really come from the triplestore itself
|
allStats := it.allIt.Stats()
|
||||||
fanoutFactor := int64(20)
|
|
||||||
checkConstant := int64(1)
|
|
||||||
nextConstant := int64(2)
|
|
||||||
return graph.IteratorStats{
|
return graph.IteratorStats{
|
||||||
NextCost: nextConstant + subitStats.NextCost,
|
NextCost: allStats.NextCost + primaryStats.ContainsCost,
|
||||||
ContainsCost: checkConstant + subitStats.ContainsCost,
|
ContainsCost: primaryStats.ContainsCost,
|
||||||
Size: fanoutFactor * subitStats.Size,
|
Size: allStats.Size - primaryStats.Size,
|
||||||
Next: it.runstats.Next,
|
Next: it.runstats.Next,
|
||||||
Contains: it.runstats.Contains,
|
Contains: it.runstats.Contains,
|
||||||
ContainsNext: it.runstats.ContainsNext,
|
ContainsNext: it.runstats.ContainsNext,
|
||||||
|
|
|
||||||
44
graph/iterator/not_iterator_test.go
Normal file
44
graph/iterator/not_iterator_test.go
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
package iterator
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNotIteratorBasics(t *testing.T) {
|
||||||
|
allIt := newFixed()
|
||||||
|
allIt.Add(1)
|
||||||
|
allIt.Add(2)
|
||||||
|
allIt.Add(3)
|
||||||
|
allIt.Add(4)
|
||||||
|
|
||||||
|
toComplementIt := newFixed()
|
||||||
|
toComplementIt.Add(2)
|
||||||
|
toComplementIt.Add(4)
|
||||||
|
|
||||||
|
not := NewNot(toComplementIt, allIt)
|
||||||
|
|
||||||
|
if v, _ := not.Size(); v != 2 {
|
||||||
|
t.Errorf("Unexpected iterator size: got:%d, expected: %d", v, 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
expect := []int{1, 3}
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
if got := iterated(not); !reflect.DeepEqual(got, expect) {
|
||||||
|
t.Errorf("Failed to iterate Not correctly on repeat %d: got:%v expected:%v", i, got, expect)
|
||||||
|
}
|
||||||
|
not.Reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range []int{1, 3} {
|
||||||
|
if !not.Contains(v) {
|
||||||
|
t.Errorf("Failed to correctly check %d as true", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range []int{2, 4} {
|
||||||
|
if not.Contains(v) {
|
||||||
|
t.Errorf("Failed to correctly check %d as false", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue