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)
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
19 #include "lib/test/run.hpp"
20 #include "lib/test/test-helper.hpp"
21 #include "lib/multifact.hpp"
22 #include "lib/format-cout.hpp"
23 #include "lib/util.hpp"
24 
25 #include <boost/lexical_cast.hpp>
26 #include <string>
27 
28 
29 
30 namespace lib {
31 namespace test{
32 
33  using boost::lexical_cast;
34  using lib::test::showSizeof;
35  using util::isSameObject;
36  using util::isnil;
37  using std::ostream;
38  using std::string;
39 
40  using LERR_(INVALID);
41 
42 
43  namespace { // hierarchy of test dummy objects
44 
45  struct Interface
46  {
47  virtual ~Interface() {};
48  virtual operator string () const =0;
49  };
50 
51 
52  enum theID
53  { ONE = 1
54  , TWO
55  , THR
56  , FOU
57  };
58 
60 
61 
62  template<theID ii>
64  : public Interface
65  {
66  operator string() const override
67  {
68  return "Impl-"+lexical_cast<string> (ii);
69  }
70  };
71 
73  TestFactory theFact;
74 
75  // Configure the products to be fabricated....
80  }
81 
82 
83 
84 
85 
86  /******************************************************************/
103  class MultiFactSingleton_test : public Test
104  {
105  void
106  run (Arg)
107  {
108  cout << theFact(ONE) << endl;
109  cout << theFact(TWO) << endl;
110  cout << theFact(THR) << endl;
111  cout << theFact(FOU) << endl;
112  cout << showSizeof (theFact) << endl;
113 
114  Interface & o1 = theFact(ONE);
115  Interface & o2 = theFact(ONE);
116  CHECK (isSameObject(o1,o2));
117 
118  TestFactory anotherFact;
119  CHECK (isnil (anotherFact));
120  VERIFY_ERROR (INVALID, anotherFact(ONE) );
121 
122  TestFactory::Singleton<Implementation<ONE>> anotherSingletonHolder (anotherFact,ONE);
123  Interface & o3 = anotherFact(ONE);
124  CHECK (isSameObject(o2,o3));
125  }
126  };
127 
128 
130  LAUNCHER (MultiFactSingleton_test, "unit common");
131 
132 
133 
134 }} // namespace lib::test
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:40
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:49
Implementation namespace for support and library code.
Factory for creating a family of objects by ID.
Definition: multifact.hpp:253
Target object to be instantiated as Singleton Allocates a variable amount of additional heap memory a...
Simplistic 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:323
bool isSameObject(A const &a, B const &b)
compare plain object identity, based directly on the referee&#39;s memory identities. ...
Definition: util.hpp:421