Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / conversion / test / polymorphic_cast_test.cpp
1 //
2 // Test boost::polymorphic_cast, boost::polymorphic_downcast
3 //
4 // Copyright 1999 Beman Dawes
5 // Copyright 1999 Dave Abrahams
6 // Copyright 2014 Peter Dimov
7 //
8 // Distributed under the Boost Software License, Version 1.0.
9 //
10 // See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
11 //
12
13 #define BOOST_ENABLE_ASSERT_HANDLER
14 #include <boost/polymorphic_cast.hpp>
15 #include <boost/core/lightweight_test.hpp>
16 #include <string>
17
18 static bool expect_assertion = false;
19 static int assertion_failed_count = 0;
20
21 // BOOST_ASSERT custom handler
22 void boost::assertion_failed( char const * expr, char const * function, char const * file, long line )
23 {
24     if( expect_assertion )
25     {
26         ++assertion_failed_count;
27     }
28     else
29     {
30         BOOST_ERROR( "unexpected assertion" );
31
32         BOOST_LIGHTWEIGHT_TEST_OSTREAM
33           << file << "(" << line << "): assertion '" << expr << "' failed in function '"
34           << function << "'" << std::endl;
35     }
36 }
37
38 //
39
40 struct Base
41 {
42     virtual ~Base() {}
43     virtual std::string kind() { return "Base"; }
44 };
45
46 struct Base2
47 {
48     virtual ~Base2() {}
49     virtual std::string kind2() { return "Base2"; }
50 };
51
52 struct Derived : public Base, Base2
53 {
54     virtual std::string kind() { return "Derived"; }
55 };
56
57 static void test_polymorphic_cast()
58 {
59     Base * base = new Derived;
60
61     Derived * derived;
62     
63     try
64     {
65         derived = boost::polymorphic_cast<Derived*>( base );
66
67         BOOST_TEST( derived != 0 );
68
69         if( derived != 0 )
70         {
71             BOOST_TEST_EQ( derived->kind(), "Derived" );
72         }
73     }
74     catch( std::bad_cast const& )
75     {
76         BOOST_ERROR( "boost::polymorphic_cast<Derived*>( base ) threw std::bad_cast" );
77     }
78
79     Base2 * base2;
80
81     try
82     {
83         base2 = boost::polymorphic_cast<Base2*>( base ); // crosscast
84
85         BOOST_TEST( base2 != 0 );
86
87         if( base2 != 0 )
88         {
89             BOOST_TEST_EQ( base2->kind2(), "Base2" );
90         }
91     }
92     catch( std::bad_cast const& )
93     {
94         BOOST_ERROR( "boost::polymorphic_cast<Base2*>( base ) threw std::bad_cast" );
95     }
96
97     delete base;
98 }
99
100 static void test_polymorphic_downcast()
101 {
102     Base * base = new Derived;
103
104     Derived * derived = boost::polymorphic_downcast<Derived*>( base );
105
106     BOOST_TEST( derived != 0 );
107
108     if( derived != 0 )
109     {
110         BOOST_TEST_EQ( derived->kind(), "Derived" );
111     }
112
113     // polymorphic_downcast can't do crosscasts
114
115     delete base;
116 }
117
118 static void test_polymorphic_cast_fail()
119 {
120     Base * base = new Base;
121
122     BOOST_TEST_THROWS( boost::polymorphic_cast<Derived*>( base ), std::bad_cast );
123
124     delete base;
125 }
126
127 static void test_polymorphic_downcast_fail()
128 {
129     Base * base = new Base;
130
131     int old_count = assertion_failed_count;
132     expect_assertion = true;
133
134     boost::polymorphic_downcast<Derived*>( base ); // should assert
135
136     BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
137     expect_assertion = false;
138
139     delete base;
140 }
141
142 int main()
143 {
144     test_polymorphic_cast();
145     test_polymorphic_downcast();
146     test_polymorphic_cast_fail();
147     test_polymorphic_downcast_fail();
148
149     return boost::report_errors();
150 }