Skip to content

Commit f1b82bf

Browse files
NathanJPhillipstautschnig
authored andcommitted
Unit test to check that irept implements sharing
Separately test irept and exprt, where the latter are expected to have the very same behaviour.
1 parent 54e3ee4 commit f1b82bf

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

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_sharing.cpp \
4546
util/message.cpp \
4647
util/parameter_indices.cpp \
4748
util/simplify_expr.cpp \

unit/util/irep_sharing.cpp

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Copyright 2018 DiffBlue Limited. All Rights Reserved.
2+
3+
/// \file Tests that irep sharing works correctly
4+
5+
#include <testing-utils/catch.hpp>
6+
#include <util/expr.h>
7+
8+
#ifdef SHARING
9+
10+
SCENARIO("irept_sharing", "[core][utils][irept]")
11+
{
12+
GIVEN("An irept created with move_to_sub and a copy")
13+
{
14+
irept test_irep;
15+
irept test_subirep;
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("Getting a reference to an operand in a copy should break sharing")
25+
{
26+
irept irep = test_irep;
27+
irept &operand = irep.get_sub()[0];
28+
REQUIRE(&irep.read() != &test_irep.read());
29+
operand = irept(ID_0);
30+
REQUIRE(irep.get_sub()[0].id() != test_irep.get_sub()[0].id());
31+
}
32+
THEN(
33+
"Getting a reference to an operand in the original should break sharing")
34+
{
35+
irept irep = test_irep;
36+
irept &operand = test_irep.get_sub()[0];
37+
REQUIRE(&irep.read() != &test_irep.read());
38+
operand = irept(ID_0);
39+
REQUIRE(irep.get_sub()[0].id() != test_irep.get_sub()[0].id());
40+
}
41+
THEN("Changing an id in the original should break sharing")
42+
{
43+
irept irep = test_irep;
44+
test_irep.id(ID_1);
45+
REQUIRE(&irep.read() != &test_irep.read());
46+
REQUIRE(irep.id() != test_irep.id());
47+
}
48+
// the following would be desirable for irept to be safe, but we know it
49+
// presently does not hold; see #1855 for a proposed solution
50+
// THEN("Holding a reference to an operand should prevent sharing")
51+
// {
52+
// irept &operand = test_irep.get_sub()[0];
53+
// irept irep = test_irep;
54+
// REQUIRE(&irep.read() != &test_irep.read());
55+
// operand = irept(ID_0);
56+
// REQUIRE(irep.get_sub()[0].id() != test_irep.get_sub()[0].id());
57+
// }
58+
THEN("Changing an id should not prevent sharing")
59+
{
60+
test_irep.id(ID_1);
61+
irept irep = test_irep;
62+
REQUIRE(&irep.read() == &test_irep.read());
63+
}
64+
}
65+
}
66+
67+
SCENARIO("exprt_sharing", "[core][utils][exprt]")
68+
{
69+
GIVEN("A sample expression created with move_to_operands and a copy")
70+
{
71+
exprt test_expr;
72+
exprt test_subexpr;
73+
exprt test_subsubexpr(ID_1);
74+
test_subexpr.move_to_operands(test_subsubexpr);
75+
test_expr.move_to_operands(test_subexpr);
76+
THEN("Calling read() on a copy should return object with the same address")
77+
{
78+
exprt expr = test_expr;
79+
REQUIRE(&expr.read() == &test_expr.read());
80+
}
81+
THEN("Getting a reference to an operand in a copy should break sharing")
82+
{
83+
exprt expr = test_expr;
84+
exprt &operand = expr.op0();
85+
REQUIRE(&expr.read() != &test_expr.read());
86+
operand = exprt(ID_0);
87+
REQUIRE(expr.op0().id() != test_expr.op0().id());
88+
}
89+
THEN(
90+
"Getting a reference to an operand in the original should break sharing")
91+
{
92+
exprt expr = test_expr;
93+
exprt &operand = test_expr.op0();
94+
REQUIRE(&expr.read() != &test_expr.read());
95+
operand = exprt(ID_0);
96+
REQUIRE(expr.op0().id() != test_expr.op0().id());
97+
}
98+
THEN("Changing an id in the original should break sharing")
99+
{
100+
exprt expr = test_expr;
101+
test_expr.id(ID_1);
102+
REQUIRE(&expr.read() != &test_expr.read());
103+
REQUIRE(expr.id() != test_expr.id());
104+
}
105+
// the following would be desirable for irept to be safe, but we know it
106+
// presently does not hold; see #1855 for a proposed solution
107+
// THEN("Holding a reference to an operand should prevent sharing")
108+
// {
109+
// exprt &operand = test_expr.op0();
110+
// exprt expr = test_expr;
111+
// REQUIRE(&expr.read() != &test_expr.read());
112+
// operand = exprt(ID_0);
113+
// REQUIRE(expr.op0().id() != test_expr.op0().id());
114+
// }
115+
THEN("Changing an id should not prevent sharing")
116+
{
117+
test_expr.id(ID_1);
118+
exprt expr = test_expr;
119+
REQUIRE(&expr.read() == &test_expr.read());
120+
}
121+
}
122+
}
123+
124+
#endif

0 commit comments

Comments
 (0)