Example #1
0
def test_cw_ifblock():
    '''Check the CWriter class ifblock method correctly prints out the
    C representation.

    '''
    from psyclone.psyir.nodes import IfBlock

    # Try with just an IfBlock node
    ifblock = IfBlock()
    cwriter = CWriter()
    with pytest.raises(VisitorError) as err:
        _ = cwriter(ifblock)
    assert ("IfBlock malformed or incomplete. It should have "
            "at least 2 children, but found 0." in str(err.value))

    # Add the if condition
    ifblock.addchild(Reference(DataSymbol('a', REAL_TYPE), parent=ifblock))
    with pytest.raises(VisitorError) as err:
        _ = cwriter(ifblock)
    assert ("IfBlock malformed or incomplete. It should have "
            "at least 2 children, but found 1." in str(err.value))

    # Fill the if_body and else_body
    ifblock.addchild(Schedule(parent=ifblock))
    ifblock.addchild(Schedule(parent=ifblock))
    ifblock.if_body.addchild(Return(parent=ifblock.if_body))

    condition = Reference(DataSymbol('b', REAL_TYPE))
    then_content = [Return()]
    else_content = [Return()]
    ifblock2 = IfBlock.create(condition, then_content, else_content)
    ifblock2.parent = ifblock.if_body
    ifblock.else_body.addchild(ifblock2)

    result = cwriter(ifblock)
    assert result == ("if (a) {\n"
                      "  return;\n"
                      "} else {\n"
                      "  if (b) {\n"
                      "    return;\n"
                      "  } else {\n"
                      "    return;\n"
                      "  }\n"
                      "}\n")
Example #2
0
def test_ifblock_properties():
    '''Test that an IfBlock node properties can be retrieved'''
    ifblock = IfBlock()

    # Condition can't be retrieved before it is added as a child.
    with pytest.raises(InternalError) as err:
        _ = ifblock.condition
    assert("IfBlock malformed or incomplete. It should have "
           "at least 2 children, but found 0." in str(err.value))

    ref1 = Reference(DataSymbol('condition1', BOOLEAN_TYPE),
                     parent=ifblock)
    ifblock.addchild(ref1)

    # If_body can't be retrieved before is added as a child.
    with pytest.raises(InternalError) as err:
        _ = ifblock.if_body
    assert("IfBlock malformed or incomplete. It should have "
           "at least 2 children, but found 1." in str(err.value))

    sch = Schedule()
    ifblock.addchild(sch)
    ret = Return()
    sch.addchild(ret)

    # Now we can retrieve the condition and the if_body, but else is empty
    assert ifblock.condition is ref1
    assert ifblock.if_body[0] is ret
    assert not ifblock.else_body

    sch2 = Schedule()
    ifblock.addchild(sch2)
    ret2 = Return()
    sch2.addchild(ret2)

    # Now we can retrieve else_body
    assert ifblock.else_body[0] is ret2
Example #3
0
def test_ifblock_children_validation():
    '''Test that children added to IfBlock are validated. IfBlock accepts
    DataNodes for the children 0 to 2 and a Shcedule for child 3.

    '''
    ifblock = IfBlock()
    if_condition = Literal('true', BOOLEAN_TYPE)
    if_body = Schedule()
    else_body = Schedule()

    # First child
    with pytest.raises(GenerationError) as excinfo:
        ifblock.addchild(if_body)
    assert ("Item 'Schedule' can't be child 0 of 'If'. The valid format is: "
            "'DataNode, Schedule [, Schedule]'." in str(excinfo.value))
    ifblock.addchild(if_condition)

    # Second child
    with pytest.raises(GenerationError) as excinfo:
        ifblock.addchild(if_condition)
    assert ("Item 'Literal' can't be child 1 of 'If'. The valid format is: "
            "'DataNode, Schedule [, Schedule]'." in str(excinfo.value))
    ifblock.addchild(if_body)

    # Third child
    with pytest.raises(GenerationError) as excinfo:
        ifblock.addchild(if_condition)
    assert ("Item 'Literal' can't be child 2 of 'If'. The valid format is: "
            "'DataNode, Schedule [, Schedule]'." in str(excinfo.value))
    ifblock.addchild(else_body)

    # Additional children
    with pytest.raises(GenerationError) as excinfo:
        ifblock.addchild(else_body)
    assert ("Item 'Schedule' can't be child 3 of 'If'. The valid format is: "
            "'DataNode, Schedule [, Schedule]'." in str(excinfo.value))