1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
///////////////////////////////////////////////////////////////////////////////
// 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.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef FACTORYTEST_H
#define FACTORYTEST_H
// $Id: FactoryTest.h 760 2006-10-17 20:36:13Z syntheticpp $
#include <loki/Factory.h>
///////////////////////////////////////////////////////////////////////////////
// FactoryTest
///////////////////////////////////////////////////////////////////////////////
namespace FactoryTestPrivate
{
class Shape
{
public:
virtual ~Shape() = 0;
};
inline Shape::~Shape() {}
class Polygon : public Shape
{
};
class Line : public Shape
{
};
class Circle : public Shape
{
};
Polygon *createPolygon() { return new Polygon; }
Line *createLine() { return new Line; }
Circle *createCircle() { return new Circle; }
Polygon *clonePolygon(Polygon *) { return new Polygon; }
Line *cloneLine(Line *) { return new Line; }
Circle *cloneCircle(Circle *) { return new Circle; }
typedef Loki::Factory<Shape, int> FactoryType;
bool testFactory()
{
FactoryType factory;
factory.Register(1, reinterpret_cast<Shape* (*)()>(createPolygon));
factory.Register(2, reinterpret_cast<Shape* (*)()>(createCircle));
factory.Register(3, reinterpret_cast<Shape* (*)()>(createLine));
Shape *s;
s = factory.CreateObject(1);
delete s;
bool test1=s!=NULL;
s = factory.CreateObject(2);
delete s;
bool test2=s!=NULL;
s = factory.CreateObject(3);
delete s;
bool test3=s!=NULL;
bool test4=true;
// try
// {
// factory.CreateObject(4);
//
// test4=false;
// }
// catch (std::exception&)
// {
// }
return test1 && test2 && test3 && test4;
}
typedef Loki::CloneFactory<Shape> CloneFactoryType;
bool testCloneFactory()
{
CloneFactoryType factory;
factory.Register(Loki::TypeInfo(typeid(Polygon)), reinterpret_cast<Shape* (*)(const Shape*)>(clonePolygon));
factory.Register(Loki::TypeInfo(typeid(Circle)), reinterpret_cast<Shape* (*)(const Shape*)>(cloneCircle));
factory.Register(Loki::TypeInfo(typeid(Line)), reinterpret_cast<Shape* (*)(const Shape*)>(cloneLine));
Polygon p;
Circle c;
Line l;
Shape *s;
s = factory.CreateObject(&p);
delete s;
bool test1=s!=NULL;
s = factory.CreateObject(&c);
delete s;
bool test2=s!=NULL;
s = factory.CreateObject(&l);
delete s;
bool test3=s!=NULL;
return test1 && test2 && test3;
}
}
class FactoryTest : public Test
{
public:
FactoryTest() : Test("Factory.h") {}
virtual void execute(TestResult &result)
{
printName(result);
bool test1=FactoryTestPrivate::testFactory();
bool test2=FactoryTestPrivate::testCloneFactory();
bool r=test1 && test2;
testAssert("Factory",r,result);
std::cout << '\n';
}
} factoryTest;
#endif
|