Search in sources :

Example 1 with ABSData

use of apet.testCases.ABSData in project abstools by abstools.

the class ABSUnitTestCaseBuilder method makeSetStatements.

void makeSetStatements(Map<String, String> typeHierarchy, Map<String, List<Stmt>> initialisations, List<String> initialisationsOrders, String testName, Set<String> heapNames, Map<ABSRef, ABSObject> initialHeap, Map<String, InterfaceTypeUse> objectsInHeap, ABSRef ref, ABSObject state, ClassDecl testClass) {
    String rn = heapRefBuilder.heapReferenceForTest(testName, getABSDataValue(ref));
    String concreteTypeName = getABSObjectType(state);
    ClassDecl concreteType = getDecl(model, ClassDecl.class, new DeclNamePredicate<ClassDecl>(concreteTypeName));
    if (concreteType == null) {
        throw new IllegalStateException("Cannot find class: " + concreteTypeName);
    }
    List<Stmt> statements = new ArrayList<Stmt>();
    initialisations.put(rn, statements);
    if (!initialisationsOrders.contains(rn)) {
        initialisationsOrders.add(rn);
    }
    Map<String, ABSData> fields = getABSObjectFields(state);
    abs.frontend.ast.List<ParamDecl> params = concreteType.getParamList();
    PureExp[] constructorArgs = new PureExp[params.getNumChild()];
    for (int i = 0; i < params.getNumChild(); i++) {
        ParamDecl param = params.getChild(i);
        String name = param.getName();
        assert fields.containsKey(name);
        ABSData d = fields.remove(name);
        objectsInHeap.putAll(getTypesFromABSData(testName, d));
        PureExp exp = pureExpBuilder.createPureExpression(rn, initialisationsOrders, testName, heapNames, d);
        constructorArgs[i] = exp;
    }
    statements.add(getVAssign(rn, newObj(concreteType, false, constructorArgs)));
    for (String fn : fields.keySet()) {
        ABSData d = fields.get(fn);
        objectsInHeap.putAll(getTypesFromABSData(testName, d));
        PureExp exp = pureExpBuilder.createPureExpression(rn, initialisationsOrders, testName, heapNames, d);
        Call call = getCall(new VarUse(rn), testCaseNameBuilder.setterMethodName(fn), true, exp);
        statements.add(getExpStmt(call));
    }
    // ADD getter and setter
    deltaBuilder.updateDelta(typeHierarchy, objectsInHeap.get(rn), getDecl(model, ClassDecl.class, new DeclNamePredicate<ClassDecl>(concreteTypeName)));
}
Also used : Call(abs.frontend.ast.Call) AbsASTBuilderUtil.getCall(abs.backend.tests.AbsASTBuilderUtil.getCall) PreviousCall(apet.testCases.PreviousCall) DeclNamePredicate(abs.backend.tests.AbsASTBuilderUtil.DeclNamePredicate) ArrayList(java.util.ArrayList) PureExp(abs.frontend.ast.PureExp) VarUse(abs.frontend.ast.VarUse) AbsASTBuilderUtil.getExpStmt(abs.backend.tests.AbsASTBuilderUtil.getExpStmt) Stmt(abs.frontend.ast.Stmt) ClassDecl(abs.frontend.ast.ClassDecl) ParamDecl(abs.frontend.ast.ParamDecl) ABSData(apet.testCases.ABSData)

Example 2 with ABSData

use of apet.testCases.ABSData in project abstools by abstools.

the class ABSUnitTestCaseBuilder method getTypesFromABSData.

Map<String, InterfaceTypeUse> getTypesFromABSData(String testName, ABSData data) {
    Map<String, InterfaceTypeUse> map = new HashMap<String, InterfaceTypeUse>();
    if (data instanceof ABSRef) {
        String type = getABSDataType(data);
        String value = getABSDataValue(data);
        if (!value.equals(NULL)) {
            map.put(heapRefBuilder.heapReferenceForTest(testName, value), new InterfaceTypeUse(type, new abs.frontend.ast.List<abs.frontend.ast.Annotation>()));
        }
    } else if (data instanceof ABSTerm) {
        ABSTerm term = (ABSTerm) data;
        for (ABSData t : getABSTermArgs(term)) {
            map.putAll(getTypesFromABSData(testName, t));
        }
    }
    return map;
}
Also used : ABSTerm(apet.testCases.ABSTerm) HashMap(java.util.HashMap) ABSRef(apet.testCases.ABSRef) InterfaceTypeUse(abs.frontend.ast.InterfaceTypeUse) List(java.util.List) ArrayList(java.util.ArrayList) ABSData(apet.testCases.ABSData)

Example 3 with ABSData

use of apet.testCases.ABSData in project abstools by abstools.

the class ABSUnitTestCaseBuilder method makeGetAndAssertStatementsForHeapRef.

void makeGetAndAssertStatementsForHeapRef(String testName, Set<String> heapNames, Map<ABSRef, ABSObject> finalHeap, ABSRef ref, ABSObject state, Set<String> visited, Block block) {
    String rn = heapRefBuilder.heapReferenceForTest(testName, getABSDataValue(ref));
    if (!visited.add(rn)) {
        return;
    }
    Map<String, ABSData> fields = getABSObjectFields(state);
    ClassDecl clazz = getDecl(model, ClassDecl.class, new DeclNamePredicate<ClassDecl>(getABSObjectType(state)));
    abs.frontend.ast.List<FieldDecl> fieldDecls = clazz.getFieldList();
    for (int i = 0; i < fieldDecls.getNumChild(); i++) {
        FieldDecl field = fieldDecls.getChild(i);
        String fn = field.getName();
        if (fields.containsKey(fn)) {
            ABSData d = fields.get(fn);
            block.addStmtNoTransform(getVarDecl(testCaseNameBuilder.resultOfGetterMethodName(fn), field.getAccess().treeCopyNoTransform(), getCall(new VarUse(rn), testCaseNameBuilder.getterMethodName(fn), true)));
            makeOracle(testName, heapNames, finalHeap, testCaseNameBuilder.resultOfGetterMethodName(fn), field.getAccess().treeCopyNoTransform(), d, visited, block);
        }
    }
}
Also used : FieldDecl(abs.frontend.ast.FieldDecl) ClassDecl(abs.frontend.ast.ClassDecl) ABSData(apet.testCases.ABSData) VarUse(abs.frontend.ast.VarUse)

Example 4 with ABSData

use of apet.testCases.ABSData in project abstools by abstools.

the class ABSUnitTestCaseBuilder method buildTestCase.

/**
 * @param testCase
 * @param testClass
 * @param method
 * @param access
 * @param unitUnderTest
 */
void buildTestCase(TestCase testCase, ClassDecl testClass, MethodImpl method, Access access, String unitUnderTest) {
    // initial arg
    List<ABSData> inputArguments = getInputArgs(testCase);
    String testName = method.getMethodSig().getName();
    Block block = method.getBlock();
    Map<String, InterfaceTypeUse> typesOfObjectInHeap = new HashMap<String, InterfaceTypeUse>();
    for (ABSData d : inputArguments) {
        typesOfObjectInHeap.putAll(getTypesFromABSData(testName, d));
    }
    Map<ABSRef, ABSObject> initial = getInitialState(testCase);
    for (ABSObject obj : initial.values()) {
        typesOfObjectInHeap.putAll(getTypesFromABSObject(testName, obj));
    }
    Set<String> initialHeapNames = referenceNames(initial.keySet());
    createObjectsInHeap(testName, initialHeapNames, typesOfObjectInHeap, testClass, initial, block);
    List<PreviousCall> calls = getPreviousCalls(testCase);
    List<Exp> previous = makePreviousCalls(testName, initialHeapNames, calls);
    for (Exp pc : previous) {
        // does not care about return value
        block.addStmtNoTransform(getExpStmt(pc));
    }
    // test execution
    Exp test = makeTestExecution(testName, initialHeapNames, unitUnderTest, inputArguments);
    final boolean hasReturnValue;
    if (access instanceof DataTypeUse && ((DataTypeUse) access).getName().equals("Unit")) {
        // no return value
        block.addStmtNoTransform(getExpStmt(test));
        hasReturnValue = false;
    } else {
        block.addStmtNoTransform(getVarDecl("returnValue", access.treeCopyNoTransform(), test));
        hasReturnValue = true;
    }
    Map<ABSRef, ABSObject> finalHeap = getAfterState(testCase);
    if (finalHeap.isEmpty()) {
        // the method under test is side-effect free?
        // use the initial heap as oracle
        finalHeap = initial;
    }
    Set<String> finalHeapNames = referenceNames(finalHeap.keySet());
    // need to remember which objects in the heap we have already handled.
    Set<String> visited = new HashSet<String>();
    // assertions of object states can be done in the heap assertions
    if (hasReturnValue) {
        ABSData rd = getReturnData(testCase);
        PureExp exp = pureExpBuilder.createPureExpression(testName, finalHeapNames, rd);
        block.addStmtNoTransform(getExpStmt(getCall(new VarUse(ASSERT_HELPER), "assertTrue", true, new EqExp(new VarUse("returnValue"), exp))));
    }
    // check return value (using deltas)
    makeGetAndAssertStatements(testName, finalHeapNames, testClass, finalHeap, visited, block);
}
Also used : HashMap(java.util.HashMap) DataTypeUse(abs.frontend.ast.DataTypeUse) ABSObject(apet.testCases.ABSObject) PureExp(abs.frontend.ast.PureExp) VarUse(abs.frontend.ast.VarUse) EqExp(abs.frontend.ast.EqExp) ABSRef(apet.testCases.ABSRef) InterfaceTypeUse(abs.frontend.ast.InterfaceTypeUse) Block(abs.frontend.ast.Block) ABSData(apet.testCases.ABSData) EqExp(abs.frontend.ast.EqExp) PureExp(abs.frontend.ast.PureExp) Exp(abs.frontend.ast.Exp) NullExp(abs.frontend.ast.NullExp) PreviousCall(apet.testCases.PreviousCall) HashSet(java.util.HashSet)

Example 5 with ABSData

use of apet.testCases.ABSData in project abstools by abstools.

the class FunctionTestCaseBuilder method makeTestExecution.

@Override
Exp makeTestExecution(String testName, Set<String> heap, String testExecutionName, List<ABSData> inArgs) {
    abs.frontend.ast.List<PureExp> ps = new abs.frontend.ast.List<PureExp>();
    FnApp fa = new FnApp();
    if (inArgs.size() == 0) {
        throw new IllegalStateException("Inputs for a method must at least have a reference");
    }
    fa.setName(testExecutionName);
    fa.setParamList(ps);
    for (int i = 0; i < inArgs.size(); i++) {
        ABSData d = inArgs.get(i);
        PureExp exp = pureExpBuilder.createPureExpression(testName, heap, d);
        fa.setParam(exp, i);
    }
    return fa;
}
Also used : FnApp(abs.frontend.ast.FnApp) List(java.util.List) ABSData(apet.testCases.ABSData) PureExp(abs.frontend.ast.PureExp)

Aggregations

ABSData (apet.testCases.ABSData)8 PureExp (abs.frontend.ast.PureExp)6 VarUse (abs.frontend.ast.VarUse)4 PreviousCall (apet.testCases.PreviousCall)4 ABSRef (apet.testCases.ABSRef)3 ArrayList (java.util.ArrayList)3 AbsASTBuilderUtil.getCall (abs.backend.tests.AbsASTBuilderUtil.getCall)2 Call (abs.frontend.ast.Call)2 ClassDecl (abs.frontend.ast.ClassDecl)2 Exp (abs.frontend.ast.Exp)2 InterfaceTypeUse (abs.frontend.ast.InterfaceTypeUse)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DeclNamePredicate (abs.backend.tests.AbsASTBuilderUtil.DeclNamePredicate)1 AbsASTBuilderUtil.getExpStmt (abs.backend.tests.AbsASTBuilderUtil.getExpStmt)1 Block (abs.frontend.ast.Block)1 DataConstructorExp (abs.frontend.ast.DataConstructorExp)1 DataTypeUse (abs.frontend.ast.DataTypeUse)1 EqExp (abs.frontend.ast.EqExp)1 FieldDecl (abs.frontend.ast.FieldDecl)1