class TestDijkstra
Public Instance Methods
setup()
click to toggle source
# File test/dijkstra_test.rb, line 10 def setup @graph = AdjacencyGraph[1,2, 1,3, 2,3, 2,4, 3,4] @edge_weights = { [1, 2] => 10, [1, 3] => 1, [2, 3] => 1, [2, 4] => 1, [3, 4] => 10 } @edge_weights_lambda = lambda { |edge| @edge_weights[edge] } end
test_edge_weights_map_object_in_argument()
click to toggle source
# File test/dijkstra_test.rb, line 131 def test_edge_weights_map_object_in_argument weights_map = EdgePropertiesMap.new(@edge_weights, @graph.directed?) dijkstra = DijkstraAlgorithm.new(@graph, weights_map, DijkstraVisitor.new(@graph)) assert_equal([1, 3, 2, 4], dijkstra.shortest_path(1, 4)) end
test_missing_edge_weight()
click to toggle source
# File test/dijkstra_test.rb, line 126 def test_missing_edge_weight @edge_weights.delete([2, 3]) assert_raises(ArgumentError, 'weight of edge (2, 3) is not defined') { shortest_path(1, 5) } end
test_negative_edge_weight()
click to toggle source
# File test/dijkstra_test.rb, line 116 def test_negative_edge_weight @edge_weights[[2, 3]] = -7 assert_raises(ArgumentError, 'weight of edge (2, 3) is negative') { shortest_path(1, 5) } end
test_negative_edge_weight_with_lambda()
click to toggle source
# File test/dijkstra_test.rb, line 121 def test_negative_edge_weight_with_lambda @edge_weights[[2, 3]] = -7 assert_raises(ArgumentError, 'weight of edge (2, 3) is negative') { shortest_path(1, 5, @edge_weights_lambda) } end
test_path_for_unreachable_vertex()
click to toggle source
# File test/dijkstra_test.rb, line 36 def test_path_for_unreachable_vertex @graph.add_vertex(5) assert_equal(nil, shortest_path(1, 5)) end
test_shortest_path_search()
click to toggle source
# File test/dijkstra_test.rb, line 24 def test_shortest_path_search assert_equal([1, 3, 2, 4], shortest_path(1, 4)) end
test_shortest_path_search_with_lambda()
click to toggle source
# File test/dijkstra_test.rb, line 28 def test_shortest_path_search_with_lambda assert_equal([1, 3, 2, 4], shortest_path(1, 4, @edge_weights_lambda)) end
test_shortest_path_to_the_source()
click to toggle source
# File test/dijkstra_test.rb, line 32 def test_shortest_path_to_the_source assert_equal([1], shortest_path(1, 1)) end
test_shortest_paths_search()
click to toggle source
# File test/dijkstra_test.rb, line 41 def test_shortest_paths_search assert_equal( { 1 => [1], 2 => [1, 3, 2], 3 => [1, 3], 4 => [1, 3, 2, 4] }, shortest_paths(1) ) end
test_shortest_paths_search_with_lambda()
click to toggle source
# File test/dijkstra_test.rb, line 53 def test_shortest_paths_search_with_lambda assert_equal( { 1 => [1], 2 => [1, 3, 2], 3 => [1, 3], 4 => [1, 3, 2, 4] }, shortest_paths(1, @edge_weights_lambda) ) end
test_shortest_paths_search_with_unreachable_vertex()
click to toggle source
# File test/dijkstra_test.rb, line 65 def test_shortest_paths_search_with_unreachable_vertex @graph.add_vertex(5) assert_equal( { 1 => [1], 2 => [1, 3, 2], 3 => [1, 3], 4 => [1, 3, 2, 4], 5 => nil }, shortest_paths(1) ) end
test_visitor()
click to toggle source
# File test/dijkstra_test.rb, line 80 def test_visitor visitor = DijkstraVisitor.new(@graph) events = [] %w[examine_vertex examine_edge edge_relaxed edge_not_relaxed finish_vertex].each do |event| visitor.send("set_#{event}_event_handler") { |*args| events << { event.to_sym => args } } end @graph.dijkstra_shortest_paths(@edge_weights, 1, visitor) assert_equal( [ { :examine_vertex => [1] }, { :examine_edge => [1, 2] }, { :edge_relaxed => [1, 2] }, { :examine_edge => [1, 3] }, { :edge_relaxed => [1, 3] }, { :finish_vertex => [1] }, { :examine_vertex => [3] }, { :examine_edge => [3, 2] }, { :edge_relaxed => [3, 2] }, { :examine_edge => [3, 4] }, { :edge_relaxed => [3, 4] }, { :finish_vertex => [3] }, { :examine_vertex => [2] }, { :examine_edge => [2, 4] }, { :edge_relaxed => [2, 4] }, { :finish_vertex => [2] }, { :examine_vertex => [4] }, { :finish_vertex => [4] }, ], events ) end