Lumiera  0.pre.03
»edit your freedom«
multifact-singleton-test.cpp
Go to the documentation of this file.
1 /*
2  MultiFactSingleton(Test) - using MultiFact to manage a family of singletons
3 
4  Copyright (C) Lumiera.org
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
30 #include "lib/multifact.hpp"
31 #include "lib/format-cout.hpp"
32 #include "lib/util.hpp"
33 
34 #include <boost/lexical_cast.hpp>
35 #include <string>
36 
37 
38 
39 namespace lib {
40 namespace test{
41 
42  using boost::lexical_cast;
43  using lib::test::showSizeof;
44  using util::isSameObject;
45  using util::isnil;
46  using std::ostream;
47  using std::string;
48 
49  using LERR_(INVALID);
50 
51 
52  namespace { // hierarchy of test dummy objects
53 
54  struct Interface
55  {
56  virtual ~Interface() {};
57  virtual operator string () const =0;
58  };
59 
60 
61  enum theID
62  { ONE = 1
63  , TWO
64  , THR
65  , FOU
66  };
67 
69 
70 
71  template<theID ii>
73  : public Interface
74  {
75  operator string() const override
76  {
77  return "Impl-"+lexical_cast<string> (ii);
78  }
79  };
80 
82  TestFactory theFact;
83 
84  // Configure the products to be fabricated....
89  }
90 
91 
92 
93 
94 
95  /******************************************************************/
112  class MultiFactSingleton_test : public Test
113  {
114  void
115  run (Arg)
116  {
117  cout << theFact(ONE) << endl;
118  cout << theFact(TWO) << endl;
119  cout << theFact(THR) << endl;
120  cout << theFact(FOU) << endl;
121  cout << showSizeof (theFact) << endl;
122 
123  Interface & o1 = theFact(ONE);
124  Interface & o2 = theFact(ONE);
125  CHECK (isSameObject(o1,o2));
126 
127  TestFactory anotherFact;
128  CHECK (isnil (anotherFact));
129  VERIFY_ERROR (INVALID, anotherFact(ONE) );
130 
131  TestFactory::Singleton<Implementation<ONE>> anotherSingletonHolder (anotherFact,ONE);
132  Interface & o3 = anotherFact(ONE);
133  CHECK (isSameObject(o2,o3));
134  }
135  };
136 
137 
139  LAUNCHER (MultiFactSingleton_test, "unit common");
140 
141 
142 
143 }} // namespace lib::test
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:49
Framework for building a configurable factory, to generate families of related objects.
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
string showSizeof(size_t siz, string name)
for printing sizeof().
Definition: test-helper.cpp:57
Implementation namespace for support and library code.
Factory for creating a family of objects by ID.
Definition: multifact.hpp:262
Target object to be instantiated as Singleton Allocates a variable amount of additional heap memory a...
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
A collection of frequently used helper functions to support unit testing.
Convenience shortcut for automatically setting up a production line, to fabricate a singleton instanc...
Definition: multifact.hpp:332
bool isSameObject(A const &a, B const &b)
compare plain object identity, bypassing any custom comparison operators.
Definition: util.hpp:372