Lumiera  0.pre.03
»edit your freedom«
singleton-test.cpp
Go to the documentation of this file.
1 /*
2  Singleton(Test) - unittest for our Singleton template
3 
4  Copyright (C) Lumiera.org
5  2008, 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/format-string.hpp"
30 #include "lib/format-cout.hpp"
31 #include "lib/util.hpp"
32 
33 #include "test-target-obj.hpp"
34 #include "lib/depend.hpp"
35 
36 #include <boost/lexical_cast.hpp>
37 
38 using boost::lexical_cast;
39 using util::isSameObject;
40 using util::_Fmt;
41 using util::isnil;
42 using std::string;
43 
44 
45 namespace lib {
46 namespace test{
47 
48 
54  class TargetObj : public TestTargetObj
55  {
56  public:
57  static int cnt;
58  static void setCountParam (uint c) { TargetObj::cnt = c; }
59  protected:
60  TargetObj () : TestTargetObj(cnt) {}
61 
62  friend class lib::DependencyFactory<TargetObj>;
63  };
64 
65  int TargetObj::cnt = 0;
66 
67 
68 
69 
70 
71 
72 
73  /***************************************************************/
80  class Singleton_test : public Test
81  {
82 
83  virtual void
84  run (Arg arg)
85  {
86  uint num= isnil(arg)? 1 : lexical_cast<uint>(arg[1]);
87 
88  Depend<TargetObj> singleton;
89 
90  cout << _Fmt("testing TargetObj(%d) as Singleton\n") % num;
91  TargetObj::setCountParam(num);
92  TargetObj& t1 = singleton();
93  TargetObj& t2 = singleton();
94 
95  CHECK (isSameObject(t1, t2), "not a Singleton, got two different instances." );
96 
97  cout << "calling a non-static method on the Singleton instance" <<endl
98  << t1 << endl;
99  }
100  };
101 
102 
103 
105  LAUNCHER (Singleton_test, "unit common");
106 
107 
108 
109 }} // namespace lib::test
Automatically use custom string conversion in C++ stream output.
Dummy target object to be created by factory for unit tests Used to verify sane memory management and...
Definition: run.hpp:49
Front-end for printf-style string template interpolation.
A front-end for using printf-style formatting.
Access point to singletons and other kinds of dependencies designated by type.
Definition: depend.hpp:289
Implementation namespace for support and library code.
Target object to be created by Test-Factories or as Singleton.
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...
Singleton services and Dependency Injection.
Helper to abstract creation and lifecycle of a dependency.
Definition: depend.hpp:134
bool isSameObject(A const &a, B const &b)
compare plain object identity, bypassing any custom comparison operators.
Definition: util.hpp:372