/////////////////////////////////////////////////////////////////////////////// // Unit Test for Loki // // Copyright Terje Slettebų and Pavel Vozenilek 2002. // // Permission to use, copy, modify, and distribute this software for any // purpose is hereby granted without fee, provided that this copyright and // permissions notice appear in all copies and derivatives. // // This software is provided "as is" without express or implied warranty. // // Last update: September 16, 2002 /////////////////////////////////////////////////////////////////////////////// #ifndef SINGLETONTEST_H #define SINGLETONTEST_H #include #include #include "UnitTest.h" #define MAKE_TEST(name)\ if(singletonTest && name::Instance().a != 99)\ singletonTest=false;\ ++name::Instance().a;\ if(singletonTest && name::Instance().a != 100)\ singletonTest=false; /////////////////////////////////////////////////////////////////////////////// // SingletonTest /////////////////////////////////////////////////////////////////////////////// template class MyClass { public: MyClass() : a(99), wasDestroyed(false) {} virtual ~MyClass() { assert(!wasDestroyed); wasDestroyed = true; } public: int a; bool wasDestroyed; }; inline unsigned GetLongevity(MyClass<3> *) { return 6; } inline unsigned GetLongevity(MyClass<7> *) { return 5; } inline unsigned GetLongevity(MyClass<11> *) { return 4; } inline unsigned GetLongevity(MyClass<15> *) { return 1; } inline unsigned GetLongevity(MyClass<19> *) { return 2; } inline unsigned GetLongevity(MyClass<23> *) { return 3; } namespace { using namespace Loki; typedef SingletonHolder > t0; typedef SingletonHolder, CreateUsingNew, DefaultLifetime, SingleThreaded> t1; typedef SingletonHolder, CreateUsingNew, PhoenixSingleton, SingleThreaded> t2; typedef SingletonHolder, CreateUsingNew, SingletonWithLongevity, SingleThreaded> t3; typedef SingletonHolder, CreateUsingNew, NoDestroy, SingleThreaded> t4; typedef SingletonHolder, CreateUsingMalloc, DefaultLifetime, SingleThreaded> t9; typedef SingletonHolder, CreateUsingMalloc, PhoenixSingleton, SingleThreaded> t10; typedef SingletonHolder, CreateUsingMalloc, SingletonWithLongevity, SingleThreaded> t11; typedef SingletonHolder, CreateUsingMalloc, NoDestroy, SingleThreaded> t12; typedef SingletonHolder, CreateStatic, DefaultLifetime, SingleThreaded> t17; typedef SingletonHolder, CreateStatic, PhoenixSingleton, SingleThreaded> t18; typedef SingletonHolder, CreateStatic, SingletonWithLongevity, SingleThreaded> t19; typedef SingletonHolder, CreateStatic, NoDestroy, SingleThreaded> t20; #if !__INTEL_COMPILER && !__GNUC__ typedef SingletonHolder, CreateUsingNew, DefaultLifetime, ClassLevelLockable> t5; typedef SingletonHolder, CreateUsingNew, PhoenixSingleton, ClassLevelLockable> t6; typedef SingletonHolder, CreateUsingNew, SingletonWithLongevity, ClassLevelLockable> t7; typedef SingletonHolder, CreateUsingNew, NoDestroy, ClassLevelLockable> t8; typedef SingletonHolder, CreateUsingMalloc, DefaultLifetime, ClassLevelLockable> t13; typedef SingletonHolder, CreateUsingMalloc, PhoenixSingleton, ClassLevelLockable> t14; typedef SingletonHolder, CreateUsingMalloc, SingletonWithLongevity, ClassLevelLockable> t15; typedef SingletonHolder, CreateUsingMalloc, NoDestroy, ClassLevelLockable> t16; typedef SingletonHolder, CreateStatic, DefaultLifetime, ClassLevelLockable> t21; typedef SingletonHolder, CreateStatic, PhoenixSingleton, ClassLevelLockable> t22; typedef SingletonHolder, CreateStatic, SingletonWithLongevity, ClassLevelLockable> t23; typedef SingletonHolder, CreateStatic, NoDestroy, ClassLevelLockable> t24; #endif } class SingletonTest : public Test { public: SingletonTest() : Test("Singleton.h") {} virtual void execute(TestResult &result) { printName(result); using namespace Loki; singletonTest=true; MAKE_TEST(t0) MAKE_TEST(t1) MAKE_TEST(t2) MAKE_TEST(t3) MAKE_TEST(t4) MAKE_TEST(t9) MAKE_TEST(t10) MAKE_TEST(t11) MAKE_TEST(t12) MAKE_TEST(t17) MAKE_TEST(t18) MAKE_TEST(t19) MAKE_TEST(t20) #if !__INTEL_COMPILER && !__GNUC__ MAKE_TEST(t5) MAKE_TEST(t6) MAKE_TEST(t7) MAKE_TEST(t8) MAKE_TEST(t13) MAKE_TEST(t14) MAKE_TEST(t15) MAKE_TEST(t16) MAKE_TEST(t21) MAKE_TEST(t22) MAKE_TEST(t23) MAKE_TEST(t24) #endif testAssert("Singleton",singletonTest,result); std::cout << '\n'; } private: bool singletonTest; }; #endif