public VariableReference appendStringPrimitive(String string) { StringPrimitiveStatement primitiveStmt = new StringPrimitiveStatement( tc, string); tc.addStatement(primitiveStmt); return primitiveStmt.getReturnValue(); }
randomize(); return; if (Randomness.nextDouble() < P) { s = removeCharAt(s, i - 1); if (Randomness.nextDouble() < P) { s = replaceCharAt(s, i, Randomness.nextChar()); if (s.length() > 0) pos = Randomness.nextInt(s.length()); s = StringInsert(s, pos);
String oldValue = p.getValue(); p.increment(); else p.randomize(); logger.info("Probing string " + oldValue + " ->" + p.getCode()); int result = objective.hasChanged(test); if (result < 0) { logger.info("Applying local search to string " + p.getCode()); logger.info("Statement: " + p.getCode()); if (replaceCharacters(objective, test, p, statement)) hasImproved = true; logger.info("Statement: " + p.getCode()); if (addCharacters(objective, test, p, statement)) hasImproved = true; logger.info("Statement: " + p.getCode()); logger.info("Resulting string: " + p.getValue()); return hasImproved;
oldValue = p.getValue(); ExecutionResult oldResult = test.getLastExecutionResult(); logger.debug("Trying increment " + delta + " of " + p.getCode()); char oldChar = oldValue.charAt(character); logger.info(" -> Character " + character + ": " + oldChar); characters[character] = replacement; String newString = new String(characters); p.setValue(newString); oldValue = p.getValue(); oldResult = test.getLastExecutionResult(); improvement = true; characters[character] = replacement; newString = new String(characters); p.setValue(newString); logger.debug("No improvement on " + p.getCode()); p.setValue(oldValue); test.setLastExecutionResult(oldResult); test.setChanged(false); logger.debug("Final value of this iteration: " + p.getValue());
/** * <p> * increment * </p> */ public void increment() { String s = value; if(s == null) { randomize(); return; } else if (s.isEmpty()) { s += Randomness.nextChar(); } else { s = replaceCharAt(s, Randomness.nextInt(s.length()), Randomness.nextChar()); } value = s; }
public void constructPopulation(){ Properties.POPULATION = 16; TestSuiteChromosome[] testSuiteChromObjects = new TestSuiteChromosome[Properties.POPULATION]; for(int i=0; i<testSuiteChromObjects.length; i++){ testSuiteChromObjects[i] = new TestSuiteChromosome(); DefaultTestCase testcase = new DefaultTestCase(); StringPrimitiveStatement stringStatement = new StringPrimitiveStatement(testcase, "any string statatement for TS"+i); testcase.addStatement(stringStatement); testSuiteChromObjects[i].addTest(testcase); population.add(testSuiteChromObjects[i]); } }
private void after(StringPrimitiveStatement statement, Scope scope) { String valueOf = statement.getValue(); VariableReference varRef = statement.getReturnValue(); String varRefName = varRef.getName(); StringVariable stringVariable = buildStringVariable(varRefName, valueOf); symb_expressions.put(varRefName, stringVariable); String string_instance; try { String string_interned = (String) varRef.getObject(scope); string_instance = new String(string_interned); scope.setObject(varRef, string_instance); } catch (CodeUnderTestException e) { throw new EvosuiteError(e); } ReferenceConstant stringRef = newStringReference(string_instance, stringVariable); symb_references.put(varRefName, stringRef); }
private void backup(TestChromosome test, StringPrimitiveStatement p) { oldValue = new String(p.getValue()); oldResult = test.getLastExecutionResult(); oldChanged = test.isChanged(); }
private String StringInsert(String s, int pos) { final double ALPHA = 0.5; int count = 1; while (Randomness.nextDouble() <= Math.pow(ALPHA, count) && s.length() < Properties.STRING_LENGTH) { count++; // logger.info("Before insert: '"+s+"'"); s = insertCharAt(s, pos, Randomness.nextChar()); // logger.info("After insert: '"+s+"'"); } return s; }
DefaultTestCase test = new DefaultTestCase(); StringPrimitiveStatement stmt0 = new StringPrimitiveStatement(test, className); VariableReference string0 = test.addStatement(stmt0); try {
logger.debug("Trying increment of " + p.getCode()); characters[i] = replacement; String newString = new String(characters); p.setValue(newString); logger.info(" " + i + " " + oldValue + "/" + oldValue.length() + " -> " + newString + "/" + newString.length()); oldValue = p.getValue(); oldResult = test.getLastExecutionResult(); p.setValue(oldValue); test.setLastExecutionResult(oldResult); test.setChanged(false); logger.debug("Trying decrement of " + p.getCode()); replacement -= 2; characters[i] = replacement; newString = new String(characters); p.setValue(newString); logger.info(" " + i + " " + oldValue + "/" + oldValue.length() + " -> " + newString + "/" + newString.length()); done = false; iterate(-2, objective, test, p, i, statement); oldValue = p.getValue(); oldResult = test.getLastExecutionResult(); p.setValue(oldValue);
private void backup(TestChromosome test, StringPrimitiveStatement p) { oldValue = new String(p.getValue()); oldResult = test.getLastExecutionResult(); oldChanged = test.isChanged(); }
statement = new BytePrimitiveStatement(tc); } else if (clazz.equals(String.class)) { statement = new StringPrimitiveStatement(tc); } else if (GenericTypeReflector.erase(clazz).isEnum()) { statement = new EnumPrimitiveStatement(tc, GenericTypeReflector.erase(clazz));
String oldValue = p.getValue(); for (int i = 0; i < Properties.LOCAL_SEARCH_PROBES; i++) { if (Randomness.nextDouble() > 0.5) p.increment(); else p.randomize(); logger.info("Probing string " + oldValue + " ->" + p.getCode()); int result = objective.hasChanged(test); if (result < 0) { logger.info("Applying local search to string " + p.getCode()); logger.info("Statement: " + p.getCode()); if (replaceCharacters(objective, test, p, statement)) hasImproved = true; logger.info("Statement: " + p.getCode()); if (addCharacters(objective, test, p, statement)) hasImproved = true; logger.info("Statement: " + p.getCode()); logger.info("Resulting string: " + p.getValue()); return hasImproved;
@Test public void testStringQuoting() throws NoSuchMethodException, SecurityException { DefaultTestCase test = new DefaultTestCase(); ConstructorStatement cs = new ConstructorStatement(test, new GenericConstructor(StringConstantInliningExample.class.getConstructor(), StringConstantInliningExample.class), new ArrayList<VariableReference>()); VariableReference objectVar = test.addStatement(cs); StringPrimitiveStatement stringStatement = new StringPrimitiveStatement(test, "EXAMPLE"); VariableReference stringParam = test.addStatement(stringStatement); List<VariableReference> parameters = new ArrayList<VariableReference>(); parameters.add(stringParam); test.addStatement(new MethodStatement(test, new GenericMethod(StringConstantInliningExample.class.getMethods()[0], StringConstantInliningExample.class), objectVar, parameters)); System.out.println(test.toCode()); ConstantInliner inliner = new ConstantInliner(); inliner.inline(test); String code = test.toCode(); System.out.println(code); assertFalse(code.contains("foo(EXAMPLE)")); assertTrue(code.contains("foo(\"EXAMPLE\")")); }
@Test public void testStringEndingWithClass() throws NoSuchMethodException, SecurityException { DefaultTestCase test = new DefaultTestCase(); ConstructorStatement cs = new ConstructorStatement(test, new GenericConstructor(StringConstantInliningExample.class.getConstructor(), StringConstantInliningExample.class), new ArrayList<VariableReference>()); VariableReference objectVar = test.addStatement(cs); StringPrimitiveStatement stringStatement = new StringPrimitiveStatement(test, "test.class"); VariableReference stringParam = test.addStatement(stringStatement); List<VariableReference> parameters = new ArrayList<VariableReference>(); parameters.add(stringParam); test.addStatement(new MethodStatement(test, new GenericMethod(StringConstantInliningExample.class.getMethods()[0], StringConstantInliningExample.class), objectVar, parameters)); System.out.println(test.toCode()); ConstantInliner inliner = new ConstantInliner(); inliner.inline(test); String code = test.toCode(); System.out.println(code); assertFalse(code.contains("foo(test.class)")); assertTrue(code.contains("foo(\"test.class\")")); }
private static DefaultTestCase buildLoadTargetClassTestCase(String className) throws EvosuiteError { DefaultTestCase test = new DefaultTestCase(); StringPrimitiveStatement stmt0 = new StringPrimitiveStatement(test, className); VariableReference string0 = test.addStatement(stmt0); try { Method currentThreadMethod = Thread.class.getMethod("currentThread"); Statement currentThreadStmt = new MethodStatement(test, new GenericMethod(currentThreadMethod, currentThreadMethod.getDeclaringClass()), null, Collections.emptyList()); VariableReference currentThreadVar = test.addStatement(currentThreadStmt); Method getContextClassLoaderMethod = Thread.class.getMethod("getContextClassLoader"); Statement getContextClassLoaderStmt = new MethodStatement(test, new GenericMethod(getContextClassLoaderMethod, getContextClassLoaderMethod.getDeclaringClass()), currentThreadVar, Collections.emptyList()); VariableReference contextClassLoaderVar = test.addStatement(getContextClassLoaderStmt); Method loadClassMethod = ClassLoader.class.getMethod("loadClass", String.class); Statement loadClassStmt = new MethodStatement(test, new GenericMethod(loadClassMethod, loadClassMethod.getDeclaringClass()), contextClassLoaderVar, Collections.singletonList(string0)); test.addStatement(loadClassStmt); return test; } catch (NoSuchMethodException | SecurityException e) { throw new EvosuiteError("Unexpected exception while creating Class Initializer Test Case"); } }
@Test public void testSame(){ TestCase tc = new DefaultTestCase(); PrimitiveStatement<?> aInt = new IntPrimitiveStatement(tc,42); Assert.assertTrue(aInt.same(aInt)); Assert.assertFalse(aInt.same(null)); PrimitiveStatement<?> fooString = new StringPrimitiveStatement(tc,"foo"); Assert.assertFalse(aInt.same(fooString)); PrimitiveStatement<?> nullString = new StringPrimitiveStatement(tc,null); Assert.assertFalse(nullString.same(fooString)); Assert.assertFalse(fooString.same(nullString)); //TODO: how to make it work? //PrimitiveStatement<?> anotherNullString = new StringPrimitiveStatement(tc,null); //Assert.assertTrue(nullString.same(anotherNullString)); //Assert.assertTrue(anotherNullString.same(nullString)); } }
@Test public void testNoNullInputs_notNull() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("initServlet",Servlet.class), EvoServletState.class), 1, 0); StringPrimitiveStatement foo = new StringPrimitiveStatement(tc.getTestCase(), "foo"); tc.getTestCase().addStatement(foo); VariableReference con = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getConfiguration"), EvoServletState.class), 3, 0); factory.addMethodFor(tc.getTestCase(), con, new GenericMethod(EvoServletConfig.class.getDeclaredMethod("createDispatcher", String.class), EvoServletConfig.class), 4); Assert.assertEquals(5, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }
VariableReference stringVar = test.addStatement(new StringPrimitiveStatement(test, "baz5"));