summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/outcome/test/tests/swap.cpp
blob: 2820af4f032f1561abc45bde45f03a1b01a7acc3 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* Unit testing for outcomes
(C) 2013-2022 Niall Douglas <http://www.nedproductions.biz/> (5 commits)


Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include <boost/outcome.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_monitor.hpp>

/* Should be this:

78 move constructor count = 2
65 move assignment count = 3
78 move assignment count = 1
65 move constructor count = 2
78 move assignment count = 0
65 move assignment count = 1

78 move constructor count = 1
65 move assignment count = 2
78 move assignment count = 0
65 move assignment count = 1
78 move assignment count = 0

78 move constructor count = 2
65 move assignment count = 3
78 move assignment count = 1
65 move constructor count = 2
78 move assignment count = 0
65 move assignment count = 1

78 move constructor count = 1
65 move assignment count = 2
78 move assignment count = 0
65 move assignment count = 1
78 move assignment count = 0

78 move constructor count = 2
65 move assignment count = 3
78 move assignment count = 1
65 move constructor count = 2
78 move assignment count = 0
65 move assignment count = 1

78 move constructor count = 1
65 move assignment count = 2
78 move assignment count = 0
65 move assignment count = 1
78 move assignment count = 0

78 move constructor count = 2
65 move assignment count = 3
78 move assignment count = 1
65 move constructor count = 2
78 move assignment count = 0
65 move assignment count = 1

78 move constructor count = 1
65 move assignment count = 2
78 move assignment count = 0
65 move assignment count = 1
78 move assignment count = 0
*/

#ifndef BOOST_NO_EXCEPTIONS
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4297)  // function assumed not to throw an exception but does
#endif
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wterminate"
#endif
template <bool mc, bool ma> struct Throwy
{
  int count{0}, inc{0}, id{0};
  Throwy() = default;
  Throwy(int c, int d, int i = 1) noexcept
      : count(c)
      , inc(i)
      , id(d)
  {
  }
  Throwy(const Throwy &) = delete;
  Throwy &operator=(const Throwy &) = delete;
  Throwy(Throwy &&o) noexcept(!mc)
      : count(o.count - o.inc)
      , inc(o.inc)
      , id(o.id)  // NOLINT
  {
    if(mc)
    {
      std::cout << "   " << id << " move constructor count = " << count << std::endl;
      if(!count)
      {
        std::cout << "      " << id << " move constructor throws!" << std::endl;
        throw std::bad_alloc();
      }
    }
    o.count = 0;
    o.inc = 0;
    o.id = 0;
  }
  Throwy &operator=(Throwy &&o) noexcept(!ma)
  {
    count = o.count - o.inc;
    if(ma)
    {
      std::cout << "   " << o.id << " move assignment count = " << count << std::endl;
      if(!count)
      {
        std::cout << "      " << o.id << " move assignment throws!" << std::endl;
        throw std::bad_alloc();
      }
    }
    inc = o.inc;
    id = o.id;
    o.count = 0;
    o.inc = 0;
    o.id = 0;
    return *this;
  }
  ~Throwy() = default;
};
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
#ifdef _MSC_VER
#pragma warning(pop)
#endif
enum class ErrorCode
{
  dummy
};
enum class ErrorCode2
{
  dummy
};
template <bool mc, bool ma> using resulty1 = BOOST_OUTCOME_V2_NAMESPACE::result<Throwy<mc, ma>, ErrorCode, BOOST_OUTCOME_V2_NAMESPACE::policy::all_narrow>;
template <bool mc, bool ma> using resulty2 = BOOST_OUTCOME_V2_NAMESPACE::result<ErrorCode, Throwy<mc, ma>, BOOST_OUTCOME_V2_NAMESPACE::policy::all_narrow>;
template <bool mc, bool ma> using outcomey1 = BOOST_OUTCOME_V2_NAMESPACE::outcome<ErrorCode, Throwy<mc, ma>, ErrorCode2, BOOST_OUTCOME_V2_NAMESPACE::policy::all_narrow>;
template <bool mc, bool ma> using outcomey2 = BOOST_OUTCOME_V2_NAMESPACE::outcome<ErrorCode, ErrorCode2, Throwy<mc, ma>, BOOST_OUTCOME_V2_NAMESPACE::policy::all_narrow>;
#endif

BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_swap, "Tests that the outcome swaps as intended")
{
  using namespace BOOST_OUTCOME_V2_NAMESPACE;
  {  // Does swap actually swap?
    outcome<std::string> a("niall"), b("douglas");
    BOOST_CHECK(a.value() == "niall");
    BOOST_CHECK(b.value() == "douglas");
    swap(a, b);
    BOOST_CHECK(a.value() == "douglas");
    BOOST_CHECK(b.value() == "niall");
    a = boost::system::errc::not_enough_memory;
    swap(a, b);
    BOOST_CHECK(a.value() == "niall");
    BOOST_CHECK(b.error() == boost::system::errc::not_enough_memory);
    BOOST_CHECK(!a.has_lost_consistency());
    BOOST_CHECK(!b.has_lost_consistency());
  }
#ifndef BOOST_NO_EXCEPTIONS
  {  // Is noexcept propagated?
    using nothrow_t = Throwy<false, false>;
    using nothrow = resulty1<false, false>;
    static_assert(std::is_nothrow_move_constructible<nothrow_t>::value, "throwy not correct!");
    static_assert(std::is_nothrow_move_assignable<nothrow_t>::value, "throwy not correct!");
    static_assert(std::is_nothrow_move_constructible<nothrow>::value, "type not correct!");
    static_assert(std::is_nothrow_move_assignable<nothrow>::value, "type not correct!");

    static_assert(detail::is_nothrow_swappable<nothrow_t>::value, "is_nothrow_swappable is not correct!");

    static_assert(noexcept(nothrow(0, 0)), "type has a throwing value constructor!");
    nothrow a(1, 78), b(1, 65);
    a.swap(b);
    static_assert(noexcept(a.swap(b)), "type has a throwing swap!");
  }
  {  // Is noexcept propagated?
    using nothrow_t = Throwy<false, false>;
    using nothrow = resulty2<false, false>;
    static_assert(std::is_nothrow_move_constructible<nothrow_t>::value, "throwy not correct!");
    static_assert(std::is_nothrow_move_assignable<nothrow_t>::value, "throwy not correct!");
    static_assert(std::is_nothrow_move_constructible<nothrow>::value, "type not correct!");
    static_assert(std::is_nothrow_move_assignable<nothrow>::value, "type not correct!");

    static_assert(detail::is_nothrow_swappable<nothrow_t>::value, "is_nothrow_swappable is not correct!");

    static_assert(noexcept(nothrow(0, 0)), "type has a throwing value constructor!");
    nothrow a(1, 78), b(1, 65);
    a.swap(b);
    static_assert(noexcept(a.swap(b)), "type has a throwing swap!");
  }

  {  // Does swap implement the strong guarantee?
    using throwy_t = Throwy<true, true>;
    using throwy = resulty1<true, true>;
    static_assert(!std::is_nothrow_move_constructible<throwy_t>::value, "throwy not correct!");
    static_assert(!std::is_nothrow_move_assignable<throwy_t>::value, "throwy not correct!");
    static_assert(!std::is_nothrow_move_constructible<throwy>::value, "type not correct!");
    static_assert(!std::is_nothrow_move_assignable<throwy>::value, "type not correct!");

    static_assert(!detail::is_nothrow_swappable<throwy_t>::value, "is_nothrow_swappable is not correct!");

    std::cout << "Result value first swap succeeds, second swap second move assignment throws:" << std::endl;
    {
      throwy a(3, 78), b(4, 65);
      a.swap(b);
      static_assert(!noexcept(a.swap(b)), "type has a non-throwing swap!");
      BOOST_CHECK(a.value().id == 65);
      BOOST_CHECK(b.value().id == 78);
      try
      {
        a.swap(b);  // fails on first assignment
        BOOST_REQUIRE(false);
      }
      catch(const std::bad_alloc & /*unused*/)
      {
        BOOST_CHECK(a.value().id == 65);  // ensure it is perfectly restored
        BOOST_CHECK(b.value().id == 78);
      }
      BOOST_CHECK(!a.has_lost_consistency());
      BOOST_CHECK(!b.has_lost_consistency());
    }
    std::cout << "\nResult value second move assignment throws, on recover second move assignment throws:" << std::endl;
    {
      throwy a(2, 78), b(3, 65);  // fails on second assignment, cannot restore
      try
      {
        a.swap(b);
        BOOST_REQUIRE(false);
      }
      catch(const std::bad_alloc & /*unused*/)
      {
        BOOST_CHECK(a.has_lost_consistency());  // both must be marked tainted
        BOOST_CHECK(b.has_lost_consistency());
      }
    }
  }
  std::cout << "\nResult error first swap succeeds, second swap first move assignment throws:" << std::endl;
  {  // Does swap implement the strong guarantee?
    using throwy_t = Throwy<true, true>;
    using throwy = resulty2<true, true>;
    static_assert(!std::is_nothrow_move_constructible<throwy_t>::value, "throwy not correct!");
    static_assert(!std::is_nothrow_move_assignable<throwy_t>::value, "throwy not correct!");
    static_assert(!std::is_nothrow_move_constructible<throwy>::value, "type not correct!");
    static_assert(!std::is_nothrow_move_assignable<throwy>::value, "type not correct!");

    static_assert(!detail::is_nothrow_swappable<throwy_t>::value, "is_nothrow_swappable is not correct!");

    {
      throwy a(3, 78), b(4, 65);
      a.swap(b);
      static_assert(!noexcept(a.swap(b)), "type has a non-throwing swap!");
      BOOST_CHECK(a.error().id == 65);
      BOOST_CHECK(b.error().id == 78);
      try
      {
        a.swap(b);  // fails on first assignment
        BOOST_REQUIRE(false);
      }
      catch(const std::bad_alloc & /*unused*/)
      {
        BOOST_CHECK(a.error().id == 65);  // ensure it is perfectly restored
        BOOST_CHECK(b.error().id == 78);
      }
      BOOST_CHECK(!a.has_lost_consistency());
      BOOST_CHECK(!b.has_lost_consistency());
    }
    std::cout << "\nResult error second move assignment throws, on recover second move assignment throws:" << std::endl;
    {
      throwy a(2, 78), b(3, 65);  // fails on second assignment, cannot restore
      try
      {
        a.swap(b);
        BOOST_REQUIRE(false);
      }
      catch(const std::bad_alloc & /*unused*/)
      {
        BOOST_CHECK(a.has_lost_consistency());  // both must be marked tainted
        BOOST_CHECK(b.has_lost_consistency());
      }
    }
  }

  {  // Is noexcept propagated?
    using nothrow_t = Throwy<false, false>;
    using nothrow = outcomey1<false, false>;
    static_assert(std::is_nothrow_move_constructible<nothrow_t>::value, "throwy not correct!");
    static_assert(std::is_nothrow_move_assignable<nothrow_t>::value, "throwy not correct!");
    static_assert(std::is_nothrow_move_constructible<nothrow>::value, "type not correct!");
    static_assert(std::is_nothrow_move_assignable<nothrow>::value, "type not correct!");

    static_assert(detail::is_nothrow_swappable<nothrow_t>::value, "is_nothrow_swappable is not correct!");

    static_assert(noexcept(nothrow(0, 0)), "type has a throwing value constructor!");
    nothrow a(1, 78), b(1, 65);
    a.swap(b);
    static_assert(noexcept(a.swap(b)), "type has a throwing swap!");
  }
  {  // Is noexcept propagated?
    using nothrow_t = Throwy<false, false>;
    using nothrow = outcomey1<false, false>;
    static_assert(std::is_nothrow_move_constructible<nothrow_t>::value, "throwy not correct!");
    static_assert(std::is_nothrow_move_assignable<nothrow_t>::value, "throwy not correct!");
    static_assert(std::is_nothrow_move_constructible<nothrow>::value, "type not correct!");
    static_assert(std::is_nothrow_move_assignable<nothrow>::value, "type not correct!");

    static_assert(detail::is_nothrow_swappable<nothrow_t>::value, "is_nothrow_swappable is not correct!");

    static_assert(noexcept(nothrow(0, 0)), "type has a throwing value constructor!");
    nothrow a(1, 78), b(1, 65);
    a.swap(b);
    static_assert(noexcept(a.swap(b)), "type has a throwing swap!");
  }

  std::cout << "\n\nOutcome value first swap succeeds, second swap first move assignment throws:" << std::endl;
  {  // Does swap implement the strong guarantee?
    using throwy_t = Throwy<true, true>;
    using throwy = outcomey1<true, true>;
    static_assert(!std::is_nothrow_move_constructible<throwy_t>::value, "throwy not correct!");
    static_assert(!std::is_nothrow_move_assignable<throwy_t>::value, "throwy not correct!");
    static_assert(!std::is_nothrow_move_constructible<throwy>::value, "type not correct!");
    static_assert(!std::is_nothrow_move_assignable<throwy>::value, "type not correct!");

    static_assert(!detail::is_nothrow_swappable<throwy_t>::value, "is_nothrow_swappable is not correct!");

    {
      throwy a(3, 78), b(4, 65);
      a.swap(b);
      static_assert(!noexcept(a.swap(b)), "type has a non-throwing swap!");
      BOOST_CHECK(a.error().id == 65);
      BOOST_CHECK(b.error().id == 78);
      try
      {
        a.swap(b);  // fails on first assignment
        BOOST_REQUIRE(false);
      }
      catch(const std::bad_alloc & /*unused*/)
      {
        BOOST_CHECK(a.error().id == 65);  // ensure it is perfectly restored
        BOOST_CHECK(b.error().id == 78);
      }
      BOOST_CHECK(!a.has_lost_consistency());
      BOOST_CHECK(!b.has_lost_consistency());
    }
    std::cout << "\nOutcome value second move assignment throws, on recover second move assignment throws:" << std::endl;
    {
      throwy a(2, 78), b(3, 65);  // fails on second assignment, cannot restore
      try
      {
        a.swap(b);
        BOOST_REQUIRE(false);
      }
      catch(const std::bad_alloc & /*unused*/)
      {
        BOOST_CHECK(a.has_lost_consistency());  // both must be marked tainted
        BOOST_CHECK(b.has_lost_consistency());
      }
    }
  }
  std::cout << "\nOutcome error first swap succeeds, second swap first move assignment throws:" << std::endl;
  {  // Does swap implement the strong guarantee?
    using throwy_t = Throwy<true, true>;
    using throwy = outcomey2<true, true>;
    static_assert(!std::is_nothrow_move_constructible<throwy_t>::value, "throwy not correct!");
    static_assert(!std::is_nothrow_move_assignable<throwy_t>::value, "throwy not correct!");
    static_assert(!std::is_nothrow_move_constructible<throwy>::value, "type not correct!");
    static_assert(!std::is_nothrow_move_assignable<throwy>::value, "type not correct!");

    static_assert(!detail::is_nothrow_swappable<throwy_t>::value, "is_nothrow_swappable is not correct!");

    {
      throwy a(3, 78), b(4, 65);
      a.swap(b);
      static_assert(!noexcept(a.swap(b)), "type has a non-throwing swap!");
      BOOST_CHECK(a.exception().id == 65);
      BOOST_CHECK(b.exception().id == 78);
      try
      {
        a.swap(b);  // fails on first assignment
        BOOST_REQUIRE(false);
      }
      catch(const std::bad_alloc & /*unused*/)
      {
        BOOST_CHECK(a.exception().id == 65);  // ensure it is perfectly restored
        BOOST_CHECK(b.exception().id == 78);
      }
      BOOST_CHECK(!a.has_lost_consistency());
      BOOST_CHECK(!b.has_lost_consistency());
    }
    std::cout << "\nOutcome error second move assignment throws, on recover second move assignment throws:" << std::endl;
    {
      throwy a(2, 78), b(3, 65);  // fails on second assignment, cannot restore
      try
      {
        a.swap(b);
        BOOST_REQUIRE(false);
      }
      catch(const std::bad_alloc & /*unused*/)
      {
        BOOST_CHECK(a.has_lost_consistency());  // both must be marked tainted
        BOOST_CHECK(b.has_lost_consistency());
      }
    }
    std::cout << std::endl;
  }
#endif
}