@@ -37,7 +37,7 @@ def create_city_graph() -> networkx.Graph:
3737
3838@pytest .mark .benchmark (group = 'graph_neighbors' )
3939@pytest .mark .parametrize (
40- argnames = 'graph, source, expected ' ,
40+ argnames = 'graph, source, expected_neighbors ' ,
4141 argvalues = [
4242 (city_graph , "Seattle" , ["Chicago" , "San Francisco" ]),
4343 (city_graph , "San Francisco" , ["Seattle" , "Riverside" , "Los Angeles" ]),
@@ -55,24 +55,39 @@ def create_city_graph() -> networkx.Graph:
5555 (city_graph , "Philadelphia" , ["New York" , "Washington" ]),
5656 (city_graph , "Washington" , ["Atlanta" , "Miami" , "Detroit" , "Philadelphia" ])
5757 ])
58- def test_graph_neighbors (benchmark , graph , source , expected ):
59- result = benchmark (graph .neighbors , source )
60- assert sorted (expected ) == sorted (result )
58+ def test_graph_neighbors (benchmark , graph , source , expected_neighbors ):
59+ """
60+ Find the neighboring city nodes of the source city node in the United States city graph.
61+ :param benchmark: benchmark fixture
62+ :param graph: city graph of the United States
63+ :param source: source city node
64+ :param expected_neighbors: list of neighboring city nodes
65+ """
66+ neighbor_iterator = benchmark (graph .neighbors , source )
67+ assert sorted (expected_neighbors ) == sorted (neighbor_iterator )
6168
6269
6370@pytest .mark .benchmark (group = 'graph_breadth_first_search' )
6471@pytest .mark .parametrize (
65- argnames = 'graph, source, distance, expected ' ,
72+ argnames = 'graph, source, distance, expected_nodes ' ,
6673 argvalues = [
6774 (city_graph , "Boston" , 1 , ["Detroit" , "New York" ]),
6875 (city_graph , "Boston" , 2 , ["Chicago" , "Washington" , "Philadelphia" ]),
6976 (city_graph , "Boston" , 3 , ["Seattle" , "Riverside" , "Dallas" , "Atlanta" , "Miami" ]),
7077 ],
71- ids = ["distance1" , "distance2" , "distance3" ])
72- def test_graph_breadth_first_search (benchmark , graph , source , distance , expected ):
78+ ids = ['distance1' , 'distance2' , 'distance3' ])
79+ def test_graph_breadth_first_search (benchmark , graph , source , distance , expected_nodes ):
80+ """
81+ Find the city nodes at a given distance from the source city node in the United States city graph.
82+ :param benchmark: benchmark fixture
83+ :param graph: city graph of the United States
84+ :param source: source city node
85+ :param distance: distance from the source city node
86+ :param expected_nodes: list of city nodes at the given distance from the source city node
87+ """
7388 bfs_generator = benchmark (networkx .bfs_layers , graph , source )
74- path = []
89+ nodes = []
7590 for i , layer in enumerate (bfs_generator ):
7691 if i == distance :
77- path = list (layer )
78- assert path == expected
92+ nodes = list (layer )
93+ assert nodes == expected_nodes
0 commit comments