Skip to content

Commit 51f1e45

Browse files
committed
Add unit tests for expr2c for rol/ror
Adds unit test for expr2c of rol and ror operators, testing both signed and unisgned conversions.
1 parent 7431025 commit 51f1e45

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ SRC += analyses/ai/ai.cpp \
3636
analyses/variable-sensitivity/value_set_abstract_object/merge.cpp \
3737
analyses/variable-sensitivity/value_set_abstract_object/widening_merge.cpp \
3838
analyses/variable-sensitivity/variable_sensitivity_test_helpers.cpp \
39+
ansi-c/expr2c.cpp \
3940
ansi-c/max_malloc_size.cpp \
4041
ansi-c/type2name.cpp \
4142
big-int/big-int.cpp \

unit/ansi-c/expr2c.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test for expr2c
4+
5+
Author: Diffblue
6+
7+
\*******************************************************************/
8+
9+
#include <ansi-c/expr2c.h>
10+
#include <util/arith_tools.h>
11+
#include <util/bitvector_types.h>
12+
#include <util/bitvector_expr.h>
13+
#include <util/namespace.h>
14+
#include <util/symbol_table.h>
15+
#include <testing-utils/use_catch.h>
16+
#include <util/config.h>
17+
18+
TEST_CASE(
19+
"rol_2_c_conversion_unsigned",
20+
"[core][ansi-c][expr2c]")
21+
{
22+
auto lhs = from_integer(31, unsignedbv_typet(32));
23+
auto rhs = from_integer(3, unsignedbv_typet(32));
24+
auto rol = shift_exprt(lhs, ID_rol, rhs);
25+
CHECK(expr2c(rol, namespacet{symbol_tablet{}}) ==
26+
"31 << 3 % 50 | 31 >> 50 - 3 % 50");
27+
}
28+
29+
TEST_CASE(
30+
"rol_2_c_conversion_signed",
31+
"[core][ansi-c][expr2c][convert_with_precedence]")
32+
{
33+
config.ansi_c.mode = configt::ansi_ct::flavourt::GCC;
34+
config.ansi_c.set_arch_spec_i386();
35+
auto lhs = from_integer(31, signedbv_typet(8));
36+
auto rhs = from_integer(3, signedbv_typet(8));
37+
auto rol = shift_exprt(lhs, ID_rol, rhs);
38+
CHECK(expr2c(rol, namespacet{symbol_tablet{}}) ==
39+
"(unsigned char)31 << 3 % 8 | (unsigned char)31 >> 8 - 3 % 8");
40+
}
41+
42+
TEST_CASE(
43+
"ror_2_c_conversion_unsigned",
44+
"[core][ansi-c][expr2c][convert_with_precedence]")
45+
{
46+
auto lhs = from_integer(31, unsignedbv_typet(32));
47+
auto rhs = from_integer(3, unsignedbv_typet(32));
48+
auto ror = shift_exprt(lhs, ID_ror, rhs);
49+
CHECK(expr2c(ror, namespacet{symbol_tablet{}}) ==
50+
"31 >> 3 % 50 | 31 << 50 - 3 % 50");
51+
}
52+
53+
TEST_CASE(
54+
"ror_2_c_conversion_signed",
55+
"[core][ansi-c][expr2c][convert_with_precedence]")
56+
{
57+
config.ansi_c.mode = configt::ansi_ct::flavourt::GCC;
58+
config.ansi_c.set_arch_spec_i386();
59+
auto lhs = from_integer(31, integer_bitvector_typet(ID_signedbv, 32));
60+
auto rhs = from_integer(3, integer_bitvector_typet(ID_signedbv, 32));
61+
auto ror = shift_exprt(lhs, ID_ror, rhs);
62+
CHECK(expr2c(ror, namespacet{symbol_tablet{}}) ==
63+
"(unsigned int)31 >> 3 % 50 | (unsigned int)31 << 50 - 3 % 50");
64+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
testing-utils
22
ansi-c
3+
util

0 commit comments

Comments
 (0)