a28a5ef3a87b97b75fc6b5fb793e7eb64b0e4c18
[senf.git] / senf / Utils / singleton.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief singleton unit tests */
25
26 //#include "singleton.test.hh"
27 //#include "singleton.test.ih"
28
29 // Custom includes
30 #include <iostream>
31 #include "singleton.hh"
32 #include "IgnoreValue.hh"
33
34 #include <senf/Utils/auto_unit_test.hh>
35 #include <boost/test/test_tools.hpp>
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 namespace {
41
42     class Test : public senf::singleton<Test>
43     {
44         friend class senf::singleton<Test>;
45
46         Test() : foo_(1234) {}
47
48         int foo_;
49
50     public:
51         using senf::singleton<Test>::instance;
52         using senf::singleton<Test>::alive;
53
54         int foo() { return foo_; }
55     };
56 }
57
58 SENF_AUTO_UNIT_TEST(sInGlEtOn)
59 {
60     BOOST_CHECK_EQUAL( Test::instance().foo(), 1234 );
61     BOOST_CHECK( Test::alive() );
62 }
63
64 namespace {
65
66     bool test1Dead (false);
67     bool test2Dead (false);
68
69     bool test1Alive (false);
70     bool test2Alive (false);
71
72     struct AliveTest1 : public senf::singleton<AliveTest1>
73     {
74         friend class senf::singleton<AliveTest1>;
75         using senf::singleton<AliveTest1>::alive;
76         using senf::singleton<AliveTest1>::instance;
77         AliveTest1();
78         ~AliveTest1();
79     };
80
81     struct AliveTest2 : public senf::singleton<AliveTest2>
82     {
83         friend class senf::singleton<AliveTest2>;
84         using senf::singleton<AliveTest2>::alive;
85         using senf::singleton<AliveTest2>::instance;
86         AliveTest2();
87         ~AliveTest2();
88     };
89
90     AliveTest1::AliveTest1() 
91     {
92         test2Alive = AliveTest2::alive();
93     }
94
95     AliveTest1::~AliveTest1()
96     {
97         if (test2Dead) {
98             assert( ! AliveTest2::alive() );
99             std::cerr << "singleton alive test ok\n";
100         }
101         test1Dead = true;
102     }
103
104     AliveTest2::AliveTest2()
105     {
106         test1Alive = AliveTest1::alive();
107     }
108
109     AliveTest2::~AliveTest2()
110     {
111         if (test1Dead) {
112             assert( ! AliveTest1::alive() );
113             std::cerr << "singleton alive test ok\n";
114         }
115         test2Dead = true;
116     }
117
118 }
119
120 SENF_AUTO_UNIT_TEST(singletonAlive)
121 {
122     senf::IGNORE( AliveTest1::instance() );
123     senf::IGNORE( AliveTest2::instance() );
124
125     BOOST_CHECK( (test1Alive && !test2Alive) || (!test1Alive && test2Alive) );
126     BOOST_CHECK( AliveTest1::alive() );
127     BOOST_CHECK( AliveTest2::alive() );
128 }
129
130 ///////////////////////////////cc.e////////////////////////////////////////
131 #undef prefix_
132
133 \f
134 // Local Variables:
135 // mode: c++
136 // fill-column: 100
137 // comment-column: 40
138 // c-file-style: "senf"
139 // indent-tabs-mode: nil
140 // ispell-local-dictionary: "american"
141 // compile-command: "scons -u test"
142 // End: