Python/graphs/tests/test_graphs_floyd_warshall.py

55 lines
1.3 KiB
Python
Raw Normal View History

2024-10-17 07:59:27 +00:00
import pytest
from graphs.graphs_floyd_warshall import floyd_warshall
def test_no_edges():
graph = [
[0, float("inf"), float("inf")],
[float("inf"), 0, float("inf")],
[float("inf"), float("inf"), 0],
]
expected = [
[0, float("inf"), float("inf")],
[float("inf"), 0, float("inf")],
[float("inf"), float("inf"), 0],
]
dist, _ = floyd_warshall(graph, 3)
assert dist == expected
2024-10-17 08:13:46 +00:00
def test_example_input(capsys):
2024-10-17 07:59:27 +00:00
num_vertices = 3
graph = [
[float("inf"), float("inf"), float("inf")],
[float("inf"), float("inf"), float("inf")],
[float("inf"), float("inf"), float("inf")],
]
for i in range(num_vertices):
graph[i][i] = 0.0
graph[0][1] = 2
graph[1][0] = 1
expected = [
[0, 2, float("inf")],
[1, 0, float("inf")],
[float("inf"), float("inf"), 0],
]
dist, _ = floyd_warshall(graph, num_vertices)
2024-10-17 08:13:46 +00:00
_ = capsys.readouterr()
2024-10-17 07:59:27 +00:00
assert dist == expected
def test_unreachable_vertices():
graph = [
[0, 1, float("inf")],
[float("inf"), 0, 2],
[float("inf"), float("inf"), 0],
]
expected = [[0, 1, 3], [float("inf"), 0, 2], [float("inf"), float("inf"), 0]]
dist, _ = floyd_warshall(graph, 3)
assert dist == expected
if __name__ == "__main__":
pytest.main()