|
| 1 | +# This file contains test cases that you need to pass to get a grade |
| 2 | +# You MUST NOT touch anything here except ONE block below |
| 3 | +# You CAN modify this file IF AND ONLY IF you have found a bug and are willing to fix it |
| 4 | +# Otherwise, please report it |
| 5 | +import pyformlang.finite_automaton |
| 6 | +from networkx import MultiDiGraph |
| 7 | +from pyformlang.regular_expression import Regex |
| 8 | +import pytest |
| 9 | +import random |
| 10 | +import itertools |
| 11 | +import networkx as nx |
| 12 | + |
| 13 | +# Fix import statements in try block to run tests |
| 14 | +try: |
| 15 | + from project.task4 import reachability_with_constraints |
| 16 | + from project.task2 import regex_to_dfa, graph_to_nfa |
| 17 | + from project.task3 import paths_ends |
| 18 | +except ImportError: |
| 19 | + pytestmark = pytest.mark.skip("Task 4 is not ready to test!") |
| 20 | + |
| 21 | +QUERIES = [ |
| 22 | + "a", |
| 23 | + "a*", |
| 24 | + "ab", |
| 25 | + "abc", |
| 26 | + "abcd", |
| 27 | + "a*b*", |
| 28 | + "(ab)*", |
| 29 | + "ab*", |
| 30 | + "ab*c*", |
| 31 | + "ab*c", |
| 32 | + "abc*", |
| 33 | + "(a|b|c|d|e)*", |
| 34 | + "(a|b|c|d|e)(a|b|c|d|e)*", |
| 35 | + "(a|b|c|d|e)f*", |
| 36 | + "(a|b)*", |
| 37 | + "(a|b)*(c|d)*", |
| 38 | + "(a | b)*(c | d)*(e | f)*", |
| 39 | + "(a | b | c)*(d | e | f)*", |
| 40 | + "((a|b)*c)*", |
| 41 | + "((a | b) * c)*(d | e)", |
| 42 | + "((a | b)*c)*((d | e)*f)*", |
| 43 | +] |
| 44 | +LABELS = ["a", "b", "c", "d", "e", "f", "g", "h"] |
| 45 | + |
| 46 | +LABEL = "label" |
| 47 | +IS_FINAL = "is_final" |
| 48 | +IS_START = "is_start" |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture(scope="class", params=range(5)) |
| 52 | +def graph(request) -> MultiDiGraph: |
| 53 | + n_of_nodes = random.randint(1, 20) |
| 54 | + graph = nx.scale_free_graph(n_of_nodes) |
| 55 | + |
| 56 | + for _, _, data in graph.edges(data=True): |
| 57 | + data[LABEL] = random.choice(LABELS) |
| 58 | + return graph |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture(scope="class", params=range(10)) |
| 62 | +def query(request) -> str: |
| 63 | + return random.choice(QUERIES) |
| 64 | + |
| 65 | + |
| 66 | +class TestReachability: |
| 67 | + def test(self, graph, query) -> None: |
| 68 | + start_nodes = set( |
| 69 | + random.choices( |
| 70 | + list(graph.nodes().keys()), k=random.randint(1, len(graph.nodes)) |
| 71 | + ) |
| 72 | + ) |
| 73 | + final_nodes = set( |
| 74 | + random.choices( |
| 75 | + list(graph.nodes().keys()), k=random.randint(1, len(graph.nodes)) |
| 76 | + ) |
| 77 | + ) |
| 78 | + fa = graph_to_nfa(graph, start_nodes, final_nodes) |
| 79 | + constraint_fa = regex_to_dfa(Regex(query)) |
| 80 | + reachable: dict = reachability_with_constraints(fa, constraint_fa) |
| 81 | + ends = paths_ends(graph, start_nodes, final_nodes, query) |
| 82 | + |
| 83 | + equivalency_flag = True |
| 84 | + for start, final in ends: |
| 85 | + if start in reachable.keys() and final in reachable[start]: |
| 86 | + continue |
| 87 | + else: |
| 88 | + equivalency_flag = False |
| 89 | + break |
| 90 | + assert equivalency_flag |
0 commit comments