forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr_iterator.cpp
158 lines (141 loc) · 4.12 KB
/
expr_iterator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*******************************************************************\
Module: Example Catch Tests
Author: DiffBlue Limited. All rights reserved.
\*******************************************************************/
#include <testing-utils/catch.hpp>
#include <util/expr.h>
#include <util/expr_iterator.h>
TEST_CASE("Depth iterator over empty exprt")
{
exprt expr;
std::vector<std::reference_wrapper<const exprt>> results;
std::copy(expr.depth_begin(),
expr.depth_end(),
std::back_inserter(results));
REQUIRE(results.size()==1);
REQUIRE(results.front().get()==expr);
}
TEST_CASE("Unique depth iterator over empty exprt")
{
exprt expr;
std::vector<std::reference_wrapper<const exprt>> results;
std::copy(expr.unique_depth_begin(),
expr.unique_depth_end(),
std::back_inserter(results));
REQUIRE(results.size()==1);
REQUIRE(results.front().get()==expr);
}
TEST_CASE("Iterate over a node with 3 children")
{
std::vector<exprt> input(4);
input[0].operands()={ input[1], input[2], input[3] };
std::vector<std::reference_wrapper<const exprt>> results;
std::copy(input[0].depth_begin(),
input[0].depth_end(),
std::back_inserter(results));
REQUIRE(results.size()==input.size());
auto it=results.begin();
for(const auto &expr : input)
{
REQUIRE(it->get()==expr);
it++;
}
}
TEST_CASE("Iterate over a node with 5 children, ignoring duplicates")
{
std::vector<exprt> input(4);
input[1].id(ID_int);
input[2].id(ID_symbol);
input[3].id(ID_array);
input[0].operands()={ input[1], input[2], input[1], input[3], input[2] };
std::vector<std::reference_wrapper<const exprt>> results;
std::copy(input[0].unique_depth_begin(),
input[0].unique_depth_end(),
std::back_inserter(results));
REQUIRE(results.size()==input.size());
auto it=results.begin();
for(const auto &expr : input)
{
REQUIRE(it->get()==expr);
it++;
}
}
TEST_CASE("Iterate over a 3-level node")
{
std::vector<exprt> input(4);
input[1].id(ID_int);
input[2].operands()={ input[3] };
input[0].operands()={ input[1], input[2] };
std::vector<std::reference_wrapper<const exprt>> results;
std::copy(input[0].depth_begin(),
input[0].depth_end(),
std::back_inserter(results));
REQUIRE(results.size()==input.size());
auto it=results.begin();
for(const auto &expr : input)
{
REQUIRE(it->get()==expr);
it++;
}
}
TEST_CASE("Iterate over a 3-level tree, ignoring duplicates")
{
std::vector<exprt> input(4);
input[1].id(ID_int);
input[2].operands()={ input[3], input[1] };
input[0].operands()={ input[1], input[2], input[3] };
std::vector<std::reference_wrapper<const exprt>> results;
std::copy(input[0].unique_depth_begin(),
input[0].unique_depth_end(),
std::back_inserter(results));
REQUIRE(results.size()==input.size());
auto it=results.begin();
for(const auto &expr : input)
{
REQUIRE(it->get()==expr);
it++;
}
}
TEST_CASE("Iterate over a 3-level tree, mutate - set all types to ID_symbol")
{
std::vector<exprt> input(4);
input[1].id(ID_int);
input[2].operands()={ input[3] };
input[0].operands()={ input[1], input[2] };
std::vector<std::reference_wrapper<exprt>> results;
for(auto it=input[0].depth_begin(),
end=input[0].depth_end();
it != end; ++it)
{
exprt &expr=it.mutate();
results.push_back(std::ref(expr));
REQUIRE(*it==expr);
expr.id(ID_symbol);
}
REQUIRE(results.size()==input.size());
for(const auto& expr : results)
{
REQUIRE(expr.get().id()==ID_symbol);
}
}
TEST_CASE("next_sibling_or_parent, next sibling")
{
std::vector<exprt> input(4);
input[1].operands()={ input[3] };
input[2].id(ID_int);
input[0].operands()={ input[1], input[2] };
auto it=input[0].depth_begin();
it++;
it.next_sibling_or_parent();
REQUIRE(*it==input[2]);
}
TEST_CASE("next_sibling_or_parent, next parent ")
{
std::vector<exprt> input(3);
input[1].operands()={ input[2] };
input[0].operands()={ input[1] };
auto it=input[0].depth_begin();
it++;
it.next_sibling_or_parent();
REQUIRE(it==input[0].depth_end());
}