Skip to content

Commit 546bd03

Browse files
committed
Pull out pytests from algorithms doctest
1 parent 9563ccc commit 546bd03

3 files changed

Lines changed: 69 additions & 20 deletions

File tree

pymathics/graph/algorithms.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ class ConnectedComponents(_NetworkXBuiltin):
3232
</dl>
3333
3434
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
35-
= {{3, 4}, {2}, {1}}
35+
= ...
3636
3737
>> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g]
38-
= {{1, 2, 3}}
38+
= ...
3939
4040
>> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5}]; ConnectedComponents[g]
41-
= {{4, 5}, {1, 2, 3}}
41+
= ...
4242
"""
4343

4444
def eval(
@@ -102,17 +102,11 @@ class GraphDistance(_NetworkXBuiltin):
102102
= Infinity
103103
104104
>> GraphDistance[{1 <-> 2, 2 <-> 3, 3 <-> 4, 2 <-> 4, 4 -> 5}, 3]
105-
= {2, 1, 0, 1, 2}
105+
= ...
106106
107107
>> GraphDistance[{1 <-> 2, 3 <-> 4}, 3]
108108
= {Infinity, Infinity, 0, 1}
109109
110-
#> GraphDistance[{}, 1, 1]
111-
: The vertex at position 2 in GraphDistance[{}, 1, 1] does not belong to the graph at position 1.
112-
= GraphDistance[{}, 1, 1]
113-
#> GraphDistance[{1 -> 2}, 3, 4]
114-
: The vertex at position 2 in GraphDistance[{1 -> 2}, 3, 4] does not belong to the graph at position 1.
115-
= GraphDistance[{1 -> 2}, 3, 4]
116110
"""
117111

118112
def eval_s(
@@ -203,7 +197,7 @@ class PlanarGraphQ(_NetworkXBuiltin):
203197
#> PlanarGraphQ[Graph[{}]]
204198
= False
205199
206-
200+
207201
>> PlanarGraphQ["abc"]
208202
: Expected a graph at position 1 in PlanarGraphQ[abc].
209203
= False

test/test_algorithms.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,44 @@ def setup_module(module):
1313

1414
def test_connected_components():
1515
for str_expr, str_expected in [
16-
("PlanarGraphQ[Graph[{}]]", "False"),
17-
("g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}];", "Null"),
18-
("SortList[ConnectedComponents[g]]", "{{1}, {2}, {3, 4}}"),
19-
("g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}];", "Null"),
20-
("SortList[ConnectedComponents[g]]", "{{1, 2, 3}}"),
16+
("PlanarGraphQ[Graph[{}]]", "False"),
17+
("g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}];", "Null"),
18+
("SortList[ConnectedComponents[g]]", "{{1}, {2}, {3, 4}}"),
19+
("g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}];", "Null"),
20+
("SortList[ConnectedComponents[g]]", "{{1, 2, 3}}"),
2121
]:
2222
check_evaluation(str_expr, str_expected)
2323

2424

2525
def test_graph_distance():
26-
for str_expr, str_expected in [
27-
("GraphDistance[{1 <-> 2, 2 <-> 3, 3 <-> 4, 2 <-> 4, 4 -> 5}, 1, 5]", "3"),
28-
("GraphDistance[{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 -> 2, 4 -> 5}, 1, 5]", "4"),
26+
for str_expr, str_expected, mess in [
27+
(
28+
"GraphDistance[{1 <-> 2, 2 <-> 3, 3 <-> 4, 2 <-> 4, 4 -> 5}, 1, 5]",
29+
"3",
30+
None,
31+
),
32+
("GraphDistance[{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 -> 2, 4 -> 5}, 1, 5]", "4", None),
2933
# ("GraphDistance[{1 <-> 2, 2 <-> 3, 4 -> 3, 4 -> 2, 4 -> 5}, 1, 5]", "Infinity"),
3034
(
3135
"Sort[GraphDistance[{1 <-> 2, 2 <-> 3, 3 <-> 4, 2 <-> 4, 4 -> 5}, 3]]",
3236
"{0, 1, 1, 2, 2}",
37+
None,
38+
),
39+
(
40+
"GraphDistance[{}, 1, 1]",
41+
"GraphDistance[{}, 1, 1]",
42+
[
43+
"The vertex at position 2 in GraphDistance[{}, 1, 1] does not belong to "
44+
"the graph at position 1."
45+
],
46+
),
47+
(
48+
"GraphDistance[{1 -> 2}, 3, 4]",
49+
"GraphDistance[{1 -> 2}, 3, 4]",
50+
[
51+
"The vertex at position 2 in GraphDistance[{1 -> 2}, 3, 4] does not belong "
52+
"to the graph at position 1."
53+
],
3354
),
3455
]:
35-
check_evaluation(str_expr, str_expected)
56+
check_evaluation(str_expr, str_expected, expected_messages=mess)

test/test_generators.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Unit tests for mathics.builtins.numbers.algebra
4+
"""
5+
from test.helper import check_evaluation, evaluate, evaluate_value
6+
7+
8+
def setup_module(module):
9+
"""Load pymathics.graph"""
10+
assert evaluate_value('LoadModule["pymathics.graph"]') == "pymathics.graph"
11+
evaluate("SortList[list_] := Sort[Map[Sort, list]]")
12+
13+
14+
def test_connected_components():
15+
for str_expr, str_expected in [
16+
("g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}];", "Null"),
17+
("SortList[ConnectedComponents[g]]", "{{1}, {2}, {3, 4}}"),
18+
("g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}];", "Null"),
19+
("SortList[ConnectedComponents[g]]", "{{1, 2, 3}}"),
20+
]:
21+
check_evaluation(str_expr, str_expected)
22+
23+
24+
def test_graph_distance():
25+
for str_expr, str_expected in [
26+
("GraphDistance[{1 <-> 2, 2 <-> 3, 3 <-> 4, 2 <-> 4, 4 -> 5}, 1, 5]", "3"),
27+
("GraphDistance[{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 -> 2, 4 -> 5}, 1, 5]", "4"),
28+
# ("GraphDistance[{1 <-> 2, 2 <-> 3, 4 -> 3, 4 -> 2, 4 -> 5}, 1, 5]", "Infinity"),
29+
(
30+
"Sort[GraphDistance[{1 <-> 2, 2 <-> 3, 3 <-> 4, 2 <-> 4, 4 -> 5}, 3]]",
31+
"{0, 1, 1, 2, 2}",
32+
),
33+
]:
34+
check_evaluation(str_expr, str_expected)

0 commit comments

Comments
 (0)