summaryrefslogtreecommitdiff
path: root/ACE/tests/Compiler_Features_13_Test.cpp
blob: 15308dc8af317c4db50fc52db42e20279001c8fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
 * 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>
#include <typeinfo>

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,")
                   ACE_TEXT(" 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")));
    }
  else
    {
      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 a bad_cast exception
  Base & b3 = a;
  try
    {
      (void) dynamic_cast<Derived&>(b3);

      status = 1;
      ACE_ERROR((LM_ERROR,
                 ACE_TEXT("dynamic_cast should have raised std::bad_cast\n")));
    }
  catch(std::bad_cast const &)
    {
    }
  catch(...)
    {
      status = 1;
      ACE_ERROR((LM_ERROR,
                 ACE_TEXT("dynamic_cast should have raised std::bad_cast\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 ")
                   ACE_TEXT("optimized away backfired!\n")));
      }
  }

  ACE_END_TEST;
  return status;
}