Move sourcecode into 'senf/' directory
[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
33 #include "../Utils/auto_unit_test.hh"
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 namespace {
40
41     class Test : public senf::singleton<Test>
42     {
43         friend class senf::singleton<Test>;
44
45         Test() : foo_(1234) {}
46
47         int foo_;
48
49     public:
50         using senf::singleton<Test>::instance;
51         using senf::singleton<Test>::alive;
52
53         int foo() { return foo_; }
54     };
55 }
56
57 BOOST_AUTO_UNIT_TEST(sInGlEtOn)
58 {
59     BOOST_CHECK_EQUAL( Test::instance().foo(), 1234 );
60     BOOST_CHECK( Test::alive() );
61 }
62
63 namespace {
64
65     bool test1Dead (false);
66     bool test2Dead (false);
67
68     bool test1Alive (false);
69     bool test2Alive (false);
70
71     struct AliveTest1 : public senf::singleton<AliveTest1>
72     {
73         friend class senf::singleton<AliveTest1>;
74         using senf::singleton<AliveTest1>::alive;
75         using senf::singleton<AliveTest1>::instance;
76         AliveTest1();
77         ~AliveTest1();
78     };
79
80     struct AliveTest2 : public senf::singleton<AliveTest2>
81     {
82         friend class senf::singleton<AliveTest2>;
83         using senf::singleton<AliveTest2>::alive;
84         using senf::singleton<AliveTest2>::instance;
85         AliveTest2();
86         ~AliveTest2();
87     };
88
89     AliveTest1::AliveTest1() 
90     {
91         test2Alive = AliveTest2::alive();
92     }
93
94     AliveTest1::~AliveTest1()
95     {
96         if (test2Dead) {
97             assert( ! AliveTest2::alive() );
98             std::cerr << "singleton alive test ok\n";
99         }
100         test1Dead = true;
101     }
102
103     AliveTest2::AliveTest2()
104     {
105         test1Alive = AliveTest1::alive();
106     }
107
108     AliveTest2::~AliveTest2()
109     {
110         if (test1Dead) {
111             assert( ! AliveTest1::alive() );
112             std::cerr << "singleton alive test ok\n";
113         }
114         test2Dead = true;
115     }
116
117 }
118
119 BOOST_AUTO_UNIT_TEST(singletonAlive)
120 {
121     (void) AliveTest1::instance();
122     (void) AliveTest2::instance();
123
124     BOOST_CHECK( (test1Alive && !test2Alive) || (!test1Alive && test2Alive) );
125     BOOST_CHECK( AliveTest1::alive() );
126     BOOST_CHECK( AliveTest2::alive() );
127 }
128
129 ///////////////////////////////cc.e////////////////////////////////////////
130 #undef prefix_
131
132 \f
133 // Local Variables:
134 // mode: c++
135 // fill-column: 100
136 // comment-column: 40
137 // c-file-style: "senf"
138 // indent-tabs-mode: nil
139 // ispell-local-dictionary: "american"
140 // compile-command: "scons -u test"
141 // End: