File: irep_sharing.cpp

package info (click to toggle)
cbmc 6.6.0-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,852 kB
  • sloc: cpp: 386,459; ansic: 114,466; java: 28,405; python: 6,003; yacc: 4,552; makefile: 4,041; lex: 2,487; xml: 2,388; sh: 2,050; perl: 557; pascal: 184; javascript: 163; ada: 36
file content (222 lines) | stat: -rw-r--r-- 7,353 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/// Author: Diffblue Ltd.

/// \file Tests that irep sharing works correctly

#include <testing-utils/use_catch.h>

#include <util/irep.h>
#include <util/std_expr.h>

#ifdef SHARING

SCENARIO("irept_sharing_trade_offs", "[core][utils][irept]" XFAIL)
{
  GIVEN("An irept created with move_to_sub")
  {
    irept test_irep(ID_1);
    irept test_subirep(ID_1);
    irept test_subsubirep(ID_1);
    test_subirep.move_to_sub(test_subsubirep);
    test_irep.move_to_sub(test_subirep);
    THEN("Calling read() on a copy should return object with the same address")
    {
      irept irep = test_irep;
      REQUIRE(&irep.read() == &test_irep.read());
    }
    THEN("Changing subs in the original using a reference taken before "
      "copying should not change them in the copy")
    {
      irept &sub = test_irep.get_sub()[0];
      irept irep = test_irep;
      sub.id(ID_0);
      REQUIRE(test_irep.get_sub()[0].id() == ID_0);
      REQUIRE(irep.get_sub()[0].id() == ID_1);
    }
    THEN("Holding a reference to a sub should not prevent sharing")
    {
      irept &sub = test_irep.get_sub()[0];
      irept irep = test_irep;
      REQUIRE(&irep.read() == &test_irep.read());
      sub = irept(ID_0);
      REQUIRE(irep.get_sub()[0].id() == test_irep.get_sub()[0].id());
    }
  }
}

SCENARIO("irept_sharing", "[core][utils][irept]")
{
  GIVEN("An irept created with move_to_sub")
  {
    irept test_irep(ID_1);
    irept test_subirep(ID_1);
    irept test_subsubirep(ID_1);
    test_subirep.move_to_sub(test_subsubirep);
    test_irep.move_to_sub(test_subirep);
    THEN("Copies of a copy should return object with the same address")
    {
      irept irep1 = test_irep;
      irept irep2 = irep1;
      REQUIRE(&irep1.read() == &irep2.read());
    }
    THEN("Changing subs in the original should not change them in a copy")
    {
      irept irep = test_irep;
      test_irep.get_sub()[0].id(ID_0);
      REQUIRE(irep.get_sub()[0].id() == ID_1);
    }
    THEN("Changing subs in a copy should not change them in the original")
    {
      irept irep = test_irep;
      irep.get_sub()[0].id(ID_0);
      REQUIRE(test_irep.get_sub()[0].id() == ID_1);
      REQUIRE(irep.get_sub()[0].id() == ID_0);
    }
    THEN("Getting a reference to a sub in a copy should break sharing")
    {
      irept irep = test_irep;
      irept &sub = irep.get_sub()[0];
      REQUIRE(&irep.read() != &test_irep.read());
      sub = irept(ID_0);
      REQUIRE(irep.get_sub()[0].id() != test_irep.get_sub()[0].id());
    }
    THEN(
      "Getting a reference to a sub in the original should break sharing")
    {
      irept irep = test_irep;
      irept &sub = test_irep.get_sub()[0];
      REQUIRE(&irep.read() != &test_irep.read());
      sub = irept(ID_0);
      REQUIRE(irep.get_sub()[0].id() != test_irep.get_sub()[0].id());
    }
    THEN("Changing an id in the original should break sharing")
    {
      irept irep = test_irep;
      test_irep.id(ID_0);
      REQUIRE(&irep.read() != &test_irep.read());
      REQUIRE(irep.id() != test_irep.id());
    }
    // the following would be desirable for irept to be safe, but we know it
    // presently does not hold; see #1855 for a proposed solution
    // THEN("Holding a reference to an operand should prevent sharing")
    // {
    //   irept &operand = test_irep.get_sub()[0];
    //   irept irep = test_irep;
    //   REQUIRE(&irep.read() != &test_irep.read());
    //   operand = irept(ID_0);
    //   REQUIRE(irep.get_sub()[0].id() != test_irep.get_sub()[0].id());
    // }
    THEN("Changing an id should not prevent sharing")
    {
      test_irep.id(ID_0);
      irept irep = test_irep;
      REQUIRE(&irep.read() == &test_irep.read());
    }
  }
}

SCENARIO("exprt_sharing_trade_offs", "[core][utils][exprt]" XFAIL)
{
  GIVEN("An expression created with add_to_operands(std::move(...))")
  {
    multi_ary_exprt test_expr(ID_1, {}, typet());
    exprt test_subexpr(ID_1);
    exprt test_subsubexpr(ID_1);
    test_subexpr.add_to_operands(std::move(test_subsubexpr));
    test_expr.add_to_operands(std::move(test_subexpr));
    THEN("Calling read() on a copy should return object with the same address")
    {
      exprt expr = test_expr;
      REQUIRE(&expr.read() == &test_expr.read());
    }
    THEN("Changing operands in the original using a reference taken before "
      "copying should not change them in the copy")
    {
      exprt &operand = test_expr.op0();
      multi_ary_exprt expr = test_expr;
      operand.id(ID_0);
      REQUIRE(test_expr.op0().id() == ID_0);
      REQUIRE(expr.op0().id() == ID_1);
    }
    THEN("Holding a reference to an operand should not prevent sharing")
    {
      exprt &operand = test_expr.op0();
      multi_ary_exprt expr = test_expr;
      REQUIRE(&expr.read() == &test_expr.read());
      operand = exprt(ID_0);
      REQUIRE(expr.op0().id() == test_expr.op0().id());
    }
  }
}

SCENARIO("exprt_sharing", "[core][utils][exprt]")
{
  GIVEN("An expression created with add_to_operands(std::move(...))")
  {
    multi_ary_exprt test_expr(ID_1, {}, typet());
    exprt test_subexpr(ID_1);
    exprt test_subsubexpr(ID_1);
    test_subexpr.add_to_operands(std::move(test_subsubexpr));
    test_expr.add_to_operands(std::move(test_subexpr));
    THEN("Copies of a copy should return object with the same address")
    {
      exprt expr1 = test_expr;
      exprt expr2 = expr1;
      REQUIRE(&expr1.read() == &expr2.read());
    }
    THEN("Changing operands in the original should not change them in a copy")
    {
      multi_ary_exprt expr = test_expr;
      test_expr.op0().id(ID_0);
      REQUIRE(expr.op0().id() == ID_1);
    }
    THEN("Changing operands in a copy should not change them in the original")
    {
      multi_ary_exprt expr = test_expr;
      expr.op0().id(ID_0);
      REQUIRE(test_expr.op0().id() == ID_1);
      REQUIRE(expr.op0().id() == ID_0);
    }
    THEN("Getting a reference to an operand in a copy should break sharing")
    {
      multi_ary_exprt expr = test_expr;
      exprt &operand = expr.op0();
      REQUIRE(&expr.read() != &test_expr.read());
      operand = exprt(ID_0);
      REQUIRE(expr.op0().id() != test_expr.op0().id());
    }
    THEN(
      "Getting a reference to an operand in the original should break sharing")
    {
      multi_ary_exprt expr = test_expr;
      exprt &operand = test_expr.op0();
      REQUIRE(&expr.read() != &test_expr.read());
      operand = exprt(ID_0);
      REQUIRE(expr.op0().id() != test_expr.op0().id());
    }
    THEN("Changing an id in the original should break sharing")
    {
      exprt expr = test_expr;
      test_expr.id(ID_0);
      REQUIRE(&expr.read() != &test_expr.read());
      REQUIRE(expr.id() != test_expr.id());
    }
    // the following would be desirable for irept to be safe, but we know it
    // presently does not hold; see #1855 for a proposed solution
    // THEN("Holding a reference to an operand should prevent sharing")
    // {
    //   exprt &operand = test_expr.op0();
    //   exprt expr = test_expr;
    //   REQUIRE(&expr.read() != &test_expr.read());
    //   operand = exprt(ID_0);
    //   REQUIRE(expr.op0().id() != test_expr.op0().id());
    // }
    THEN("Changing an id should not prevent sharing")
    {
      test_expr.id(ID_0);
      exprt expr = test_expr;
      REQUIRE(&expr.read() == &test_expr.read());
    }
  }
}

#endif