def test_routine_properties(): ''' Check the various properties of the Routine class. ''' node1 = Routine("hello") assert node1.dag_name == "routine_hello_0" assert node1.return_type is None assert node1.is_program is False assert node1.name == "hello" # Give the Routine a child to get full coverage of __str__ method node1.addchild(Assignment()) assert "Routine[name:'hello']:\nAssignment" in str(node1) node2 = Routine("bonjour", return_type=INTEGER_TYPE) assert node2.return_type == INTEGER_TYPE assert node2.is_program is False node3 = Routine("gutentag", is_program=True) assert node3.return_type is None assert node3.is_program node4 = Routine("welcome", is_program=True, return_type=INTEGER_TYPE) assert node4.return_type == INTEGER_TYPE assert node4.is_program with pytest.raises(TypeError) as err: node4.name = node3 assert "must be a str but got" in str(err.value) node4.name = "goodbye" assert node4.name == "goodbye"
def test_reference_can_be_printed(): '''Test that a Reference instance can always be printed (i.e. is initialised fully)''' kschedule = KernelSchedule("kname") symbol = DataSymbol("rname", INTEGER_SINGLE_TYPE) kschedule.symbol_table.add(symbol) assignment = Assignment(parent=kschedule) ref = Reference(symbol, assignment) assert "Reference[name:'rname']" in str(ref)
def test_array_can_be_printed(): '''Test that an Array instance can always be printed (i.e. is initialised fully)''' kschedule = KernelSchedule("kname") symbol = DataSymbol("aname", INTEGER_SINGLE_TYPE) kschedule.symbol_table.add(symbol) assignment = Assignment(parent=kschedule) array = Array(symbol, assignment) assert "ArrayReference[name:'aname']\n" in str(array)
def test_reference_node_str(): ''' Check the node_str method of the Reference class.''' kschedule = KernelSchedule("kname") symbol = DataSymbol("rname", INTEGER_SINGLE_TYPE) kschedule.symbol_table.add(symbol) assignment = Assignment(parent=kschedule) ref = Reference(symbol, assignment) coloredtext = colored("Reference", Reference._colour) assert coloredtext + "[name:'rname']" in ref.node_str()
def test_reference_node_str(): ''' Check the node_str method of the Reference class.''' from psyclone.psyir.nodes.node import colored, SCHEDULE_COLOUR_MAP kschedule = KernelSchedule("kname") symbol = DataSymbol("rname", INTEGER_SINGLE_TYPE) kschedule.symbol_table.add(symbol) assignment = Assignment(parent=kschedule) ref = Reference(symbol, assignment) coloredtext = colored("Reference", SCHEDULE_COLOUR_MAP["Reference"]) assert coloredtext + "[name:'rname']" in ref.node_str()
def test_array_node_str(): ''' Check the node_str method of the ArrayReference class.''' kschedule = KernelSchedule("kname") array_type = ArrayType(INTEGER_SINGLE_TYPE, [ArrayType.Extent.ATTRIBUTE]) symbol = DataSymbol("aname", array_type) kschedule.symbol_table.add(symbol) assignment = Assignment(parent=kschedule) array = ArrayReference(symbol, parent=assignment) coloredtext = colored("ArrayReference", ArrayReference._colour) assert coloredtext+"[name:'aname']" in array.node_str()
def test_array_node_str(): ''' Check the node_str method of the Array class.''' from psyclone.psyir.nodes.node import colored, SCHEDULE_COLOUR_MAP kschedule = KernelSchedule("kname") array_type = ArrayType(INTEGER_SINGLE_TYPE, [ArrayType.Extent.ATTRIBUTE]) symbol = DataSymbol("aname", array_type) kschedule.symbol_table.add(symbol) assignment = Assignment(parent=kschedule) array = Array(symbol, parent=assignment) coloredtext = colored("ArrayReference", SCHEDULE_COLOUR_MAP["Reference"]) assert coloredtext + "[name:'aname']" in array.node_str()
def test_replace_with(): '''Check that the replace_with method behaves as expected.''' parent_node = Schedule() node1 = Statement() node2 = Statement() node3 = Statement() parent_node.children = [node1, node2, node3] new_node = Assignment() node2.replace_with(new_node) assert parent_node.children[1] is new_node assert new_node.parent is parent_node assert node2.parent is None
def test_routine_properties(): ''' Check the various properties of the Routine class. ''' node1 = Routine("hello") assert node1.dag_name == "routine_hello_0" assert node1.return_symbol is None assert node1.is_program is False assert node1.name == "hello" # Give the Routine a child to get full coverage of __str__ method node1.addchild(Assignment()) assert "Routine[name:'hello']:\nAssignment" in str(node1) node2 = Routine("bonjour") assert node2.is_program is False node3 = Routine("gutentag", is_program=True) assert node3.is_program
def test_sched_children_validation(): '''Test that children added to Schedule are validated. Schedule accepts Statements as children. ''' schedule = Schedule() statement = Assignment() nonstatement = Range() # Invalid child with pytest.raises(GenerationError) as excinfo: schedule.addchild(nonstatement) assert ("Item 'Range' can't be child 0 of 'Schedule'. The valid" " format is: '[Statement]*'." in str(excinfo.value)) # Valid children schedule.addchild(statement)
def test_array_children_validation(): '''Test that children added to Array are validated. Array accepts DataNodes and Range children.''' array_type = ArrayType(REAL_SINGLE_TYPE, shape=[5, 5]) array = Array(DataSymbol("rname", array_type)) datanode1 = Literal("1", INTEGER_SINGLE_TYPE) erange = Range() assignment = Assignment() # Invalid child with pytest.raises(GenerationError) as excinfo: array.addchild(assignment) assert ("Item 'Assignment' can't be child 0 of 'ArrayReference'. The valid" " format is: '[DataNode | Range]*'." in str(excinfo.value)) # Valid children array.addchild(datanode1) array.addchild(erange)
def test_replace_with_error1(): '''Check that the replace_with method raises the expected exception if the type of node is invalid for the location it is being added to. ''' iterator = DataSymbol("i", INTEGER_TYPE) start = Literal("0", INTEGER_TYPE) stop = Literal("1", INTEGER_TYPE) step = Literal("1", INTEGER_TYPE) loop = Loop.create(iterator, start, stop, step, []) new_node = Assignment() # The first child of a loop is the loop start value which should # be a DataNode. with pytest.raises(GenerationError) as info: loop.children[0].replace_with(new_node) assert ("Item 'Assignment' can't be child 0 of 'Loop'. The valid " "format is: 'DataNode, DataNode, DataNode, Schedule'" in str(info.value))
def test_assignment_semantic_navigation(): '''Test that the Assignment navigation properties reference the expected children''' assignment = Assignment() # lhs should fail if first child is not present with pytest.raises(InternalError) as err: _ = assignment.lhs assert "' malformed or incomplete. It needs at least 1 child to have " \ "a lhs." in str(err.value) ref = Reference(DataSymbol("a", REAL_SINGLE_TYPE), assignment) assignment.addchild(ref) # rhs should fail if second child is not present with pytest.raises(InternalError) as err: _ = assignment.rhs assert " malformed or incomplete. It needs at least 2 children to have " \ "a rhs." in str(err.value) lit = Literal("1", INTEGER_SINGLE_TYPE, assignment) assignment.addchild(lit) assert assignment.lhs is assignment._children[0] assert assignment.rhs is assignment._children[1]
def test_assignment_can_be_printed(): '''Test that an Assignment instance can always be printed (i.e. is initialised fully)''' assignment = Assignment() assert "Assignment[]\n" in str(assignment)
def test_assignment_node_str(): ''' Check the node_str method of the Assignment class.''' assignment = Assignment() coloredtext = colored("Assignment", Assignment._colour) assert coloredtext + "[]" in assignment.node_str()
def test_children_validation(): ''' Test that nodes are validated when inserted as children of other nodes. For simplicity we use Node subclasses to test this functionality across a range of possible operations. The specific logic of each validate method will be tested individually inside each Node test file. ''' assignment = Assignment() return_stmt = Return() reference = Reference(DataSymbol("a", INTEGER_TYPE)) assert isinstance(assignment.children, (ChildrenList, list)) # Try adding a invalid child (e.g. a return_stmt into an assingment) with pytest.raises(GenerationError) as error: assignment.addchild(return_stmt) assert "Item 'Return' can't be child 0 of 'Assignment'. The valid format" \ " is: 'DataNode, DataNode'." in str(error.value) # The same behaviour occurs when list insertion operations are used. with pytest.raises(GenerationError): assignment.children.append(return_stmt) with pytest.raises(GenerationError): assignment.children[0] = return_stmt with pytest.raises(GenerationError): assignment.children.insert(0, return_stmt) with pytest.raises(GenerationError): assignment.children.extend([return_stmt]) with pytest.raises(GenerationError): assignment.children = assignment.children + [return_stmt] # Valid nodes are accepted assignment.addchild(reference) # Check displaced items are also be checked when needed start = Literal("0", INTEGER_TYPE) stop = Literal("1", INTEGER_TYPE) step = Literal("2", INTEGER_TYPE) child_node = Assignment.create(Reference(DataSymbol("tmp", REAL_TYPE)), Reference(DataSymbol("i", REAL_TYPE))) loop_variable = DataSymbol("idx", INTEGER_TYPE) loop = Loop.create(loop_variable, start, stop, step, [child_node]) with pytest.raises(GenerationError): loop.children.insert(1, Literal("0", INTEGER_TYPE)) with pytest.raises(GenerationError): loop.children.remove(stop) with pytest.raises(GenerationError): del loop.children[2] with pytest.raises(GenerationError): loop.children.pop(2) with pytest.raises(GenerationError): loop.children.reverse() # But the in the right circumstances they work fine assert isinstance(loop.children.pop(), Schedule) loop.children.reverse() assert loop.children[0].value == "2"