use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_fconst.
@Test
public void test_fconst() throws Exception {
ProgramState programState = execute(new Instruction(Opcodes.FCONST_0));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.ZERO, BooleanConstraint.FALSE, ObjectConstraint.NOT_NULL } });
programState = execute(new Instruction(Opcodes.FCONST_1));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, BooleanConstraint.TRUE, ObjectConstraint.NOT_NULL } });
programState = execute(new Instruction(Opcodes.FCONST_2));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, ObjectConstraint.NOT_NULL } });
}
use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_pop.
@Test
public void test_pop() throws Exception {
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
ProgramState programState = execute(new Instruction(Opcodes.POP), ProgramState.EMPTY_STATE.stackValue(sv1).stackValue(sv2));
assertThat(programState.peekValue()).isEqualTo(sv1);
programState = execute(new Instruction(Opcodes.POP));
assertEmptyStack(programState);
}
use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_conversion.
@Test
public void test_conversion() throws Exception {
int[] toLongOrDouble = { Opcodes.I2D, Opcodes.I2L, Opcodes.F2D, Opcodes.F2L };
for (int opcode : toLongOrDouble) {
SymbolicValue sv = new SymbolicValue();
ProgramState initialPs = ProgramState.EMPTY_STATE.stackValue(sv);
ProgramState ps = execute(new Instruction(opcode), initialPs);
assertThat(isDoubleOrLong(ps, sv)).isTrue();
assertThatThrownBy(() -> execute(new Instruction(opcode))).hasMessage(Printer.OPCODES[opcode] + " needs value on stack");
}
int[] fromLongOrDouble = { Opcodes.D2F, Opcodes.D2I, Opcodes.L2F, Opcodes.L2I };
for (int opcode : fromLongOrDouble) {
SymbolicValue sv = new SymbolicValue();
ProgramState initialPs = ProgramState.EMPTY_STATE.stackValue(sv);
initialPs = setDoubleOrLong(initialPs, sv, true);
ProgramState ps = execute(new Instruction(opcode), initialPs);
assertThat(isDoubleOrLong(ps, sv)).isFalse();
assertThatThrownBy(() -> execute(new Instruction(opcode))).hasMessage(Printer.OPCODES[opcode] + " needs value on stack");
}
}
use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_dup_x1.
@Test
public void test_dup_x1() throws Exception {
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
SymbolicValue sv3 = new SymbolicValue();
ProgramState programState = execute(new Instruction(Opcodes.DUP_X1), ProgramState.EMPTY_STATE.stackValue(sv3).stackValue(sv2).stackValue(sv1));
ProgramState.Pop pop = programState.unstackValue(4);
assertThat(pop.values).containsExactly(sv1, sv2, sv1, sv3);
assertThat(pop.state).isEqualTo(ProgramState.EMPTY_STATE);
assertThatThrownBy(() -> execute(new Instruction(Opcodes.DUP_X1), ProgramState.EMPTY_STATE.stackValue(sv1))).hasMessage("DUP_X1 needs 2 values on stack");
}
use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_swap.
@Test
public void test_swap() throws Exception {
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
ProgramState programState = execute(new Instruction(Opcodes.SWAP), ProgramState.EMPTY_STATE.stackValue(sv1).stackValue(sv2));
ProgramState.Pop pop = programState.unstackValue(2);
assertThat(pop.values).containsExactly(sv1, sv2);
assertThat(pop.state).isEqualTo(ProgramState.EMPTY_STATE);
assertThatThrownBy(() -> execute(new Instruction(Opcodes.SWAP), ProgramState.EMPTY_STATE.stackValue(sv1))).hasMessage("SWAP needs 2 values on stack");
}
Aggregations