2005-09-24 15:25:20 +00:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Unit Test for Loki
|
|
|
|
|
//
|
|
|
|
|
// Copyright Terje Sletteb<65> 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.
|
|
|
|
|
//
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
#ifndef ABSTRACTFACTORYTEST_H
|
|
|
|
|
#define ABSTRACTFACTORYTEST_H
|
|
|
|
|
|
2006-10-17 20:36:13 +00:00
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
|
|
2005-09-24 15:25:20 +00:00
|
|
|
|
#include <memory>
|
|
|
|
|
#include <typeinfo>
|
|
|
|
|
#include <loki/AbstractFactory.h>
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// AbstractFactoryTest
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
class Soldier { public: virtual ~Soldier() {} };
|
|
|
|
|
class Monster { public: virtual ~Monster() {} };
|
|
|
|
|
class SuperMonster { public: virtual ~SuperMonster() {} };
|
|
|
|
|
|
|
|
|
|
class SillySoldier : public Soldier {};
|
|
|
|
|
class SillyMonster : public Monster {};
|
|
|
|
|
class SillySuperMonster : public SuperMonster {};
|
|
|
|
|
|
|
|
|
|
class BadSoldier : public Soldier {};
|
|
|
|
|
class BadMonster : public Monster {};
|
|
|
|
|
class BadSuperMonster : public SuperMonster {};
|
|
|
|
|
|
2005-10-30 13:49:44 +00:00
|
|
|
|
#ifndef LOKI_DISABLE_TYPELIST_MACROS
|
|
|
|
|
|
2005-09-26 07:33:05 +00:00
|
|
|
|
typedef Loki::AbstractFactory<LOKI_TYPELIST_3(Soldier, Monster, SuperMonster)> AbstractEnemyFactory;
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
|
|
|
|
typedef Loki::ConcreteFactory<AbstractEnemyFactory, Loki::OpNewFactoryUnit,
|
2005-09-26 07:33:05 +00:00
|
|
|
|
LOKI_TYPELIST_3(SillySoldier, SillyMonster, SillySuperMonster)> EasyLevelEnemyFactory;
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
|
|
|
|
typedef Loki::ConcreteFactory<AbstractEnemyFactory, Loki::OpNewFactoryUnit,
|
2005-09-26 07:33:05 +00:00
|
|
|
|
LOKI_TYPELIST_3(BadSoldier, BadMonster, BadSuperMonster)> HardLevelEnemyFactory;
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
2005-10-30 13:49:44 +00:00
|
|
|
|
#else // LOKI_DISABLE_TYPELIST_MACROS
|
|
|
|
|
|
|
|
|
|
typedef Loki::AbstractFactory<Seq<Soldier, Monster, SuperMonster>::Type > AbstractEnemyFactory;
|
|
|
|
|
|
|
|
|
|
typedef Loki::ConcreteFactory<AbstractEnemyFactory, Loki::OpNewFactoryUnit,
|
|
|
|
|
Seq<SillySoldier, SillyMonster, SillySuperMonster>::Type > EasyLevelEnemyFactory;
|
|
|
|
|
|
|
|
|
|
typedef Loki::ConcreteFactory<AbstractEnemyFactory, Loki::OpNewFactoryUnit,
|
|
|
|
|
Seq<BadSoldier, BadMonster, BadSuperMonster>::Type > HardLevelEnemyFactory;
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2005-09-24 15:25:20 +00:00
|
|
|
|
class AbstractFactoryTest : public Test
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
AbstractFactoryTest() : Test("AbstractFactory.h") {}
|
|
|
|
|
|
|
|
|
|
virtual void execute(TestResult &result)
|
|
|
|
|
{
|
|
|
|
|
printName(result);
|
|
|
|
|
|
|
|
|
|
using namespace Loki;
|
|
|
|
|
|
|
|
|
|
bool r;
|
|
|
|
|
|
|
|
|
|
std::auto_ptr<AbstractEnemyFactory> easyFactory(new EasyLevelEnemyFactory);
|
|
|
|
|
std::auto_ptr<AbstractEnemyFactory> hardFactory(new HardLevelEnemyFactory);
|
|
|
|
|
|
|
|
|
|
Soldier *s;
|
|
|
|
|
|
|
|
|
|
s = easyFactory->Create<Soldier>();
|
|
|
|
|
|
|
|
|
|
r= !!(typeid(*s)==typeid(SillySoldier)); //SGB !! eliminates bool-to-int performance warning
|
|
|
|
|
|
|
|
|
|
delete s;
|
|
|
|
|
|
|
|
|
|
#ifndef __BORLANDC__
|
|
|
|
|
|
|
|
|
|
s = hardFactory->Create<Soldier>(); //BCB bug!!! - always creates SillySoldier
|
|
|
|
|
|
|
|
|
|
r=r && typeid(*s)==typeid(BadSoldier);
|
|
|
|
|
|
|
|
|
|
delete s;
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
testAssert("AbstractFactory",r,result);
|
|
|
|
|
|
|
|
|
|
std::cout << '\n';
|
|
|
|
|
}
|
|
|
|
|
} abstractFactoryTest;
|
|
|
|
|
|
|
|
|
|
#endif
|