Beispiel #1
0
    def test_minimization(self) -> None:
        nfa = NFA.load("examples/bdiv3.json")
        nfa.minimize()
        self.assertEqual(len(nfa.states), 3)
        true_cases = {"", "aa", "bbb", "abababa", "aaaaabbaababaaabb"}
        false_cases = {"ba", "bbaaa", "ababababa", "bbaaabb", "babbaaabba"}
        self.nfa_test(nfa, true_cases, false_cases)

        nfa = NFA.load("examples/one1.json")
        nfa.minimize()
        self.assertEqual(len(nfa.states), 2)
        true_cases = {"1", "01", "10", "000000000100000"}
        false_cases = {"", "0", "00", "11", "111", "0100001", "1000001"}
        self.nfa_test(nfa, true_cases, false_cases)

        nfa = NFA.load("examples/div5.json")
        nfa.determinize()
        nfa.minimize()
        self.assertEqual(len(nfa.states), 6)
        true_cases = {"0", "101", "1000101011"}
        false_cases = {"", "1", "101010111", "11101010"}
        self.nfa_test(nfa, true_cases, false_cases)

        nfa = regex_to_dfa("aaa*|a*")
        nfa.minimize()
        self.assertEqual(len(nfa.states), 1)

        nfa = NFA.load("examples/endsWbb.json")
        with self.assertRaises(RuntimeError):
            nfa.minimize()
        with self.assertRaises(RuntimeError):
            nfa.merge_equivalent()
Beispiel #2
0
    def test_intersection(self) -> None:
        first_nfa = NFA.load("examples/aaORbb.json")
        second_nfa = NFA.load("examples/aa.json")

        first_nfa.intersection(second_nfa)
        self.nfa_test(first_nfa, {"aa"}, {"", "bb", "bbaa"})

        first_nfa = NFA.load("examples/bb.json")
        second_nfa = NFA.load("examples/aa.json")

        first_nfa.intersection(second_nfa)
        self.assertTrue(first_nfa.is_empty())

        first_nfa = regex_to_dfa("a")
        second_nfa = regex_to_dfa("b")
        first_nfa.intersection(second_nfa)
        self.assertTrue(first_nfa.is_empty())
Beispiel #3
0
    def test_emptiness(self) -> None:
        nfa = NFA.load("examples/one1.json")
        self.assertFalse(nfa.is_empty())
        self.assertFalse(nfa.is_finite())

        nfa = NFA.load("examples/bad_case.json")
        self.assertFalse(nfa.is_empty())
        self.assertFalse(nfa.is_finite())

        nfa = NFA.load("examples/aa.json")
        self.assertFalse(nfa.is_empty())
        self.assertTrue(nfa.is_finite())

        nfa = NFA.load("examples/empty.json")
        self.assertTrue(nfa.is_empty())

        nfa = NFA.load("examples/useless_loop.json")
        self.assertFalse(nfa.is_empty())
        self.assertTrue(nfa.is_finite())

        nfa = regex_to_dfa("aa|bbb|cccc")
        self.assertFalse(nfa.is_empty())
        self.assertTrue(nfa.is_finite())
Beispiel #4
0
 def _regex_to_dfa(self) -> None:
     try:
         self._nfa = regex_to_dfa(self.regexInput.text())
         self._update_table()
     except RuntimeError as error:
         QMessageBox.information(self, "Error", error.args[0])
Beispiel #5
0
 def test_bad_regex(regex: str) -> None:
     with self.assertRaises(RuntimeError):
         regex_to_dfa(regex)
Beispiel #6
0
 def test_regex(
         regex: str, true_cases: Set[str], false_cases: Set[str]) \
         -> None:
     automata = regex_to_dfa(regex)
     self.assertTrue(automata.is_deterministic())
     test_nfa.nfa_test(automata, true_cases, false_cases)