testObjectFactory.cc
Go to the documentation of this file.
1 
11 // header for unit test testObjectFactory
12 
13 #include <utl/ObjectFactory.h>
14 #include <string>
15 
16 #include <tst/Verify.h>
17 #include <cppunit/extensions/HelperMacros.h>
18 
19 using namespace utl;
20 using namespace tst;
21 using namespace std;
22 
23 
24 
25 
26 namespace {
27  // Test factory polymorphic class with a default constructor
28  class IdObjectBase {
29  public:
30  virtual ~IdObjectBase() { }
31  virtual int GetId() = 0;
32  };
33 
34  // Template to create concrete implementations of IdObjectBase
35  template<int id>
36  class IdObject : public IdObjectBase {
37  public:
38  int GetId() { return id; }
39  static IdObjectBase* Create()
40  { return new IdObject; }
41  };
42 
43  // Convenience typedef for factory
44  typedef ObjectFactory<IdObjectBase*, int> IdObjectFactory;
45 
46 
47  // Test factory polymorphic class with a one-argument constructor
48  class IdObjectCountBase {
49  public:
50  IdObjectCountBase() : fCount(0) { }
51  IdObjectCountBase(int i) : fCount(i) { }
52  virtual ~IdObjectCountBase() { }
53  virtual int GetId() = 0;
54  int GetCount() { return fCount; }
55  private:
56  int fCount;
57  };
58 
59  template<int id>
60  class IdObjectCount : public IdObjectCountBase {
61  public:
62  IdObjectCount(int i) : IdObjectCountBase(i) { }
63  int GetId() { return id; }
64  static IdObjectCountBase* Create(int i)
65  { return new IdObjectCount(i); }
66  };
67 
69  IdObjectCountFactory;
70 
71 
72  // Test factory polymorphic class with a two-argument constructor
73  class IdObjectCount2Base {
74  public:
75  IdObjectCount2Base() : fCount1(0), fCount2(0) { }
76  IdObjectCount2Base(int i, int j) : fCount1(i), fCount2(j) { }
77  virtual ~IdObjectCount2Base() { }
78  virtual int GetId() = 0;
79  int GetCount1() { return fCount1; }
80  int GetCount2() { return fCount2; }
81  private:
82  int fCount1;
83  int fCount2;
84  };
85 
86  template<int id>
87  class IdObjectCount2 : public IdObjectCount2Base {
88  public:
89  IdObjectCount2(int i, int j) : IdObjectCount2Base(i, j) { }
90  int GetId() { return id; }
91  static IdObjectCount2Base* Create(int i, int j)
92  { return new IdObjectCount2(i, j); }
93  };
94 
96  IdObjectCount2Factory;
97 }
98 
99 
104  : public CppUnit::TestFixture
105 {
106  CPPUNIT_TEST_SUITE(testObjectFactory);
107  CPPUNIT_TEST(testCreation);
108  CPPUNIT_TEST(testCreationArg);
109  CPPUNIT_TEST(testCreationArg2);
110  // CPPUNIT_TEST(testFN);
111  // CPPUNIT_TEST_EXCEPTION(testFN, anException);
112  CPPUNIT_TEST_SUITE_END();
113 public:
114  // shared data for tests
115 
116  void setUp()
117  {
118  }
119 
120  void tearDown()
121  {
122  }
123 
125  {
126  IdObjectBase* const pNull = 0;
127 
128  CPPUNIT_ASSERT(IdObjectFactory::Register(1, IdObject<1>::Create));
129 
130  IdObjectBase* p1 = IdObjectFactory::Create(1);
131  CPPUNIT_ASSERT(Verify<Not<Equal> >(p1, pNull));
132  CPPUNIT_ASSERT(Verify<Equal>(p1->GetId(), 1));
133  delete p1;
134 
135  IdObjectBase* p2 = IdObjectFactory::Create(2);
136  CPPUNIT_ASSERT(Verify<Equal>(p2, pNull));
137 
138  CPPUNIT_ASSERT(IdObjectFactory::Register(2, IdObject<2>::Create));
139 
140  p2 = IdObjectFactory::Create(2);
141  CPPUNIT_ASSERT(Verify<Not<Equal> >(p2, pNull));
142  CPPUNIT_ASSERT(Verify<Equal>(p2->GetId(), 2));
143  delete p2;
144 
145  IdObjectFactory::Iterator iter = IdObjectFactory::Begin();
146  CPPUNIT_ASSERT(Verify<Equal>(iter->first, 1));
147  ++iter;
148  CPPUNIT_ASSERT(Verify<Equal>(iter->first, 2));
149  ++iter;
150  CPPUNIT_ASSERT(iter == IdObjectFactory::End());
151  }
152 
153 
155  {
156  IdObjectCountBase* const pNull = 0;
157 
158  CPPUNIT_ASSERT(Verify<Equal>(IdObjectCountFactory::GetNumberOfCreators(),
159  0u));
160 
161  CPPUNIT_ASSERT(IdObjectCountFactory::Register("1",
162  IdObjectCount<1>::Create));
163  CPPUNIT_ASSERT(Verify<Equal>(IdObjectCountFactory::GetNumberOfCreators(),
164  1u));
165 
166  IdObjectCountBase* p1 = IdObjectCountFactory::Create("1", 5);
167  CPPUNIT_ASSERT(Verify<Not<Equal> >(p1, pNull));
168  CPPUNIT_ASSERT(Verify<Equal>(p1->GetId(), 1));
169  CPPUNIT_ASSERT(Verify<Equal>(p1->GetCount(), 5));
170  delete p1;
171 
172  IdObjectCountBase* p2 = IdObjectCountFactory::Create("2", 1);
173  CPPUNIT_ASSERT(Verify<Equal>(p2, pNull));
174 
175  CPPUNIT_ASSERT(IdObjectCountFactory::Register("2",
176  IdObjectCount<2>::Create));
177  CPPUNIT_ASSERT(Verify<Equal>(IdObjectCountFactory::GetNumberOfCreators(),
178  2u));
179 
180  p2 = IdObjectCountFactory::Create("2", 3);
181  CPPUNIT_ASSERT(Verify<Not<Equal> >(p2, pNull));
182  CPPUNIT_ASSERT(Verify<Equal>(p2->GetId(), 2));
183  CPPUNIT_ASSERT(Verify<Equal>(p2->GetCount(), 3));
184  delete p2;
185  }
186 
188  {
189  IdObjectCount2Base* const pNull = 0;
190 
191  CPPUNIT_ASSERT(Verify<Equal>(IdObjectCount2Factory::GetNumberOfCreators(),
192  0u));
193 
194  CPPUNIT_ASSERT(IdObjectCount2Factory::Register("1",
195  IdObjectCount2<1>::Create));
196  CPPUNIT_ASSERT(Verify<Equal>(IdObjectCount2Factory::GetNumberOfCreators(),
197  1u));
198 
199  IdObjectCount2Base* p1 = IdObjectCount2Factory::Create("1", 5, 6);
200  CPPUNIT_ASSERT(Verify<Not<Equal> >(p1, pNull));
201  CPPUNIT_ASSERT(Verify<Equal>(p1->GetId(), 1));
202  CPPUNIT_ASSERT(Verify<Equal>(p1->GetCount1(), 5));
203  CPPUNIT_ASSERT(Verify<Equal>(p1->GetCount2(), 6));
204  delete p1;
205  }
206 
207 };
208 
210 
211 
212 // Configure (x)emacs for this file ...
213 // Local Variables:
214 // mode:c++
215 // compile-command: "make -C .. -k testSTL && ../testSTL"
216 // End:
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
Definition: Test.h:180
bool Verify(const Predicate &pred, const T &lhs, const T &rhs)
Test condition by evaluating a predicate and print on failure.
Definition: Verify.h:38
Template for object factory.
Definition: ObjectFactory.h:71

, generated on Tue Sep 26 2023.