Added small RTTI test.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@14916 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2005-11-14 15:08:51 +00:00
parent b66d753712
commit d28317fcba
3 changed files with 91 additions and 0 deletions

View File

@ -29,4 +29,5 @@ SubInclude HAIKU_TOP src tests apps ;
SubInclude HAIKU_TOP src tests bin ; SubInclude HAIKU_TOP src tests bin ;
SubInclude HAIKU_TOP src tests kernel ; SubInclude HAIKU_TOP src tests kernel ;
SubInclude HAIKU_TOP src tests kits ; SubInclude HAIKU_TOP src tests kits ;
SubInclude HAIKU_TOP src tests misc ;
SubInclude HAIKU_TOP src tests servers ; SubInclude HAIKU_TOP src tests servers ;

5
src/tests/misc/Jamfile Normal file
View File

@ -0,0 +1,5 @@
SubDir HAIKU_TOP src tests misc ;
SetSubDirSupportedPlatformsBeOSCompatible ;
SimpleTest rtti-test : rtti-test.cpp : be ;

View File

@ -0,0 +1,85 @@
#include <stdio.h>
#include <string>
#include <typeinfo>
#include <Directory.h>
#include <File.h>
using std::string;
struct A {
A() {}
virtual ~A() {}
int a;
};
struct B : A {
B() {}
virtual ~B() {}
int b;
};
struct C : A {
C() {}
virtual ~C() {}
int c;
};
template<typename A, typename B, typename C>
static void
rtti_test(A *a, const char *inputClass, const char *classNameA,
const char *classNameB, const char *classNameC)
{
printf("class %s\n", inputClass);
printf(" dynamic_cast<%s*>(a): %p\n", classNameA, dynamic_cast<A*>(a));
printf(" dynamic_cast<%s*>(a): %p\n", classNameB, dynamic_cast<B*>(a));
printf(" dynamic_cast<%s*>(a): %p\n", classNameC, dynamic_cast<C*>(a));
const std::type_info *info = &typeid(*a);
printf(" typeinfo: %p, name: %s\n", info, (info ? info->name() : NULL));
}
int
main()
{
// test with artificial classes defined in this file
#define RTTI_TEST(obj, className) rtti_test<A, B, C>(obj, className, \
"A", "B", "C")
A a;
B b;
C c;
printf("A: %p\n", &a);
printf("B: %p\n", &b);
printf("C: %p\n", &c);
RTTI_TEST(&a, "A");
RTTI_TEST(&b, "B");
RTTI_TEST(&c, "C");
// test with real classes defined in a library
#undef RTTI_TEST
#define RTTI_TEST(obj, className) rtti_test<BNode, BFile, BDirectory>(obj, \
className, "BNode", "BFile", "BDirectory")
BNode node;
BFile file;
BDirectory dir;
printf("BNode: %p\n", &node);
printf("BFile: %p\n", &file);
printf("BDirectory: %p\n", &dir);
RTTI_TEST(&node, "BNode");
RTTI_TEST(&file, "BFile");
RTTI_TEST(&dir, "BDirectory");
return 0;
}