blob: 0c9a8f2141b1a7b9eec1f4be9d4e522668f5e9a7 [file] [log] [blame]
Marshall Clow05df4f92011-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
Asiri Rathnayake4174e8b2016-05-31 12:01:32 +000010// UNSUPPORTED: libcxxabi-no-exceptions
11
Marshall Clow05df4f92011-06-03 13:54:37 +000012#include <typeinfo>
13#include <iostream>
14
15// Test taken from 5.2.8.2
16// When typeid is applied to a glvalue expression whose type is a polymorphic
17// class type, (10.3), the result refers to a std::type_info object
18// representing the type of the most derived object (1.8) (that is, the
19// dynamic type) to which the glvalue refers. If the glvalue expression is
20// obtained by applying the unary * operator to a pointer(68) and the pointer
21// is a null pointer value (4.10), the typeid expression throws the
22// std::bad_typeid exception (18.7.3).
23//
24// 68) If p is an expression of pointer type, then *p, (*p), *(p),
25// ((*p)), *((p)), and so on all meet this requirement.
26bool bad_typeid_test () {
27 class A { virtual void f() {}};
28 class B { virtual void g() {}};
29
30 B *bp = NULL;
Eric Fiselier08bf03c2016-12-24 00:37:13 +000031 try {bool b = typeid(*bp) == typeid (A); ((void)b); }
32 catch ( const std::bad_typeid &) { return true; }
Marshall Clow05df4f92011-06-03 13:54:37 +000033 return false;
34 }
35
36
37// The value of a failed cast to pointer type is the null pointer value of
38// the required result type. A failed cast to reference type throws
39// std::bad_cast (18.7.2).
40bool bad_cast_test () {
41 class A { virtual void f() {}};
42 class B { virtual void g() {}};
43 class D : public virtual A, private B {};
44
45 D d;
46 B *bp = (B*)&d; // cast needed to break protection
Eric Fiselier08bf03c2016-12-24 00:37:13 +000047 try { D &dr = dynamic_cast<D&> (*bp); ((void)dr); }
48 catch ( const std::bad_cast & ) { return true; }
Marshall Clow05df4f92011-06-03 13:54:37 +000049 return false;
50 }
51
Eric Fiselier08bf03c2016-12-24 00:37:13 +000052int main ( ) {
Marshall Clow05df4f92011-06-03 13:54:37 +000053 int ret_val = 0;
54
55 if ( !bad_typeid_test ()) {
56 std::cerr << "TypeID test failed!" << std::endl;
57 ret_val = 1;
58 }
59
60 if ( !bad_cast_test ()) {
61 std::cerr << "Bad cast test failed!" << std::endl;
62 ret_val = 1;
63 }
64
65 return ret_val;
66 }