summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/any/test/any_test_rv.cpp
blob: 3f752ef3588fda9ca116ccadbaf675f2c765c937 (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
//  Unit test for boost::any.
//
//  See http://www.boost.org for most recent version, including documentation.
//
//  Copyright Antony Polukhin, 2013-2019.
//
//  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).

#include <cstdlib>
#include <string>
#include <utility>

#include <boost/any.hpp>
#include "test.hpp"
#include <boost/move/move.hpp>

#ifdef BOOST_NO_CXX11_RVALUE_REFERENCES

int main() 
{
    return EXIT_SUCCESS;
}

#else 

namespace any_tests
{
    typedef test<const char *, void (*)()> test_case;
    typedef const test_case * test_case_iterator;

    extern const test_case_iterator begin, end;
}

int main()
{
    using namespace any_tests;
    tester<test_case_iterator> test_suite(begin, end);
    return test_suite() ? EXIT_SUCCESS : EXIT_FAILURE;
}

namespace any_tests // test suite
{
    void test_move_construction();
    void test_move_assignment();
    void test_copy_construction();
    void test_copy_assignment();
    
    void test_move_construction_from_value();
    void test_move_assignment_from_value();
    void test_copy_construction_from_value();
    void test_copy_assignment_from_value();
    void test_construction_from_const_any_rv();
    void test_cast_to_rv();
    

    const test_case test_cases[] =
    {
        { "move construction of any",             test_move_construction      },
        { "move assignment of any",               test_move_assignment        },
        { "copy construction of any",             test_copy_construction      },
        { "copy assignment of any",               test_copy_assignment        },

        { "move construction from value",         test_move_construction_from_value },
        { "move assignment from value",           test_move_assignment_from_value  },
        { "copy construction from value",         test_copy_construction_from_value },
        { "copy assignment from value",           test_copy_assignment_from_value },
        { "constructing from const any&&",        test_construction_from_const_any_rv },
        { "casting to rvalue reference",          test_cast_to_rv }
    };

    const test_case_iterator begin = test_cases;
    const test_case_iterator end =
        test_cases + (sizeof test_cases / sizeof *test_cases);

    
    class move_copy_conting_class {
    public:
        static unsigned int moves_count;
        static unsigned int copy_count;

        move_copy_conting_class(){}
        move_copy_conting_class(move_copy_conting_class&& /*param*/) {
            ++ moves_count;
        }

        move_copy_conting_class& operator=(move_copy_conting_class&& /*param*/) {
            ++ moves_count;
            return *this;
        }

        move_copy_conting_class(const move_copy_conting_class&) {
            ++ copy_count;
        }
        move_copy_conting_class& operator=(const move_copy_conting_class& /*param*/) {
            ++ copy_count;
            return *this;
        }
    };

    unsigned int move_copy_conting_class::moves_count = 0;
    unsigned int move_copy_conting_class::copy_count = 0;
}

namespace any_tests // test definitions
{
    using namespace boost;

    void test_move_construction()
    {
        any value0 = move_copy_conting_class();
        move_copy_conting_class::copy_count = 0; 
        move_copy_conting_class::moves_count = 0;
        any value(boost::move(value0)); 

        check(value0.empty(), "moved away value is empty");
        check_false(value.empty(), "empty");
        check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
        check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
        check_equal(
            move_copy_conting_class::copy_count, 0u, 
            "checking copy counts");
        check_equal(
            move_copy_conting_class::moves_count, 0u, 
            "checking move counts");
    }

    void test_move_assignment()
    {
        any value0 = move_copy_conting_class();
        any value = move_copy_conting_class();
        move_copy_conting_class::copy_count = 0; 
        move_copy_conting_class::moves_count = 0;
        value = boost::move(value0); 

        check(value0.empty(), "moved away is empty");
        check_false(value.empty(), "empty");
        check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
        check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
        check_equal(
            move_copy_conting_class::copy_count, 0u, 
            "checking copy counts");
        check_equal(
            move_copy_conting_class::moves_count, 0u, 
            "checking move counts");
    }

    void test_copy_construction()
    {
        any value0 = move_copy_conting_class();
        move_copy_conting_class::copy_count = 0; 
        move_copy_conting_class::moves_count = 0;
        any value(value0); 

        check_false(value0.empty(), "copied value is not empty");
        check_false(value.empty(), "empty");
        check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
        check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
        check_equal(
            move_copy_conting_class::copy_count, 1u, 
            "checking copy counts");
        check_equal(
            move_copy_conting_class::moves_count, 0u, 
            "checking move counts");
    }

    void test_copy_assignment()
    {
        any value0 = move_copy_conting_class();
        any value = move_copy_conting_class();
        move_copy_conting_class::copy_count = 0; 
        move_copy_conting_class::moves_count = 0;
        value = value0; 

        check_false(value0.empty(), "copied value is not empty");
        check_false(value.empty(), "empty");
        check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
        check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
        check_equal(
            move_copy_conting_class::copy_count, 1u, 
            "checking copy counts");
        check_equal(
            move_copy_conting_class::moves_count, 0u, 
            "checking move counts");
    }

     void test_move_construction_from_value()
    {
        move_copy_conting_class value0;
        move_copy_conting_class::copy_count = 0; 
        move_copy_conting_class::moves_count = 0;
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
        any value(boost::move(value0)); 
#else
        any value(value0); 
#endif

        check_false(value.empty(), "empty");
        check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
        check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
        
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
        check_equal(
            move_copy_conting_class::copy_count, 0u, 
            "checking copy counts");
        check_equal(
            move_copy_conting_class::moves_count, 1u, 
            "checking move counts");
#endif

     }

    void test_move_assignment_from_value()
    {
        move_copy_conting_class value0;
        any value;
        move_copy_conting_class::copy_count = 0; 
        move_copy_conting_class::moves_count = 0;
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
        value = boost::move(value0); 
#else
        value = value0;
#endif 

        check_false(value.empty(), "empty");
        check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
        check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");

#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
        check_equal(
            move_copy_conting_class::copy_count, 0u, 
            "checking copy counts");
        check_equal(
            move_copy_conting_class::moves_count, 1u, 
            "checking move counts");
#endif

    }

    void test_copy_construction_from_value()
    {
        move_copy_conting_class value0;
        move_copy_conting_class::copy_count = 0; 
        move_copy_conting_class::moves_count = 0;
        any value(value0); 

        check_false(value.empty(), "empty");
        check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
        check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");

        check_equal(
            move_copy_conting_class::copy_count, 1u, 
            "checking copy counts");
        check_equal(
            move_copy_conting_class::moves_count, 0u, 
            "checking move counts");
     }

    void test_copy_assignment_from_value()
    {
        move_copy_conting_class value0;
        any value;
        move_copy_conting_class::copy_count = 0; 
        move_copy_conting_class::moves_count = 0;
        value = value0;

        check_false(value.empty(), "empty");
        check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
        check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");

        check_equal(
            move_copy_conting_class::copy_count, 1u, 
            "checking copy counts");
        check_equal(
            move_copy_conting_class::moves_count, 0u, 
            "checking move counts");
    }

    const any helper_method() {
        return true;
    }

    const bool helper_method1() {
        return true;
    }

    void test_construction_from_const_any_rv()
    {
        any values[] = {helper_method(), helper_method1() };
        (void)values;
    }

    void test_cast_to_rv()
    {
        move_copy_conting_class value0;
        any value;
        value = value0;
        move_copy_conting_class::copy_count = 0; 
        move_copy_conting_class::moves_count = 0;

        move_copy_conting_class value1 = any_cast<move_copy_conting_class&&>(value);

        check_equal(
            move_copy_conting_class::copy_count, 0u, 
            "checking copy counts");
        check_equal(
            move_copy_conting_class::moves_count, 1u, 
            "checking move counts");
        (void)value1;
/* Following code shall fail to compile
        const any cvalue = value0;
        move_copy_conting_class::copy_count = 0; 
        move_copy_conting_class::moves_count = 0;

        move_copy_conting_class value2 = any_cast<move_copy_conting_class&&>(cvalue);

        check_equal(
            move_copy_conting_class::copy_count, 1u, 
            "checking copy counts");
        check_equal(
            move_copy_conting_class::moves_count, 0u, 
            "checking move counts");
        (void)value2;
*/
    }
    
}

#endif