1 // 2 // sp_array_cast_test.cpp 3 // 4 // Copyright (c) 2012 Peter Dimov 5 // 6 // Distributed under the Boost Software License, Version 1.0. 7 // See accompanying file LICENSE_1_0.txt or copy at 8 // http://www.boost.org/LICENSE_1_0.txt 9 // 10 11 #include <boost/shared_ptr.hpp> 12 #include <boost/core/lightweight_test.hpp> 13 14 struct X 15 { 16 }; 17 static_cast_test()18void static_cast_test() 19 { 20 { 21 boost::shared_ptr<void> pv; 22 23 boost::shared_ptr<int[]> pi = boost::static_pointer_cast<int[]>( pv ); 24 BOOST_TEST( pi.get() == 0 ); 25 26 boost::shared_ptr<int[3]> pi2 = boost::static_pointer_cast<int[3]>( pv ); 27 BOOST_TEST( pi2.get() == 0 ); 28 29 boost::shared_ptr<X[]> px = boost::static_pointer_cast<X[]>( pv ); 30 BOOST_TEST( px.get() == 0 ); 31 32 boost::shared_ptr<X[5]> px2 = boost::static_pointer_cast<X[5]>( pv ); 33 BOOST_TEST( px2.get() == 0 ); 34 } 35 36 { 37 boost::shared_ptr<int[]> pi( new int[2] ); 38 boost::shared_ptr<void> pv( pi ); 39 40 boost::shared_ptr<int[]> pi2 = boost::static_pointer_cast<int[]>( pv ); 41 BOOST_TEST(pi.get() == pi2.get()); 42 BOOST_TEST(!(pi < pi2 || pi2 < pi)); 43 44 boost::shared_ptr<int[2]> pi3 = boost::static_pointer_cast<int[2]>( pv ); 45 BOOST_TEST(pi.get() == pi3.get()); 46 BOOST_TEST(!(pi < pi3 || pi3 < pi)); 47 48 boost::shared_ptr<void> pv2( pi3 ); 49 50 boost::shared_ptr<int[]> pi4 = boost::static_pointer_cast<int[]>( pv2 ); 51 BOOST_TEST(pi.get() == pi4.get()); 52 BOOST_TEST(!(pi < pi4 || pi4 < pi)); 53 } 54 55 { 56 boost::shared_ptr<X[]> px( new X[4] ); 57 boost::shared_ptr<void> pv( px ); 58 59 boost::shared_ptr<X[]> px2 = boost::static_pointer_cast<X[]>( pv ); 60 BOOST_TEST(px.get() == px2.get()); 61 BOOST_TEST(!(px < px2 || px2 < px)); 62 63 boost::shared_ptr<X[4]> px3 = boost::static_pointer_cast<X[4]>( pv ); 64 BOOST_TEST(px.get() == px3.get()); 65 BOOST_TEST(!(px < px3 || px3 < px)); 66 67 boost::shared_ptr<void> pv2( px3 ); 68 69 boost::shared_ptr<X[]> px4 = boost::static_pointer_cast<X[]>( pv2 ); 70 BOOST_TEST(px.get() == px4.get()); 71 BOOST_TEST(!(px < px4 || px4 < px)); 72 } 73 } 74 const_cast_test()75void const_cast_test() 76 { 77 { 78 boost::shared_ptr<int const volatile[]> px; 79 80 boost::shared_ptr<int[]> px2 = boost::const_pointer_cast<int[]>(px); 81 BOOST_TEST( px2.get() == 0 ); 82 } 83 84 { 85 boost::shared_ptr<int const volatile[2]> px; 86 87 boost::shared_ptr<int[2]> px2 = boost::const_pointer_cast<int[2]>(px); 88 BOOST_TEST( px2.get() == 0 ); 89 } 90 91 { 92 boost::shared_ptr<X const volatile[]> px; 93 94 boost::shared_ptr<X[]> px2 = boost::const_pointer_cast<X[]>(px); 95 BOOST_TEST( px2.get() == 0 ); 96 } 97 98 { 99 boost::shared_ptr<X const volatile[5]> px; 100 101 boost::shared_ptr<X[5]> px2 = boost::const_pointer_cast<X[5]>(px); 102 BOOST_TEST( px2.get() == 0 ); 103 } 104 105 { 106 boost::shared_ptr<int const volatile[]> px( new int[3] ); 107 108 boost::shared_ptr<int[]> px2 = boost::const_pointer_cast<int[]>(px); 109 BOOST_TEST(px.get() == px2.get()); 110 BOOST_TEST(!(px < px2 || px2 < px)); 111 } 112 113 { 114 boost::shared_ptr<int const volatile[3]> px( new int[3] ); 115 116 boost::shared_ptr<int[3]> px2 = boost::const_pointer_cast<int[3]>(px); 117 BOOST_TEST(px.get() == px2.get()); 118 BOOST_TEST(!(px < px2 || px2 < px)); 119 } 120 121 { 122 boost::shared_ptr<X const volatile[]> px( new X[4] ); 123 124 boost::shared_ptr<X[]> px2 = boost::const_pointer_cast<X[]>(px); 125 BOOST_TEST(px.get() == px2.get()); 126 BOOST_TEST(!(px < px2 || px2 < px)); 127 } 128 129 { 130 boost::shared_ptr<X const volatile[4]> px( new X[4] ); 131 132 boost::shared_ptr<X[4]> px2 = boost::const_pointer_cast<X[4]>(px); 133 BOOST_TEST(px.get() == px2.get()); 134 BOOST_TEST(!(px < px2 || px2 < px)); 135 } 136 } 137 reinterpret_cast_test()138void reinterpret_cast_test() 139 { 140 { 141 boost::shared_ptr<int[]> pi; 142 BOOST_TEST( pi.get() == 0 ); 143 144 boost::shared_ptr<int[3]> pi2 = boost::reinterpret_pointer_cast<int[3]>( pi ); 145 BOOST_TEST( pi2.get() == 0 ); 146 147 boost::shared_ptr<int[6]> pi3 = boost::reinterpret_pointer_cast<int[6]>( pi2 ); 148 BOOST_TEST( pi3.get() == 0 ); 149 } 150 151 { 152 boost::shared_ptr<X[]> px; 153 BOOST_TEST( px.get() == 0 ); 154 155 boost::shared_ptr<X[5]> px2 = boost::reinterpret_pointer_cast<X[5]>( px ); 156 BOOST_TEST( px2.get() == 0 ); 157 158 boost::shared_ptr<X[9]> px3 = boost::reinterpret_pointer_cast<X[9]>( px2 ); 159 BOOST_TEST( px3.get() == 0 ); 160 } 161 162 { 163 boost::shared_ptr<int[]> pi( new int[2] ); 164 165 boost::shared_ptr<int[2]> pi2 = boost::reinterpret_pointer_cast<int[2]>( pi ); 166 BOOST_TEST(pi.get() == pi2.get()); 167 BOOST_TEST(!(pi < pi2 || pi2 < pi)); 168 169 boost::shared_ptr<int[1]> pi3 = boost::reinterpret_pointer_cast<int[1]>( pi2 ); 170 BOOST_TEST(pi.get() == pi3.get()); 171 BOOST_TEST(!(pi < pi3 || pi3 < pi)); 172 173 boost::shared_ptr<int[]> pi4 = boost::reinterpret_pointer_cast<int[]>( pi3 ); 174 BOOST_TEST(pi.get() == pi4.get()); 175 BOOST_TEST(!(pi < pi4 || pi4 < pi)); 176 } 177 178 { 179 boost::shared_ptr<X[]> px( new X[4] ); 180 181 boost::shared_ptr<X[7]> px2 = boost::reinterpret_pointer_cast<X[7]>( px ); 182 BOOST_TEST(px.get() == px2.get()); 183 BOOST_TEST(!(px < px2 || px2 < px)); 184 185 boost::shared_ptr<X[4]> px3 = boost::reinterpret_pointer_cast<X[4]>( px2 ); 186 BOOST_TEST(px.get() == px3.get()); 187 BOOST_TEST(!(px < px3 || px3 < px)); 188 189 boost::shared_ptr<X[]> px4 = boost::reinterpret_pointer_cast<X[]>( px3 ); 190 BOOST_TEST(px.get() == px4.get()); 191 BOOST_TEST(!(px < px4 || px4 < px)); 192 } 193 } 194 main()195int main() 196 { 197 static_cast_test(); 198 const_cast_test(); 199 reinterpret_cast_test(); 200 201 return boost::report_errors(); 202 } 203