github.com/cayleygraph/cayley@v0.7.7/graph/iterator/and_optimize_test.go (about) 1 // Copyright 2014 The Cayley Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package iterator_test 16 17 // Tests relating to methods in and-iterator-optimize. Many are pretty simplistic, but 18 // nonetheless cover a lot of basic cases. 19 20 import ( 21 "testing" 22 23 . "github.com/cayleygraph/cayley/graph/iterator" 24 ) 25 26 func TestNullIteratorAnd(t *testing.T) { 27 all := newInt64(1, 3, true) 28 null := NewNull() 29 a := NewAnd(all, null) 30 newIt, changed := a.Optimize() 31 if !changed { 32 t.Error("Didn't change") 33 } 34 if _, ok := newIt.(*Null); !ok { 35 t.Errorf("Expected null iterator, got %T", newIt) 36 } 37 } 38 39 func TestReorderWithTag(t *testing.T) { 40 all := NewFixed(Int64Node(3)) 41 all2 := NewFixed( 42 Int64Node(3), 43 Int64Node(4), 44 Int64Node(5), 45 Int64Node(6), 46 ) 47 a := NewAnd() 48 // Make all2 the default iterator 49 a.AddSubIterator(all2) 50 a.AddSubIterator(all) 51 52 newIt, changed := a.Optimize() 53 if !changed { 54 t.Error("Expected new iterator") 55 } 56 newIt.Close() 57 } 58 59 func TestAndStatistics(t *testing.T) { 60 all := newInt64(100, 300, true) 61 all2 := newInt64(1, 30000, true) 62 a := NewAnd() 63 // Make all2 the default iterator 64 a.AddSubIterator(all2) 65 a.AddSubIterator(all) 66 stats1 := a.Stats() 67 newIt, changed := a.Optimize() 68 if !changed { 69 t.Error("Didn't optimize") 70 } 71 stats2 := newIt.Stats() 72 if stats2.NextCost > stats1.NextCost { 73 t.Error("And didn't optimize. Next cost old ", stats1.NextCost, "and new ", stats2.NextCost) 74 } 75 }