/** * The runtimePrimitivesSeen set contains primitive values seen during generation/execution and is * used to determine new values that should be added to the component set. The component set * initially contains a set of primitive sequences; this method puts those primitives in this set. */ private void initializeRuntimePrimitivesSeen() { Tracer.trace("initializeRuntimePrimitivesSeen"); for (Sequence s : componentManager.getAllPrimitiveSequences()) { ExecutableSequence es = new ExecutableSequence(s); es.execute(null); NormalExecution e = (NormalExecution) es.getResult(0); Object runtimeValue = e.getRuntimeValue(); runtimePrimitivesSeen.add(runtimeValue); } }
private void checkBinary(ExecutableSequence s, ObjectContract c, Set<Integer> values, int idx) { for (Integer i : values) { for (Integer j : values) { ExecutionOutcome result1 = s.getResult(i); assert result1 instanceof NormalExecution : s; ExecutionOutcome result2 = s.getResult(j); assert result2 instanceof NormalExecution : s; if (Log.isLoggingOn()) Log.logLine("Checking contract " + c.getClass() + " on " + i + ", " + j); ExecutionOutcome exprOutcome = ObjectContractUtils.execute( c, ((NormalExecution) result1).getRuntimeValue(), ((NormalExecution) result2).getRuntimeValue()); Check obs = null; if (exprOutcome instanceof NormalExecution) { NormalExecution e = (NormalExecution) exprOutcome; if (e.getRuntimeValue().equals(true)) { if (Log.isLoggingOn()) Log.logLine("Contract returned true."); continue; // Behavior ok. } else { if (Log.isLoggingOn()) Log.logLine("Contract returned false. Will add ExpressionEqFalse check"); // Create an check that records the actual value // returned by the expression, marking it as invalid // behavior. obs = new ObjectCheck(c, i, s.sequence.getVariable(i), s.sequence.getVariable(j)); s.addCheck(idx, obs, false); } } else { if (Log.isLoggingOn()) Log.logLine("Contract threw exception."); // Execution of contract resulted in exception. Do not create // a contract-violation decoration. assert exprOutcome instanceof ExceptionalExecution; } } } }
private void checkUnary(ExecutableSequence s, ObjectContract c, Set<Integer> values, int idx) { for (Integer i : values) { ExecutionOutcome result = s.getResult(i); assert result instanceof NormalExecution : s; ExecutionOutcome exprOutcome = ObjectContractUtils.execute(c, ((NormalExecution) result).getRuntimeValue()); if (exprOutcome instanceof NormalExecution) { NormalExecution e = (NormalExecution) exprOutcome; if (e.getRuntimeValue().equals(true)) { continue; // Behavior ok. } } else { // Execution of contract resulted in exception. Do not create // a contract-violation decoration. assert exprOutcome instanceof ExceptionalExecution; ExceptionalExecution e = (ExceptionalExecution) exprOutcome; if (e.getException().equals(BugInRandoopException.class)) { throw new BugInRandoopException(e.getException()); } if (!c.evalExceptionMeansFailure()) { // Exception thrown, but not considered a failure. // Will not record behavior. continue; } } // If we get here, either the contract returned false or resulted // in an exception that is considered a failure. Add // a contract violation check. // Create an check that records the actual value // returned by the expression, marking it as invalid // behavior. Check obs = new ObjectCheck(c, i, s.sequence.getVariable(i)); s.addCheck(idx, obs, false); } }
/** * Determines what indices in the given sequence are active. An active index i means that the i-th * method call creates an interesting/useful value that can be used as an input to a larger * sequence; inactive indices are never used as inputs. The effect of setting active/inactive * indices is that the SequenceCollection to which the given sequences is added only considers the * active indices when deciding whether the sequence creates values of a given type. * * <p>In addition to determining active indices, this method determines if any primitive values * created during execution of the sequence are new values not encountered before. Such values are * added to the component manager so they can be used during subsequent generation attempts. */ public void processSequence(ExecutableSequence seq) { Tracer.trace("processSequence"); if (GenInputsAbstract.offline) { Tracer.trace("offline@processSequence"); if (Log.isLoggingOn()) { Log.logLine( "Making all indices active (offline generation specified; sequences are not executed)."); } seq.sequence.setAllActiveFlags(); return; } Tracer.trace("NOT offline@processSequence"); if (seq.hasNonExecutedStatements()) { if (Log.isLoggingOn()) { Log.logLine( "Making all indices inactive (sequence has non-executed statements, so judging it inadequate for further extension)."); } seq.sequence.clearAllActiveFlags(); return; } if (seq.hasFailure()) { if (Log.isLoggingOn()) { Log.logLine( "Making all indices inactive (sequence reveals a failure, so judging it inadequate for further extension)"); } seq.sequence.clearAllActiveFlags(); return; } if (!seq.isNormalExecution()) { if (Log.isLoggingOn()) { Log.logLine( "Making all indices inactive (exception thrown, or failure revealed during execution)."); } seq.sequence.clearAllActiveFlags(); return; } // If runtime value is a primitive value, clear active flag, and // if the value is new, add a sequence corresponding to that value. for (int i = 0; i < seq.sequence.size(); i++) { // type ensured by isNormalExecution clause ealier in this method. NormalExecution e = (NormalExecution) seq.getResult(i); Object runtimeValue = e.getRuntimeValue(); if (runtimeValue == null) { if (Log.isLoggingOn()) { Log.logLine("Making index " + i + " inactive (value is null)"); } seq.sequence.clearActiveFlag(i); continue; } Class<?> objectClass = runtimeValue.getClass(); Tracer.trace("processSequence-useobjcache"); if (PrimitiveTypes.isBoxedOrPrimitiveOrStringType(objectClass)) { if (Log.isLoggingOn()) { Log.logLine("Making index " + i + " inactive (value is a primitive)"); } seq.sequence.clearActiveFlag(i); boolean looksLikeObjToString = (runtimeValue instanceof String) && PrimitiveTypes.looksLikeObjectToString((String) runtimeValue); boolean tooLongString = (runtimeValue instanceof String) && !PrimitiveTypes.stringLengthOK((String) runtimeValue); if (!looksLikeObjToString && !tooLongString && runtimePrimitivesSeen.add(runtimeValue)) { // Have not seen this value before; add it to the component set. componentManager.addGeneratedSequence( PrimitiveOrStringOrNullDecl.sequenceForPrimitive(runtimeValue)); } } else if (GenInputsAbstract.use_object_cache) { Tracer.trace("use_object_cache@processSequence-useobjcache"); objectCache.setActiveFlags(seq, i); } else { if (Log.isLoggingOn()) { Log.logLine("Making index " + i + " active."); } } } }