use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method assertStack.
private void assertStack(ProgramState ps, Constraint[]... constraints) {
ProgramState.Pop pop = ps.unstackValue(constraints.length);
assertEmptyStack(pop.state);
assertThat(pop.valuesAndSymbols).hasSize(constraints.length);
List<SymbolicValue> symbolicValues = pop.values;
int idx = 0;
for (SymbolicValue sv : symbolicValues) {
ConstraintsByDomain constraintsByDomain = ps.getConstraints(sv);
if (constraintsByDomain != null) {
constraintsByDomain = constraintsByDomain.remove(BytecodeEGWalker.StackValueCategoryConstraint.class);
}
for (Constraint expectedConstraint : constraints[idx]) {
if (expectedConstraint != null) {
Class<? extends Constraint> expectedConstraintDomain = expectedConstraint.getClass();
Constraint constraint = constraintsByDomain.get(expectedConstraintDomain);
assertThat(constraint).isEqualTo(expectedConstraint);
constraintsByDomain = constraintsByDomain.remove(expectedConstraintDomain);
} else {
assertThat(constraintsByDomain).isNull();
}
}
if (constraintsByDomain != null) {
assertThat(constraintsByDomain.isEmpty()).isTrue();
}
idx++;
}
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method assertStack.
private void assertStack(ProgramState ps, Constraint... constraints) {
Constraint[][] cs = new Constraint[constraints.length][1];
int i = 0;
for (Constraint constraint : constraints) {
cs[i] = new Constraint[] { constraint };
i++;
}
assertStack(ps, cs);
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class MethodBehaviorTest method result_with_unreducible_constraint_should_not_be_reduced.
@Test
public void result_with_unreducible_constraint_should_not_be_reduced() {
MethodBehavior mb = new MethodBehavior("foo()Ljava/lang/Object;");
addYield(mb, ObjectConstraint.NOT_NULL);
addYield(mb, ObjectConstraint.NULL);
mb.completed();
assertThat(mb.yields()).hasSize(2);
List<Constraint> resultConstraints = mb.yields().stream().map(y -> ((HappyPathYield) y).resultConstraint().get(ObjectConstraint.class)).collect(Collectors.toList());
assertThat(resultConstraints).contains(ObjectConstraint.NULL, ObjectConstraint.NOT_NULL);
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class MethodBehaviorTest method addYield.
private void addYield(MethodBehavior mb, @Nullable Constraint result, Constraint... constraints) {
HappyPathYield yield = new HappyPathYield(mb);
for (Constraint constraint : constraints) {
yield.parametersConstraints.add(ConstraintsByDomain.empty().put(constraint));
}
if (result != null) {
yield.setResult(-1, ConstraintsByDomain.empty().put(result));
}
mb.yields.add(yield);
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class MethodBehaviorTest method method_behavior_yields.
@Test
public void method_behavior_yields() {
SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/resources/se/MethodYields.java");
MethodBehavior mb = getMethodBehavior(sev, "method");
List<MethodYield> yields = mb.yields();
assertThat(yields).hasSize(3);
List<HappyPathYield> trueResults = mb.happyPathYields().filter(my -> BooleanConstraint.TRUE.equals(my.resultConstraint().get(BooleanConstraint.class))).collect(Collectors.toList());
assertThat(trueResults).hasSize(1);
HappyPathYield trueResult = trueResults.get(0);
// 'a' has constraint "null"
assertThat(((ObjectConstraint) trueResult.parametersConstraints.get(0).get(ObjectConstraint.class)).isNull()).isTrue();
// no constraint on 'b'
assertThat(((ObjectConstraint) trueResult.parametersConstraints.get(0).get(ObjectConstraint.class)).isNull()).isTrue();
// result SV is a different SV than 'a' and 'b'
assertThat(trueResult.resultIndex()).isEqualTo(-1);
List<HappyPathYield> falseResults = mb.happyPathYields().filter(my -> BooleanConstraint.FALSE.equals(my.resultConstraint().get(BooleanConstraint.class))).collect(Collectors.toList());
assertThat(falseResults).hasSize(2);
// for both "False" results, 'a' has the constraint "not null"
assertThat(falseResults.stream().filter(my -> !((ObjectConstraint) my.parametersConstraints.get(0).get(ObjectConstraint.class)).isNull()).count()).isEqualTo(2);
// 1) 'b' has constraint "false", result is 'b'
assertThat(falseResults.stream().filter(my -> BooleanConstraint.FALSE.equals(my.parametersConstraints.get(1).get(BooleanConstraint.class)) && my.resultIndex() == 1).count()).isEqualTo(1);
// 2) 'b' is "true", result is a different SV than 'a' and 'b'
assertThat(falseResults.stream().filter(my -> BooleanConstraint.TRUE.equals(my.parametersConstraints.get(1).get(BooleanConstraint.class)) && my.resultIndex() == -1).count()).isEqualTo(1);
}
Aggregations