testObjectRegistrar.cc
Go to the documentation of this file.
1 
13 // header for unit test testObjectRegistrar
14 
15 #include <utl/ObjectRegistrar.h>
16 #include <utl/ObjectFactory.h>
17 
18 #include <tst/Verify.h>
20 
21 #include <cppunit/extensions/HelperMacros.h>
22 
23 using namespace utl;
24 using namespace tst;
25 
26 
27 namespace {
28 
29  class InheritObjectBase {
30  public:
31  virtual ~InheritObjectBase() { }
32  virtual int GetId() = 0;
33  };
34 
35  typedef ObjectFactory<InheritObjectBase*, int> InheritObjectFactory;
36 
37 
38  template<int id>
39  class InheritObject : public InheritObjectBase,
40  private ObjectRegistrar<InheritObject<id>, InheritObjectFactory> {
41  public:
42  int GetId() { return GetRegistrationId(); }
43 
44  static typename InheritObjectFactory::IdentifierType
45  GetRegistrationId() { return id; }
46 
47  static InheritObjectBase* Create()
48  { return new InheritObject; }
49  };
50 
51  template class InheritObject<1>;
52  template class InheritObject<3>;
53 
54 
55  class MemberObjectBase {
56  public:
57  virtual ~MemberObjectBase() {}
58  virtual int GetId() = 0;
59  };
60 
61  typedef ObjectFactory<MemberObjectBase*, int> MemberObjectFactory;
62 
63 
64  template<int id>
65  class MemberObject : public MemberObjectBase {
66  public:
67  int GetId() { return GetRegistrationId(); }
68 
69  static typename MemberObjectFactory::IdentifierType
70  GetRegistrationId() { return id; }
71 
72  static MemberObjectBase* Create()
73  { return new MemberObject; }
74  private:
75  ObjectRegistrar<MemberObject<id>, MemberObjectFactory> fAutoReg;
76  };
77 
78  template class MemberObject<1>;
79  template class MemberObject<3>;
80 
81 }
82 
83 
88  : public CppUnit::TestFixture
89 {
90  CPPUNIT_TEST_SUITE(testObjectRegistrar);
91  CPPUNIT_TEST(testCreationMember);
92  CPPUNIT_TEST(testCreationInherit);
93  CPPUNIT_TEST(testCreationExternal);
94  // CPPUNIT_TEST(testFN);
95  // CPPUNIT_TEST_EXCEPTION(testFN, anException);
96  CPPUNIT_TEST_SUITE_END();
97 public:
98  // shared data for tests
99 
100  void setUp()
101  {
102  }
103 
104  void tearDown()
105  {
106  }
107 
109  {
110  CPPUNIT_ASSERT(Verify<Equal>(InheritObjectFactory::GetNumberOfCreators(),
111  2u));
112 
113  InheritObjectBase* const pnull = 0;
114  InheritObjectBase* p1 = InheritObjectFactory::Create(1);
115  CPPUNIT_ASSERT(Verify<Not<Equal> >(p1, pnull));
116  delete p1;
117  }
118 
119 
121  {
122  CPPUNIT_ASSERT(Verify<Equal>(MemberObjectFactory::GetNumberOfCreators(),
123  2u));
124 
125  MemberObjectBase* const pnull = 0;
126  MemberObjectBase* p1 = MemberObjectFactory::Create(1);
127  CPPUNIT_ASSERT(Verify<Not<Equal> >(p1, pnull));
128  delete p1;
129  }
130 
131 
133  {
134  CPPUNIT_ASSERT(Verify<Equal>(ExternalObjectFactory::GetNumberOfCreators(),
135  2u));
136 
137  ExternalObjectBase* const pnull = 0;
138  ExternalObjectBase* p1 = ExternalObjectFactory::Create(1);
139  CPPUNIT_ASSERT(Verify<Not<Equal> >(p1, pnull));
140  delete p1;
141  }
142 
143 };
144 
146 
147 
148 // Configure (x)emacs for this file ...
149 // Local Variables:
150 // mode:c++
151 // compile-command: "make -C .. -k testSTL && ../testSTL"
152 // 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
Class for the automatic registration of an object.
Template for object factory.
Definition: ObjectFactory.h:71

, generated on Tue Sep 26 2023.