Skip to content

Commit 375d583

Browse files
committed
Add unit tests for expr_iterator
1 parent 2c63234 commit 375d583

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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",
18+
"[core][utils][expr_iterator]")
19+
{
20+
GIVEN("An expression of depth 3")
21+
{
22+
symbol_exprt symbol("whatever", bool_typet());
23+
notequal_exprt middle1(symbol, symbol);
24+
equal_exprt middle2(symbol, symbol);
25+
implies_exprt top(middle1, middle2);
26+
27+
WHEN("Visiting the expressions with a depth iterator")
28+
{
29+
std::vector<irep_idt> ids;
30+
for(auto it = top.depth_begin(), itend = top.depth_end();
31+
it != itend;
32+
++it)
33+
{
34+
ids.push_back(it->id());
35+
}
36+
37+
THEN("We expect to see parents before children")
38+
{
39+
REQUIRE(
40+
ids ==
41+
std::vector<irep_idt>{
42+
ID_implies, ID_notequal, ID_symbol, ID_symbol, ID_equal, ID_symbol,
43+
ID_symbol});
44+
}
45+
}
46+
47+
WHEN("Replacing one of the middle nodes mid-walk")
48+
{
49+
std::vector<irep_idt> ids;
50+
for(auto it = top.depth_begin(), itend = top.depth_end();
51+
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 ==
64+
std::vector<irep_idt>{ID_implies, ID_not, ID_equal, ID_symbol, ID_symbol, ID_equal, ID_symbol,
65+
ID_symbol});
66+
}
67+
}
68+
69+
WHEN(
70+
"Replacing one of the middle nodes mid-walk and skipping the new "
71+
"children")
72+
{
73+
std::vector<irep_idt> ids;
74+
for(auto it = top.depth_begin(), itend = top.depth_end();
75+
it != itend;
76+
/* no ++it */)
77+
{
78+
bool replace_here = it->id() == ID_notequal;
79+
80+
if(replace_here)
81+
it.mutate() = not_exprt(equal_exprt(symbol, symbol));
82+
83+
ids.push_back(it->id());
84+
85+
if(replace_here)
86+
it.next_sibling_or_parent();
87+
else
88+
++it;
89+
}
90+
91+
THEN("We expect to skip the newly added child nodes")
92+
{
93+
REQUIRE(
94+
ids ==
95+
std::vector<irep_idt>{ID_implies, ID_not, ID_equal, ID_symbol, ID_symbol});
96+
}
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)