Example #1
0
def test_algorithm_run(interface: AltInterface, input_file: str,
                       algorithm: str, result_type: str, optional_param: str):
    """

    Simple algorithms test. Just run the given algorithm with given input and checks the output type.
    Testing whether the algorithm even runs and finishes without any problems.

    :param interface: `pytest fixture` returning :class:`~backend.python_interface.AltInterface` instance
    :param input_file: path to XML file containing input
    :param algorithm: algorithm to be run
    :param result_type: expected type of the output
    :param optional_param: extra parameter for algorithms that need it (currently only regexp derivation and CYK)

    """
    xml_input = read_input(input_file)
    res = interface.algorithms(xml_input, algorithm, optional_param)
    assert res.endswith(result_type + '\n')
Example #2
0
def test_algorithm_result(interface: AltInterface, input_file: str,
                          algorithm: str, expected_file: str,
                          optional_param: str):
    """

    Testing given algorithm on given input and comparing the result with expected output.

    .. note::

        Bare on mind that this function is heavily testing comparison. If this test fails, don't forget to check also
        comparison function and wrapper!

    :param interface: `pytest fixture` returning :class:`~backend.python_interface.AltInterface` instance
    :param input_file: path to XML file containing input
    :param algorithm: algorithm to be run
    :param expected_file: path to XML file containing expected output
    :param optional_param: extra parameter for algorithms that need it (currently only regexp derivation and CYK)

    """
    xml_input = read_input(input_file)
    expected_output = read_input(expected_file)

    res = interface.algorithms(xml_input, algorithm, optional_param)

    if 'automaton' in algorithm:
        input_type = 'fa'
    elif 'grammar' in algorithm:
        input_type = 'cfg'
    elif 'regexp' in algorithm:
        input_type = 're'
    else:
        pytest.fail('Invalid algorithm passed as argument!!')
        return

    # When comparing step tables of minimization, just compare two strings
    if (algorithm == AlgorithmTypes.AUTOMATON_MINIMIZATION
            or algorithm == AlgorithmTypes.GRAMMAR_CYK
            or algorithm == AlgorithmTypes.GRAMMAR_CYK_NO_VERBOSE):
        assert res == expected_output
    else:
        # Also testing comparison. If this test fails, don't forget to check also that!
        res = interface.comparison(res, input_type, expected_output,
                                   input_type)
        assert res is True
Example #3
0
def test_epsilon_trim_det_min(interface: AltInterface, automaton: str):
    """

    Testing sequence of automata algorithms: epsilon transition removal, automaton trim, determinization and
    minimization. In each step checking for valid output type.

    :param interface: `pytest fixture` returning :class:`~backend.python_interface.AltInterface` instance
    :param automaton: path to XML file containing input automaton

    """
    res = read_input(automaton)

    for algorithm, result_type in [
        (AlgorithmTypes.AUTOMATON_EPSILON_REMOVAL, '</NFA>'),
        (AlgorithmTypes.AUTOMATON_TRIM, '</NFA>'),
        (AlgorithmTypes.AUTOMATON_DETERMINIZATION, '</DFA>'),
        (AlgorithmTypes.AUTOMATON_MINIMIZATION_NO_VERBOSE, '</DFA>')
    ]:
        res = interface.algorithms(res, algorithm)
        assert res.endswith(result_type + '\n')
Example #4
0
def test_epsilon_reduction_unit_recursion(interface: AltInterface,
                                          grammar: str):
    """

    Testing sequence of grammar algorithms: epsilon rules removal, grammar reduction, unit rules removal and left
    recursion removal. In each step checking for valid output type.

    :param interface: `pytest fixture` returning :class:`~backend.python_interface.AltInterface` instance
    :param grammar: path to XML file containing input grammar

    """
    res = read_input(grammar)

    for algorithm in [
            AlgorithmTypes.GRAMMAR_EPSILON_REMOVAL,
            AlgorithmTypes.GRAMMAR_REDUCTION,
            AlgorithmTypes.GRAMMAR_UNIT_RULES_REMOVAL,
            AlgorithmTypes.GRAMMAR_LEFT_RECURSION_REMOVAL
    ]:
        res = interface.algorithms(res, algorithm)
        assert res.endswith('</EpsilonFreeCFG>\n')
Example #5
0
def test_fails(interface: AltInterface):
    """

    Testing invalid requests for :class:`~backend.python_interface.AltInterface` that should always fail.

    :param interface: `pytest fixture` returning :class:`~backend.python_interface.AltInterface` instance

    """

    # Passing file as parameter
    with pytest.raises(AltInterfaceException, match='Cannot parse the XML'):
        interface.algorithms(REGEXPS + '/regexp.xml',
                             AlgorithmTypes.REGEXP_TRIM)

    # Omitting the optional parameter which is mandatory for some algorithms OR giving optional parameter when it's not
    # wanted
    xml_input = read_input(REGEXPS + '/regexp.xml')
    with pytest.raises(AltInterfaceException,
                       match='No string to differentiate by was given!'):
        interface.algorithms(xml_input, AlgorithmTypes.REGEXP_DERIVATION)
    with pytest.raises(
            AltInterfaceException,
            match='Optional parameter was given even though it can\'t be used!'
    ):
        interface.algorithms(xml_input, AlgorithmTypes.REGEXP_TRIM,
                             'ThisShouldNotBeHere')

    # Passing invalid algorithm
    xml_input = read_input(AUTOMATA + '/NFSM1.xml')
    with pytest.raises(AltInterfaceException,
                       match='Unknown algorithm passed as parameter!'):
        interface.algorithms(xml_input, 'determinization')
    with pytest.raises(AltInterfaceException,
                       match='Unknown algorithm passed as parameter!'):
        interface.algorithms(xml_input, 'automaton_determinize')

    # Passing invalid input type
    with pytest.raises(AltInterfaceException, match='Entry overload'):
        interface.algorithms(xml_input, AlgorithmTypes.AUTOMATON_MINIMIZATION)

    # Passing invalid source or target type to conversion
    with pytest.raises(
            AltInterfaceException,
            match='Unknown \'from\' parameter passed as parameter!'):
        interface.conversion(xml_input, 'fsm', 'rg')
    with pytest.raises(AltInterfaceException,
                       match='Unknown \'to\' parameter passed as parameter'):
        interface.conversion(xml_input, 'fa', 'cfg')
    with pytest.raises(AltInterfaceException,
                       match='Unknown \'from\' parameter passed as parameter'):
        interface.conversion(xml_input, 'pda', 'cfg')

    # Trying to convert ENFA to RG
    xml_input = read_input(AUTOMATA + '/ENFA1.EPSILON.xml')
    with pytest.raises(AltInterfaceException, match='Entry overload'):
        interface.conversion(xml_input, 'fa', 'rg')

    # Trying to compare FA and CFG
    xml_input2 = read_input(GRAMMARS + '/CFG1.UNIT.xml')
    with pytest.raises(AltInterfaceException, match='Entry overload'):
        interface.comparison(xml_input, 'fa', xml_input2, 'cfg')