/[resiprocate]/main/rutil/test/testSharedPtr.cxx
ViewVC logotype

Contents of /main/rutil/test/testSharedPtr.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5934 - (show annotations) (download)
Thu Feb 16 23:58:28 2006 UTC (13 years, 8 months ago) by dworley
File MIME type: text/plain
File size: 5723 byte(s)
Start cleaning up svn:mime-type in files.

1 #include <iostream>
2 #include "rutil/Log.hxx"
3 #include "rutil/SharedPtr.hxx"
4 #include "rutil/Data.hxx"
5 #include "rutil/ThreadIf.hxx"
6 #include "rutil/Timer.hxx"
7 #ifndef WIN32
8 #include <unistd.h>
9 #endif
10
11 using namespace resip;
12 using namespace std;
13
14 void sleepMS(unsigned int ms)
15 {
16 #ifdef WIN32
17 Sleep(ms);
18 #else
19 usleep(ms*1000);
20 #endif
21 }
22
23 class Foo
24 {
25 public:
26 Foo(const Data& val)
27 : mVal(val)
28 {
29 cerr << "Foo created." << endl;
30 }
31
32 ~Foo()
33 {
34 cerr << "Foo destroyed." << endl;
35 }
36
37 Data mVal;
38 };
39
40 class TestThread : public ThreadIf
41 {
42 public:
43 TestThread(SharedPtr<Foo>&);
44 virtual ~TestThread()
45 {
46 #ifdef VERBOSE
47 cerr << "TestThread finishing..." << endl;
48 #endif
49 shutdown();
50 join();
51 #ifdef VERBOSE
52 cerr << "TestThread finished" << endl;
53 #endif
54 };
55
56 void thread();
57
58 private:
59 SharedPtr<Foo> mFoo;
60 };
61
62 TestThread::TestThread(SharedPtr<Foo>& f) :
63 mFoo(f)
64 {}
65
66 void TestThread::thread()
67 {
68 while (!mShutdown)
69 {
70 sleepMS(5);
71 assert(mFoo->mVal == Data("data"));
72 }
73 mFoo.reset();
74 }
75
76 int
77 main()
78 {
79 Log::initialize(Log::Cout, Log::Debug, Data::Empty);
80
81 {
82 cerr << "!! Test counter logic" << endl;
83
84 SharedPtr<Foo> foo1(new Foo("data"));
85 cerr << "foo1 mVal=" << foo1->mVal << " use count=" << foo1.use_count() << endl;
86 assert(foo1.use_count() == 1);
87 {
88 SharedPtr<Foo> foo2(foo1);
89
90 cerr << "foo1 mVal=" << foo1->mVal << " use count=" << foo1.use_count() << endl;
91 assert(foo1.use_count() == 2);
92 cerr << "foo2 mVal=" << foo2->mVal << " use count=" << foo2.use_count() << endl;
93 assert(foo2.use_count() == 2);
94 }
95 cerr << "foo1 mVal=" << foo1->mVal << " use count=" << foo1.use_count() << endl;
96 assert(foo1.use_count() == 1);
97 }
98
99 {
100 cerr << "!! Test threads" << endl;
101
102 SharedPtr<Foo> foo(new Foo("data"));
103
104 cerr << "foo mVal=" << foo->mVal << " use count=" << foo.use_count() << endl;
105 assert(foo.use_count() == 1);
106
107 TestThread thread1(foo);
108 TestThread thread2(foo);
109 TestThread thread3(foo);
110 TestThread thread4(foo);
111 TestThread thread5(foo);
112 TestThread thread6(foo);
113 TestThread thread7(foo);
114 TestThread thread8(foo);
115 TestThread thread9(foo);
116 TestThread thread10(foo);
117
118 cerr << "foo mVal=" << foo->mVal << " use count=" << foo.use_count() << endl;
119 assert(foo.use_count() == 11);
120
121 thread1.run();
122 thread2.run();
123 thread3.run();
124 thread4.run();
125 thread5.run();
126 thread6.run();
127 thread7.run();
128 thread8.run();
129 thread9.run();
130 thread10.run();
131
132 sleepMS(500);
133
134 thread1.shutdown();
135 thread2.shutdown();
136 thread3.shutdown();
137 thread4.shutdown();
138 thread5.shutdown();
139 thread6.shutdown();
140 thread7.shutdown();
141 thread8.shutdown();
142 thread9.shutdown();
143 thread10.shutdown();
144
145 thread1.join();
146 thread2.join();
147 thread3.join();
148 thread4.join();
149 thread5.join();
150 thread6.join();
151 thread7.join();
152 thread8.join();
153 thread9.join();
154 thread10.join();
155
156 cerr << "foo mVal=" << foo->mVal << " use count=" << foo.use_count() << endl;
157 assert(foo.use_count() == 1);
158 }
159
160 cerr << "All OK" << endl;
161 return 0;
162 }
163
164 /* ====================================================================
165 * The Vovida Software License, Version 1.0
166 *
167 * Redistribution and use in source and binary forms, with or without
168 * modification, are permitted provided that the following conditions
169 * are met:
170 *
171 * 1. Redistributions of source code must retain the above copyright
172 * notice, this list of conditions and the following disclaimer.
173 *
174 * 2. Redistributions in binary form must reproduce the above copyright
175 * notice, this list of conditions and the following disclaimer in
176 * the documentation and/or other materials provided with the
177 * distribution.
178 *
179 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
180 * and "Vovida Open Communication Application Library (VOCAL)" must
181 * not be used to endorse or promote products derived from this
182 * software without prior written permission. For written
183 * permission, please contact vocal@vovida.org.
184 *
185 * 4. Products derived from this software may not be called "VOCAL", nor
186 * may "VOCAL" appear in their name, without prior written
187 * permission of Vovida Networks, Inc.
188 *
189 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
190 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
191 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
192 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
193 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
194 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
195 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
196 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
197 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
198 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
199 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
200 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
201 * DAMAGE.
202 *
203 * ====================================================================
204 *
205 * This software consists of voluntary contributions made by Vovida
206 * Networks, Inc. and many individuals on behalf of Vovida Networks,
207 * Inc. For more information on Vovida Networks, Inc., please see
208 * <http://www.vovida.org/>.
209 *
210 */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27