File: recursive_wrapper.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 (185 lines) | stat: -rw-r--r-- 3,992 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
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
#include "catch.hpp"

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

#include <type_traits>
#include <utility>

using rwi = mapbox::util::recursive_wrapper<int>;
using rwp = mapbox::util::recursive_wrapper<std::pair<int, int>>;

static_assert(std::is_same<rwi::type, int>::value, "type check failed");

TEST_CASE("recursive wrapper of int")
{

    SECTION("construct with value")
    {
        rwi a{7};

        REQUIRE(a.get() == 7);
        REQUIRE(*a.get_pointer() == 7);

        a = 8;
        REQUIRE(a.get() == 8);

        rwi b{a};
        REQUIRE(b.get() == 8);

        rwi c;
        c = b;
        REQUIRE(b.get() == 8);
        REQUIRE(c.get() == 8);

        c = 9;
        REQUIRE(c.get() == 9);

        int x = 10;
        c = x;
        REQUIRE(c.get() == 10);

        b = std::move(c);
        REQUIRE(b.get() == 10);
    }

    SECTION("construct with const reference")
    {
        int i = 7;
        rwi a{i};

        REQUIRE(a.get() == 7);
    }

    SECTION("implicit conversion to reference of underlying type")
    {

        SECTION("const")
        {
            rwi const a{7};
            REQUIRE(a.get() == 7);
            REQUIRE(*a.get_pointer() == 7);

            rwi::type const& underlying = a;
            REQUIRE(underlying == 7);
        }

        SECTION("non const")
        {
            rwi a{7};
            REQUIRE(a.get() == 7);
            REQUIRE(*a.get_pointer() == 7);

            rwi::type& underlying = a;
            REQUIRE(underlying == 7);
            a = 8;
            REQUIRE(underlying == 8);
        }
    }
}

TEST_CASE("move of recursive wrapper")
{
    rwi a{1};

    SECTION("move constructor")
    {
        rwi b{std::move(a)};
        REQUIRE(b.get() == 1);
    }

    SECTION("operator= on rvalue")
    {
        rwi b{2};
        b = std::move(a);
        REQUIRE(b.get() == 1);
    }
}

TEST_CASE("swap")
{
    rwi a{1};
    rwi b{2};

    REQUIRE(a.get() == 1);
    REQUIRE(b.get() == 2);

    using std::swap;
    swap(a, b);

    REQUIRE(a.get() == 2);
    REQUIRE(b.get() == 1);
}

TEST_CASE("recursive wrapper of pair<int, int>")
{

    SECTION("default constructed")
    {
        rwp a;
        REQUIRE(a.get().first == 0);
        REQUIRE(a.get().second == 0);
    }

    SECTION("construct with value")
    {
        rwp a{std::make_pair(1, 2)};

        REQUIRE(a.get().first == 1);
        REQUIRE(a.get().second == 2);

        REQUIRE(a.get_pointer()->first == 1);
        REQUIRE(a.get_pointer()->second == 2);

        a = {3, 4};
        REQUIRE(a.get().first == 3);
        REQUIRE(a.get().second == 4);

        rwp b{a};
        REQUIRE(b.get().first == 3);
        REQUIRE(b.get().second == 4);

        rwp c;
        c = b;
        REQUIRE(b.get().first == 3);
        REQUIRE(b.get().second == 4);
        REQUIRE(c.get().first == 3);
        REQUIRE(c.get().second == 4);

        c = {5, 6};
        REQUIRE(c.get().first == 5);
        REQUIRE(c.get().second == 6);

        b = std::move(c);
        REQUIRE(b.get().first == 5);
        REQUIRE(b.get().second == 6);
        //REQUIRE(c.get_pointer() == nullptr);
    }

    SECTION("Multiple recurssive wrappers of polymorphic types")
    {
        // https://github.com/mapbox/variant/issues/146
        // (Visual Studio 2015 update 3)
        using namespace mapbox::util;
        struct Base;
        struct Derived;
        using Variant = variant<recursive_wrapper<Base>, recursive_wrapper<Derived>>;
        struct Base { };
        struct Derived : public Base { };
        {
            Base base;
            Derived derived;
            Variant v;
            v = base;
            v = derived; // compile error prior https://github.com/mapbox/variant/pull/147
            CHECK(v.is<Derived>());
        }
        {
            Derived derived;
            Variant v(derived); // compile error prior https://github.com/mapbox/variant/pull/147
            CHECK(v.is<Derived>());
        }


    }
}