Skip to content

Commit 1c01ee7

Browse files
Unit test for try_evaluate_pointer_comparisions
1 parent dcc9d79 commit 1c01ee7

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ SRC += analyses/ai/ai.cpp \
3535
goto-symex/is_constant.cpp \
3636
goto-symex/symex_level0.cpp \
3737
goto-symex/symex_level1.cpp \
38+
goto-symex/try_evaluate_pointer_comparisons.cpp \
3839
interpreter/interpreter.cpp \
3940
json/json_parser.cpp \
4041
json_symbol_table.cpp \
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for try_evaluate_pointer_comparisons
4+
5+
Author: Romain Brenguier, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#include <testing-utils/message.h>
10+
#include <testing-utils/use_catch.h>
11+
12+
#include <goto-symex/goto_symex.h>
13+
#include <util/c_types.h>
14+
15+
static void add_to_symbol_table(
16+
symbol_tablet &symbol_table,
17+
const symbol_exprt &symbol_expr)
18+
{
19+
symbolt symbol;
20+
symbol.name = symbol_expr.get_identifier();
21+
symbol.type = symbol_expr.type();
22+
symbol.value = symbol_expr;
23+
symbol.is_thread_local = true;
24+
symbol_table.insert(symbol);
25+
}
26+
27+
SCENARIO(
28+
"Try to evaluate pointer comparisons",
29+
"[core][goto-symex][try_evaluate_pointer_comparisons]")
30+
{
31+
const unsignedbv_typet int_type{32};
32+
const pointer_typet ptr_type = pointer_type(int_type);
33+
const symbol_exprt ptr1{"ptr1", ptr_type};
34+
const symbol_exprt ptr2{"ptr2", ptr_type};
35+
const symbol_exprt value1{"value1", int_type};
36+
const address_of_exprt address1{value1};
37+
const symbol_exprt value2{"value2", int_type};
38+
const address_of_exprt address2{value2};
39+
const symbol_exprt b{"b", bool_typet{}};
40+
41+
// Add symbols to symbol table
42+
symbol_tablet symbol_table;
43+
namespacet ns{symbol_table};
44+
add_to_symbol_table(symbol_table, ptr1);
45+
add_to_symbol_table(symbol_table, ptr2);
46+
add_to_symbol_table(symbol_table, value1);
47+
add_to_symbol_table(symbol_table, value2);
48+
add_to_symbol_table(symbol_table, b);
49+
50+
// Initialize goto state
51+
std::list<goto_programt::instructiont> target;
52+
symex_targett::sourcet source{"fun", target.begin()};
53+
guard_managert guard_manager;
54+
std::size_t count = 0;
55+
auto fresh_name = [&count](const irep_idt &) { return count++; };
56+
goto_symex_statet state{source, guard_manager, fresh_name};
57+
58+
GIVEN("A value set in which pointer symbol `ptr1` only points to `&value1`")
59+
{
60+
value_sett value_set;
61+
const renamedt<exprt, L1> ptr1_l1 = state.rename<L1>(ptr1, ns);
62+
const renamedt<exprt, L1> address1_l1 = state.rename<L1>(address1, ns);
63+
// ptr1 <- &value1
64+
value_set.assign(ptr1_l1.get(), address1_l1.get(), ns, false, false);
65+
66+
WHEN("Evaluating ptr1 == &value1")
67+
{
68+
const equal_exprt comparison{ptr1, address1};
69+
const renamedt<exprt, L2> renamed_comparison =
70+
state.rename(comparison, ns);
71+
auto result = try_evaluate_pointer_comparisons(
72+
renamed_comparison, value_set, ID_java, ns);
73+
THEN("Evaluation succeeds")
74+
{
75+
REQUIRE(result.get() == true_exprt{});
76+
}
77+
}
78+
79+
WHEN("Evaluating ptr1 != &value1")
80+
{
81+
const notequal_exprt comparison{ptr1, address1};
82+
const renamedt<exprt, L2> renamed_comparison =
83+
state.rename(comparison, ns);
84+
auto result = try_evaluate_pointer_comparisons(
85+
renamed_comparison, value_set, ID_java, ns);
86+
THEN("Evaluation succeeds")
87+
{
88+
REQUIRE(result.get() == false_exprt{});
89+
}
90+
}
91+
92+
WHEN("Evaluating ptr1 == ptr2")
93+
{
94+
const equal_exprt comparison{ptr1, ptr2};
95+
const renamedt<exprt, L2> renamed_comparison =
96+
state.rename(comparison, ns);
97+
auto result = try_evaluate_pointer_comparisons(
98+
renamed_comparison, value_set, ID_java, ns);
99+
THEN("Evaluation leaves the expression unchanged")
100+
{
101+
REQUIRE(result.get() == renamed_comparison.get());
102+
}
103+
}
104+
}
105+
106+
GIVEN(
107+
"A value set in which pointer symbol `ptr1` can point to `&value1` or "
108+
"`&value2`")
109+
{
110+
value_sett value_set;
111+
const if_exprt if_expr{b, address1, address2};
112+
const renamedt<exprt, L1> ptr1_l1 = state.rename<L1>(ptr1, ns);
113+
const renamedt<exprt, L1> if_expr_l1 = state.rename<L1>(if_expr, ns);
114+
// ptr1 <- b ? &value1 : &value2
115+
value_set.assign(ptr1_l1.get(), if_expr_l1.get(), ns, false, false);
116+
117+
WHEN("Evaluating ptr1 == &value1")
118+
{
119+
const equal_exprt comparison{ptr1, address1};
120+
const renamedt<exprt, L2> renamed_comparison =
121+
state.rename(comparison, ns);
122+
auto result = try_evaluate_pointer_comparisons(
123+
renamed_comparison, value_set, ID_java, ns);
124+
THEN("Evaluation leaves the expression unchanged")
125+
{
126+
REQUIRE(result.get() == renamed_comparison.get());
127+
}
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)