/** * Create random primitive statement * * @param tc a {@link org.evosuite.testcase.TestCase} object. * @param clazz a {@link java.lang.reflect.Type} object. * @param position an integer. * @return a {@link org.evosuite.testcase.statements.PrimitiveStatement} object. */ public static PrimitiveStatement<?> getRandomStatement(TestCase tc, GenericClass clazz, int position) { PrimitiveStatement<?> statement = getPrimitiveStatement(tc, clazz); statement.randomize(); return statement; }
public static PrimitiveStatement<?> getPrimitiveStatement(TestCase tc, Class<?> clazz) { return getPrimitiveStatement(tc, new GenericClass(clazz)); }
/** * {@inheritDoc} */ @Override public Statement copy(TestCase newTestCase, int offset) { @SuppressWarnings("unchecked") PrimitiveStatement<T> clone = (PrimitiveStatement<T>) getPrimitiveStatement(newTestCase, retval.getGenericClass()); clone.setValue(value); // clone.assertions = copyAssertions(newTestCase, offset); return clone; }
@SuppressWarnings({ "rawtypes", "unchecked", "unused" }) private void createConcretePrimitives(TestCase test) { // Execute test to collect concrete values TestCaseExecutor executor = TestCaseExecutor.getInstance(); ConcreteValueObserver observer = new ConcreteValueObserver(); executor.addObserver(observer); executor.execute(test); executor.removeObserver(observer); // Now replace references to concrete values with new primitive statements Map<Integer, Object> concreteValues = observer.getConcreteValues(); List<Integer> positions = new ArrayList<Integer>(concreteValues.keySet()); Collections.sort(positions, Collections.reverseOrder()); for (Integer position : positions) { Object value = concreteValues.get(position); Statement statement = test.getStatement(position); PrimitiveStatement primitive = PrimitiveStatement.getPrimitiveStatement(test, new GenericClass( value.getClass())); primitive.setValue(value); VariableReference replacement = test.addStatement(primitive, position); test.replace(statement.getReturnValue(), replacement); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void createPlainInitStmt(CaptureLog log, int logRecNo) { // NOTE: PLAIN INIT: has always one non-null param // TODO: use primitives final int oid = log.objectIds.get(logRecNo); if (this.oidToVarRefMap.containsKey(oid)) { // TODO this might happen because of Integer.valueOf(), for example. . Is this approach ok? return; } final String type = log.getTypeName(oid); final Object value = log.params.get(logRecNo)[0]; final VariableReference varRef; if (value instanceof Class) { // final PrimitiveStatement cps = ClassPrimitiveStatement.getPrimitiveStatement(testCase, getClassForName(type)); final PrimitiveStatement cps = new ClassPrimitiveStatement(testCase, getClassForName(type)); cps.setValue(value); varRef = testCase.addStatement(cps); } else { final PrimitiveStatement primitiveValue = PrimitiveStatement.getPrimitiveStatement(testCase, getClassForName(type)); primitiveValue.setValue(value); varRef = testCase.addStatement(primitiveValue); } this.oidToVarRefMap.put(oid, varRef); }
@Test public void testSerializationNonEmptyTest() throws IOException, ClassNotFoundException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); double fitness = 0.9950513142057124d; TestChromosome chromosome = new TestChromosome(); TestCase test = new DefaultTestCase(); PrimitiveStatement<?> statement = PrimitiveStatement.getPrimitiveStatement(test, int.class); test.addStatement(statement); chromosome.setTestCase(test); chromosome.setFitness(null, fitness); oos.writeObject(chromosome); byte [] baSerialized = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(baSerialized); ObjectInputStream ois = new ObjectInputStream(bais); TestChromosome copy = (TestChromosome) ois.readObject(); Assert.assertEquals(chromosome.getFitness(), copy.getFitness(), 0.0); }
VariableReference retVal = null; if (index.isPrimitive()) { PrimitiveStatement<?> primitive = PrimitiveStatement.getPrimitiveStatement(test, index.getGenericClass()); retVal = test.addStatement(primitive, position++);
@Test public void testNumericArrayIndexInlining() throws NoSuchMethodException, SecurityException { DefaultTestCase test = new DefaultTestCase(); PrimitiveStatement<?> primitiveStatement = PrimitiveStatement.getPrimitiveStatement(test, int.class); VariableReference intVar = test.addStatement(primitiveStatement);
test.addStatement(ms1); IntPrimitiveStatement ps1 = (IntPrimitiveStatement) PrimitiveStatement.getPrimitiveStatement(test, int.class); test.addStatement(ps1);
TestChromosome testChromosome = new TestChromosome(); TestCase test = new DefaultTestCase(); PrimitiveStatement<?> statement = PrimitiveStatement.getPrimitiveStatement(test, int.class); test.addStatement(statement); testChromosome.setTestCase(test);