Skip to content

Commit 42bd7ed

Browse files
author
thk123
committed
Adding tests for simplification of lhs
1 parent e0b4515 commit 42bd7ed

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

unit/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
SRC = unit_tests.cpp \
44
catch_example.cpp \
5+
util/simplify_expr/simplify_typecast.cpp \
6+
unit-src/unit_util.cpp \
57
# Empty last line
68

79
INCLUDES= -I ../src/ -I.
@@ -24,6 +26,7 @@ LIBS += ../src/ansi-c/ansi-c$(LIBEXT) \
2426
../src/assembler/assembler$(LIBEXT) \
2527
../src/analyses/analyses$(LIBEXT) \
2628
../src/solvers/solvers$(LIBEXT) \
29+
../src/util/util$(LIBEXT) \
2730
# Empty last line
2831

2932
TESTS = unit_tests$(EXEEXT) \

unit/unit-src/unit_util.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*******************************************************************\
2+
3+
Module: Unit Test Utility Functions
4+
5+
Author: DiffBlue Limited. All rights reserved.
6+
7+
\*******************************************************************/
8+
#include "unit_util.h"
9+
10+
std::ostream &operator<<(std::ostream &os, const irept &value)
11+
{
12+
os << value.pretty();
13+
return os;
14+
}

unit/unit-src/unit_util.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*******************************************************************\
2+
3+
Module: Unit Test Utility Functions
4+
5+
Author: DiffBlue Limited. All rights reserved.
6+
7+
\*******************************************************************/
8+
#ifndef CPROVER_UNIT_SRC_UNIT_UTIL_H
9+
#define CPROVER_UNIT_SRC_UNIT_UTIL_H
10+
11+
#include <ostream>
12+
#include <util/irep.h>
13+
14+
/// For printing irept data structures when used inside the unit tests
15+
/// (e.g. for INFO, REQUIRE etc macros
16+
/// \param os: The ostream to write to
17+
/// \param value: The irept data structure to print
18+
/// \returns The ostream after writing to it
19+
std::ostream &operator<<(std::ostream &os, const irept &value);
20+
21+
#endif // CPROVER_UNIT_SRC_UNIT_UTIL_H
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*******************************************************************\
2+
3+
Module: MODULE NAME
4+
5+
Author: DiffBlue Limited. All rights reserved.
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Unit tests for util/simplify_expr.cpp
11+
12+
#include <catch.hpp>
13+
#include <util/config.h>
14+
#include <util/namespace.h>
15+
#include <util/symbol_table.h>
16+
#include <util/simplify_expr.h>
17+
#include <util/std_expr.h>
18+
#include <util/std_types.h>
19+
#include <util/ui_message.h>
20+
21+
#include <ansi-c/ansi_c_language.h>
22+
23+
24+
#include <unit-src/unit_util.h>
25+
26+
SCENARIO("Simplifying an expression", "[core][util][simplify_expr]"
27+
"[simplify_typecast]")
28+
{
29+
ui_message_handlert message_handler;
30+
ansi_c_languaget language;
31+
language.set_message_handler(message_handler);
32+
33+
symbol_tablet symbol_table;
34+
namespacet ns(symbol_table);
35+
GIVEN("A namespace with some symbols in on a default architecture")
36+
{
37+
array_typet array_type(
38+
signedbv_typet(32), constant_exprt::integer_constant(5));
39+
40+
symbolt a_symbol;
41+
a_symbol.base_name="a";
42+
a_symbol.name="a";
43+
a_symbol.type=array_type;
44+
a_symbol.is_lvalue=true;
45+
symbol_table.add(a_symbol);
46+
47+
symbolt i_symbol;
48+
i_symbol.base_name="i";
49+
i_symbol.name="i";
50+
i_symbol.type=signedbv_typet(32);
51+
i_symbol.is_lvalue=true;
52+
symbol_table.add(i_symbol);
53+
54+
config.set_arch("none");
55+
56+
WHEN("Simplifying a[(signed long int)0]")
57+
{
58+
exprt out_expr;
59+
bool success=language.to_expr("a[(signed long int)0]", "", out_expr, ns);
60+
61+
// Validate the conversion to expr
62+
REQUIRE_FALSE(success);
63+
THEN("Should get a[0]")
64+
{
65+
exprt simplified_expr=simplify_expr(out_expr, ns);
66+
REQUIRE(simplified_expr.id()==ID_index);
67+
68+
index_exprt index=to_index_expr(simplified_expr);
69+
70+
const exprt &index_part=index.index();
71+
REQUIRE(index_part.id()==ID_constant);
72+
}
73+
}
74+
WHEN("Simplifying a[(signed long int)i]")
75+
{
76+
exprt out_expr;
77+
bool success=language.to_expr("a[(signed long int)i]", "", out_expr, ns);
78+
79+
// Validate the conversion to expr
80+
REQUIRE_FALSE(success);
81+
THEN("Should get a[i]")
82+
{
83+
exprt simplified_expr=simplify_expr(out_expr, ns);
84+
REQUIRE(simplified_expr.id()==ID_index);
85+
86+
index_exprt index=to_index_expr(simplified_expr);
87+
88+
const exprt &index_part=index.index();
89+
REQUIRE(index_part.id()==ID_symbol);
90+
const symbol_exprt symbol_expr=to_symbol_expr(index_part);
91+
REQUIRE(symbol_expr.get_identifier()=="i");
92+
}
93+
}
94+
WHEN("Simplifying a[(signed int)i]")
95+
{
96+
exprt out_expr;
97+
bool success=language.to_expr("a[(signed int)i]", "", out_expr, ns);
98+
99+
// Validate the conversion to expr
100+
REQUIRE_FALSE(success);
101+
THEN("Should get a[i]")
102+
{
103+
exprt simplified_expr=simplify_expr(out_expr, ns);
104+
REQUIRE(simplified_expr.id()==ID_index);
105+
106+
index_exprt index=to_index_expr(simplified_expr);
107+
108+
const exprt &index_part=index.index();
109+
REQUIRE(index_part.id()==ID_symbol);
110+
const symbol_exprt symbol_expr=to_symbol_expr(index_part);
111+
REQUIRE(symbol_expr.get_identifier()=="i");
112+
}
113+
}
114+
WHEN("Simplifying a[(signed short)i]")
115+
{
116+
exprt out_expr;
117+
bool success=language.to_expr("a[(signed short)i]", "", out_expr, ns);
118+
119+
// Validate the conversion to expr
120+
REQUIRE_FALSE(success);
121+
122+
// Since this is cast could go wrong (if i has a value greater than
123+
// SHRT_MAX for example) it should not be removed by the simplify
124+
THEN("Should get a[(signed short)i]")
125+
{
126+
exprt simplified_expr=simplify_expr(out_expr, ns);
127+
REQUIRE(simplified_expr.id()==ID_index);
128+
129+
index_exprt index=to_index_expr(simplified_expr);
130+
131+
const exprt &index_part=index.index();
132+
133+
// The expression will have changed as we are still able to remove
134+
// one type cast (the cast from short to long)
135+
REQUIRE(index_part.id()==ID_typecast);
136+
REQUIRE(index_part.type().id()==ID_signedbv);
137+
signedbv_typet signed_bv_type=to_signedbv_type(index_part.type());
138+
REQUIRE(signed_bv_type.get_width()==config.ansi_c.short_int_width);
139+
}
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)