Permalink
Browse files

Fixed Graph#get_neighbours.

  • Loading branch information...
1 parent a21c63e commit 4262396d3fb4549e3832b8da6d46bcc7114423bf @bfontaine committed Feb 13, 2013
Showing with 70 additions and 4 deletions.
  1. +70 −4 tests/graph_tests.rb
View
@@ -1,4 +1,4 @@
-#! /usr/bin/ruby1.9.1
+#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
class Graph_test < Test::Unit::TestCase
@@ -180,6 +180,19 @@ def test_equal_graphs
assert_equal(true, g1==g2)
end
+ def test_equal_graph_and_non_graph
+
+ g = Graph.new
+
+ assert_equal(false, g==[])
+ assert_equal(false, g=={})
+ assert_equal(false, g==0)
+ assert_equal(false, g==false)
+ assert_equal(false, g==nil)
+ assert_equal(false, g==Graph::Node.new)
+
+ end
+
# == Graph::NodeArray#set_default == #
def test_nodearray_set_default_unexisting_property
@@ -380,6 +393,17 @@ def test_sample_graph_plus_itself
assert_equal(g2, g+g)
end
+ def test_graph_plus_non_graph
+
+ g = @@sample_graph
+
+ assert_equal(nil, g+42)
+ assert_equal(nil, g+[])
+ assert_equal(nil, g+{})
+ assert_equal(nil, g+Graph::Node.new)
+
+ end
+
# == Graph#| == #
def test_empty_graph_OR_empty_graph
@@ -409,6 +433,17 @@ def test_sample_graph_OR_other_sample_graph
assert_equal(g4, g2|g1)
end
+ def test_graph_OR_non_graph
+
+ g = @@sample_graph
+
+ assert_equal(nil, g|42)
+ assert_equal(nil, g|[])
+ assert_equal(nil, g|{})
+ assert_equal(nil, g|Graph::Node.new)
+
+ end
+
# == Graph#- == #
def test_empty_graph_minus_empty_graph
@@ -433,6 +468,17 @@ def test_sample_graph_minus_itself
assert_equal(@@empty, g-g)
end
+ def test_graph_minus_non_graph
+
+ g = @@sample_graph
+
+ assert_equal(nil, g-42)
+ assert_equal(nil, g-[])
+ assert_equal(nil, g-{})
+ assert_equal(nil, g-Graph::Node.new)
+
+ end
+
# == Graph#not == #
def test_empty_graph_NOT_empty_graph
@@ -597,10 +643,13 @@ def test_graph_get_neighbours_unexisting_node_object
def test_graph_get_neighbours_undirected_graph
g = @@sample_graph
+ g.attrs[:directed] = false
- n = g.get_neighbours 'foo'
+ n1 = g.get_neighbours 'chuck'
+ n2 = g.get_neighbours 'foo'
- assert_equal([ 'bar', 'chuck' ], n.map { |m| m.label })
+ assert_equal([ 'bar', 'foo' ], n1.map { |m| m.label })
+ assert_equal([ 'bar', 'chuck' ], n2.map { |m| m.label })
end
@@ -612,7 +661,24 @@ def test_graph_get_neighbours_directed_graph
assert_equal([ 'bar' ], n.map { |m| m.label })
n = g.get_neighbours 'bar'
- assert_equal([], n.map { |m| m.label })
+ assert_equal([], n)
+
+ end
+
+ def test_graph_get_neighbours_bad_edges
+
+ g = Graph.new(
+ [ { :label => 'foo' },
+ { :label => 'bar' },
+ { :label => 'moo' }],
+
+ [ { :node1 => 'foo' }, # missing :node2 attr
+ { :node1 => 'foo', :node2 => 'moo' }])
+
+ n = g.get_neighbours 'foo'
+
+ assert_equal(1, n.length)
+ assert_equal('moo', n[0].label)
end

0 comments on commit 4262396

Please sign in to comment.