cast_test.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include <boost/test/minimal.hpp> // see "Header Implementation Option"
  13. #include "boost/lambda/lambda.hpp"
  14. #include "boost/lambda/casts.hpp"
  15. #include <string>
  16. using namespace boost::lambda;
  17. using namespace std;
  18. class base {
  19. int x;
  20. public:
  21. virtual std::string class_name() const { return "const base"; }
  22. virtual std::string class_name() { return "base"; }
  23. virtual ~base() {}
  24. };
  25. class derived : public base {
  26. int y[100];
  27. public:
  28. virtual std::string class_name() const { return "const derived"; }
  29. virtual std::string class_name() { return "derived"; }
  30. };
  31. void do_test() {
  32. derived *p_derived = new derived;
  33. base *p_base = new base;
  34. base *b = 0;
  35. derived *d = 0;
  36. (var(b) = ll_static_cast<base *>(p_derived))();
  37. (var(d) = ll_static_cast<derived *>(b))();
  38. BOOST_CHECK(b->class_name() == "derived");
  39. BOOST_CHECK(d->class_name() == "derived");
  40. (var(b) = ll_dynamic_cast<derived *>(b))();
  41. BOOST_CHECK(b != 0);
  42. BOOST_CHECK(b->class_name() == "derived");
  43. (var(d) = ll_dynamic_cast<derived *>(p_base))();
  44. BOOST_CHECK(d == 0);
  45. const derived* p_const_derived = p_derived;
  46. BOOST_CHECK(p_const_derived->class_name() == "const derived");
  47. (var(d) = ll_const_cast<derived *>(p_const_derived))();
  48. BOOST_CHECK(d->class_name() == "derived");
  49. int i = 10;
  50. char* cp = reinterpret_cast<char*>(&i);
  51. int* ip;
  52. (var(ip) = ll_reinterpret_cast<int *>(cp))();
  53. BOOST_CHECK(*ip == 10);
  54. // typeid
  55. BOOST_CHECK(string(ll_typeid(d)().name()) == string(typeid(d).name()));
  56. // sizeof
  57. BOOST_CHECK(ll_sizeof(_1)(p_derived) == sizeof(p_derived));
  58. BOOST_CHECK(ll_sizeof(_1)(*p_derived) == sizeof(*p_derived));
  59. BOOST_CHECK(ll_sizeof(_1)(p_base) == sizeof(p_base));
  60. BOOST_CHECK(ll_sizeof(_1)(*p_base) == sizeof(*p_base));
  61. int an_array[100];
  62. BOOST_CHECK(ll_sizeof(_1)(an_array) == 100 * sizeof(int));
  63. delete p_derived;
  64. delete p_base;
  65. }
  66. int test_main(int, char *[]) {
  67. do_test();
  68. return 0;
  69. }