Skip to content

Commit 17bee52

Browse files
committed
Add unit tests for expr_iterator
1 parent a817543 commit 17bee52

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ SRC += analyses/ai/ai.cpp \
5050
util/cmdline.cpp \
5151
util/expr_cast/expr_cast.cpp \
5252
util/expr.cpp \
53+
util/expr_iterator.cpp \
5354
util/file_util.cpp \
5455
util/format_number_range.cpp \
5556
util/get_base_name.cpp \

unit/util/expr_iterator.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for expr_iterator
4+
5+
Author: Diffblue Ltd
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// expr_iterator unit tests
11+
12+
#include <testing-utils/use_catch.h>
13+
14+
#include <util/expr_iterator.h>
15+
#include <util/std_expr.h>
16+
17+
SCENARIO("expr_iterator", "[core][utils][expr_iterator]")
18+
{
19+
GIVEN("An expression of depth 3")
20+
{
21+
symbol_exprt symbol("whatever", bool_typet());
22+
notequal_exprt middle1(symbol, symbol);
23+
equal_exprt middle2(symbol, symbol);
24+
implies_exprt top(middle1, middle2);
25+
26+
WHEN("Visiting the expressions with a depth iterator")
27+
{
28+
std::vector<irep_idt> ids;
29+
for(auto it = top.depth_begin(), itend = top.depth_end(); it != itend;
30+
++it)
31+
{
32+
ids.push_back(it->id());
33+
}
34+
35+
THEN("We expect to see parents before children")
36+
{
37+
REQUIRE(
38+
ids == std::vector<irep_idt>{ID_implies,
39+
ID_notequal,
40+
ID_symbol,
41+
ID_symbol,
42+
ID_equal,
43+
ID_symbol,
44+
ID_symbol});
45+
}
46+
}
47+
48+
WHEN("Replacing one of the middle nodes mid-walk")
49+
{
50+
std::vector<irep_idt> ids;
51+
for(auto it = top.depth_begin(), itend = top.depth_end(); it != itend;
52+
++it)
53+
{
54+
if(it->id() == ID_notequal)
55+
it.mutate() = not_exprt(equal_exprt(symbol, symbol));
56+
57+
ids.push_back(it->id());
58+
}
59+
60+
THEN("We expect to see the newly added child nodes")
61+
{
62+
REQUIRE(
63+
ids == std::vector<irep_idt>{ID_implies,
64+
ID_not,
65+
ID_equal,
66+
ID_symbol,
67+
ID_symbol,
68+
ID_equal,
69+
ID_symbol,
70+
ID_symbol});
71+
}
72+
}
73+
74+
WHEN(
75+
"Replacing one of the middle nodes mid-walk and skipping the new "
76+
"children")
77+
{
78+
std::vector<irep_idt> ids;
79+
for(auto it = top.depth_begin(), itend = top.depth_end(); it != itend;
80+
/* no ++it */)
81+
{
82+
bool replace_here = it->id() == ID_notequal;
83+
84+
if(replace_here)
85+
it.mutate() = not_exprt(equal_exprt(symbol, symbol));
86+
87+
ids.push_back(it->id());
88+
89+
if(replace_here)
90+
it.next_sibling_or_parent();
91+
else
92+
++it;
93+
}
94+
95+
THEN("We expect to skip the newly added child nodes")
96+
{
97+
REQUIRE(
98+
ids == std::vector<irep_idt>{
99+
ID_implies, ID_not, ID_equal, ID_symbol, ID_symbol});
100+
}
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)