Skip to content

Commit 8e61d1c

Browse files
committed
Add regression tests for the right shifting behaviour of the new SMT backend
1 parent 6a3773c commit 8e61d1c

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdint.h>
2+
3+
// In C, whether a right-shift is arithmetic or logical depends on the
4+
// original type being shifted. An unsigned value will be shifted to
5+
// the right in a logical manner (this assigns `0` to the leftmost bit).
6+
// If the type is signed, right shift will assign the sign bit to the
7+
// leftmost digit.
8+
9+
int main()
10+
{
11+
int first;
12+
uint8_t second;
13+
14+
int place;
15+
__CPROVER_assume(place >= 1);
16+
17+
int result_signed = first >> place;
18+
uint8_t result_unsigned = second >> place;
19+
20+
// This assertion captures the intend of the expected behaviour of
21+
// bit-shifting an unsigned int (logical shift)
22+
__CPROVER_assert(
23+
result_unsigned != 64,
24+
"Right shifting a uint with leftmost bit set is a logical shift");
25+
// The following assertions capture the expected behaviour of
26+
// a right logical (in the case of a signed positive int) and
27+
// arithmetic shift (in the case of a signed negative int).
28+
if(first >= 0)
29+
{
30+
__CPROVER_assert(
31+
result_signed >= 0,
32+
"Right shifting a positive number has a lower bound of 0");
33+
}
34+
else
35+
{
36+
__CPROVER_assert(
37+
result_signed <= -1,
38+
"Right shifting a negative number has a lower bound value of -1");
39+
}
40+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
shift_right.c
3+
--incremental-smt2-solver 'z3 --smt2 -in' --slice-formula --trace
4+
\[main\.assertion\.1\] line \d+ Right shifting a uint with leftmost bit set is a logical shift: FAILURE
5+
\[main\.assertion\.2\] line \d+ Right shifting a positive number has a lower bound of 0: SUCCESS
6+
\[main\.assertion\.3\] line \d+ Right shifting a negative number has a lower bound value of -1: SUCCESS
7+
result_unsigned=64
8+
^EXIT=10$
9+
^SIGNAL=0$
10+
--
11+
--

0 commit comments

Comments
 (0)