| [29] | 1 | // boost utility cast test program -----------------------------------------// |
|---|
| 2 | |
|---|
| 3 | // (C) Copyright Beman Dawes, Dave Abrahams 1999. Distributed under the Boost |
|---|
| 4 | // Software License, Version 1.0. (See accompanying file |
|---|
| 5 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 6 | |
|---|
| 7 | // See http://www.boost.org for most recent version including documentation. |
|---|
| 8 | |
|---|
| 9 | // Revision History |
|---|
| 10 | // 28 Set 04 factored out numeric_cast<> test (Fernando Cacciola) |
|---|
| 11 | // 20 Jan 01 removed use of <limits> for portability to raw GCC (David Abrahams) |
|---|
| 12 | // 28 Jun 00 implicit_cast removed (Beman Dawes) |
|---|
| 13 | // 30 Aug 99 value_cast replaced by numeric_cast |
|---|
| 14 | // 3 Aug 99 Initial Version |
|---|
| 15 | |
|---|
| 16 | #include <iostream> |
|---|
| 17 | #include <climits> |
|---|
| 18 | #include <cfloat> // for DBL_MAX (Peter Schmid) |
|---|
| 19 | #include <boost/cast.hpp> |
|---|
| 20 | |
|---|
| 21 | # if SCHAR_MAX == LONG_MAX |
|---|
| 22 | # error "This test program doesn't work if SCHAR_MAX == LONG_MAX" |
|---|
| 23 | # endif |
|---|
| 24 | |
|---|
| 25 | using namespace boost; |
|---|
| 26 | using std::cout; |
|---|
| 27 | |
|---|
| 28 | namespace |
|---|
| 29 | { |
|---|
| 30 | struct Base |
|---|
| 31 | { |
|---|
| 32 | virtual char kind() { return 'B'; } |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | struct Base2 |
|---|
| 36 | { |
|---|
| 37 | virtual char kind2() { return '2'; } |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | struct Derived : public Base, Base2 |
|---|
| 41 | { |
|---|
| 42 | virtual char kind() { return 'D'; } |
|---|
| 43 | }; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | int main( int argc, char * argv[] ) |
|---|
| 48 | { |
|---|
| 49 | # ifdef NDEBUG |
|---|
| 50 | cout << "NDEBUG is defined\n"; |
|---|
| 51 | # else |
|---|
| 52 | cout << "NDEBUG is not defined\n"; |
|---|
| 53 | # endif |
|---|
| 54 | |
|---|
| 55 | cout << "\nBeginning tests...\n"; |
|---|
| 56 | |
|---|
| 57 | // test polymorphic_cast ---------------------------------------------------// |
|---|
| 58 | |
|---|
| 59 | // tests which should succeed |
|---|
| 60 | Base * base = new Derived; |
|---|
| 61 | Base2 * base2 = 0; |
|---|
| 62 | Derived * derived = 0; |
|---|
| 63 | derived = polymorphic_downcast<Derived*>( base ); // downcast |
|---|
| 64 | assert( derived->kind() == 'D' ); |
|---|
| 65 | |
|---|
| 66 | derived = 0; |
|---|
| 67 | derived = polymorphic_cast<Derived*>( base ); // downcast, throw on error |
|---|
| 68 | assert( derived->kind() == 'D' ); |
|---|
| 69 | |
|---|
| 70 | base2 = polymorphic_cast<Base2*>( base ); // crosscast |
|---|
| 71 | assert( base2->kind2() == '2' ); |
|---|
| 72 | |
|---|
| 73 | // tests which should result in errors being detected |
|---|
| 74 | int err_count = 0; |
|---|
| 75 | base = new Base; |
|---|
| 76 | |
|---|
| 77 | if ( argc > 1 && *argv[1] == '1' ) |
|---|
| 78 | { derived = polymorphic_downcast<Derived*>( base ); } // #1 assert failure |
|---|
| 79 | |
|---|
| 80 | bool caught_exception = false; |
|---|
| 81 | try { derived = polymorphic_cast<Derived*>( base ); } |
|---|
| 82 | catch (std::bad_cast) |
|---|
| 83 | { cout<<"caught bad_cast\n"; caught_exception = true; } |
|---|
| 84 | if ( !caught_exception ) ++err_count; |
|---|
| 85 | // the following is just so generated code can be inspected |
|---|
| 86 | if ( derived->kind() == 'B' ) ++err_count; |
|---|
| 87 | |
|---|
| 88 | cout << err_count << " errors detected\nTest " |
|---|
| 89 | << (err_count==0 ? "passed\n" : "failed\n"); |
|---|
| 90 | return err_count; |
|---|
| 91 | } // main |
|---|