Search in sources :

Example 11 with ArrayIndex

use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.

the class LocalSearchArraySystemTest method getArrayTest.

private TestCase getArrayTest(int length) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException {
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    TestFactory testFactory = TestFactory.getInstance();
    VariableReference callee = testFactory.addConstructor(test, gc, 0, 0);
    VariableReference arrayVar = test.addStatement(new ArrayStatement(test, int[].class, length));
    for (int i = 0; i < length; i++) {
        // Add value
        VariableReference intVar = test.addStatement(new IntPrimitiveStatement(test, 0));
        test.addStatement(new AssignmentStatement(test, new ArrayIndex(test, (ArrayReference) arrayVar, i), intVar));
    }
    Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int[].class });
    GenericMethod method = new GenericMethod(m, sut);
    MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { arrayVar }));
    test.addStatement(ms);
    return test;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) GenericClass(org.evosuite.utils.generic.GenericClass) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestFactory(org.evosuite.testcase.TestFactory) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement)

Example 12 with ArrayIndex

use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.

the class TestCaseBuilder method appendAssignment.

/**
 * var := array[index]
 *
 * @param var
 * @param array
 * @param index
 */
public void appendAssignment(VariableReference var, ArrayReference array, int index) {
    ArrayIndex arrayIndex = new ArrayIndex(tc, array, index);
    AssignmentStatement stmt = new AssignmentStatement(tc, var, arrayIndex);
    tc.addStatement(stmt);
}
Also used : AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

Example 13 with ArrayIndex

use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.

the class TestCodeVisitorTest method testWrapperCastInArray.

@Test
public void testWrapperCastInArray() {
    // Short[] shortArray0 = new Short[5];
    // Integer[] integerArray0 = new Integer[9];
    // integerArray0[0] = (Integer) shortArray0[3];
    TestCase tc = new DefaultTestCase();
    ArrayStatement shortArrayStatement = new ArrayStatement(tc, Short[].class, 5);
    tc.addStatement(shortArrayStatement);
    ArrayStatement intArrayStatement = new ArrayStatement(tc, Integer[].class, 9);
    tc.addStatement(intArrayStatement);
    ArrayIndex intIndex = new ArrayIndex(tc, intArrayStatement.getArrayReference(), 0);
    ArrayIndex shortIndex = new ArrayIndex(tc, shortArrayStatement.getArrayReference(), 3);
    AssignmentStatement assignmentStatement = new AssignmentStatement(tc, intIndex, shortIndex);
    tc.addStatement(assignmentStatement);
    String code = tc.toCode();
    System.out.println(tc);
    assertFalse(code.contains("integerArray0[0] = (Integer) shortArray0[3]"));
}
Also used : ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) Test(org.junit.Test)

Example 14 with ArrayIndex

use of org.evosuite.testcase.variable.ArrayIndex in project evosuite by EvoSuite.

the class EvoTestCaseCodeGenerator method createArrayInitStmt.

@Override
public void createArrayInitStmt(final CaptureLog log, final int logRecNo) {
    final int oid = log.objectIds.get(logRecNo);
    final Object[] params = log.params.get(logRecNo);
    final String arrTypeName = log.getTypeName(oid);
    final Class<?> arrType = getClassForName(arrTypeName);
    // --- create array instance creation e.g. int[] var = new int[10];
    final ArrayReference arrRef;
    // create array only once
    if (this.oidToVarRefMap.containsKey(oid)) {
        arrRef = (ArrayReference) this.oidToVarRefMap.get(oid);
    } else {
        arrRef = new ArrayReference(testCase, arrType);
        final ArrayStatement arrStmt = new ArrayStatement(testCase, arrRef);
        arrStmt.setSize(params.length);
        testCase.addStatement(arrStmt);
        this.oidToVarRefMap.put(oid, arrRef);
    }
    final Class<?> arrCompClass = arrType.getComponentType();
    AssignmentStatement assignStmt;
    ArrayIndex arrIndex;
    VariableReference valueRef;
    // is either an oid or null
    Integer argOID;
    for (int i = 0; i < params.length; i++) {
        argOID = (Integer) params[i];
        if (argOID == null) {
            valueRef = testCase.addStatement(new NullStatement(testCase, arrCompClass));
        } else {
            valueRef = this.oidToVarRefMap.get(argOID);
            if (valueRef == null) {
                logger.info("ValueREF is NULL for " + argOID);
                continue;
            }
        }
        arrIndex = new ArrayIndex(testCase, arrRef, i);
        assignStmt = new AssignmentStatement(testCase, arrIndex, valueRef);
        testCase.addStatement(assignStmt);
        logger.debug("Adding assignment (array): " + assignStmt.getCode());
    }
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) NullStatement(org.evosuite.testcase.statements.NullStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

Aggregations

ArrayIndex (org.evosuite.testcase.variable.ArrayIndex)14 VariableReference (org.evosuite.testcase.variable.VariableReference)11 ArrayReference (org.evosuite.testcase.variable.ArrayReference)6 Test (org.junit.Test)5 FieldReference (org.evosuite.testcase.variable.FieldReference)4 GenericMethod (org.evosuite.utils.generic.GenericMethod)4 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)3 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)3 ArrayList (java.util.ArrayList)2 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)2 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)2 ObjectParameter (com.examples.with.different.packagename.ObjectParameter)1 TrivialInt (com.examples.with.different.packagename.TrivialInt)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 TestCase (org.evosuite.testcase.TestCase)1 TestFactory (org.evosuite.testcase.TestFactory)1 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)1 Scope (org.evosuite.testcase.execution.Scope)1