require 'test/unit' require 'graph' class TestGraph < Test::Unit::TestCase # adjacent edges def test_0 @g = Graph.new(false) @g.a_r_b!("socrates", "is", "a man") @g.a_r_b!("socrates", "is", "a philosopher") @g.a_r_b!("a man", "is", "mortal") actual = @g.adjacent("socrates") expected = [["a man", "is"], ["a philosopher", "is"]] assert_equal(expected, actual) end # transitive adjacency def test_1 @g = Graph.new(false) @g.a_r_b!("socrates", "is", "a man") @g.a_r_b!("socrates", "is", "a philosopher") @g.a_r_b!("a man", "is", "mortal") assert(@g.a_r_b?("socrates", "is", "mortal")) arr = @g.a_r_?("socrates", "is") #puts "\narr.inspect: #{arr.inspect}" assert(arr.include?("a man")) assert(arr.include?("mortal")) assert(arr.include?("a philosopher")) @g.a_r_b!("a", "=", "b") @g.a_r_b!("b", "=", "c") arr = @g.a_r_?("a", "=") #puts "\narr.inspect: #{arr.inspect}" assert(arr.include?("b")) assert(arr.include?("c")) end # shortest path between two vertices def test_2 @g = Graph.new(false) @g.a_r_b!("socrates", "is", "a man") @g.a_r_b!("a man", "is", "mortal") @g.a_r_b!("socrates", "is", "a philosopher") #puts "@g.to_s==#{@g.to_s}" assert(@g.a_r_b?("socrates", "is", "mortal")) actual = @g.shortest_path("socrates", "is", "mortal") expected = [ "socrates is a man", "a man is mortal" ] assert_equal(expected, actual) end # remove edge def test_3 @g = Graph.new(false) @g.a_r_b!("a", "is", "b") assert(@g.remove_edge("a", "is", "b")) actual = @g.to_s #puts "@g.to_s==#{actual}" #expected = /"a"=>\[\], "b"=>\[\]/ expected = "" assert_match(expected, actual) end # save and load def test_4 @g = Graph.new(false) @g.a_r_b!("a", "=", "b") @g.a_r_b("b", "=", "c") filename = File.expand_path(File.dirname(__FILE__)) + "/testgraph.yaml" #puts "filename==#{filename}" actual = @g.save(filename) expected = "#{filename} saved." assert_equal(expected, actual) #puts @g.to_s @g2 = Graph.new(false) actual = @g2.load(filename) expected = "#{filename} loaded." assert_equal(expected, actual) assert(@g2.a_r_b?("a", "=", "c")) #puts @g2.to_s File.delete(filename) end # Test a_r_? with r == an Array and r == a String. def test_5 @g = Graph.new(false) actual = @g.a_r_b!("a", "is", "b") assert(actual) actual = @g.a_r_?("a", "is") expected = ["b"] assert_equal(expected, actual) actual = @g.a_r_b!("a", "has", "b") expected = /Okay/ assert(actual) actual = @g.a_r_?("a", "has") expected = ["b"] assert_equal(expected, actual) actual = @g.a_r_?("a", ["has", "is"]) expected = ["b"] assert_equal(expected, actual) actual = @g.a_r_b!("a", "has", "c") assert(actual) actual = @g.a_r_?("a", "has") expected = ["b", "c"] assert_equal(expected, actual) actual = @g.a_r_?("a", "is") expected = ["b"] assert_equal(expected, actual) actual = @g.a_r_b!("a", "is", "d") assert(actual) actual = @g.a_r_?("a", ["has", "is"]) expected = ["b", "c", "d"] assert_equal(expected, actual) actual = @g.a_r_b?("a", "is", "d") assert(actual) actual = @g.a_r_b?("a", "has", "c") assert(actual) actual = @g.a_r_b?("a", ["is", "has"], "d") assert(actual) actual = @g.a_r_b?("a", ["has", "is"], "c") assert(actual) end # adding duplicate edge def test_6 @g = Graph.new(false) @g.a_r_b!("a", "is", "b") @g.a_r_b!("a", "is", "b") actual = @g.to_s expected = /^\Aa is b\Z/ assert_match(expected, actual) end def test_7 @g = Graph.new(false) @g.a_r_b!("x", "eats", "y") actual = @g.what_r_b?("eats", "y") assert_equal(["x"], actual) @g.a_r_b!("z", "eats", "y") actual = @g.what_r_b("eats", "y") assert_equal(["x", "z"], actual) @g.a_r_b!("y2", "is", "y") actual = @g.what_r_b?("eats", "y2") assert_equal(["x", "z"], actual) end end # class