Search in sources :

Example 1 with ABSRef

use of apet.testCases.ABSRef 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 2 with ABSRef

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

the class PureExpressionBuilder method createPureExpression.

/**
 * @param currentHeapReference
 * @param initialisationsOrders
 * @param heapNames
 * @param dataValue
 * @return
 */
PureExp createPureExpression(String currentHeapReference, List<String> initialisationsOrders, String testName, Set<String> heapNames, ABSData dataValue) {
    String type = getABSDataType(dataValue);
    String value = getABSDataValue(dataValue);
    if (type.contains("(") && dataValue instanceof ABSTerm) {
        ABSTerm term = (ABSTerm) dataValue;
        type = getABSTermTypeName(term);
    }
    // import type
    Decl decl = getDecl(model, Decl.class, namePred(type));
    importType(decl);
    if (dataValue instanceof ABSTerm) {
        ABSTerm term = (ABSTerm) dataValue;
        return makeDataTermValue(currentHeapReference, initialisationsOrders, testName, heapNames, term, decl);
    } else if (dataValue instanceof ABSRef) {
        if (heapNames.contains(value)) {
            String ref = heapRefBuilder.heapReferenceForTest(testName, value);
            if (currentHeapReference != null) {
                // we need to consider initialisation order!
                updateInitialisationOrder(initialisationsOrders, currentHeapReference, ref);
            }
            return new VarUse(ref);
        } else {
            return new NullExp();
        }
    } else {
        throw new IllegalStateException("Cannot handle ABSData that is not " + "either a ABSRef or a ABSTerm");
    }
}
Also used : ABSTerm(apet.testCases.ABSTerm) ABSRef(apet.testCases.ABSRef) NullExp(abs.frontend.ast.NullExp) AbsASTBuilderUtil.getDecl(abs.backend.tests.AbsASTBuilderUtil.getDecl) DataTypeDecl(abs.frontend.ast.DataTypeDecl) InterfaceDecl(abs.frontend.ast.InterfaceDecl) Decl(abs.frontend.ast.Decl) TypeParameterDecl(abs.frontend.ast.TypeParameterDecl) TypeSynDecl(abs.frontend.ast.TypeSynDecl) ParametricDataTypeDecl(abs.frontend.ast.ParametricDataTypeDecl) VarUse(abs.frontend.ast.VarUse)

Example 3 with ABSRef

use of apet.testCases.ABSRef 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 4 with ABSRef

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

the class ABSUnitTestCaseBuilder method makeGetAndAssertStatements.

void makeGetAndAssertStatements(String testMethodName, Set<String> heapNames, ClassDecl testClass, Map<ABSRef, ABSObject> finalHeap, Set<String> visited, Block testMethodBlock) {
    String assertMethodForTest = testCaseNameBuilder.assertTestMethodName(testMethodName);
    Block modifyBlock = initialiseDeltaForTestClass(testClass, assertMethodForTest);
    testMethodBlock.addStmtNoTransform(getExpStmt(getCall(getThis(), assertMethodForTest, true)));
    for (ABSRef r : finalHeap.keySet()) {
        makeGetAndAssertStatementsForHeapRef(testMethodName, heapNames, finalHeap, r, finalHeap.get(r), visited, modifyBlock);
    }
}
Also used : ABSRef(apet.testCases.ABSRef) Block(abs.frontend.ast.Block)

Example 5 with ABSRef

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

the class ABSUnitTestCaseBuilder method makeOracle.

void makeOracle(String testName, Set<String> heapNames, Map<ABSRef, ABSObject> finalHeap, String actual, Access access, ABSData data, Set<String> visited, Block block) {
    InterfaceDecl inf = null;
    if (access instanceof DataTypeUse) {
        inf = getDecl(model, InterfaceDecl.class, new DeclNamePredicate<InterfaceDecl>(((DataTypeUse) access).getName()));
    }
    PureExp exp = pureExpBuilder.createPureExpression(testName, heapNames, data);
    block.addStmtNoTransform(getExpStmt(getCall(new VarUse(ASSERT_HELPER), "assertTrue", true, new EqExp(new VarUse(actual), exp))));
    if (inf != null && !(exp instanceof NullExp)) {
        String ref = getABSDataValue(data);
        for (ABSRef r : finalHeap.keySet()) {
            if (getABSDataValue(r).equals(ref)) {
                makeGetAndAssertStatementsForHeapRef(testName, heapNames, finalHeap, r, finalHeap.get(r), visited, block);
                break;
            }
        }
    }
}
Also used : EqExp(abs.frontend.ast.EqExp) DataTypeUse(abs.frontend.ast.DataTypeUse) ABSRef(apet.testCases.ABSRef) DeclNamePredicate(abs.backend.tests.AbsASTBuilderUtil.DeclNamePredicate) NullExp(abs.frontend.ast.NullExp) InterfaceDecl(abs.frontend.ast.InterfaceDecl) PureExp(abs.frontend.ast.PureExp) VarUse(abs.frontend.ast.VarUse)

Aggregations

ABSRef (apet.testCases.ABSRef)7 VarUse (abs.frontend.ast.VarUse)4 Block (abs.frontend.ast.Block)3 InterfaceTypeUse (abs.frontend.ast.InterfaceTypeUse)3 NullExp (abs.frontend.ast.NullExp)3 PureExp (abs.frontend.ast.PureExp)3 ABSData (apet.testCases.ABSData)3 HashMap (java.util.HashMap)3 DataTypeUse (abs.frontend.ast.DataTypeUse)2 EqExp (abs.frontend.ast.EqExp)2 InterfaceDecl (abs.frontend.ast.InterfaceDecl)2 ABSTerm (apet.testCases.ABSTerm)2 PreviousCall (apet.testCases.PreviousCall)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DeclNamePredicate (abs.backend.tests.AbsASTBuilderUtil.DeclNamePredicate)1 AbsASTBuilderUtil.getCall (abs.backend.tests.AbsASTBuilderUtil.getCall)1 AbsASTBuilderUtil.getDecl (abs.backend.tests.AbsASTBuilderUtil.getDecl)1 AbsASTBuilderUtil.getExpStmt (abs.backend.tests.AbsASTBuilderUtil.getExpStmt)1 AddFieldModifier (abs.frontend.ast.AddFieldModifier)1