Skip to content

Commit 12167a2

Browse files
committed
Start our first tests
1 parent 9b95bc4 commit 12167a2

4 files changed

Lines changed: 65 additions & 19 deletions

File tree

.pre-commit-config.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ repos:
99
stages: [commit]
1010
- id: end-of-file-fixer
1111
stages: [commit]
12-
- id: trailing-whitespace
12+
# - repo: https://github.com/pycqa/isort
13+
# rev: 5.10.1
14+
# hooks:
15+
# - id: isort
16+
# stages: [commit]
1317
- repo: https://github.com/psf/black
1418
rev: 22.3.0
1519
hooks:
1620
- id: black
1721
language_version: python3
1822
exclude: 'pymathics/graph/version.py'
23+
stages: [commit]
1924
- repo: https://github.com/pycqa/flake8
2025
rev: 3.9.2
2126
hooks:

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
# remake --tasks to shows the targets and the comments
66

77
GIT2CL ?= admin-tools/git2cl
8-
PYTHON ?= python3
9-
PIP ?= pip3
8+
PYTHON ?= python
9+
PIP ?= $(PYTHON) -m pip
1010
RM ?= rm
1111

1212
.PHONY: all build \
@@ -40,7 +40,7 @@ install: pypi-setup
4040
$(PYTHON) setup.py install
4141

4242
# Run tests
43-
check: pytest doctest
43+
test check: pytest
4444

4545
#: Remove derived files
4646
clean: clean-pyc
@@ -51,7 +51,7 @@ clean-pyc:
5151

5252
#: Run py.test tests. Use environment variable "o" for pytest options
5353
pytest:
54-
py.test test $o
54+
$(PYTHON) -m pytest test $o
5555

5656

5757
# #: Create data that is used to in Django docs and to build TeX PDF

pymathics/graph/algorithms.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
networkx does all the heavy lifting.
66
"""
77

8+
from typing import Optional
9+
810
from mathics.core.convert.expression import to_mathics_list
911
from mathics.core.convert.python import from_python
12+
from mathics.core.evaluation import Evaluation
1013
from mathics.core.expression import Expression
1114
from mathics.core.list import ListExpression
1215
from mathics.core.symbols import SymbolFalse
@@ -25,7 +28,7 @@
2528
class ConnectedComponents(_NetworkXBuiltin):
2629
"""
2730
## >> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
28-
## = {{4, 3}, {2}, {1}}
31+
## = {{3, 4}, {2}, {1}}
2932
3033
## >> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g]
3134
## = {{1, 2, 3}}
@@ -34,7 +37,9 @@ class ConnectedComponents(_NetworkXBuiltin):
3437
## = {{4, 5}, {1, 2, 3}}
3538
"""
3639

37-
def eval(self, graph, expression, evaluation, options):
40+
def eval(
41+
self, graph, expression, evaluation: Evaluation, options: dict
42+
) -> Optional[ListExpression]:
3843
"ConnectedComponents[graph_, OptionsPattern[%(name)s]]"
3944
graph = self._build_graph(graph, evaluation, options, expression)
4045
if graph:
@@ -54,10 +59,10 @@ def eval(self, graph, expression, evaluation, options):
5459
# <dd>returns a Hamiltonian path in the given tournament graph.
5560
# </dl>
5661
# """
57-
# def eval_(self, graph, expression, evaluation, options):
58-
# "%(name)s[graph_, OptionsPattern[%(name)s]]"
62+
# def eval_(self, graph, expression, evaluation: Evaluation, options):
63+
# "FindHamiltonianPath[graph_, OptionsPattern[FindHamiltonPath]]"
5964

60-
# graph = self._build_graph(graph, evaluation, options, expression)
65+
# graph = self._build_graph(graph, evaluation: Evaluation, options, expression)
6166
# if graph:
6267
# # FIXME: for this to work we need to fill in all O(n^2) edges as an adjacency matrix?
6368
# path = nx.algorithms.tournament.hamiltonian_path(graph.G)
@@ -106,17 +111,19 @@ class GraphDistance(_NetworkXBuiltin):
106111
= GraphDistance[{1 -> 2}, 3, 4]
107112
"""
108113

109-
def eval_s(self, graph, s, expression, evaluation, options):
110-
"%(name)s[graph_, s_, OptionsPattern[%(name)s]]"
114+
def eval_s(
115+
self, graph, s, expression, evaluation: Evaluation, options: dict
116+
) -> Optional[ListExpression]:
117+
"GraphDistance[graph_, s_, OptionsPattern[GraphDistance]]"
111118
graph = self._build_graph(graph, evaluation, options, expression)
112119
if graph:
113120
weight = graph.update_weights(evaluation)
114121
d = nx.shortest_path_length(graph.G, source=s, weight=weight)
115122
inf = Expression(SymbolDirectedInfinity, 1)
116123
return to_mathics_list(*[d.get(v, inf) for v in graph.vertices])
117124

118-
def eval_s_t(self, graph, s, t, expression, evaluation, options):
119-
"%(name)s[graph_, s_, t_, OptionsPattern[%(name)s]]"
125+
def eval_s_t(self, graph, s, t, expression, evaluation: Evaluation, options: dict):
126+
"GraphDistance[graph_, s_, t_, OptionsPattern[GraphDistance]]"
120127
graph = self._build_graph(graph, evaluation, options, expression)
121128
if not graph:
122129
return
@@ -147,7 +154,7 @@ class FindSpanningTree(_NetworkXBuiltin):
147154

148155
options = DEFAULT_GRAPH_OPTIONS
149156

150-
def eval(self, graph, expression, evaluation, options):
157+
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
151158
"%(name)s[graph_, OptionsPattern[%(name)s]]"
152159
graph = self._build_graph(graph, evaluation, options, expression)
153160
if graph:
@@ -182,8 +189,8 @@ class PlanarGraphQ(_NetworkXBuiltin):
182189

183190
options = DEFAULT_GRAPH_OPTIONS
184191

185-
def eval(self, graph, expression, evaluation, options):
186-
"%(name)s[graph_, OptionsPattern[%(name)s]]"
192+
def eval(self, graph, expression, evaluation: Evaluation, options: dict):
193+
"PlanarGraphQ[graph_, OptionsPattern[PlanarGraphQ]]"
187194
graph = self._build_graph(graph, evaluation, options, expression)
188195
if not graph:
189196
return SymbolFalse
@@ -203,8 +210,8 @@ class WeaklyConnectedComponents(_NetworkXBuiltin):
203210
= {{1, 2, 3, 4, 5}, {6, 7, 8}}
204211
"""
205212

206-
def eval(self, graph, expression, evaluation, options):
207-
"WeaklyConnectedComponents[graph_, OptionsPattern[%(name)s]]"
213+
def eval(self, graph, expression, evaluation: Evaluation, options):
214+
"WeaklyConnectedComponents[graph_, OptionsPattern[WeaklyConnectedComponents]]"
208215
graph = self._build_graph(graph, evaluation, options, expression)
209216
if graph:
210217
components = nx.connected_components(graph.G.to_undirected())

test/test_algorithms.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)