blob: 0d239b47d431b7fe812e896dc582e80da5e9ba6b [file] [log] [blame]
Marshall Clowf8da5b22011-06-03 13:54:37 +00001//===-------------------------- test_aux_runtime.cpp ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include <typeinfo>
11#include <iostream>
12
13// Test taken from 5.2.8.2
14// When typeid is applied to a glvalue expression whose type is a polymorphic
15// class type, (10.3), the result refers to a std::type_info object
16// representing the type of the most derived object (1.8) (that is, the
17// dynamic type) to which the glvalue refers. If the glvalue expression is
18// obtained by applying the unary * operator to a pointer(68) and the pointer
19// is a null pointer value (4.10), the typeid expression throws the
20// std::bad_typeid exception (18.7.3).
21//
22// 68) If p is an expression of pointer type, then *p, (*p), *(p),
23// ((*p)), *((p)), and so on all meet this requirement.
24bool bad_typeid_test () {
25 class A { virtual void f() {}};
26 class B { virtual void g() {}};
27
28 B *bp = NULL;
29 try { typeid(*bp) == typeid (A); }
30 catch ( const std::bad_typeid &bc ) { return true; }
31 return false;
32 }
33
34
35// The value of a failed cast to pointer type is the null pointer value of
36// the required result type. A failed cast to reference type throws
37// std::bad_cast (18.7.2).
38bool bad_cast_test () {
39 class A { virtual void f() {}};
40 class B { virtual void g() {}};
41 class D : public virtual A, private B {};
42
43 D d;
44 B *bp = (B*)&d; // cast needed to break protection
45 try { D &dr = dynamic_cast<D&> (*bp); }
46 catch ( const std::bad_cast &bc ) { return true; }
47 return false;
48 }
49
50int main ( int argc, char *argv [] ) {
51 int ret_val = 0;
52
53 if ( !bad_typeid_test ()) {
54 std::cerr << "TypeID test failed!" << std::endl;
55 ret_val = 1;
56 }
57
58 if ( !bad_cast_test ()) {
59 std::cerr << "Bad cast test failed!" << std::endl;
60 ret_val = 1;
61 }
62
63 return ret_val;
64 }