X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=spec%2Fgraph%2Fgraph_spec.rb;h=8339946af89830e51f4d7d5dac5dab0ec29d39c0;hb=32518a2c5089dbd3b9f74aad780ded127e3040ea;hp=09cc6a433cddd36d28d78fa4c09fc9f2542450da;hpb=5d0d968af54bf21b892c25074a9ca452604bb210;p=graph.njae.git diff --git a/spec/graph/graph_spec.rb b/spec/graph/graph_spec.rb index 09cc6a4..8339946 100644 --- a/spec/graph/graph_spec.rb +++ b/spec/graph/graph_spec.rb @@ -16,12 +16,21 @@ module GraphNjae g.edges.should be_empty g.vertices.should be_empty end + + it "creates a graph with some parameters" do + g = Graph.new :value1 => 1, :value2 => "value2", :value3 => :v3 + g.value1.should == 1 + g.value2.should == "value2" + g.value3.should == :v3 + g.value4.should be_nil + end + end # #initialize describe "adds attribues" do it "adds then reports arbitrary attributes" do g.score = 15 - g.score == 15 + g.score.should == 15 end end # adds attributes @@ -75,8 +84,115 @@ module GraphNjae describe "connect" do it "adds and records an edge between vertices" do + g.vertices.should be_empty + g.edges.should be_empty + v1 = Vertex.new(:name => :v1) + v2 = Vertex.new(:name => :v2) + g.connect(v1, v2) + + g.should have(2).vertices + g.vertices.should include(v1) + g.vertices.should include(v2) + g.should have(1).edges end end # #connect + describe "product" do + it "finds a product graph of a pair of one-vertex graphs" do + g1 = Graph.new + g2 = Graph.new + g1v1 = Vertex.new + g1 << g1v1 + g2v1 = Vertex.new + g2 << g2v1 + product = g1.product g2 + + product.should have(1).vertices + product.vertices.first.g1_vertex.should == g1v1 + product.vertices.first.g2_vertex.should == g2v1 + product.edges.should be_empty + end + + it "finds a product graph of a pair of simple graphs" do + g1 = Graph.new + g2 = Graph.new + g1v1 = Vertex.new(:name => :g1v1) + g1v2 = Vertex.new(:name => :g1v2) + g1.connect(g1v1, g1v2) + g2v1 = Vertex.new(:name => :g2v1) + g2v2 = Vertex.new(:name => :g2v2) + g2.connect(g2v1, g2v2) + pg = g1.product g2 + + pg.should have(4).vertices + pg.should have(2).edges + end + + it "finds a product graph of not-quite-simple graph" do + end + + end + + describe "initial_similarity" do + before(:each) do + g1 = Graph.new + g2 = Graph.new + g1v1 = Vertex.new(:name => :g1v1) + g1v2 = Vertex.new(:name => :g1v2) + g1.connect(g1v1, g1v2) + g2v1 = Vertex.new(:name => :g2v1) + g2v2 = Vertex.new(:name => :g2v2) + g2.connect(g2v1, g2v2) + @pg = g1.product g2 + end + + def simple_name_similarity(n1, n2) + 1 - n1.to_s.codepoints.to_a.delete_if {|c| n2.to_s.codepoints.to_a.include? c}.length / n1.to_s.length.to_f + end + + it "should give all nodes an initial similarity of 1 if no block is given" do + @pg.initial_similarity + @pg.vertices.each do |v| + v.initial_similarity.should be_within(0.001).of(1.0) + v.similarity.should be_within(0.001).of(1.0) + end + end + + it "should give all nodes the similarity as defined by the given block" do + @pg.initial_similarity {|v| simple_name_similarity v.g1_vertex.name, v.g2_vertex.name} + @pg.vertices.each do |v| + v.initial_similarity.should be_within(0.001).of( simple_name_similarity v.g1_vertex.name, v.g2_vertex.name ) + v.similarity.should be_within(0.001).of( simple_name_similarity v.g1_vertex.name, v.g2_vertex.name ) + end + + end + end + + describe "similarity flood" do + it "similarity floods a graph of two nodes" do + g1 = Graph.new + g2 = Graph.new + g1v1 = Vertex.new(:name => :g1v1) + g1v2 = Vertex.new(:name => :g1v2) + g1.connect(g1v1, g1v2) + g2v1 = Vertex.new(:name => :g2v1) + g2v2 = Vertex.new(:name => :g2v2) + g2.connect(g2v1, g2v2) + pg = g1.product g2 + + pg.initial_similarity + pg.similarity_flood + pg.vertices.each do |v| + v.similarity.should be_within(0.001).of(1.0) + end + end + + it "similarity floods a graph of three nodes, a -- b -- c" do + end + + it "simialrity floods a sample graph" do + end + end + end end