a6084c51de42cee70dacd23493182bdeea333ea2
[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 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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             if (! AliveTest2::alive() )
99                 std::cerr << "singleton alive test ok\n";
100             else {
101                 std::cerr << "singleton alive test 2 NOT ok!\n";
102                 throw 1;
103             }
104         }
105         test1Dead = true;
106     }
107
108     AliveTest2::AliveTest2()
109     {
110         test1Alive = AliveTest1::alive();
111     }
112
113     AliveTest2::~AliveTest2()
114     {
115         if (test1Dead) {
116             if (! AliveTest1::alive() )
117                 std::cerr << "singleton alive test ok\n";
118             else {
119                 std::cerr << "singleton alive test 1 NOT ok!\n";
120                 throw 1;
121             }
122         }
123         test2Dead = true;
124     }
125
126 }
127
128 SENF_AUTO_UNIT_TEST(singletonAlive)
129 {
130     senf::IGNORE( AliveTest1::instance() );
131     senf::IGNORE( AliveTest2::instance() );
132
133     BOOST_CHECK( (test1Alive && !test2Alive) || (!test1Alive && test2Alive) );
134     BOOST_CHECK( AliveTest1::alive() );
135     BOOST_CHECK( AliveTest2::alive() );
136 }
137
138 //-/////////////////////////////////////////////////////////////////////////////////////////////////
139 #undef prefix_
140
141 \f
142 // Local Variables:
143 // mode: c++
144 // fill-column: 100
145 // comment-column: 40
146 // c-file-style: "senf"
147 // indent-tabs-mode: nil
148 // ispell-local-dictionary: "american"
149 // compile-command: "scons -u test"
150 // End: