diff options
author | coryan <coryan@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 2009-08-01 12:42:14 +0000 |
---|---|---|
committer | coryan <coryan@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 2009-08-01 12:42:14 +0000 |
commit | f1b8f42c382f3fc8939d9c9924f6e3fadb6020bf (patch) | |
tree | 2cf23212457f35cb8e682464b498a64510a11aab /ACE/tests/Compiler_Features_13_Test.cpp | |
parent | fefc32997e6fd73caf0302ee794043ff943bde44 (diff) | |
download | ATCD-f1b8f42c382f3fc8939d9c9924f6e3fadb6020bf.tar.gz |
ChangeLogTag:Sat Aug 1 12:19:58 UTC 2009 Carlos O'Ryan <coryan@glamdring>
Diffstat (limited to 'ACE/tests/Compiler_Features_13_Test.cpp')
-rw-r--r-- | ACE/tests/Compiler_Features_13_Test.cpp | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/ACE/tests/Compiler_Features_13_Test.cpp b/ACE/tests/Compiler_Features_13_Test.cpp new file mode 100644 index 00000000000..f9f4317cbfc --- /dev/null +++ b/ACE/tests/Compiler_Features_13_Test.cpp @@ -0,0 +1,145 @@ +// $Id$ + +/** + * @file + * + * This program checks if the compiler / platform supports the + * standard cast operators template parameters. The motivation for + * this test was a discussion on the development mailing list, and the + * documentation was captured in: + * + * http://bugzilla.dre.vanderbilt.edu/show_bug.cgi?id=3715 + * + */ + +#include "test_config.h" + +#include <stdexcept> + +ACE_RCSID(tests, Compiler_Features_13_Test, "$Id$") + +namespace +{ + /** + * Helper class for test + */ + struct Base + { + virtual ~Base() + {} + }; + + /** + * Helper class for test + */ + struct Derived : public Base + { + int value; + }; + + /** + * Helper class for test + */ + struct Another : public Base + { + int x; + int y; + }; +} + + +int +run_main (int, ACE_TCHAR *[]) +{ + ACE_START_TEST (ACE_TEXT("Compiler_Features_13_Test")); + + // As usual, the exit status from the test is 0 on success, 1 on + // failure + int status = 0; + + { + // Make sure const cast works. Compilation is interesting, the + // functionality test here is just to make sure the compiler does + // not optimize things away ... + int x = 5; + int const & y = x; + const_cast<int&>(y) = 3; + + if (x != 3) + { + status = 1; + ACE_ERROR((LM_ERROR, + ACE_TEXT("Wrong value after const_cast," + " expected %d, got %d\n"), + 3, x)); + } + } + + // Make sure dynamic cast through pointers work ... + Derived d; + d.value = 24; + Base * b1 = &d; + Derived * d1 = dynamic_cast<Derived*>(b1); + if (d1 == 0) + { + status = 1; + ACE_ERROR((LM_ERROR, + ACE_TEXT("dynamic_cast returns null, expected value\n"))); + } + d1->value = 42; + if (d.value != 42) + { + ACE_ERROR((LM_ERROR, + ACE_TEXT("Wrong value after dynamic_cast, expected %d, got %d\n"), + 42, d.value)); + } + + // Make sure dynamic cast detects invalid casts + Another a; + Base * b2 = &a; + Derived * d2 = dynamic_cast<Derived*>(b2); + if (d2 != 0) + { + status = 1; + ACE_ERROR((LM_ERROR, + ACE_TEXT("dynamic_cast should return null\n"))); + } + + // Make sure dynamic cast raises an exception + Base & b3 = a; + try + { + (void) dynamic_cast<Derived&>(b3); + + status = 1; + ACE_ERROR((LM_ERROR, + ACE_TEXT("dynamic_cast should have raised exception\n"))); + } + catch(std::exception const &) + { + } + catch(...) + { + status = 1; + ACE_ERROR((LM_ERROR, + ACE_TEXT("dynamic_cast should have raised std::exception\n"))); + } + + { + // Just test these compile ... + double x = 42.0; + int y = static_cast<int>(x); + void * z = reinterpret_cast<void*>(y); + + if (z == 0) + { + ACE_ERROR((LM_ERROR, + ACE_TEXT("My hack to make sure the code is not " + "optimized away backfired!\n"))); + } + } + + ACE_END_TEST; + return status; +} + |