switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / singleton.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief singleton unit tests */
30
31 //#include "singleton.test.hh"
32 //#include "singleton.test.ih"
33
34 // Custom includes
35 #include <iostream>
36 #include "singleton.hh"
37 #include "IgnoreValue.hh"
38
39 #include <senf/Utils/auto_unit_test.hh>
40 #include <boost/test/test_tools.hpp>
41
42 #define prefix_
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44
45 namespace {
46
47     class Test : public senf::singleton<Test>
48     {
49         friend class senf::singleton<Test>;
50
51         Test() : foo_(1234) {}
52
53         int foo_;
54
55     public:
56         using senf::singleton<Test>::instance;
57         using senf::singleton<Test>::alive;
58
59         int foo() { return foo_; }
60     };
61 }
62
63 SENF_AUTO_UNIT_TEST(sInGlEtOn)
64 {
65     BOOST_CHECK_EQUAL( Test::instance().foo(), 1234 );
66     BOOST_CHECK( Test::alive() );
67 }
68
69 namespace {
70
71     bool test1Dead (false);
72     bool test2Dead (false);
73
74     bool test1Alive (false);
75     bool test2Alive (false);
76
77     struct AliveTest1 : public senf::singleton<AliveTest1>
78     {
79         friend class senf::singleton<AliveTest1>;
80         using senf::singleton<AliveTest1>::alive;
81         using senf::singleton<AliveTest1>::instance;
82         AliveTest1();
83         ~AliveTest1();
84     };
85
86     struct AliveTest2 : public senf::singleton<AliveTest2>
87     {
88         friend class senf::singleton<AliveTest2>;
89         using senf::singleton<AliveTest2>::alive;
90         using senf::singleton<AliveTest2>::instance;
91         AliveTest2();
92         ~AliveTest2();
93     };
94
95     AliveTest1::AliveTest1()
96     {
97         test2Alive = AliveTest2::alive();
98     }
99
100     AliveTest1::~AliveTest1()
101     {
102         if (test2Dead) {
103             if (! AliveTest2::alive() )
104                 std::cerr << "singleton alive test ok\n";
105             else {
106                 std::cerr << "singleton alive test 2 NOT ok!\n";
107                 throw 1;
108             }
109         }
110         test1Dead = true;
111     }
112
113     AliveTest2::AliveTest2()
114     {
115         test1Alive = AliveTest1::alive();
116     }
117
118     AliveTest2::~AliveTest2()
119     {
120         if (test1Dead) {
121             if (! AliveTest1::alive() )
122                 std::cerr << "singleton alive test ok\n";
123             else {
124                 std::cerr << "singleton alive test 1 NOT ok!\n";
125                 throw 1;
126             }
127         }
128         test2Dead = true;
129     }
130
131 }
132
133 SENF_AUTO_UNIT_TEST(singletonAlive)
134 {
135     senf::IGNORE( AliveTest1::instance() );
136     senf::IGNORE( AliveTest2::instance() );
137
138     BOOST_CHECK( (test1Alive && !test2Alive) || (!test1Alive && test2Alive) );
139     BOOST_CHECK( AliveTest1::alive() );
140     BOOST_CHECK( AliveTest2::alive() );
141 }
142
143 //-/////////////////////////////////////////////////////////////////////////////////////////////////
144 #undef prefix_
145
146 \f
147 // Local Variables:
148 // mode: c++
149 // fill-column: 100
150 // comment-column: 40
151 // c-file-style: "senf"
152 // indent-tabs-mode: nil
153 // ispell-local-dictionary: "american"
154 // compile-command: "scons -u test"
155 // End: