Skip to content

Commit 743b042

Browse files
committed
Unit test of irept's public API
1 parent 105d522 commit 743b042

File tree

2 files changed

+241
-0
lines changed

2 files changed

+241
-0
lines changed

Diff for: unit/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ SRC += unit_tests.cpp \
4242
solvers/refinement/string_refinement/union_find_replace.cpp \
4343
util/expr_cast/expr_cast.cpp \
4444
util/expr_iterator.cpp \
45+
util/irep.cpp \
4546
util/irep_sharing.cpp \
4647
util/message.cpp \
4748
util/parameter_indices.cpp \

Diff for: unit/util/irep.cpp

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

0 commit comments

Comments
 (0)