summaryrefslogtreecommitdiff
path: root/src/sexpr/variant_test.cpp
blob: c671264bc1954e8374bbcb4715df420cef5bd250 (plain) (blame)
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
#include "variant.hpp"
//    variant_test.cpp - Testsuite for multi-type container that's better than boost's.
//
//    Copyright © 2014 Ben Longbons <b.r.longbons@gmail.com>
//
//    This file is part of The Mana World (Athena server)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <gtest/gtest.h>

#include "../strings/vstring.hpp"

//#include "../poison.hpp"


namespace tmwa
{
    struct Tracker
    {
        int id, moves, copies;

        explicit
        Tracker(int i)
        : id(i), moves(0), copies(0)
        {}
        Tracker(Tracker&& rhs)
        : id(rhs.id), moves(rhs.moves + 1), copies(rhs.copies)
        { rhs.id = 0; }
        Tracker(const Tracker& rhs)
        : id(rhs.id), moves(rhs.moves), copies(rhs.copies + 1)
        {}
        Tracker& operator = (Tracker&& rhs)
        { id = rhs.id; moves = rhs.moves + 1; copies = rhs.copies; rhs.id = 0; return *this; }
        Tracker& operator = (const Tracker& rhs)
        { id = rhs.id; moves = rhs.moves; copies = rhs.copies + 1; return *this; }
    };
    struct Foo : Tracker
    {
        // needed for first param of variant
        Foo() noexcept : Tracker(0) { abort(); }

        Foo(int i) : Tracker(i) {}
    };
    struct Bar : Tracker
    {
        Bar(int i) : Tracker(i) {}
    };
    struct Qux : Tracker
    {
        // needed for first param of variant
        Qux() noexcept : Tracker(0) { abort(); }

        Qux(int i) : Tracker(i) {}
        Qux(Qux&&) = default;
        Qux(const Qux&) = delete;
        Qux& operator = (Qux&&) = default;
        Qux& operator = (const Qux&) = default;
    };

TEST(variant, match)
{
    struct Sub : sexpr::Variant<Foo, Bar>
    {
        Sub()
        : sexpr::Variant<Foo, Bar>(Foo(1))
        {}
    };

    Sub v1;
    MATCH_BEGIN (v1)
    {
        MATCH_DEFAULT ()
        {
            FAIL();
        }
        MATCH_CASE (Foo, f)
        {
            (void)f;
            SUCCEED();
        }
        MATCH_CASE (Bar, b)
        {
            (void)b;
            FAIL();
        }
    }
    MATCH_END ();

    v1.emplace<Bar>(2);
    MATCH_BEGIN (v1)
    {
        MATCH_DEFAULT ()
        {
            FAIL();
        }
        MATCH_CASE (Foo, f)
        {
            (void)f;
            FAIL();
        }
        MATCH_CASE (Bar, b)
        {
            (void)b;
            SUCCEED();
        }
    }
    MATCH_END ();
}

TEST(variant, copymove1)
{
    sexpr::Variant<Qux> moveonly(Qux(3));
    (void)moveonly;
}

TEST(variant, copymove2)
{
    struct Move
    {
        Move() = default;
        Move(Move&&) = default;
        Move(const Move&) = delete;
        Move& operator = (Move&&) = default;
        Move& operator = (const Move&) = delete;
        ~Move() = default;
    };
    struct Copy
    {
        Copy() = default;
        Copy(Copy&&) = default;
        Copy(const Copy&) = default;
        Copy& operator = (Copy&&) = default;
        Copy& operator = (const Copy&) = default;
        ~Copy() = default;
    };

    using VarMv = sexpr::Variant<Move>;
    using VarCp = sexpr::Variant<Copy>;

    VarMv mv1;
    VarMv mv3 = std::move(mv1);
    mv1 = std::move(mv3);
    VarCp cp1;
    VarCp cp2 = cp1;
    VarCp cp3 = std::move(cp1);
    cp1 = cp2;
    cp1 = std::move(cp3);
}
} // namespace tmwa