Home | History | Annotate | Download | only in test
      1 //===-------------------------- 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 // UNSUPPORTED: libcxxabi-no-exceptions
     11 
     12 #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.
     26 bool bad_typeid_test () {
     27     class A { virtual void f() {}};
     28     class B { virtual void g() {}};
     29 
     30     B *bp = NULL;
     31     try {bool b = typeid(*bp) == typeid (A); ((void)b); }
     32     catch ( const std::bad_typeid &) { return true; }
     33     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).
     40 bool 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
     47     try { D &dr = dynamic_cast<D&> (*bp); ((void)dr); }
     48     catch ( const std::bad_cast & ) { return true; }
     49     return false;
     50     }
     51 
     52 int main ( ) {
     53     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     }
     67