public static PrimitiveStatement<?> getPrimitiveStatement(TestCase tc, Class<?> clazz) { return getPrimitiveStatement(tc, new GenericClass(clazz)); }
/** * Add primitive statement at position * * @param test * @param old * @param position * @return * @throws ConstructionFailedException */ private VariableReference addPrimitive(TestCase test, PrimitiveStatement<?> old, int position) throws ConstructionFailedException { logger.debug("Adding primitive"); Statement st = old.clone(test); return test.addStatement(st, position); }
/** * {@inheritDoc} */ @Override public boolean mutate(TestCase test, TestFactory factory) { if (!hasMoreThanOneValue()) return false; T oldVal = value; while (value == oldVal && value != null) { if (Randomness.nextDouble() <= Properties.RANDOM_PERTURBATION) { // When using TT, then an integer may represent a boolean, // and we would lose "negation" as a mutation if (Properties.TT && getClass().equals(IntPrimitiveStatement.class)) { if (Randomness.nextDouble() <= Properties.RANDOM_PERTURBATION) { // mutateTransformedBoolean(test); ((IntPrimitiveStatement) this).negate(); } else randomize(); } else { randomize(); } } else delta(); } return true; }
/** * 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; }
/** * {@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; }
if (p.getValue().getClass().equals(Character.class)) { char charValue = (char) value.intValue(); p.setValue(charValue); } else if (p.getValue().getClass().equals(Long.class)) { p.setValue(value); } else if (p.getValue().getClass().equals(Integer.class)) { p.setValue(value.intValue()); } else if (p.getValue().getClass().equals(Short.class)) { p.setValue(value.shortValue()); } else if (p.getValue().getClass().equals(Boolean.class)) { p.setValue(value.intValue() > 0); } else if (p.getValue().getClass().equals(Byte.class)) { p.setValue(value.byteValue()); logger.warn("New value is of an unsupported type: " + p.getValue().getClass() + val); } else if (val instanceof String) { String name = ((String) key).replace("__SYM", ""); if (p.getValue().getClass().equals(Character.class)) p.setValue((char) Integer.parseInt(val.toString())); else p.setValue(val.toString()); } else if (val instanceof Double) { Double value = (Double) val; if (p.getValue().getClass().equals(Double.class)) p.setValue(value); else if (p.getValue().getClass().equals(Float.class)) p.setValue(value.floatValue());
VariableReference retval = statement.getReturnValue(); Object value = statement.getValue();
logger.warn("Class not found - keeping old class loader ", e); super.changeClassLoader(loader);
/** * {@inheritDoc} */ @Override public String toString() { return getCode(); }
private static Class<?> getUnderlyingType(PrimitiveStatement ps) { return ps.getReturnClass(); }
public static boolean isNull(VariableReference vr, TestCase tc){ if(vr instanceof NullReference){ return true; } Statement varSource = tc.getStatement(vr.getStPosition()); if(varSource instanceof PrimitiveStatement){ //eg for String Object obj = ((PrimitiveStatement)varSource).getValue(); if(obj==null){ return true; } } return false; }
/** * Create and return a new primitive variable * * @param test * @param position * @param recursionDepth * @return * @throws ConstructionFailedException */ private VariableReference createPrimitive(TestCase test, GenericClass clazz, int position, int recursionDepth) throws ConstructionFailedException { // Special case: we cannot instantiate Class<Class<?>> if (clazz.isClass()) { if (clazz.hasWildcardOrTypeVariables()) { logger.debug("Getting generic instantiation of class"); clazz = clazz.getGenericInstantiation(); logger.debug("Chosen: " + clazz); } Type parameterType = clazz.getParameterTypes().get(0); if (!(parameterType instanceof WildcardType) && GenericTypeReflector.erase(parameterType).equals(Class.class)) { throw new ConstructionFailedException( "Cannot instantiate a class with a class"); } } Statement st = PrimitiveStatement.getRandomStatement(test, clazz, position); VariableReference ret = test.addStatement(st, position); ret.setDistance(recursionDepth); return ret; }
copy = originalStatement.clone(test); if (originalStatement instanceof PrimitiveStatement<?>) { ((PrimitiveStatement<?>)copy).delta();
@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); }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) { PrimitiveStatement<Boolean> p = (PrimitiveStatement<Boolean>) test.getTestCase().getStatement(statement); ExecutionResult oldResult = test.getLastExecutionResult(); oldValue = p.getValue(); p.setValue(!oldValue); if (!objective.hasImproved(test)) { // Restore original p.setValue(oldValue); test.setLastExecutionResult(oldResult); test.setChanged(false); return false; } else { return true; } }
@Override public void changeClassLoader(ClassLoader loader) { super.changeClassLoader(loader); GenericClass genericClass = new GenericClass(value); genericClass.changeClassLoader(loader); value = genericClass.getRawClass(); }
@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); }
@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); } }
if (p.getValue().getClass().equals(Character.class)) p.setValue((char) value.intValue()); else if (p.getValue().getClass().equals(Long.class)) p.setValue(value); else if (p.getValue().getClass().equals(Integer.class)) p.setValue(value.intValue()); else if (p.getValue().getClass().equals(Short.class)) p.setValue(value.shortValue()); else if (p.getValue().getClass().equals(Boolean.class)) p.setValue(value.intValue() > 0); else if (p.getValue().getClass().equals(Byte.class)) p.setValue(value.byteValue() > 0); else logger.warn("New value is of an unsupported type: " + p.getValue().getClass() + val); } else if (val instanceof String) { String name = ((String) key).replace("__SYM", ""); if (p.getValue().getClass().equals(Character.class)) p.setValue((char) Integer.parseInt(val.toString())); else p.setValue(val.toString()); } else if (val instanceof Double) { Double value = (Double) val; if (p.getValue().getClass().equals(Double.class)) p.setValue(value); else if (p.getValue().getClass().equals(Float.class)) p.setValue(value.floatValue()); else
VariableReference retVal = null; if (index.isPrimitive()) { PrimitiveStatement<?> primitive = PrimitiveStatement.getPrimitiveStatement(test, index.getGenericClass()); retVal = test.addStatement(primitive, position++);