Skip to content

Commit

Permalink
test: Add tests for graph_to_nfa and regex_to_dfa functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tepa46 committed Sep 29, 2024
1 parent 59204e2 commit 1ce9dca
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion tests/autotests/test_task02.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

# Fix import statements in try block to run tests
try:
from project.task2 import regex_to_dfa, graph_to_nfa
from project.graph_utils import graph_to_nfa
from project.regex_utils import regex_to_dfa
except ImportError:
pytestmark = pytest.mark.skip("Task 2 is not ready to test!")

Expand Down
23 changes: 21 additions & 2 deletions tests/test_graph_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import pytest
import networkx as nx
import cfpq_data
from pathlib import Path
from project.graph_utils import (
get_graph_basic_info,
create_and_save_labeled_two_cycles_graph,
graph_to_nfa,
GraphBasicInfo,
)

Expand Down Expand Up @@ -62,12 +64,12 @@ def test_get_graph_basic_info(
@pytest.mark.parametrize(
"first_cycle_nodes_number, second_cycle_nodes_number, labels, expected_graph_path",
[
(5, 2, ("7", "8"), Path(EXPECTED_PATH, "expected_graph_1.dot")),
(5, 2, ("7", "8"), Path(EXPECTED_PATH, "graph_example_1.dot")),
(
1,
5,
("8", "1"),
Path(EXPECTED_PATH, "expected_graph_2.dot"),
Path(EXPECTED_PATH, "graph_example_2.dot"),
),
],
)
Expand All @@ -83,3 +85,20 @@ def test_create_and_save_labeled_two_cycles_graph(
actual_graph = nx.nx_pydot.read_dot(tmp_filepath)

assert nx.utils.graphs_equal(expected_graph, actual_graph)


@pytest.mark.parametrize(
"graph_path, start_states, final_states",
[
(Path(EXPECTED_PATH, "graph_example_1.dot"), {4}, {6}),
(Path(EXPECTED_PATH, "graph_example_2.dot"), {0, 1}, {5, 4}),
],
)
def test_graph_to_nfa(graph_path, start_states, final_states):
graph = nx.nx_pydot.read_dot(graph_path)

res = graph_to_nfa(graph, start_states, final_states)

assert res.start_states == start_states
assert res.final_states == final_states
assert res.symbols == set(cfpq_data.get_sorted_labels(graph))
11 changes: 11 additions & 0 deletions tests/test_regex_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from project.regex_utils import regex_to_dfa


def test_regex_to_dfa():
res = regex_to_dfa("a.b.c*|d")

assert res.is_equivalent_to(res.minimize())
assert res.is_deterministic()
assert res.accepts("abccc")
assert res.accepts("d")
assert not res.accepts("cd")

0 comments on commit 1ce9dca

Please sign in to comment.