Example #1
0
def test_open_file_dual():
    FILE_NAME = 'main_lp.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)
    output_file = os.path.join(CUR_DIR, 'files/output_tests/output_dual.txt')

    parser = AplosParser(filename=test_file)
    matrices = parser.read_matrices_from_file(output_file, dual=True)

    ex_A = [[1, 2], [2, 5]]
    ex_b = [3, 2]
    ex_c = [9, 4]
    ex_Eqin = [1, 1]
    ex_minmax = [-1]
    ex_varconstr = [1, 1]

    ex = {
        'A': ex_A,
        'b': ex_b,
        'c': ex_c,
        "Eqin": ex_Eqin,
        'MinMax': ex_minmax,
        'VarConstr': ex_varconstr
    }

    assert ex == matrices
Example #2
0
def test_dim_empty():

    with pytest.warns(RuntimeWarning):
        parser = AplosParser(text=' ')

    with pytest.raises(exceptions.EmptyLPException):
        parser.get_dimensions()
Example #3
0
def test_get_matrix_errors():
    FILE_NAME = 'error_lp_1.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)

    parser = AplosParser(filename=test_file)
    with pytest.raises(exceptions.LPErrorException):
        A = parser.get_matrix('A')
Example #4
0
def test_get_matrix_empty():

    with pytest.warns(RuntimeWarning):
        parser = AplosParser(text='')

        with pytest.raises(exceptions.EmptyLPException):
            if not parser.detect_errors():
                A = parser.get_matrix('A')
Example #5
0
def test_dim_no_detect_errors():

    FILE_NAME = 'main_lp.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)

    parser = AplosParser(filename=test_file)

    with pytest.raises(exceptions.LPErrorException):
        parser.get_dimensions()
Example #6
0
def test_save_to_file_empty():

    with pytest.warns(RuntimeWarning):
        parser = AplosParser(text='')

    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    output_file = os.path.join(CUR_DIR, 'files/output_tests/output.txt')

    with pytest.raises(exceptions.EmptyLPException):
        parser.write_matrices_to_file(output_file)
Example #7
0
def test_open_file_error():
    FILE_NAME = 'main_lp.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)
    output_file = os.path.join(CUR_DIR, 'files/output_tests/output_wrong.txt')

    parser = AplosParser(filename=test_file)

    with pytest.raises(exceptions.LPReadException):
        matrices = parser.read_matrices_from_file(output_file)
Example #8
0
def test_open_file_non_existing():

    FILE_NAME = 'main_lp.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)

    parser = AplosParser(filename=test_file)

    with pytest.raises(IOError):
        matrices = parser.read_matrices_from_file('a_path')
Example #9
0
def test_get_matrix_no_arg():

    FILE_NAME = 'main_lp.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)

    parser = AplosParser(filename=test_file)
    if not parser.detect_errors():

        with pytest.raises(exceptions.MissingArgumentsException):
            A = parser.get_matrix()
Example #10
0
def test_det_errors_no_errors():

    FILE_NAME = 'main_lp.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)

    parser = AplosParser(filename=test_file)

    errors = parser.detect_errors()

    assert errors == []
Example #11
0
def test_save_to_file_errors():

    FILE_NAME = 'error_lp_1.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)

    parser = AplosParser(filename=test_file)

    with pytest.raises(exceptions.LPErrorException):
        output_file = os.path.join(CUR_DIR, 'files/output_tests/output.txt')

        parser.write_matrices_to_file(output_file)
Example #12
0
def test_dim_normal():

    FILE_NAME = 'secondary_lp.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)

    parser = AplosParser(filename=test_file)

    if not parser.detect_errors():
        dims = parser.get_dimensions()
        expected = {'m': 2, 'n': 6}

        assert dims == expected
Example #13
0
def test_get_matrix():

    FILE_NAME = 'main_lp.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/'+FILE_NAME)

    parser = AplosParser(filename=test_file)
    if not parser.detect_errors():
        A = parser.get_dual_matrix('A')
        b = parser.get_dual_matrix('b')
        c = parser.get_dual_matrix('c')
        Eqin = parser.get_dual_matrix('EQin')
        minmax = parser.get_dual_matrix('minMAX')
        var_constr = parser.get_dual_matrix('var_constr')

        expected_A = [[1,2],[2,5]]
        expected_b = [3,2]
        expected_c = [9,4]
        expected_Eqin = [1,1]
        expected_minmax = [-1]
        expected_var_constr = [1,1]

        assert A == expected_A and \
               b == expected_b and \
               c == expected_c and \
               Eqin == expected_Eqin and \
               minmax == expected_minmax and \
               var_constr == expected_var_constr
Example #14
0
def test_save_to_file_normal():

    FILE_NAME = 'main_lp.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)

    parser = AplosParser(filename=test_file)

    if not parser.detect_errors():
        output_file = os.path.join(CUR_DIR, 'files/output_tests/output.txt')
        expected_file = os.path.join(CUR_DIR, 'files/matrices_file.txt')
        parser.write_matrices_to_file(output_file)

        with open(output_file, 'r') as of, open(expected_file, 'r') as ef:
            assert of.read() == ef.read()
Example #15
0
def test_det_errors_normal():

    FILE_NAME = 'error_lp_2.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)

    with pytest.warns(RuntimeWarning):
        parser = AplosParser(filename=test_file)
        errors = parser.detect_errors()

        expected = []
        expected.append('Min/Max is not specified for object function')
        expected.append("Constraint initializer 's.t' or similar is missing")

    assert errors == expected
Example #16
0
def test_det_errors_no_end():

    FILE_NAME = 'error_lp_1.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)

    with pytest.warns(RuntimeWarning):
        parser = AplosParser(filename=test_file)
        errors = parser.detect_errors(print_msg=True)

        expected = []
        expected.append('Min/Max is not specified for object function')
        expected.append('No END statement found')
        expected.append('Constraint type missing from some constraints')
        expected.append('Right side argument missing from some constraints')

    assert errors == expected
Example #17
0
def test_open_file_not_exists():

    FILE_NAME = 'NOT_EXISTS.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)

    with pytest.raises(IOError):
        parser = AplosParser(filename=test_file)
Example #18
0
def test_text_wrong_delimeter():
    text = '''Max 3x1 +2x2|          s.t. x1+2x2<=9       |2 x1+5x2<=4|End'''

    parser = AplosParser(text=text, delimeter='.')

    res = []
    res.append('Max3x1+2x2|s')
    res.append('t')
    res.append('x1+2x2<=9|2x1+5x2<=4|End')

    assert parser.lp_lines == res
Example #19
0
def test_open_file_no_lines():

    FILE_NAME = 'empty.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)

    with pytest.warns(RuntimeWarning):
        parser = AplosParser(filename=test_file)

    res = []

    assert parser.lp_lines == res
Example #20
0
def test_text_custom_delimeter_line():
    text = '''Max 3x1 +2x2|          s.t. x1+2x2<=9       |2 x1+5x2<=4|End'''

    parser = AplosParser(text=text, delimeter='|')

    res = []
    res.append('Max3x1+2x2')
    res.append('s.t.x1+2x2<=9')
    res.append('2x1+5x2<=4')
    res.append('End')

    assert parser.lp_lines == res
Example #21
0
def test_text_custom_delimeter_comma():
    text = '''Max 3x1 +2x2,s.t. x1+2x2<=9,2x1+5x2<=4,End'''

    parser = AplosParser(text=text, delimeter=',')

    res = []
    res.append('Max3x1+2x2')
    res.append('s.t.x1+2x2<=9')
    res.append('2x1+5x2<=4')
    res.append('End')

    assert parser.lp_lines == res
Example #22
0
def test_open_file_exists():

    FILE_NAME = 'main_lp.txt'
    CUR_DIR = os.path.dirname(os.path.abspath(__file__))
    test_file = os.path.join(CUR_DIR, 'files/' + FILE_NAME)

    parser = AplosParser(filename=test_file)

    res = []
    res.append('Max3x1+2x2')
    res.append('s.t.x1+2x2<=9')
    res.append('2x1+5x2<=4')
    res.append('End')

    assert parser.lp_lines == res
Example #23
0
def test_normal_text():
    text = '''Max 3x1 +2x2

              s.t. x1+2x2<=9

              2x1+5x2<=4

              End'''

    parser = AplosParser(text=text)

    res = []
    res.append('Max3x1+2x2')
    res.append('s.t.x1+2x2<=9')
    res.append('2x1+5x2<=4')
    res.append('End')

    assert parser.lp_lines == res
Example #24
0
def test_text_no_arguments():

    with pytest.raises(exceptions.MissingArgumentsException):
        parser = AplosParser()
Example #25
0
def test_empty_text():

    with pytest.warns(RuntimeWarning):
        parser = AplosParser(text='')
Example #26
0
def test_det_errors_empty():

    with pytest.warns(RuntimeWarning), pytest.raises(
            exceptions.EmptyLPException):
        parser = AplosParser(text=' ')
        errors = parser.detect_errors()