Exemple #1
0
def test_getkernel_isarg(content):
    '''Test that the get_kernel function recognises standard arguments and
    returns them correctly. Tests for Name, Part_Ref, Data_Ref and
    Function_Reference, but does not include Proc_Component_Ref
    (i.e. an argument to a Structure Constructor) which is tested
    separately in test_getkernel_proc_component.

    '''
    tree = Part_Ref("sub({0})".format(content))
    kern_name, args = get_kernel(tree, "dummy.f90", {})
    assert kern_name == "sub"
    assert len(args) == 1
    arg = args[0]
    assert isinstance(arg, Arg)
    assert not arg.is_literal()
    assert arg.text == content
    assert arg.varname == "a_rg"
    assert arg._datatype is None
    kern_name, args = get_kernel(
        tree, "dummy.f90", {"a_rg": ("info"), "rg": ("info")})
    arg = args[0]
    if content == "a % rg()":
        # Datatype information is not captured for function references
        assert arg._datatype is None
    else:
        assert arg._datatype == ("info")
Exemple #2
0
def test_getkernel_argerror(monkeypatch):
    '''Test that the get_kernel function raises an exception if it does
    not recognise the fparser2 parse tree for an argument.

    '''
    tree = Part_Ref("sub(dummy)")
    monkeypatch.setattr(tree, "items", ["sub", None])
    with pytest.raises(InternalError) as excinfo:
        _, _ = get_kernel(tree, "dummy.f90", None)
    assert "Unsupported argument structure " in str(excinfo.value)
Exemple #3
0
def test_getkernel_noexpr(content):
    '''Test that the get_kernel function recognises an expression
    containing a variable and raises an exception (as this is not
    currently supported).

    '''
    tree = Part_Ref("sub({0})".format(content))
    with pytest.raises(NotImplementedError) as excinfo:
        _, _ = get_kernel(tree, "dummy.f90", None)
    assert "Expressions containing variables are not yet supported" \
        in str(excinfo.value)
Exemple #4
0
def test_getkernel_isarg(content):
    '''Test that the get_kernel function recognises standard arguments,
    including a function reference, and returns them correctly

    '''
    tree = Part_Ref("sub({0})".format(content))
    kern_name, args = get_kernel(tree, "dummy.f90")
    assert kern_name == "sub"
    assert len(args) == 1
    arg = args[0]
    assert isinstance(arg, Arg)
    assert not arg.is_literal()
    assert arg.text == content
    assert arg.varname == "a_rg"
Exemple #5
0
def test_getkernel_isliteral(parser, content):
    '''Test that the get_kernel function recognises the possible forms of
    literal argument and returns them correctly.

    '''
    # pylint: disable=unused-argument
    tree = Part_Ref("sub({0})".format(content))
    kern_name, args = get_kernel(tree, "dummy.f90")
    assert kern_name == "sub"
    assert len(args) == 1
    arg = args[0]
    assert isinstance(arg, Arg)
    assert arg.is_literal()
    assert arg.text == content
    assert arg.varname is None
Exemple #6
0
def test_getkernel_invalid_children(parser, monkeypatch):
    '''Test that if the get_kernel function finds Part_Ref as the top
    level of the parse tree but this does not have two children then
    it raises an exception in the expected way. Create the parse_tree
    in-place rather than running PSyclone. Once created make the
    parse_tree content invalid using monkeypatch.

    '''
    # pylint: disable=unused-argument
    parse_tree = Part_Ref("kernel(arg)")
    monkeypatch.setattr(parse_tree, "items", [None, None, None])
    with pytest.raises(InternalError) as excinfo:
        _ = get_kernel(parse_tree, "dummy.f90")
    assert "Expected Part_Ref to have 2 children but found 3." \
        in str(excinfo.value)
Exemple #7
0
def test_getkernel_invalid_arg(monkeypatch):
    '''Test that if the get_kernel function does not recognise the type of
    argument inside a kernel passed to it, then it raises an exception
    in the expected way. Create the parse_tree in-place rather than
    running PSyclone. Once created make the parse_tree content invalid
    using monkeypatch.

    '''
    parse_tree = Part_Ref("kernel(arg)")
    monkeypatch.setattr(parse_tree, "items", [None, "invalid"])
    with pytest.raises(InternalError) as excinfo:
        _ = get_kernel(parse_tree, "dummy.f90")
    assert ("Unsupported argument structure") in str(excinfo.value)
    assert (
        "value 'invalid', kernel 'None(invalid)' in file 'dummy.f90'.") \
        in str(excinfo.value)
Exemple #8
0
    '''
    name = "class"
    if six.PY2:
        name = "type"
    content = Data_Ref("a%b")
    monkeypatch.setattr(content, "items", ["invalid", "invalid"])
    with pytest.raises(InternalError) as excinfo:
        _ = create_var_name(content)
    assert ("algorithm.py:create_var_name unrecognised structure "
            "'<{0} 'str'>' in '<class 'fparser.two.Fortran2003."
            "Data_Ref'>'".format(name) in str(excinfo.value))


@pytest.mark.parametrize("expression,expected", [
    (Name("a"), "a"), (Part_Ref("a"), "a"), (Part_Ref("a(1,n)"), "a"),
    (Data_Ref("c%b%a"), "c_b_a"), (Data_Ref("c(0)%b%a(1,n)"), "c_b_a"),
    (Proc_Component_Ref("self%a"), "self_a")])
def test_createvarname(expression, expected):
    '''Test that create var name works as expected (for cases with Name,
    Part_Ref, Data_Ref and Proc_Component_Ref).

    '''
    assert create_var_name(expression) == expected

# class KernelCall() tests


def test_kernelcall_repr():
    '''Test that the __repr__ method in KernelCall() behaves as expected.'''