File: polymorphic_cast_test.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (391 lines) | stat: -rw-r--r-- 9,981 bytes parent folder | download | duplicates (9)
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
//
// Test boost::polymorphic_cast, boost::polymorphic_downcast and 
// boost::polymorphic_pointer_cast, boost::polymorphic_pointer_downcast
//
// Copyright 1999 Beman Dawes
// Copyright 1999 Dave Abrahams
// Copyright 2014 Peter Dimov
// Copyright 2014 Boris Rasin, 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
//

#define BOOST_ENABLE_ASSERT_HANDLER
#include <boost/polymorphic_cast.hpp>
#include <boost/polymorphic_pointer_cast.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/intrusive_ptr.hpp>
#include <boost/smart_ptr/intrusive_ref_counter.hpp>
#include <boost/core/lightweight_test.hpp>
#include <string>
#include <memory>

static bool expect_assertion = false;
static int assertion_failed_count = 0;

//assertion handler throws it to exit like assert, but to be able to catch it and stop
//usage: BOOST_TEST_THROWS( function_with_assert(), expected_assertion );
struct expected_assertion {};

// BOOST_ASSERT custom handler
void boost::assertion_failed( char const * expr, char const * function, char const * file, long line )
{
    if( expect_assertion )
    {
        ++assertion_failed_count;
        throw expected_assertion();
    }
    else
    {
        BOOST_ERROR( "unexpected assertion" );

        BOOST_LIGHTWEIGHT_TEST_OSTREAM
          << file << "(" << line << "): assertion '" << expr << "' failed in function '"
          << function << "'" << std::endl;
    }
}

//

struct Base : boost::intrusive_ref_counter<Base>
{
    virtual ~Base() {}
    virtual std::string kind() { return "Base"; }
};

struct Base2
{
    virtual ~Base2() {}
    virtual std::string kind2() { return "Base2"; }
};

struct Derived : public Base, Base2
{
    virtual std::string kind() { return "Derived"; }
};

static void test_polymorphic_cast()
{
    Base * base = new Derived;

    Derived * derived;
    
    try
    {
        derived = boost::polymorphic_cast<Derived*>( base );

        BOOST_TEST( derived != 0 );

        if( derived != 0 )
        {
            BOOST_TEST_EQ( derived->kind(), "Derived" );
        }
    }
    catch( std::bad_cast const& )
    {
        BOOST_ERROR( "boost::polymorphic_cast<Derived*>( base ) threw std::bad_cast" );
    }

    Base2 * base2;

    try
    {
        base2 = boost::polymorphic_cast<Base2*>( base ); // crosscast

        BOOST_TEST( base2 != 0 );

        if( base2 != 0 )
        {
            BOOST_TEST_EQ( base2->kind2(), "Base2" );
        }
    }
    catch( std::bad_cast const& )
    {
        BOOST_ERROR( "boost::polymorphic_cast<Base2*>( base ) threw std::bad_cast" );
    }

    delete base;
}

static void test_polymorphic_pointer_cast()
{
    Base * base = new Derived;

    Derived * derived;

    try
    {
        derived = boost::polymorphic_pointer_cast<Derived>( base );

        BOOST_TEST( derived != 0 );

        if( derived != 0 )
        {
            BOOST_TEST_EQ( derived->kind(), "Derived" );
        }
    }
    catch( std::bad_cast const& )
    {
        BOOST_ERROR( "boost::polymorphic_pointer_cast<Derived>( base ) threw std::bad_cast" );
    }

    Base2 * base2;

    try
    {
        base2 = boost::polymorphic_pointer_cast<Base2>( base ); // crosscast

        BOOST_TEST( base2 != 0 );

        if( base2 != 0 )
        {
            BOOST_TEST_EQ( base2->kind2(), "Base2" );
        }
    }
    catch( std::bad_cast const& )
    {
        BOOST_ERROR( "boost::polymorphic_pointer_cast<Base2>( base ) threw std::bad_cast" );
    }

    boost::shared_ptr<Base> sp_base( base );
    boost::shared_ptr<Base2> sp_base2;
    try
    {
        sp_base2 = boost::polymorphic_pointer_cast<Base2>( sp_base ); // crosscast

        BOOST_TEST( sp_base2 != 0 );

        if( sp_base2 != 0 )
        {
            BOOST_TEST_EQ( sp_base2->kind2(), "Base2" );
        }
    }
    catch( std::bad_cast const& )
    {
        BOOST_ERROR( "boost::polymorphic_pointer_cast<Base2>( sp_base ) threw std::bad_cast" );
    }

    // we do not `delete base;` because sahred_ptr is holding base
}

static void test_polymorphic_downcast()
{
    Base *base_pointer = new Derived;

    // test raw pointer cast
    Derived *derived_pointer = boost::polymorphic_downcast<Derived *>(base_pointer);

    BOOST_TEST(derived_pointer != 0);

    if (derived_pointer != 0)
    {
        BOOST_TEST_EQ(derived_pointer->kind(), "Derived");
    }

    // test reference cast
    Derived& derived_ref = boost::polymorphic_downcast<Derived&>(*base_pointer);
    BOOST_TEST_EQ(derived_ref.kind(), "Derived");

    delete base_pointer;
}

static void test_polymorphic_pointer_downcast_builtin()
{
    Base * base = new Derived;

    Derived * derived = boost::polymorphic_pointer_downcast<Derived>( base );

    BOOST_TEST( derived != 0 );

    if( derived != 0 )
    {
        BOOST_TEST_EQ( derived->kind(), "Derived" );
    }

    // polymorphic_pointer_downcast can't do crosscasts

    delete base;
}

static void test_polymorphic_pointer_downcast_boost_shared()
{
    boost::shared_ptr<Base> base (new Derived);

    boost::shared_ptr<Derived> derived = boost::polymorphic_pointer_downcast<Derived>( base );

    BOOST_TEST( derived != 0 );

    if( derived != 0 )
    {
        BOOST_TEST_EQ( derived->kind(), "Derived" );
    }
}

static void test_polymorphic_pointer_downcast_intrusive()
{
    boost::intrusive_ptr<Base> base (new Derived);

    boost::intrusive_ptr<Derived> derived = boost::polymorphic_pointer_downcast<Derived>( base );

    BOOST_TEST( derived != 0 );

    if( derived != 0 )
    {
        BOOST_TEST_EQ( derived->kind(), "Derived" );
    }
}

#ifndef BOOST_NO_CXX11_SMART_PTR

static void test_polymorphic_pointer_downcast_std_shared()
{
    std::shared_ptr<Base> base (new Derived);

    std::shared_ptr<Derived> derived = boost::polymorphic_pointer_downcast<Derived>( base );

    BOOST_TEST( derived != 0 );

    if( derived != 0 )
    {
        BOOST_TEST_EQ( derived->kind(), "Derived" );
    }
}

#endif

static void test_polymorphic_cast_fail()
{
    Base * base = new Base;

    BOOST_TEST_THROWS( boost::polymorphic_cast<Derived*>( base ), std::bad_cast );

    delete base;
}

static void test_polymorphic_pointer_cast_fail()
{
    Base * base = new Base;
    BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( base ), std::bad_cast );
    delete base;

    BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( boost::shared_ptr<Base>(new Base) ), std::bad_cast );

#ifndef BOOST_NO_CXX11_SMART_PTR
    BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( std::shared_ptr<Base>(new Base) ), std::bad_cast );
#endif

    BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( boost::intrusive_ptr<Base>(new Base) ), std::bad_cast );
}

static void test_polymorphic_downcast_fail()
{
    Base * base_pointer = new Base;

    {
        // test raw pointer cast

        int old_count = assertion_failed_count;
        expect_assertion = true;

        BOOST_TEST_THROWS(boost::polymorphic_downcast<Derived *>(base_pointer), expected_assertion); // should assert

        BOOST_TEST_EQ(assertion_failed_count, old_count + 1);
        expect_assertion = false;
    }
    {
        // test reference cast

        int old_count = assertion_failed_count;
        expect_assertion = true;

        BOOST_TEST_THROWS(boost::polymorphic_downcast<Derived &>(*base_pointer), expected_assertion); // should assert

        BOOST_TEST_EQ(assertion_failed_count, old_count + 1);
        expect_assertion = false;
    }

    delete base_pointer;
}

static void test_polymorphic_pointer_downcast_builtin_fail()
{
    Base * base = new Base;

    int old_count = assertion_failed_count;
    expect_assertion = true;

    BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion ); // should assert

    BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
    expect_assertion = false;

    delete base;
}

static void test_polymorphic_pointer_downcast_boost_shared_fail()
{
    boost::shared_ptr<Base> base (new Base);

    int old_count = assertion_failed_count;
    expect_assertion = true;

    BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion ); // should assert

    BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
    expect_assertion = false;
}

#ifndef BOOST_NO_CXX11_SMART_PTR

static void test_polymorphic_pointer_downcast_std_shared_fail()
{
    std::shared_ptr<Base> base (new Base);

    int old_count = assertion_failed_count;
    expect_assertion = true;

    BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion ); // should assert

    BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
    expect_assertion = false;
}

#endif

static void test_polymorphic_pointer_downcast_intrusive_fail()
{
    boost::intrusive_ptr<Base> base (new Base);

    int old_count = assertion_failed_count;
    expect_assertion = true;

    BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion); // should assert

    BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
    expect_assertion = false;
}

int main()
{
    test_polymorphic_cast();
    test_polymorphic_pointer_cast();
    test_polymorphic_downcast();
    test_polymorphic_pointer_downcast_builtin();
    test_polymorphic_pointer_downcast_boost_shared();
    test_polymorphic_pointer_downcast_intrusive();
    test_polymorphic_cast_fail();
    test_polymorphic_pointer_cast_fail();
    test_polymorphic_downcast_fail();
    test_polymorphic_pointer_downcast_builtin_fail();
    test_polymorphic_pointer_downcast_boost_shared_fail();
    test_polymorphic_pointer_downcast_intrusive_fail();

#ifndef BOOST_NO_CXX11_SMART_PTR
    test_polymorphic_pointer_downcast_std_shared();
    test_polymorphic_pointer_downcast_std_shared_fail();
#endif

    return boost::report_errors();
}