1 //  cast_tests.cpp  -- The Boost Lambda Library ------------------
2 //
3 // Copyright (C) 2000-2003 Jaakko Jarvi ([email protected])
4 // Copyright (C) 2000-2003 Gary Powell ([email protected])
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:
class_name() const30   virtual const char* class_name() const { return "const base"; }
class_name()31   virtual const char* class_name() { return "base"; }
~base()32   virtual ~base() {}
33 };
34 
35 class derived : public base {
36   int y[100];
37 public:
class_name() const38   virtual const char* class_name() const { return "const derived"; }
class_name()39   virtual const char* class_name() { return "derived"; }
40 };
41 
42 
43 
44 
do_test()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(b) = ll_static_cast<base *>(p_derived))();
54   (var(d) = ll_static_cast<derived *>(b))();
55 
56   BOOST_TEST_CSTR_EQ(b->class_name(), "derived");
57   BOOST_TEST_CSTR_EQ(d->class_name(), "derived");
58 
59   (var(b) = ll_dynamic_cast<derived *>(b))();
60   BOOST_TEST_NE(b, static_cast<base *>(NULL));
61   BOOST_TEST_CSTR_EQ(b->class_name(), "derived");
62 
63   (var(d) = ll_dynamic_cast<derived *>(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(d) = ll_const_cast<derived *>(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(ip) = ll_reinterpret_cast<int *>(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 
main()103 int main()
104 {
105   do_test();
106   return boost::report_errors();
107 }
108