Skip to content

Commit 1e3a9cd

Browse files
authored
Merge pull request diffblue#1980 from NathanJPhillips/tests/irept
Tests for irept
2 parents 772b603 + 0902ae7 commit 1e3a9cd

File tree

3 files changed

+428
-0
lines changed

3 files changed

+428
-0
lines changed

unit/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ SRC += unit_tests.cpp \
4545
solvers/refinement/string_refinement/union_find_replace.cpp \
4646
util/expr_cast/expr_cast.cpp \
4747
util/expr_iterator.cpp \
48+
util/irep.cpp \
49+
util/irep_sharing.cpp \
4850
util/message.cpp \
4951
util/parameter_indices.cpp \
5052
util/simplify_expr.cpp \

unit/util/irep.cpp

+224
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
// Copyright 2018 Michael Tautschnig
2+
3+
/// \file Tests that irept memory consumption is fixed
4+
5+
#include <testing-utils/catch.hpp>
6+
#include <util/irep.h>
7+
8+
SCENARIO("irept_memory", "[core][utils][irept]")
9+
{
10+
GIVEN("Always")
11+
{
12+
THEN("An irept is just a pointer")
13+
{
14+
REQUIRE(sizeof(irept) == sizeof(void *));
15+
}
16+
17+
THEN("get_nil_irep yields ID_nil")
18+
{
19+
REQUIRE(get_nil_irep().id() == ID_nil);
20+
REQUIRE(get_nil_irep().is_nil());
21+
REQUIRE(!get_nil_irep().is_not_nil());
22+
}
23+
}
24+
25+
GIVEN("An initialized irep")
26+
{
27+
irept irep("some_id");
28+
irept irep_copy(irep);
29+
irept irep_assign = irep;
30+
31+
irept irep_other("some_other_id");
32+
33+
THEN("Its id is some_id")
34+
{
35+
REQUIRE(irep.id() == "some_id");
36+
REQUIRE(irep_copy.id() == "some_id");
37+
REQUIRE(irep_assign.id() == "some_id");
38+
39+
REQUIRE(irep_other.id() == "some_other_id");
40+
41+
// TODO(tautschnig): id_string() should be deprecated in favour of
42+
// id2string(...)
43+
REQUIRE(irep.id_string().size() == 7);
44+
}
45+
46+
THEN("Swapping works")
47+
{
48+
irep.swap(irep_other);
49+
50+
REQUIRE(irep.id() == "some_other_id");
51+
REQUIRE(irep_copy.id() == "some_id");
52+
REQUIRE(irep_assign.id() == "some_id");
53+
54+
REQUIRE(irep_other.id() == "some_id");
55+
}
56+
}
57+
58+
GIVEN("An irep")
59+
{
60+
irept irep;
61+
62+
THEN("Its id is empty")
63+
{
64+
REQUIRE(irep.is_not_nil());
65+
REQUIRE(irep.id().empty());
66+
}
67+
68+
THEN("Its id can be set")
69+
{
70+
irep.id("new_id");
71+
REQUIRE(irep.id() == "new_id");
72+
}
73+
74+
THEN("find of a non-existent element yields nil")
75+
{
76+
REQUIRE(irep.find("no-such-element").is_nil());
77+
}
78+
79+
THEN("Adding/removing elements is possible")
80+
{
81+
REQUIRE(irep.get_sub().empty());
82+
REQUIRE(irep.get_named_sub().empty());
83+
REQUIRE(irep.get_comments().empty());
84+
85+
irept &e = irep.add("a_new_element");
86+
REQUIRE(e.id().empty());
87+
e.id("some_id");
88+
REQUIRE(irep.find("a_new_element").id() == "some_id");
89+
90+
irept irep2("second_irep");
91+
irep.add("a_new_element", irep2);
92+
REQUIRE(irep.find("a_new_element").id() == "second_irep");
93+
REQUIRE(irep.get_named_sub().size() == 1);
94+
95+
irep.add("#a_comment", irep2);
96+
REQUIRE(irep.find("#a_comment").id() == "second_irep");
97+
REQUIRE(irep.get_comments().size() == 1);
98+
99+
irept bak(irep);
100+
irep.remove("no_such_id");
101+
REQUIRE(bak == irep);
102+
irep.remove("a_new_element");
103+
REQUIRE(bak != irep);
104+
REQUIRE(irep.find("a_new_element").is_nil());
105+
106+
irep.move_to_sub(bak);
107+
REQUIRE(irep.get_sub().size() == 1);
108+
109+
irep.move_to_named_sub("another_entry", irep2);
110+
REQUIRE(irep.get_sub().size() == 1);
111+
REQUIRE(irep.get_named_sub().size() == 1);
112+
REQUIRE(irep.get_comments().size() == 1);
113+
114+
irept irep3;
115+
irep.move_to_named_sub("#a_comment", irep3);
116+
REQUIRE(irep.find("#a_comment").id().empty());
117+
REQUIRE(irep.get_sub().size() == 1);
118+
REQUIRE(irep.get_named_sub().size() == 1);
119+
REQUIRE(irep.get_comments().size() == 1);
120+
121+
irept irep4;
122+
irep.move_to_named_sub("#another_comment", irep4);
123+
REQUIRE(irep.get_comments().size() == 2);
124+
}
125+
126+
THEN("Setting and getting works")
127+
{
128+
// TODO(tautschnig): get_string() should be deprecated in favour of
129+
// id2string(...)
130+
REQUIRE(irep.get_string("no_such_id").empty());
131+
132+
REQUIRE(irep.get("no_such_id").empty());
133+
// TODO(tautschnig): it's not clear whether we need all of the below
134+
// variants in the API
135+
REQUIRE(!irep.get_bool("no_such_id"));
136+
REQUIRE(irep.get_int("no_such_id") == 0);
137+
REQUIRE(irep.get_unsigned_int("no_such_id") == 0u);
138+
REQUIRE(irep.get_size_t("no_such_id") == 0u);
139+
REQUIRE(irep.get_long_long("no_such_id") == 0);
140+
141+
irep.set("some_id", "some string");
142+
REQUIRE(irep.get("some_id") == "some string");
143+
irept irep2("second_irep");
144+
irep.set("a_new_element", irep2);
145+
REQUIRE(irep.find("a_new_element").id() == "second_irep");
146+
irep.set("numeric_id", 1);
147+
REQUIRE(irep.get_bool("numeric_id"));
148+
irep.set("numeric_id", 42);
149+
REQUIRE(!irep.get_bool("numeric_id"));
150+
REQUIRE(irep.get_int("numeric_id") == 42);
151+
REQUIRE(irep.get_unsigned_int("numeric_id") == 42u);
152+
REQUIRE(irep.get_size_t("numeric_id") == 42u);
153+
REQUIRE(irep.get_long_long("numeric_id") == 42);
154+
155+
irep.clear();
156+
REQUIRE(irep.id().empty());
157+
REQUIRE(irep.get_sub().empty());
158+
REQUIRE(irep.get_named_sub().empty());
159+
REQUIRE(irep.get_comments().empty());
160+
161+
irep.make_nil();
162+
REQUIRE(irep.id() == ID_nil);
163+
REQUIRE(irep.get_sub().empty());
164+
REQUIRE(irep.get_named_sub().empty());
165+
REQUIRE(irep.get_comments().empty());
166+
}
167+
168+
THEN("Pretty printing works")
169+
{
170+
irept sub("sub_id");
171+
172+
irep.id("our_id");
173+
irep.add("some_op", sub);
174+
irep.add("#comment", sub);
175+
irep.move_to_sub(sub);
176+
177+
std::string pretty = irep.pretty();
178+
REQUIRE(
179+
pretty ==
180+
"our_id\n"
181+
" * some_op: sub_id\n"
182+
" * #comment: sub_id\n"
183+
" 0: sub_id");
184+
}
185+
186+
THEN("Hashing works")
187+
{
188+
irep.id("some_id");
189+
irep.set("#a_comment", 42);
190+
191+
REQUIRE(irep.hash() != 0);
192+
REQUIRE(irep.full_hash() != 0);
193+
REQUIRE(irep.hash() != irep.full_hash());
194+
}
195+
}
196+
197+
GIVEN("Multiple ireps")
198+
{
199+
irept irep1, irep2;
200+
201+
THEN("Comparison works")
202+
{
203+
REQUIRE(irep1 == irep2);
204+
REQUIRE(irep1.full_eq(irep2));
205+
206+
irep1.id("id1");
207+
irep2.id("id2");
208+
REQUIRE(irep1 != irep2);
209+
const bool one_lt_two = irep1 < irep2;
210+
const bool two_lt_one = irep2 < irep1;
211+
REQUIRE(one_lt_two != two_lt_one);
212+
REQUIRE(irep1.ordering(irep2) != irep2.ordering(irep1));
213+
REQUIRE(irep1.compare(irep2) != 0);
214+
215+
irep2.id("id1");
216+
REQUIRE(irep1 == irep2);
217+
REQUIRE(irep1.full_eq(irep2));
218+
219+
irep2.set("#a_comment", 42);
220+
REQUIRE(irep1 == irep2);
221+
REQUIRE(!irep1.full_eq(irep2));
222+
}
223+
}
224+
}

0 commit comments

Comments
 (0)