| 1 | // cast_tests.cpp -- The Boost Lambda Library ------------------ |
|---|
| 2 | // |
|---|
| 3 | // Copyright (C) 2000-2003 Jaakko Järvi (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/test/minimal.hpp> // see "Header Implementation Option" |
|---|
| 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 std::string class_name() const { return "const base"; } |
|---|
| 31 | virtual std::string class_name() { return "base"; } |
|---|
| 32 | |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | class derived : public base { |
|---|
| 36 | int y[100]; |
|---|
| 37 | public: |
|---|
| 38 | virtual std::string class_name() const { return "const derived"; } |
|---|
| 39 | virtual std::string 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 = 0; |
|---|
| 51 | derived *d = 0; |
|---|
| 52 | |
|---|
| 53 | (var(b) = ll_static_cast<base *>(p_derived))(); |
|---|
| 54 | (var(d) = ll_static_cast<derived *>(b))(); |
|---|
| 55 | |
|---|
| 56 | BOOST_CHECK(b->class_name() == "derived"); |
|---|
| 57 | BOOST_CHECK(d->class_name() == "derived"); |
|---|
| 58 | |
|---|
| 59 | (var(b) = ll_dynamic_cast<derived *>(b))(); |
|---|
| 60 | BOOST_CHECK(b != 0); |
|---|
| 61 | BOOST_CHECK(b->class_name() == "derived"); |
|---|
| 62 | |
|---|
| 63 | (var(d) = ll_dynamic_cast<derived *>(p_base))(); |
|---|
| 64 | BOOST_CHECK(d == 0); |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | const derived* p_const_derived = p_derived; |
|---|
| 69 | |
|---|
| 70 | BOOST_CHECK(p_const_derived->class_name() == "const derived"); |
|---|
| 71 | (var(d) = ll_const_cast<derived *>(p_const_derived))(); |
|---|
| 72 | BOOST_CHECK(d->class_name() == "derived"); |
|---|
| 73 | |
|---|
| 74 | int i = 10; |
|---|
| 75 | char* cp = reinterpret_cast<char*>(&i); |
|---|
| 76 | |
|---|
| 77 | int* ip; |
|---|
| 78 | (var(ip) = ll_reinterpret_cast<int *>(cp))(); |
|---|
| 79 | BOOST_CHECK(*ip == 10); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | // typeid |
|---|
| 83 | |
|---|
| 84 | BOOST_CHECK(string(ll_typeid(d)().name()) == string(typeid(d).name())); |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | // sizeof |
|---|
| 88 | |
|---|
| 89 | BOOST_CHECK(ll_sizeof(_1)(p_derived) == sizeof(p_derived)); |
|---|
| 90 | BOOST_CHECK(ll_sizeof(_1)(*p_derived) == sizeof(*p_derived)); |
|---|
| 91 | BOOST_CHECK(ll_sizeof(_1)(p_base) == sizeof(p_base)); |
|---|
| 92 | BOOST_CHECK(ll_sizeof(_1)(*p_base) == sizeof(*p_base)); |
|---|
| 93 | |
|---|
| 94 | int an_array[100]; |
|---|
| 95 | BOOST_CHECK(ll_sizeof(_1)(an_array) == 100 * sizeof(int)); |
|---|
| 96 | |
|---|
| 97 | delete p_derived; |
|---|
| 98 | delete p_base; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | int test_main(int, char *[]) { |
|---|
| 104 | |
|---|
| 105 | do_test(); |
|---|
| 106 | return 0; |
|---|
| 107 | } |
|---|