summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/variant/test/variant_nonempty_check.cpp
blob: d6150e693165a20b46c42de92e260095723c0170 (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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//-----------------------------------------------------------------------------
// boost-libs variant/test/variant_nonempty_check.cpp source file
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
//
// Copyright (c) 2014-2019 Antony Polukhin
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)


// In this file we are making tests to ensure that variant guarantees nonemptiness.
//
// For that purpose we create a `throwing_class`, that throws exception at a specified
// assignment attempt. If exception was thrown during move/assignemnt operation we make sure
// that data in variant is same as before move/assignemnt operation or that a fallback type is
// stored in variant.
//
// Different nonthrowing_class'es are used to tests different variant internal policies:
// with/without fallback type + throw/nothrow copyable + throw/nothrow movable


#include "boost/variant/variant.hpp"
#include "boost/variant/get.hpp"
#include "boost/core/lightweight_test.hpp"
#include <stdexcept>

struct exception_on_assignment : std::exception {};
struct exception_on_move_assignment : exception_on_assignment {};

void prevent_compiler_noexcept_detection() {
    char* p = new char;
    *p = '\0';
    delete p;
}


struct throwing_class {
    int trash;
    enum helper_enum {
        do_not_throw = 780,
        throw_after_5,
        throw_after_4,
        throw_after_3,
        throw_after_2,
        throw_after_1
    };

    bool is_throw() {
        if (trash < do_not_throw) {
            return true;
        }

        if (trash > do_not_throw && trash <= throw_after_1) {
            ++ trash;
            return false;
        }

        return trash != do_not_throw;
    }

    throwing_class(int value = 123) BOOST_NOEXCEPT_IF(false) : trash(value) {
        prevent_compiler_noexcept_detection();
    }

    throwing_class(const throwing_class& b) BOOST_NOEXCEPT_IF(false) : trash(b.trash) {
        if (is_throw()) {
            throw exception_on_assignment();
        }
    }

    const throwing_class& operator=(const throwing_class& b) BOOST_NOEXCEPT_IF(false) {
        trash = b.trash;
        if (is_throw()) {
            throw exception_on_assignment();
        }

        return *this;
    }

#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
    throwing_class(throwing_class&& b) BOOST_NOEXCEPT_IF(false) : trash(b.trash) {
        if (is_throw()) {
            throw exception_on_move_assignment();
        }
    }

    const throwing_class& operator=(throwing_class&& b) BOOST_NOEXCEPT_IF(false) {
        trash = b.trash;
        if (is_throw()) {
            throw exception_on_move_assignment();
        }

        return *this;
    }
#endif

    virtual ~throwing_class() {}
};

struct nonthrowing_class {
    int trash;

    nonthrowing_class() BOOST_NOEXCEPT_IF(false) : trash(123) {
        prevent_compiler_noexcept_detection();
    }

    nonthrowing_class(const nonthrowing_class&) BOOST_NOEXCEPT_IF(false) {
        prevent_compiler_noexcept_detection();
    }

    const nonthrowing_class& operator=(const nonthrowing_class&) BOOST_NOEXCEPT_IF(false) {
        prevent_compiler_noexcept_detection();
        return *this;
    }

#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
    nonthrowing_class(nonthrowing_class&&) BOOST_NOEXCEPT_IF(false) {
        prevent_compiler_noexcept_detection();
    }

    const nonthrowing_class& operator=(nonthrowing_class&&) BOOST_NOEXCEPT_IF(false) {
        prevent_compiler_noexcept_detection();
        return *this;
    }
#endif
};

struct nonthrowing_class2 {
    int trash;

    nonthrowing_class2() BOOST_NOEXCEPT_IF(false) : trash(123) {
        prevent_compiler_noexcept_detection();
    }
};

struct nonthrowing_class3 {
    int trash;

    nonthrowing_class3() BOOST_NOEXCEPT_IF(true) : trash(123) {}

    nonthrowing_class3(const nonthrowing_class3&) BOOST_NOEXCEPT_IF(false) {
        prevent_compiler_noexcept_detection();
    }

    const nonthrowing_class3& operator=(const nonthrowing_class3&) BOOST_NOEXCEPT_IF(false) {
        prevent_compiler_noexcept_detection();
        return *this;
    }

#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
    nonthrowing_class3(nonthrowing_class3&&) BOOST_NOEXCEPT_IF(false) {
        prevent_compiler_noexcept_detection();
    }

    const nonthrowing_class3& operator=(nonthrowing_class3&&) BOOST_NOEXCEPT_IF(false) {
        prevent_compiler_noexcept_detection();
        return *this;
    }
#endif
};

struct nonthrowing_class4 {
    int trash;

    nonthrowing_class4() BOOST_NOEXCEPT_IF(false) : trash(123) {
        prevent_compiler_noexcept_detection();
    }

    nonthrowing_class4(const nonthrowing_class4&) BOOST_NOEXCEPT_IF(false) {
        prevent_compiler_noexcept_detection();
    }

    const nonthrowing_class4& operator=(const nonthrowing_class4&) BOOST_NOEXCEPT_IF(false) {
        prevent_compiler_noexcept_detection();
        return *this;
    }

#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
    nonthrowing_class4(nonthrowing_class4&&) BOOST_NOEXCEPT_IF(true) {
    }

    const nonthrowing_class4& operator=(nonthrowing_class4&&) BOOST_NOEXCEPT_IF(true) {
        return *this;
    }
#endif
};


// Tests /////////////////////////////////////////////////////////////////////////////////////


template <class Nonthrowing>
inline void check_1_impl(int helper)
{
    boost::variant<throwing_class, Nonthrowing> v;
    try {
        v = throwing_class(helper);
        BOOST_TEST(!v.which());
        BOOST_TEST(boost::get<throwing_class>(&v));
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(!v.which());
        BOOST_TEST(boost::get<throwing_class>(&v));
    }

    try {
        throwing_class tc(helper);
        v = tc;
        BOOST_TEST(!v.which());
        BOOST_TEST(boost::get<throwing_class>(&v));
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(!v.which());
        BOOST_TEST(boost::get<throwing_class>(&v));
    }
}

inline void check_1(int helper = 1)
{
    check_1_impl<nonthrowing_class>(helper);
    check_1_impl<nonthrowing_class2>(helper);
    check_1_impl<nonthrowing_class3>(helper);
    check_1_impl<nonthrowing_class4>(helper);
    check_1_impl<boost::blank>(helper);
}

template <class Nonthrowing>
inline void check_2_impl(int helper)
{
    boost::variant<Nonthrowing, throwing_class> v;
    try {
        v = throwing_class(helper);
        BOOST_TEST(v.which() == 1);
        BOOST_TEST(boost::get<throwing_class>(&v));
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(!v.which());
        BOOST_TEST(boost::get<Nonthrowing>(&v));
    }

    try {
        throwing_class cl(helper);
        v = cl;
        BOOST_TEST(v.which() == 1);
        BOOST_TEST(boost::get<throwing_class>(&v));
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(!v.which());
        BOOST_TEST(boost::get<Nonthrowing>(&v));
    }
}

inline void check_2(int helper = 1)
{
    check_2_impl<nonthrowing_class>(helper);
    check_2_impl<nonthrowing_class2>(helper);
    check_2_impl<nonthrowing_class3>(helper);
    check_2_impl<nonthrowing_class4>(helper);
    check_2_impl<boost::blank>(helper);
}

template <class Nonthrowing>
inline void check_3_impl(int helper)
{
    boost::variant<Nonthrowing, throwing_class> v1, v2;

    swap(v1, v2);
    try {
        v1 = throwing_class(helper);
        BOOST_TEST(v1.which() == 1);
        BOOST_TEST(boost::get<throwing_class>(&v1));
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(!v1.which());
        BOOST_TEST(boost::get<Nonthrowing>(&v1));
    }


    try {
        v2 = throwing_class(helper);
        BOOST_TEST(v2.which() == 1);
        BOOST_TEST(boost::get<throwing_class>(&v2));
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(!v2.which());
        BOOST_TEST(boost::get<Nonthrowing>(&v2));
    }


    if (!v1.which() && !v2.which()) {
        swap(v1, v2); // Make sure that two backup holders swap well
        BOOST_TEST(!v1.which());
        BOOST_TEST(boost::get<Nonthrowing>(&v1));
        BOOST_TEST(!v2.which());
        BOOST_TEST(boost::get<Nonthrowing>(&v2));

        v1 = v2;
    }
}

inline void check_3(int helper = 1)
{
    check_3_impl<nonthrowing_class>(helper);
    check_3_impl<nonthrowing_class2>(helper);
    check_3_impl<nonthrowing_class3>(helper);
    check_3_impl<nonthrowing_class4>(helper);
    check_3_impl<boost::blank>(helper);
}

inline void check_4(int helper = 1)
{
    // This one has a fallback
    boost::variant<int, throwing_class> v1, v2;

    swap(v1, v2);
    try {
        v1 = throwing_class(helper);
        BOOST_TEST(v1.which() == 1);
        BOOST_TEST(boost::get<throwing_class>(&v1));
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(!v1.which());
        BOOST_TEST(boost::get<int>(&v1));
    }


    try {
        v2 = throwing_class(helper);
        BOOST_TEST(v2.which() == 1);
        BOOST_TEST(boost::get<throwing_class>(&v2));
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(!v2.which());
        BOOST_TEST(boost::get<int>(&v2));
    }

    if (!v1.which() && !v2.which()) {
        swap(v1, v2);
        BOOST_TEST(!v1.which());
        BOOST_TEST(boost::get<int>(&v1));
        BOOST_TEST(!v2.which());
        BOOST_TEST(boost::get<int>(&v2));

        v1 = v2;
    }
}

template <class Nonthrowing>
inline void check_5_impl(int helper)
{
    boost::variant<Nonthrowing, throwing_class> v1, v2;
    throwing_class throw_not_now;
    throw_not_now.trash = throwing_class::do_not_throw;
    v1 = throw_not_now;
    v2 = throw_not_now;

    boost::get<throwing_class>(v1).trash = 1;
    boost::get<throwing_class>(v2).trash = 1;

    try {
        v1 = throwing_class(helper);
        BOOST_TEST(v1.which() == 1);
        BOOST_TEST(boost::get<throwing_class>(&v1));
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(v1.which() == 1);
        BOOST_TEST(boost::get<throwing_class>(&v1));
    }

    boost::get<throwing_class>(v1).trash = throwing_class::do_not_throw;
    boost::get<throwing_class>(v2).trash = throwing_class::do_not_throw;
    v1 = Nonthrowing();
    v2 = Nonthrowing();
    try {
        v1 = throwing_class(helper);
        BOOST_TEST(v1.which() == 1);
        BOOST_TEST(boost::get<throwing_class>(&v1));
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(v1.which() == 0);
        BOOST_TEST(boost::get<Nonthrowing>(&v1));
    }

    int v1_type = v1.which();
    int v2_type = v2.which();
    try {
        swap(v1, v2); // Make sure that backup holders swap well
        BOOST_TEST(v1.which() == v2_type);
        BOOST_TEST(v2.which() == v1_type);
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(v1.which() == v1_type);
        BOOST_TEST(v2.which() == v2_type);
    }
}


inline void check_5(int helper = 1)
{
    check_5_impl<nonthrowing_class>(helper);
    check_5_impl<nonthrowing_class2>(helper);
    check_5_impl<nonthrowing_class3>(helper);
    check_5_impl<nonthrowing_class4>(helper);
    check_5_impl<boost::blank>(helper);
}

template <class Nonthrowing>
inline void check_6_impl(int helper)
{
    boost::variant<Nonthrowing, throwing_class> v1, v2;
    throwing_class throw_not_now;
    throw_not_now.trash = throwing_class::do_not_throw;
    v1 = throw_not_now;
    v2 = throw_not_now;

    v1 = throw_not_now;
    v2 = throw_not_now;
    swap(v1, v2);
    boost::get<throwing_class>(v1).trash = 1;
    boost::get<throwing_class>(v2).trash = 1;

    v1 = throwing_class(throw_not_now);
    v2 = v1;

    v1 = Nonthrowing();
    try {
        throwing_class tc;
        tc.trash = helper;
        v1 = tc;
        BOOST_TEST(v1.which() == 1);
        BOOST_TEST(boost::get<throwing_class>(&v1));
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(v1.which() == 0);
    }

    v2 = Nonthrowing();
    try {
        v2 = 2;
        BOOST_TEST(false);
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(v2.which() == 0);
    }

    // Probably the most significant test:
    // unsuccessful swap must preserve old values of variant
    v1 = throw_not_now;
    boost::get<throwing_class>(v1).trash = helper;
    try {
        swap(v1, v2);
    } catch (const exception_on_assignment& /*e*/) {
        BOOST_TEST(v1.which() == 1);
        BOOST_TEST(v2.which() == 0);
        BOOST_TEST(boost::get<throwing_class>(v1).trash == helper);
    }
}


inline void check_6(int helper = 1)
{
    check_6_impl<nonthrowing_class>(helper);
    check_6_impl<nonthrowing_class2>(helper);
    check_6_impl<nonthrowing_class3>(helper);
    check_6_impl<nonthrowing_class4>(helper);
    check_6_impl<boost::blank>(helper);
}

int main()
{
    // throwing_class::throw_after_1 + 1  => throw on first assignment/construction
    // throwing_class::throw_after_1  => throw on second assignment/construction
    // throwing_class::throw_after_2  => throw on third assignment/construction
    // ...
    for (int i = throwing_class::throw_after_1 + 1; i != throwing_class::do_not_throw; --i) {
        check_1(i);
        check_2(i);
        check_3(i);
        check_4(i);
        check_5(i);
        check_6(i);
    }

    return boost::report_errors();
}