Skip to content

Commit abe290b

Browse files
Tests to demonstrate irept sharing is broken
1 parent 1a88479 commit abe290b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Diff for: unit/util/irep_sharing.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
SCENARIO("irept_sharing", "[!mayfail][core][utils][irept]")
9+
{
10+
GIVEN("A sample expression created with move_to_operands and a copy")
11+
{
12+
exprt test_expr;
13+
exprt test_subexpr;
14+
exprt test_subsubexpr(ID_1);
15+
test_subexpr.move_to_operands(test_subsubexpr);
16+
test_expr.move_to_operands(test_subexpr);
17+
THEN("Calling read() on a copy should return object with the same address")
18+
{
19+
exprt expr = test_expr;
20+
REQUIRE(&expr.read() == &test_expr.read());
21+
}
22+
THEN("Getting a reference to an operand in a copy should break sharing")
23+
{
24+
exprt expr = test_expr;
25+
exprt &operand = expr.op0();
26+
REQUIRE(&expr.read() != &test_expr.read());
27+
operand = exprt(ID_0);
28+
REQUIRE(expr.op0().id() != test_expr.op0().id());
29+
}
30+
THEN(
31+
"Getting a reference to an operand in the original should break sharing")
32+
{
33+
exprt expr = test_expr;
34+
exprt &operand = test_expr.op0();
35+
REQUIRE(&expr.read() != &test_expr.read());
36+
operand = exprt(ID_0);
37+
REQUIRE(expr.op0().id() != test_expr.op0().id());
38+
}
39+
THEN("Changing an id in the original should break sharing")
40+
{
41+
exprt expr = test_expr;
42+
test_expr.id(ID_1);
43+
REQUIRE(&expr.read() != &test_expr.read());
44+
REQUIRE(expr.id() != test_expr.id());
45+
}
46+
THEN("Holding a reference to an operand should prevent sharing")
47+
{
48+
exprt &operand = test_expr.op0();
49+
exprt expr = test_expr;
50+
REQUIRE(&expr.read() != &test_expr.read());
51+
operand = exprt(ID_0);
52+
REQUIRE(expr.op0().id() != test_expr.op0().id());
53+
}
54+
THEN("Changing an id should not prevent sharing")
55+
{
56+
test_expr.id(ID_1);
57+
exprt expr = test_expr;
58+
REQUIRE(&expr.read() == &test_expr.read());
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)