|
| 1 | +require 'minitest/autorun' |
| 2 | +require 'set' |
| 3 | +require_relative 'weighted_graph' |
| 4 | + |
| 5 | +class TestWeightedGraph < Minitest::Test |
| 6 | + def test_directed_weighted_graph_creation |
| 7 | + graph = WeightedGraph.new(nodes: [:u, :v, :w], edges: {:u => [[:v, 1]]}, directed: true) |
| 8 | + |
| 9 | + assert graph.nodes.to_set == Set[:u, :v, :w] |
| 10 | + assert graph.edges(:u) == {:v => 1} |
| 11 | + assert graph.edges(:v).empty? |
| 12 | + assert graph.edges(:w).empty? |
| 13 | + end |
| 14 | + |
| 15 | + def test_undirected_weighted_graph_creation |
| 16 | + graph = WeightedGraph.new(nodes: [:u, :v, :w], edges: {:u => [[:v, 1]]}, directed: false) |
| 17 | + |
| 18 | + assert graph.nodes.to_set == Set[:u, :v, :w] |
| 19 | + assert graph.edges(:u) == {:v => 1} |
| 20 | + assert graph.edges(:v) == {:u => 1} |
| 21 | + assert graph.edges(:w).empty? |
| 22 | + end |
| 23 | + |
| 24 | + def test_empty_returns_true_for_empty_graph |
| 25 | + graph = WeightedGraph.new |
| 26 | + |
| 27 | + assert graph.empty? |
| 28 | + end |
| 29 | + |
| 30 | + def test_empty_returns_false_for_non_empty_graph |
| 31 | + graph = WeightedGraph.new(nodes: [:u]) |
| 32 | + |
| 33 | + assert !graph.empty? |
| 34 | + end |
| 35 | + |
| 36 | + def test_include_returns_true_for_graph_nodes |
| 37 | + graph = WeightedGraph.new(nodes: [:u]) |
| 38 | + |
| 39 | + assert graph.include?(:u) |
| 40 | + end |
| 41 | + |
| 42 | + def test_include_returns_false_for_non_graph_nodes |
| 43 | + graph = WeightedGraph.new |
| 44 | + |
| 45 | + assert !graph.include?(:u) |
| 46 | + end |
| 47 | + |
| 48 | + def test_has_neighbor_returns_true_for_graph_node_neighbors |
| 49 | + graph = WeightedGraph.new(nodes: [:u, :v], edges: {:u => [[:v, 1]]}) |
| 50 | + |
| 51 | + assert graph.has_neighbor?(:u, :v) |
| 52 | + end |
| 53 | + |
| 54 | + def test_has_neighbor_returns_false_for_non_graph_node_neighbors |
| 55 | + graph = WeightedGraph.new(nodes: [:u, :v]) |
| 56 | + |
| 57 | + assert !graph.has_neighbor?(:u, :v) |
| 58 | + end |
| 59 | + |
| 60 | + def test_edge_weight_returns_neighbor_edge_weight |
| 61 | + graph = WeightedGraph.new(nodes: [:u, :v], edges: {:u => [[:v, 4]]}) |
| 62 | + |
| 63 | + assert graph.edge_weight(:u, :v) == 4 |
| 64 | + end |
| 65 | + |
| 66 | + def test_add_node_adds_node_to_graph |
| 67 | + graph = WeightedGraph.new |
| 68 | + graph.add_node(:u) |
| 69 | + |
| 70 | + assert graph.nodes.to_set == Set[:u] |
| 71 | + end |
| 72 | + |
| 73 | + def test_add_edge_adds_edge_to_directed_weighted_graph |
| 74 | + graph = WeightedGraph.new(nodes: [:u, :v], directed: true) |
| 75 | + graph.add_edge(:u, :v, 2) |
| 76 | + |
| 77 | + assert graph.edges(:u) == {:v => 2} |
| 78 | + assert graph.edges(:v).empty? |
| 79 | + end |
| 80 | + |
| 81 | + def test_add_edge_adds_edge_to_directed_weighted_graph |
| 82 | + graph = WeightedGraph.new(nodes: [:u, :v], directed: false) |
| 83 | + graph.add_edge(:u, :v, 2) |
| 84 | + |
| 85 | + assert graph.edges(:u) == {:v => 2} |
| 86 | + assert graph.edges(:v) == {:u => 2} |
| 87 | + end |
| 88 | +end |
0 commit comments