Skip to content

Commit 57e6a78

Browse files
committed
Unit test of replace_symbolt
1 parent 18d08bf commit 57e6a78

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

unit/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ SRC += unit_tests.cpp \
3535
util/irep_sharing.cpp \
3636
util/message.cpp \
3737
util/optional.cpp \
38+
util/replace_symbol.cpp \
3839
util/sharing_node.cpp \
3940
util/sharing_map.cpp \
4041
util/small_map.cpp \

unit/util/replace_symbol.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*******************************************************************\
2+
3+
Module: replace_symbolt unit tests
4+
5+
Author: Michael Tautschnig
6+
7+
\*******************************************************************/
8+
9+
#include <testing-utils/catch.hpp>
10+
11+
#include <util/replace_symbol.h>
12+
#include <util/std_expr.h>
13+
14+
TEST_CASE("Replace all symbols in expression", "[core][util][replace_symbol]")
15+
{
16+
symbol_exprt s1("a", typet("some_type"));
17+
symbol_exprt s2("b", typet("some_type"));
18+
19+
exprt binary("binary", typet("some_type"));
20+
binary.copy_to_operands(s1);
21+
binary.copy_to_operands(s2);
22+
23+
array_typet array_type(typet("sub-type"), s1);
24+
REQUIRE(array_type.size() == s1);
25+
26+
exprt other_expr("other");
27+
28+
replace_symbolt r;
29+
r.insert("a", other_expr);
30+
31+
REQUIRE(r.replace(binary) == false);
32+
REQUIRE(binary.op0() == other_expr);
33+
34+
REQUIRE(r.replace(s1) == false);
35+
REQUIRE(s1 == other_expr);
36+
37+
REQUIRE(r.replace(s2) == true);
38+
REQUIRE(s2 == symbol_exprt("b", typet("some_type")));
39+
40+
REQUIRE(r.replace(array_type) == false);
41+
REQUIRE(array_type.size() == other_expr);
42+
}
43+
44+
TEST_CASE("Lvalue only", "[core][util][optional]")
45+
{
46+
symbol_exprt s1("a", typet("some_type"));
47+
array_typet array_type(typet("some_type"), s1);
48+
symbol_exprt array("b", array_type);
49+
index_exprt index(array, s1);
50+
51+
exprt binary("binary", typet("some_type"));
52+
binary.copy_to_operands(address_of_exprt(s1));
53+
binary.copy_to_operands(address_of_exprt(index));
54+
55+
constant_exprt c("some_value", typet("some_type"));
56+
57+
replace_symbolt r;
58+
r.insert("a", c);
59+
60+
REQUIRE(r.replace(binary) == false);
61+
REQUIRE(binary.op0() == address_of_exprt(s1));
62+
const index_exprt &index_expr =
63+
to_index_expr(to_address_of_expr(binary.op1()).object());
64+
REQUIRE(to_array_type(index_expr.array().type()).size() == c);
65+
REQUIRE(index_expr.index() == c);
66+
}

0 commit comments

Comments
 (0)