Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : // include files
21 :
22 : #include <osl_Condition_Const.h>
23 : #include <stdlib.h>
24 :
25 : using namespace osl;
26 : using namespace rtl;
27 :
28 : enum ConditionType
29 : {
30 : thread_type_set,
31 : thread_type_reset,
32 : thread_type_wait,
33 : thread_type_check
34 : };
35 :
36 : /** thread for testing Condition.
37 : */
38 : class ConditionThread : public Thread
39 : {
40 : public:
41 : //get the Condition to operate
42 5 : ConditionThread( ::osl::Condition& Con, ConditionType tType): m_MyCon( Con ), m_MyType( tType ) { }
43 :
44 5 : virtual ~ConditionThread( )
45 5 : {
46 : // LLA: do not throw in DTors!
47 : // LLA: CPPUNIT_ASSERT_MESSAGE( "#ConditionThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
48 5 : }
49 : protected:
50 : ::osl::Condition& m_MyCon;
51 : ConditionType m_MyType;
52 :
53 5 : void SAL_CALL run() SAL_OVERRIDE
54 : {
55 5 : switch ( m_MyType )
56 : {
57 : case thread_type_wait:
58 2 : m_MyCon.wait(); break;
59 : case thread_type_set:
60 2 : m_MyCon.set(); break;
61 : case thread_type_reset:
62 1 : m_MyCon.reset(); break;
63 : default:
64 0 : break;
65 : }
66 5 : }
67 : };
68 :
69 : // test code start here
70 :
71 : namespace osl_Condition
72 : {
73 :
74 : /** testing the method:
75 : Condition()
76 : */
77 6 : class ctors : public CppUnit::TestFixture
78 : {
79 : public:
80 : bool bRes, bRes1;
81 :
82 1 : void ctors_001( )
83 : {
84 1 : ::osl::Condition aCond;
85 1 : bRes = aCond.check( );
86 :
87 2 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition its initial check state should be sal_False.",
88 2 : !bRes );
89 1 : }
90 :
91 1 : void ctors_002( )
92 : {
93 1 : ::osl::Condition aCond;
94 1 : aCond.set( );
95 1 : bRes = aCond.check( );
96 :
97 2 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and set it.",
98 2 : bRes );
99 1 : }
100 :
101 2 : CPPUNIT_TEST_SUITE( ctors );
102 1 : CPPUNIT_TEST( ctors_001 );
103 1 : CPPUNIT_TEST( ctors_002 );
104 2 : CPPUNIT_TEST_SUITE_END( );
105 : }; // class ctors
106 :
107 : /** testing the method:
108 : void set()
109 : */
110 6 : class set : public CppUnit::TestFixture
111 : {
112 : public:
113 : bool bRes, bRes1, bRes2;
114 :
115 1 : void set_001( )
116 : {
117 1 : ::osl::Condition aCond;
118 1 : aCond.set( );
119 1 : bRes = aCond.check( );
120 :
121 2 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: check state should be sal_True after set.",
122 2 : bRes );
123 1 : }
124 :
125 1 : void set_002( )
126 : {
127 1 : ::osl::Condition aCond;
128 2 : ConditionThread myThread1( aCond, thread_type_wait );
129 1 : myThread1.create();
130 1 : bRes = myThread1.isRunning( );
131 :
132 2 : ConditionThread myThread2( aCond, thread_type_set );
133 1 : myThread2.create();
134 :
135 1 : myThread1.join( );
136 1 : bRes1 = myThread1.isRunning( );
137 1 : bRes2 = aCond.check( );
138 1 : myThread2.join( );
139 :
140 2 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
141 2 : bRes && !bRes1 && bRes2 );
142 1 : }
143 :
144 2 : CPPUNIT_TEST_SUITE( set );
145 1 : CPPUNIT_TEST( set_001 );
146 1 : CPPUNIT_TEST( set_002 );
147 2 : CPPUNIT_TEST_SUITE_END( );
148 : }; // class set
149 :
150 : /** testing the method:
151 : void reset()
152 : */
153 6 : class reset : public CppUnit::TestFixture
154 : {
155 : public:
156 : bool bRes, bRes1, bRes2;
157 :
158 1 : void reset_001( )
159 : {
160 1 : ::osl::Condition aCond;
161 1 : aCond.reset( );
162 :
163 2 : ConditionThread myThread( aCond, thread_type_wait );
164 1 : myThread.create();
165 1 : bRes = myThread.isRunning( );
166 1 : bRes2 = aCond.check( );
167 :
168 1 : aCond.set( );
169 1 : myThread.join( );
170 1 : bRes1 = myThread.isRunning( );
171 :
172 2 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait will cause a reset thread block, use set to release it.",
173 2 : bRes && !bRes1 && !bRes2 );
174 1 : }
175 :
176 1 : void reset_002( )
177 : {
178 1 : ::osl::Condition aCond;
179 1 : aCond.reset( );
180 1 : bRes = aCond.check( );
181 1 : aCond.set( );
182 1 : bRes1 = aCond.check( );
183 :
184 2 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a condition and reset/set it.",
185 2 : !bRes && bRes1 );
186 1 : }
187 :
188 2 : CPPUNIT_TEST_SUITE( reset );
189 1 : CPPUNIT_TEST( reset_001 );
190 1 : CPPUNIT_TEST( reset_002 );
191 2 : CPPUNIT_TEST_SUITE_END( );
192 : }; // class reset
193 :
194 : /** testing the method:
195 : Result wait(const TimeValue *pTimeout = 0)
196 : */
197 6 : class wait : public CppUnit::TestFixture
198 : {
199 : public:
200 : bool bRes, bRes1, bRes2;
201 : TimeValue *tv1;
202 :
203 2 : void setUp( ) SAL_OVERRIDE
204 : {
205 2 : tv1 = new TimeValue;
206 2 : tv1->Seconds = 1;
207 2 : tv1->Nanosec = 0;
208 :
209 2 : }
210 :
211 2 : void tearDown( ) SAL_OVERRIDE
212 : {
213 2 : delete tv1;
214 2 : }
215 :
216 1 : void wait_001( )
217 : {
218 1 : ::osl::Condition cond1;
219 2 : ::osl::Condition cond2;
220 2 : ::osl::Condition cond3;
221 :
222 1 : cond1.set();
223 1 : cond2.set();
224 :
225 1 : osl::Condition::Result r1=cond1.wait(tv1);
226 1 : osl::Condition::Result r2=cond2.wait();
227 1 : osl::Condition::Result r3=cond3.wait(tv1);
228 :
229 2 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: test three types of wait.",
230 : (r1 == ::osl::Condition::result_ok) &&
231 : (r2 == ::osl::Condition::result_ok) &&
232 2 : (r3 == ::osl::Condition::result_timeout) );
233 1 : }
234 :
235 1 : void wait_002( )
236 : {
237 1 : ::osl::Condition aCond;
238 : ::osl::Condition::Result wRes, wRes1;
239 :
240 1 : aCond.reset( );
241 1 : bRes = aCond.check( );
242 1 : wRes = aCond.wait( tv1 );
243 :
244 1 : aCond.set( );
245 1 : wRes1 = aCond.wait( tv1 );
246 1 : bRes1 = aCond.check( );
247 :
248 2 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: wait a condition after set/reset.",
249 : !bRes && bRes1 &&
250 : ( ::osl::Condition::result_timeout == wRes ) &&
251 2 : ( ::osl::Condition::result_ok == wRes1 ) );
252 1 : }
253 :
254 2 : CPPUNIT_TEST_SUITE( wait );
255 1 : CPPUNIT_TEST( wait_001 );
256 1 : CPPUNIT_TEST( wait_002 );
257 2 : CPPUNIT_TEST_SUITE_END( );
258 : }; // class wait
259 :
260 : /** testing the method:
261 : sal_Bool check()
262 : */
263 6 : class check : public CppUnit::TestFixture
264 : {
265 : public:
266 : bool bRes, bRes1, bRes2;
267 :
268 1 : void check_001( )
269 : {
270 1 : ::osl::Condition aCond;
271 :
272 1 : aCond.reset( );
273 1 : bRes = aCond.check( );
274 1 : aCond.set( );
275 1 : bRes1 = aCond.check( );
276 :
277 2 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: check the condition states.",
278 2 : !bRes && bRes1 );
279 1 : }
280 :
281 1 : void check_002( )
282 : {
283 1 : ::osl::Condition aCond;
284 1 : aCond.reset( );
285 :
286 2 : ConditionThread myThread( aCond, thread_type_set );
287 1 : myThread.create( );
288 1 : myThread.join( );
289 1 : bRes = aCond.check( );
290 :
291 2 : ConditionThread myThread1( aCond, thread_type_reset );
292 1 : myThread1.create( );
293 1 : myThread1.join( );
294 1 : bRes1 = aCond.check( );
295 :
296 2 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: use threads to set/reset Condition and check it in main routine.",
297 2 : bRes && !bRes1 );
298 1 : }
299 :
300 2 : CPPUNIT_TEST_SUITE( check );
301 1 : CPPUNIT_TEST( check_001 );
302 1 : CPPUNIT_TEST( check_002 );
303 2 : CPPUNIT_TEST_SUITE_END( );
304 : }; // class check
305 :
306 1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::ctors);
307 1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::set);
308 1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::reset);
309 1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::wait);
310 1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::check);
311 :
312 : } // namespace osl_Condition
313 :
314 4 : CPPUNIT_PLUGIN_IMPLEMENT();
315 :
316 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|