Skip to content

Commit 7b0ccdb

Browse files
Tests to demonstrate expected sharing behaviour of irept
1 parent 5cbb758 commit 7b0ccdb

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ SRC += unit_tests.cpp \
4444
solvers/refinement/string_refinement/union_find_replace.cpp \
4545
util/expr_cast/expr_cast.cpp \
4646
util/expr_iterator.cpp \
47+
util/irep_sharing.cpp \
4748
util/message.cpp \
4849
util/parameter_indices.cpp \
4950
util/simplify_expr.cpp \

unit/util/irep_sharing.cpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/// Author: Diffblue Ltd.
2+
3+
/// \file Tests that irep sharing works correctly
4+
5+
#include <testing-utils/catch.hpp>
6+
#include <util/irep.h>
7+
8+
#ifdef SHARING
9+
10+
SCENARIO("irept_sharing", "[!mayfail][core][utils][irept]")
11+
{
12+
GIVEN("An irept created with move_to_sub and a copy")
13+
{
14+
irept test_irep(ID_1);
15+
irept test_subirep(ID_1);
16+
irept test_subsubirep(ID_1);
17+
test_subirep.move_to_sub(test_subsubirep);
18+
test_irep.move_to_sub(test_subirep);
19+
THEN("Calling read() on a copy should return object with the same address")
20+
{
21+
irept irep = test_irep;
22+
REQUIRE(&irep.read() == &test_irep.read());
23+
}
24+
THEN("Changing subs in the original should not change them in a copy")
25+
{
26+
irept irep = test_irep;
27+
test_irep.get_sub()[0].id(ID_0);
28+
REQUIRE(irep.get_sub()[0].id() == ID_1);
29+
}
30+
THEN("Changing subs in a copy should not change them in the original")
31+
{
32+
irept irep = test_irep;
33+
irep.get_sub()[0].id(ID_0);
34+
REQUIRE(test_irep.get_sub()[0].id() == ID_1);
35+
REQUIRE(irep.get_sub()[0].id() == ID_0);
36+
}
37+
THEN("Changing subs in the original using a reference taken before "
38+
"copying should not change them in the copy")
39+
{
40+
irept &sub = test_irep.get_sub()[0];
41+
irept irep = test_irep;
42+
sub.id(ID_0);
43+
REQUIRE(test_irep.get_sub()[0].id() == ID_0);
44+
REQUIRE(irep.get_sub()[0].id() == ID_1);
45+
}
46+
THEN("Getting a reference to a sub in a copy should break sharing")
47+
{
48+
irept irep = test_irep;
49+
irept &sub = irep.get_sub()[0];
50+
REQUIRE(&irep.read() != &test_irep.read());
51+
sub = irept(ID_0);
52+
REQUIRE(irep.get_sub()[0].id() != test_irep.get_sub()[0].id());
53+
}
54+
THEN(
55+
"Getting a reference to a sub in the original should break sharing")
56+
{
57+
irept irep = test_irep;
58+
irept &sub = test_irep.get_sub()[0];
59+
REQUIRE(&irep.read() != &test_irep.read());
60+
sub = irept(ID_0);
61+
REQUIRE(irep.get_sub()[0].id() != test_irep.get_sub()[0].id());
62+
}
63+
THEN("Holding a reference to a sub should not prevent sharing")
64+
{
65+
irept &sub = test_irep.get_sub()[0];
66+
irept irep = test_irep;
67+
REQUIRE(&irep.read() == &test_irep.read());
68+
sub = irept(ID_0);
69+
REQUIRE(irep.get_sub()[0].id() == test_irep.get_sub()[0].id());
70+
}
71+
THEN("Changing an id in the original should break sharing")
72+
{
73+
irept irep = test_irep;
74+
test_irep.id(ID_0);
75+
REQUIRE(&irep.read() != &test_irep.read());
76+
REQUIRE(irep.id() != test_irep.id());
77+
}
78+
THEN("Changing an id should not prevent sharing")
79+
{
80+
test_irep.id(ID_0);
81+
irept irep = test_irep;
82+
REQUIRE(&irep.read() == &test_irep.read());
83+
}
84+
}
85+
}
86+
87+
#include <util/expr.h>
88+
89+
SCENARIO("exprt_sharing", "[!mayfail][core][utils][exprt]")
90+
{
91+
GIVEN("An expression created with move_to_operands and a copy")
92+
{
93+
exprt test_expr(ID_1);
94+
exprt test_subexpr(ID_1);
95+
exprt test_subsubexpr(ID_1);
96+
test_subexpr.move_to_operands(test_subsubexpr);
97+
test_expr.move_to_operands(test_subexpr);
98+
THEN("Calling read() on a copy should return object with the same address")
99+
{
100+
exprt expr = test_expr;
101+
REQUIRE(&expr.read() == &test_expr.read());
102+
}
103+
THEN("Changing operands in the original should not change them in a copy")
104+
{
105+
exprt expr = test_expr;
106+
test_expr.op0().id(ID_0);
107+
REQUIRE(expr.op0().id() == ID_1);
108+
}
109+
THEN("Changing operands in a copy should not change them in the original")
110+
{
111+
exprt expr = test_expr;
112+
expr.op0().id(ID_0);
113+
REQUIRE(test_expr.op0().id() == ID_1);
114+
REQUIRE(expr.op0().id() == ID_0);
115+
}
116+
THEN("Changing operands in the original using a reference taken before "
117+
"copying should not change them in the copy")
118+
{
119+
exprt &operand = test_expr.op0();
120+
exprt expr = test_expr;
121+
operand.id(ID_0);
122+
REQUIRE(test_expr.op0().id() == ID_0);
123+
REQUIRE(expr.op0().id() == ID_1);
124+
}
125+
THEN("Getting a reference to an operand in a copy should break sharing")
126+
{
127+
exprt expr = test_expr;
128+
exprt &operand = expr.op0();
129+
REQUIRE(&expr.read() != &test_expr.read());
130+
operand = exprt(ID_0);
131+
REQUIRE(expr.op0().id() != test_expr.op0().id());
132+
}
133+
THEN(
134+
"Getting a reference to an operand in the original should break sharing")
135+
{
136+
exprt expr = test_expr;
137+
exprt &operand = test_expr.op0();
138+
REQUIRE(&expr.read() != &test_expr.read());
139+
operand = exprt(ID_0);
140+
REQUIRE(expr.op0().id() != test_expr.op0().id());
141+
}
142+
THEN("Holding a reference to an operand should not prevent sharing")
143+
{
144+
exprt &operand = test_expr.op0();
145+
exprt expr = test_expr;
146+
REQUIRE(&expr.read() == &test_expr.read());
147+
operand = exprt(ID_0);
148+
REQUIRE(expr.op0().id() == test_expr.op0().id());
149+
}
150+
THEN("Changing an id in the original should break sharing")
151+
{
152+
exprt expr = test_expr;
153+
test_expr.id(ID_0);
154+
REQUIRE(&expr.read() != &test_expr.read());
155+
REQUIRE(expr.id() != test_expr.id());
156+
}
157+
THEN("Changing an id should not prevent sharing")
158+
{
159+
test_expr.id(ID_0);
160+
exprt expr = test_expr;
161+
REQUIRE(&expr.read() == &test_expr.read());
162+
}
163+
}
164+
}
165+
166+
#endif

0 commit comments

Comments
 (0)