File: issue21.cpp

package info (click to toggle)
mapbox-variant 1.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,648 kB
  • sloc: cpp: 31,068; ansic: 959; python: 424; makefile: 145; objc: 59; sh: 36
file content (52 lines) | stat: -rw-r--r-- 839 bytes parent folder | download | duplicates (4)
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
#include "catch.hpp"

#include <mapbox/variant.hpp>
#include <mapbox/variant_io.hpp>

// https://github.com/mapbox/variant/issues/21

static int count;

struct t1
{
    int value;

    t1(t1 const& rhs)
        : value(rhs.value)
    {
        ++count;
    }
    t1(int v) : value(v)
    {
        ++count;
    }
    ~t1()
    {
        --count;
    }
};

struct t2
{
    int value;
    t2(int v) : value(v)
    { // constructor fails
        throw std::runtime_error("fail");
    }
};

TEST_CASE("set() works cleanly even if the constructor throws ", "[variant]")
{

    using variant_type = mapbox::util::variant<t1, t2>;

    count = 0;
    {
        t1 obj{42};
        variant_type v = obj;
        REQUIRE(v.is<t1>());
        REQUIRE(v.get<t1>().value == 42);
        REQUIRE_THROWS(v.set<t2>(13));
    }
    REQUIRE(count == 0);
}