| 1 | // cast_tests.cpp -- The Boost Lambda Library ------------------ |
| 2 | // |
| 3 | // Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi) |
| 4 | // Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com) |
| 5 | // |
| 6 | // Distributed under the Boost Software License, Version 1.0. (See |
| 7 | // accompanying file LICENSE_1_0.txt or copy at |
| 8 | // http://www.boost.org/LICENSE_1_0.txt) |
| 9 | // |
| 10 | // For more information, see www.boost.org |
| 11 | |
| 12 | // ----------------------------------------------------------------------- |
| 13 | |
| 14 | |
| 15 | #include <boost/core/lightweight_test.hpp> |
| 16 | |
| 17 | |
| 18 | #include "boost/lambda/lambda.hpp" |
| 19 | |
| 20 | #include "boost/lambda/casts.hpp" |
| 21 | |
| 22 | #include <string> |
| 23 | |
| 24 | using namespace boost::lambda; |
| 25 | using namespace std; |
| 26 | |
| 27 | class base { |
| 28 | int x; |
| 29 | public: |
| 30 | virtual const char* class_name() const { return "const base" ; } |
| 31 | virtual const char* class_name() { return "base" ; } |
| 32 | virtual ~base() {} |
| 33 | }; |
| 34 | |
| 35 | class derived : public base { |
| 36 | int y[100]; |
| 37 | public: |
| 38 | virtual const char* class_name() const { return "const derived" ; } |
| 39 | virtual const char* class_name() { return "derived" ; } |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | void do_test() { |
| 46 | |
| 47 | derived *p_derived = new derived; |
| 48 | base *p_base = new base; |
| 49 | |
| 50 | base *b = NULL; |
| 51 | derived *d = NULL; |
| 52 | |
| 53 | (var(t&: b) = ll_static_cast<base *>(a1: p_derived))(); |
| 54 | (var(t&: d) = ll_static_cast<derived *>(a1: b))(); |
| 55 | |
| 56 | BOOST_TEST_CSTR_EQ(b->class_name(), "derived" ); |
| 57 | BOOST_TEST_CSTR_EQ(d->class_name(), "derived" ); |
| 58 | |
| 59 | (var(t&: b) = ll_dynamic_cast<derived *>(a1: b))(); |
| 60 | BOOST_TEST_NE(b, static_cast<base *>(NULL)); |
| 61 | BOOST_TEST_CSTR_EQ(b->class_name(), "derived" ); |
| 62 | |
| 63 | (var(t&: d) = ll_dynamic_cast<derived *>(a1: p_base))(); |
| 64 | BOOST_TEST_EQ(d, static_cast<derived *>(NULL)); |
| 65 | |
| 66 | |
| 67 | |
| 68 | const derived* p_const_derived = p_derived; |
| 69 | |
| 70 | BOOST_TEST_CSTR_EQ(p_const_derived->class_name(), "const derived" ); |
| 71 | (var(t&: d) = ll_const_cast<derived *>(a1: p_const_derived))(); |
| 72 | BOOST_TEST_CSTR_EQ(d->class_name(), "derived" ); |
| 73 | |
| 74 | int i = 10; |
| 75 | char* cp = reinterpret_cast<char*>(&i); |
| 76 | |
| 77 | int* ip; |
| 78 | (var(t&: ip) = ll_reinterpret_cast<int *>(a1: cp))(); |
| 79 | BOOST_TEST_EQ(*ip, 10); |
| 80 | |
| 81 | |
| 82 | // typeid |
| 83 | |
| 84 | BOOST_TEST_CSTR_EQ(ll_typeid(d)().name(), typeid(d).name()); |
| 85 | |
| 86 | |
| 87 | // sizeof |
| 88 | |
| 89 | BOOST_TEST_EQ(ll_sizeof(_1)(p_derived), sizeof(p_derived)); |
| 90 | BOOST_TEST_EQ(ll_sizeof(_1)(*p_derived), sizeof(*p_derived)); |
| 91 | BOOST_TEST_EQ(ll_sizeof(_1)(p_base), sizeof(p_base)); |
| 92 | BOOST_TEST_EQ(ll_sizeof(_1)(*p_base), sizeof(*p_base)); |
| 93 | |
| 94 | int an_array[100]; |
| 95 | BOOST_TEST_EQ(ll_sizeof(_1)(an_array), 100 * sizeof(int)); |
| 96 | |
| 97 | delete p_derived; |
| 98 | delete p_base; |
| 99 | |
| 100 | |
| 101 | } |
| 102 | |
| 103 | int main() |
| 104 | { |
| 105 | do_test(); |
| 106 | return boost::report_errors(); |
| 107 | } |
| 108 | |