use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_pop2_long_double.
@Test
public void test_pop2_long_double() throws Exception {
SymbolicValue normalSv = new SymbolicValue();
SymbolicValue longSv = new SymbolicValue();
ProgramState startingState = ProgramState.EMPTY_STATE.stackValue(normalSv).stackValue(longSv);
startingState = setDoubleOrLong(startingState, longSv, true);
ProgramState programState = execute(new Instruction(Opcodes.POP2), startingState);
assertThat(programState.peekValue()).isEqualTo(normalSv);
}
use of org.sonar.java.se.ProgramState 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.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_getstatic.
@Test
public void test_getstatic() throws Exception {
ProgramState programState = execute(new Instruction(Opcodes.GETSTATIC, new Instruction.FieldOrMethod("", "", "D", false)));
assertThat(programState.peekValue()).isNotNull();
assertThat(isDoubleOrLong(programState, programState.peekValue())).isTrue();
programState = execute(new Instruction(Opcodes.GETSTATIC, new Instruction.FieldOrMethod("", "", "I", false)));
assertThat(programState.peekValue()).isNotNull();
assertThat(isDoubleOrLong(programState, programState.peekValue())).isFalse();
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_return.
@Test
public void test_return() throws Exception {
ProgramState programState = execute(new Instruction(Opcodes.RETURN), ProgramState.EMPTY_STATE);
assertThat(programState.peekValue()).isNull();
assertThat(programState.exitValue()).isNull();
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_compare_with_zero.
@Test
public void test_compare_with_zero() {
SymbolicValue sv = new SymbolicValue();
int[] opcodes = { Opcodes.IFEQ, Opcodes.IFNE, Opcodes.IFLT, Opcodes.IFGE };
for (int opcode : opcodes) {
ProgramState programState = walker.branchingState(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(sv));
RelationalSymbolicValue relSV = (RelationalSymbolicValue) programState.peekValue();
assertThat(relSV.getLeftOp()).isSameAs(sv);
assertThat(relSV.getRightOp()).isNotSameAs(sv);
assertThat(programState.getConstraints(relSV.getRightOp()).hasConstraint(DivisionByZeroCheck.ZeroConstraint.ZERO)).isTrue();
}
// these opcodes inverse operator and swap operands
int[] swapOperandsOpcodes = { Opcodes.IFLE, Opcodes.IFGT };
for (int opcode : swapOperandsOpcodes) {
ProgramState programState = walker.branchingState(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(sv));
RelationalSymbolicValue relSV = (RelationalSymbolicValue) programState.peekValue();
assertThat(relSV.getRightOp()).isSameAs(sv);
assertThat(relSV.getLeftOp()).isNotSameAs(sv);
assertThat(programState.getConstraints(relSV.getLeftOp()).hasConstraint(DivisionByZeroCheck.ZeroConstraint.ZERO)).isTrue();
}
}
Aggregations