UnaryNumericProgramPoint implements ProgramPoint { @Override public Optional<ProgramState> execute(ProgramState state) { ExpressionStack stack = state.getStack(); ExpressionStack stackAfterExecution = stack.apply(newStack -> { SymbolicValue operandValue = newStack.pop(); Constraint operandConstraint = state.getConstraint(operandValue); execute(operandValue, operandConstraint); newStack.push(expressionValue); }); ProgramState newPS = state.withStack(stackAfterExecution); newPS = executeAssignment(newPS); return Optional.of(newPS); } UnaryNumericProgramPoint(Tree element, SymbolicExecution execution); @Override Optional<ProgramState> execute(ProgramState state); static boolean originatesFrom(Tree element);  }
@Test public void unary_plus() throws Exception { UnaryExpressionTree tree = tree("+x", Kind.UNARY_PLUS); execute(tree, Constraint.ANY_VALUE); assertThat(operandValueAfter).isEqualTo(operandValueBefore); assertThat(expressionValue).isInstanceOf(SymbolicValueWithConstraint.class); assertThat(expressionValue.baseConstraint(ProgramState.emptyState())).isEqualTo(Constraint.NUMBER_PRIMITIVE); execute(tree, Constraint.NUMBER_OBJECT); assertThat(operandValueAfter).isEqualTo(operandValueBefore); assertThat(expressionValue).isInstanceOf(SymbolicValueWithConstraint.class); assertThat(expressionValue.baseConstraint(ProgramState.emptyState())).isEqualTo(Constraint.NUMBER_PRIMITIVE); execute(tree, Constraint.POSITIVE_NUMBER_PRIMITIVE); assertThat(operandValueAfter).isEqualTo(operandValueBefore); assertThat(expressionValue).isEqualTo(operandValueBefore); execute(tree, Constraint.NEGATIVE_NUMBER_PRIMITIVE); assertThat(operandValueAfter).isEqualTo(operandValueBefore); assertThat(expressionValue).isEqualTo(operandValueBefore); execute(tree, Constraint.ZERO); assertThat(operandValueAfter).isEqualTo(operandValueBefore); assertThat(expressionValue).isEqualTo(operandValueBefore); execute(tree, Constraint.NAN); assertThat(operandValueAfter).isEqualTo(operandValueBefore); assertThat(expressionValue).isEqualTo(operandValueBefore); }
@Test public void unary_minus() throws Exception { UnaryExpressionTree tree = tree("-x", Kind.UNARY_MINUS); execute(tree, Constraint.ANY_VALUE); assertThat(operandValueAfter).isEqualTo(operandValueBefore); assertThat(expressionValue).isInstanceOf(UnaryMinusSymbolicValue.class); execute(tree, Constraint.POSITIVE_NUMBER_PRIMITIVE); assertThat(operandValueAfter).isEqualTo(operandValueBefore); assertThat(expressionValue).isInstanceOf(UnaryMinusSymbolicValue.class); }
@Test public void prefix_increment() throws Exception { UnaryExpressionTree tree = tree("++x", Kind.PREFIX_INCREMENT); execute(tree, Constraint.ANY_VALUE); assertThat(operandValueAfter).isEqualTo(expressionValue); assertThat(expressionValue).isInstanceOf(IncDecSymbolicValue.class); assertThat(((IncDecSymbolicValue) expressionValue).sign()).isEqualTo(Sign.PLUS); execute(tree, Constraint.POSITIVE_NUMBER_PRIMITIVE); assertThat(operandValueAfter).isEqualTo(expressionValue); assertThat(expressionValue).isInstanceOf(IncDecSymbolicValue.class); assertThat(((IncDecSymbolicValue) expressionValue).sign()).isEqualTo(Sign.PLUS); }
@Test public void prefix_decrement() throws Exception { UnaryExpressionTree tree = tree("--x", Kind.PREFIX_DECREMENT); execute(tree, Constraint.ANY_VALUE); assertThat(operandValueAfter).isEqualTo(expressionValue); assertThat(expressionValue).isInstanceOf(IncDecSymbolicValue.class); assertThat(((IncDecSymbolicValue) expressionValue).sign()).isEqualTo(Sign.MINUS); execute(tree, Constraint.POSITIVE_NUMBER_PRIMITIVE); assertThat(operandValueAfter).isEqualTo(expressionValue); assertThat(expressionValue).isInstanceOf(IncDecSymbolicValue.class); assertThat(((IncDecSymbolicValue) expressionValue).sign()).isEqualTo(Sign.MINUS); }
@Test public void postfix_increment() throws Exception { UnaryExpressionTree tree = tree("x++", Kind.POSTFIX_INCREMENT); execute(tree, Constraint.ANY_VALUE); assertThat(expressionValue).isInstanceOf(SymbolicValueWithConstraint.class); assertThat(expressionValue.baseConstraint(ProgramState.emptyState())).isEqualTo(Constraint.NUMBER_PRIMITIVE); assertThat(operandValueAfter).isInstanceOf(IncDecSymbolicValue.class); assertThat(((IncDecSymbolicValue) operandValueAfter).sign()).isEqualTo(Sign.PLUS); execute(tree, Constraint.POSITIVE_NUMBER_PRIMITIVE); assertThat(expressionValue).isEqualTo(operandValueBefore); assertThat(operandValueAfter).isInstanceOf(IncDecSymbolicValue.class); assertThat(((IncDecSymbolicValue) operandValueAfter).sign()).isEqualTo(Sign.PLUS); }
@Test public void postfix_decrement() throws Exception { UnaryExpressionTree tree = tree("x--", Kind.POSTFIX_DECREMENT); execute(tree, Constraint.ANY_VALUE); assertThat(expressionValue).isInstanceOf(SymbolicValueWithConstraint.class); assertThat(expressionValue.baseConstraint(ProgramState.emptyState())).isEqualTo(Constraint.NUMBER_PRIMITIVE); assertThat(operandValueAfter).isInstanceOf(IncDecSymbolicValue.class); assertThat(((IncDecSymbolicValue) operandValueAfter).sign()).isEqualTo(Sign.MINUS); execute(tree, Constraint.POSITIVE_NUMBER_PRIMITIVE); assertThat(expressionValue).isEqualTo(operandValueBefore); assertThat(operandValueAfter).isInstanceOf(IncDecSymbolicValue.class); assertThat(((IncDecSymbolicValue) operandValueAfter).sign()).isEqualTo(Sign.MINUS); }