|
1 // |
|
2 // pointer_cast_test.cpp - a test for boost/pointer_cast.hpp |
|
3 // |
|
4 // Copyright (c) 2005 Ion Gaztaņaga |
|
5 // Copyright (c) 2005 Peter Dimov |
|
6 // |
|
7 // Distributed under the Boost Software License, Version 1.0. (See |
|
8 // accompanying file LICENSE_1_0.txt or copy at |
|
9 // http://www.boost.org/LICENSE_1_0.txt) |
|
10 // |
|
11 /* |
|
12 * Š Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved. |
|
13 */ |
|
14 |
|
15 #include <boost/pointer_cast.hpp> |
|
16 |
|
17 #include <boost/shared_ptr.hpp> |
|
18 #include <boost/scoped_ptr.hpp> |
|
19 #include <boost/get_pointer.hpp> |
|
20 |
|
21 #include <boost/detail/lightweight_test.hpp> |
|
22 |
|
23 #ifdef __SYMBIAN32__ |
|
24 #include "std_log_result.h" |
|
25 #define LOG_FILENAME_LINE __FILE__, __LINE__ |
|
26 #endif |
|
27 |
|
28 namespace |
|
29 { |
|
30 |
|
31 // Let's create these inheritance relationship: |
|
32 // |
|
33 // base base2 |
|
34 // | | |
|
35 // derived |
|
36 // | |
|
37 // derived_derived |
|
38 // |
|
39 |
|
40 class base |
|
41 { |
|
42 public: |
|
43 virtual ~base(){} |
|
44 int filler [5]; |
|
45 }; |
|
46 |
|
47 class base2 |
|
48 { |
|
49 public: |
|
50 |
|
51 virtual ~base2(){} |
|
52 int filler [5]; |
|
53 }; |
|
54 |
|
55 class derived |
|
56 : public base, public base2 |
|
57 { |
|
58 int filler [5]; |
|
59 }; |
|
60 |
|
61 class derived_derived |
|
62 : public derived |
|
63 { |
|
64 int filler [5]; |
|
65 }; |
|
66 |
|
67 // And now some simple check functions |
|
68 |
|
69 template <class BasePtr> |
|
70 bool check_dynamic_pointer_cast(const BasePtr &ptr) |
|
71 { |
|
72 //Check that dynamic_pointer_cast versus dynamic_cast |
|
73 return |
|
74 //Correct cast with dynamic_pointer_cast |
|
75 boost::get_pointer(boost::dynamic_pointer_cast<derived>(ptr)) == |
|
76 //Correct cast with dynamic_cast |
|
77 dynamic_cast<derived*>(boost::get_pointer(ptr)) |
|
78 && |
|
79 //Incorrect cast with dynamic_pointer_cast |
|
80 boost::get_pointer(boost::dynamic_pointer_cast<derived_derived>(ptr)) == |
|
81 //Incorrect cast with dynamic_cast |
|
82 dynamic_cast<derived_derived*>(boost::get_pointer(ptr)); |
|
83 } |
|
84 |
|
85 template <class BasePtr> |
|
86 bool check_static_pointer_cast(const BasePtr &ptr) |
|
87 { |
|
88 return |
|
89 //Cast base -> derived -> base2 using static_pointer_cast |
|
90 boost::get_pointer( |
|
91 boost::static_pointer_cast<base2>( |
|
92 boost::static_pointer_cast<derived>(ptr))) == |
|
93 //Now the same with static_cast |
|
94 static_cast<base2*>(static_cast<derived*>(boost::get_pointer(ptr))); |
|
95 } |
|
96 |
|
97 template <class BasePtr> |
|
98 bool check_const_pointer_cast(const BasePtr &ptr) |
|
99 { |
|
100 return |
|
101 //Unconst and const again using const_pointer_cast |
|
102 boost::get_pointer( |
|
103 boost::const_pointer_cast<const base> |
|
104 (boost::const_pointer_cast<base>(ptr))) == |
|
105 //Now the same with const_cast |
|
106 const_cast<const base*>(const_cast<base*>(boost::get_pointer(ptr))); |
|
107 } |
|
108 |
|
109 } |
|
110 |
|
111 int main() |
|
112 { |
|
113 std_log(LOG_FILENAME_LINE,"[Test Case for pointer_cast_test]"); |
|
114 { |
|
115 // Try casts with shared_ptr |
|
116 |
|
117 boost::shared_ptr<base> ptr(new derived); |
|
118 |
|
119 BOOST_TEST( check_dynamic_pointer_cast( ptr ) ); |
|
120 BOOST_TEST( check_static_pointer_cast( ptr ) ); |
|
121 BOOST_TEST( check_const_pointer_cast( ptr ) ); |
|
122 } |
|
123 |
|
124 { |
|
125 // Try casts with raw pointer |
|
126 |
|
127 boost::scoped_ptr<base> ptr(new derived); |
|
128 |
|
129 BOOST_TEST( check_dynamic_pointer_cast( ptr.get() ) ); |
|
130 BOOST_TEST( check_static_pointer_cast( ptr.get() ) ); |
|
131 BOOST_TEST( check_const_pointer_cast( ptr.get() ) ); |
|
132 } |
|
133 |
|
134 #ifdef __SYMBIAN32__ |
|
135 int failures = boost::report_errors(); |
|
136 if(failures) |
|
137 { |
|
138 std_log(LOG_FILENAME_LINE,"Result : Failed"); |
|
139 assert_failed = true; |
|
140 } |
|
141 else |
|
142 { |
|
143 std_log(LOG_FILENAME_LINE,"Result : Passed"); |
|
144 } |
|
145 std_log(LOG_FILENAME_LINE,"[End Test Case ]"); |
|
146 #endif |
|
147 testResultXml("pointer_cast_test"); |
|
148 close_log_file(); |
|
149 return failures; |
|
150 } |