Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifdef WNT
30 : : #if defined _MSC_VER
31 : : #define NOMINMAX
32 : : #endif
33 : : #include <windows.h>
34 : : #else
35 : : #include <unistd.h>
36 : : #include <time.h>
37 : : #endif
38 : :
39 : : //------------------------------------------------------------------------
40 : : // include files
41 : : //------------------------------------------------------------------------
42 : : #include <sal/types.h>
43 : :
44 : : #include <rtl/string.hxx>
45 : :
46 : : #include <rtl/strbuf.hxx>
47 : :
48 : : #include <osl/thread.hxx>
49 : :
50 : : #include <osl/mutex.hxx>
51 : : #include <osl/time.h>
52 : :
53 : : #include <string.h>
54 : :
55 : : #include <cppunit/TestFixture.h>
56 : : #include <cppunit/extensions/HelperMacros.h>
57 : : #include <cppunit/plugin/TestPlugIn.h>
58 : :
59 : : #define t_print printf
60 : :
61 : : using namespace osl;
62 : :
63 : : using ::rtl::OString;
64 : :
65 : : // -----------------------------------------------------------------------------
66 : : // Small stopwatch
67 : : class StopWatch {
68 : : TimeValue t1,t2; // Start and stoptime
69 : :
70 : : protected:
71 : : sal_Int32 m_nNanoSec;
72 : : sal_Int32 m_nSeconds;
73 : :
74 : : bool m_bIsValid; // TRUE, when started and stopped
75 : : bool m_bIsRunning; // TRUE, when started
76 : :
77 : : public:
78 : : StopWatch();
79 : 0 : ~StopWatch() {}
80 : :
81 : : void start(); // Starts time
82 : : void stop(); // Stops time
83 : :
84 : : double getSeconds() const;
85 : : double getTenthSec() const;
86 : : };
87 : :
88 : : // ================================= Stop Watch =================================
89 : :
90 : : // A small stopwatch for internal use
91 : : // (c) Lars Langhans 29.12.1996 22:10
92 : :
93 : 0 : StopWatch::StopWatch():m_bIsValid(false),m_bIsRunning(false) {}
94 : :
95 : 0 : void StopWatch::start()
96 : : {
97 : : // pre: %
98 : : // post: Start Timer
99 : :
100 : 0 : m_bIsValid = false;
101 : 0 : m_bIsRunning = true;
102 : 0 : osl_getSystemTime( &t1 );
103 : 0 : t_print("# %u %u nsecs\n", (unsigned)t1.Seconds, (unsigned)t1.Nanosec);
104 : : // gettimeofday(&t1, 0);
105 : 0 : }
106 : :
107 : 0 : void StopWatch::stop()
108 : : {
109 : : // pre: Timer should be started
110 : : // post: Timer will stopped
111 : :
112 : : // gettimeofday(&t2, 0); // Ask timer
113 : 0 : osl_getSystemTime( &t2 );
114 : 0 : t_print("# %u %u nsecs\n", (unsigned) t2.Seconds, (unsigned) t2.Nanosec);
115 : :
116 [ # # ]: 0 : if (m_bIsRunning)
117 : : { // check if started.
118 : 0 : m_nSeconds = static_cast<sal_Int32>(t2.Seconds) - static_cast<sal_Int32>(t1.Seconds);
119 [ # # ]: 0 : if ( t2.Nanosec > t1.Nanosec )
120 : 0 : m_nNanoSec = static_cast<sal_Int32>(t2.Nanosec) - static_cast<sal_Int32>(t1.Nanosec);
121 : : else
122 : : {
123 : 0 : m_nNanoSec = 1000000000 + static_cast<sal_Int32>(t2.Nanosec) - static_cast<sal_Int32>(t1.Nanosec);
124 : 0 : m_nSeconds -= 1;
125 : : }
126 : 0 : t_print("# %u %u nsecs\n", (unsigned) m_nSeconds, (unsigned) m_nNanoSec );
127 : : //if (m_nNanoSec < 0)
128 : : //{
129 : : //m_nNanoSec += 1000000000;
130 : : //m_nSeconds -= 1;
131 : : //}
132 : 0 : m_bIsValid = true;
133 : 0 : m_bIsRunning = false;
134 : : }
135 : 0 : }
136 : :
137 : 0 : double StopWatch::getSeconds() const
138 : : {
139 : : // pre: valid = TRUE
140 : : // BACK: time in seconds
141 : :
142 : 0 : double nValue = 0.0;
143 [ # # ]: 0 : if (m_bIsValid)
144 : : {
145 : 0 : nValue = double(m_nNanoSec) / 1000000000.0 + m_nSeconds; // milli micro nano
146 : : }
147 : 0 : return nValue;
148 : : }
149 : :
150 : 0 : double StopWatch::getTenthSec() const
151 : : {
152 : 0 : double nValue = 0.0;
153 [ # # ]: 0 : if (m_bIsValid)
154 : : {
155 : 0 : nValue = double(m_nNanoSec) / 100000000.0 + m_nSeconds * 10;
156 : : }
157 : 0 : return nValue ;
158 : : }
159 : :
160 : : // -----------------------------------------------------------------------------
161 : : template <class T>
162 : 0 : class ThreadSafeValue
163 : : {
164 : : T m_nFlag;
165 : : Mutex m_aMutex;
166 : : public:
167 : 0 : ThreadSafeValue(T n = 0): m_nFlag(n) {}
168 : 0 : T getValue()
169 : : {
170 : : //block if already acquired by another thread.
171 [ # # ]: 0 : osl::MutexGuard g(m_aMutex);
172 [ # # ]: 0 : return m_nFlag;
173 : : }
174 : 0 : void addValue(T n)
175 : : {
176 : : //only one thread operate on the flag.
177 [ # # ]: 0 : osl::MutexGuard g(m_aMutex);
178 [ # # ]: 0 : m_nFlag += n;
179 : 0 : }
180 : 0 : void acquire() {m_aMutex.acquire();}
181 : 0 : void release() {m_aMutex.release();}
182 : : };
183 : :
184 : : // -----------------------------------------------------------------------------
185 : : namespace ThreadHelper
186 : : {
187 : 40 : void thread_sleep_tenth_sec(sal_Int32 _nTenthSec)
188 : : {
189 : : #ifdef WNT
190 : : Sleep(_nTenthSec * 100 );
191 : : #else
192 : : TimeValue nTV;
193 : 40 : nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
194 : 40 : nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
195 [ + - ]: 40 : osl_waitThread(&nTV);
196 : : #endif
197 : 39 : }
198 : :
199 : 0 : void outputPriority(oslThreadPriority const& _aPriority)
200 : : {
201 : : // LLA: output the priority
202 [ # # ]: 0 : if (_aPriority == osl_Thread_PriorityHighest)
203 : : {
204 : 0 : t_print("Prio is High\n");
205 : : }
206 [ # # ]: 0 : else if (_aPriority == osl_Thread_PriorityAboveNormal)
207 : : {
208 : 0 : t_print("Prio is above normal\n");
209 : : }
210 [ # # ]: 0 : else if (_aPriority == osl_Thread_PriorityNormal)
211 : : {
212 : 0 : t_print("Prio is normal\n");
213 : : }
214 [ # # ]: 0 : else if (_aPriority == osl_Thread_PriorityBelowNormal)
215 : : {
216 : 0 : t_print("Prio is below normal\n");
217 : : }
218 [ # # ]: 0 : else if (_aPriority == osl_Thread_PriorityLowest)
219 : : {
220 : 0 : t_print("Prio is lowest\n");
221 : : }
222 : : else
223 : : {
224 : 0 : t_print("Prio is unknown\n");
225 : : }
226 : 0 : }
227 : : }
228 : :
229 : : /** Simple thread for testing Thread-create.
230 : :
231 : : Just add 1 of value 0, and after running, result is 1.
232 : : */
233 [ # # ]: 0 : class myThread : public Thread
234 : : {
235 : : ThreadSafeValue<sal_Int32> m_aFlag;
236 : : public:
237 : 0 : sal_Int32 getValue() { return m_aFlag.getValue(); }
238 : : protected:
239 : : /** guarded value which initialized 0
240 : :
241 : : @see ThreadSafeValue
242 : : */
243 : 0 : void SAL_CALL run()
244 : : {
245 [ # # ]: 0 : while(schedule())
246 : : {
247 : 0 : m_aFlag.addValue(1);
248 : 0 : ThreadHelper::thread_sleep_tenth_sec(1);
249 : : }
250 : 0 : }
251 : :
252 : : public:
253 : :
254 : 0 : virtual void SAL_CALL suspend()
255 : : {
256 : 0 : m_aFlag.acquire();
257 : 0 : ::osl::Thread::suspend();
258 : 0 : m_aFlag.release();
259 : 0 : }
260 : :
261 : 0 : ~myThread()
262 [ # # ]: 0 : {
263 [ # # ][ # # ]: 0 : if (isRunning())
264 : : {
265 [ # # ]: 0 : t_print("error: not terminated.\n");
266 : : }
267 [ # # ]: 0 : }
268 : :
269 : : };
270 : :
271 : : // -----------------------------------------------------------------------------
272 : : /** Thread which has a flag add 1 every second until 20
273 : : */
274 : : class OCountThread : public Thread
275 : : {
276 : : ThreadSafeValue<sal_Int32> m_aFlag;
277 : : public:
278 : 0 : OCountThread()
279 [ # # ]: 0 : {
280 : 0 : m_nWaitSec = 0;
281 [ # # ][ # # ]: 0 : t_print("new OCountThread thread %u!\n", (unsigned) getIdentifier());
282 : 0 : }
283 : 0 : sal_Int32 getValue() { return m_aFlag.getValue(); }
284 : :
285 : 0 : void setWait(sal_Int32 nSec)
286 : : {
287 : 0 : m_nWaitSec = nSec;
288 : : //m_bWait = sal_True;
289 : 0 : }
290 : :
291 : 0 : virtual void SAL_CALL suspend()
292 : : {
293 : 0 : m_aFlag.acquire();
294 : 0 : ::osl::Thread::suspend();
295 : 0 : m_aFlag.release();
296 : 0 : }
297 : :
298 : : protected:
299 : : //sal_Bool m_bWait;
300 : : sal_Int32 m_nWaitSec;
301 : :
302 : 0 : void SAL_CALL run()
303 : : {
304 : : /// if the thread should terminate, schedule return false
305 [ # # ][ # # ]: 0 : while (m_aFlag.getValue() < 20 && schedule() == sal_True)
[ # # ]
306 : : {
307 : 0 : m_aFlag.addValue(1);
308 : 0 : ThreadHelper::thread_sleep_tenth_sec(1);
309 : :
310 [ # # ]: 0 : if (m_nWaitSec != 0)
311 : : {
312 : : TimeValue nTV;
313 : 0 : nTV.Seconds = m_nWaitSec / 10 ;
314 : 0 : nTV.Nanosec = ( m_nWaitSec%10 ) * 100000000 ;
315 [ # # ]: 0 : wait( nTV );
316 : 0 : m_nWaitSec = 0;
317 : : }
318 : : }
319 : 0 : }
320 : 0 : void SAL_CALL onTerminated()
321 : : {
322 : 0 : t_print("normally terminate this thread %u!\n", (unsigned) getIdentifier());
323 : 0 : }
324 : : public:
325 : :
326 : 0 : ~OCountThread()
327 [ # # ]: 0 : {
328 [ # # ][ # # ]: 0 : if (isRunning())
329 : : {
330 [ # # ]: 0 : t_print("error: not terminated.\n");
331 : : }
332 [ # # ]: 0 : }
333 : :
334 : : };
335 : :
336 : : /** call suspend in the run method
337 : : */
338 : : class OSuspendThread : public Thread
339 : : {
340 : : ThreadSafeValue<sal_Int32> m_aFlag;
341 : : public:
342 : : OSuspendThread(){ m_bSuspend = sal_False; }
343 : : sal_Int32 getValue() { return m_aFlag.getValue(); }
344 : : void setSuspend()
345 : : {
346 : : m_bSuspend = sal_True;
347 : : }
348 : 0 : virtual void SAL_CALL suspend()
349 : : {
350 : 0 : m_aFlag.acquire();
351 : 0 : ::osl::Thread::suspend();
352 : 0 : m_aFlag.release();
353 : 0 : }
354 : : protected:
355 : : sal_Bool m_bSuspend;
356 : 0 : void SAL_CALL run()
357 : : {
358 : : //if the thread should terminate, schedule return false
359 [ # # ]: 0 : while (schedule() == sal_True)
360 : : {
361 : 0 : m_aFlag.addValue(1);
362 : :
363 : 0 : ThreadHelper::thread_sleep_tenth_sec(1);
364 [ # # ]: 0 : if (m_bSuspend == sal_True)
365 : : {
366 : 0 : suspend();
367 : 0 : m_bSuspend = sal_False;
368 : : }
369 : : }
370 : 0 : }
371 : : public:
372 : :
373 : 0 : ~OSuspendThread()
374 [ # # ]: 0 : {
375 [ # # ][ # # ]: 0 : if (isRunning())
376 : : {
377 [ # # ]: 0 : t_print("error: not terminated.\n");
378 : : }
379 [ # # ]: 0 : }
380 : :
381 : : };
382 : :
383 : : /** no call schedule in the run method
384 : : */
385 : : class ONoScheduleThread : public Thread
386 : : {
387 : : ThreadSafeValue<sal_Int32> m_aFlag;
388 : : public:
389 : 0 : sal_Int32 getValue() { return m_aFlag.getValue(); }
390 : :
391 : 0 : virtual void SAL_CALL suspend()
392 : : {
393 : 0 : m_aFlag.acquire();
394 : 0 : ::osl::Thread::suspend();
395 : 0 : m_aFlag.release();
396 : 0 : }
397 : : protected:
398 : 0 : void SAL_CALL run()
399 : : {
400 [ # # ]: 0 : while (m_aFlag.getValue() < 10)
401 : : {
402 : 0 : m_aFlag.addValue(1);
403 : 0 : ThreadHelper::thread_sleep_tenth_sec(1);
404 : : }
405 : 0 : }
406 : 0 : void SAL_CALL onTerminated()
407 : : {
408 : 0 : t_print("normally terminate this thread %u!\n", (unsigned) getIdentifier());
409 : 0 : }
410 : : public:
411 : 0 : ONoScheduleThread()
412 [ # # ]: 0 : {
413 [ # # ][ # # ]: 0 : t_print("new thread id %u!\n", (unsigned) getIdentifier());
414 : 0 : }
415 : 0 : ~ONoScheduleThread()
416 [ # # ]: 0 : {
417 [ # # ][ # # ]: 0 : if (isRunning())
418 : : {
419 [ # # ]: 0 : t_print("error: not terminated.\n");
420 : : }
421 [ # # ]: 0 : }
422 : :
423 : : };
424 : :
425 : : /**
426 : : */
427 : : class OAddThread : public Thread
428 : : {
429 : : ThreadSafeValue<sal_Int32> m_aFlag;
430 : : public:
431 : : //oslThreadIdentifier m_id, m_CurId;
432 [ # # ]: 0 : OAddThread(){}
433 : 0 : sal_Int32 getValue() { return m_aFlag.getValue(); }
434 : :
435 : 0 : virtual void SAL_CALL suspend()
436 : : {
437 : 0 : m_aFlag.acquire();
438 : 0 : ::osl::Thread::suspend();
439 : 0 : m_aFlag.release();
440 : 0 : }
441 : : protected:
442 : 0 : void SAL_CALL run()
443 : : {
444 : : //if the thread should terminate, schedule return false
445 [ # # ]: 0 : while (schedule() == sal_True)
446 : : {
447 : 0 : m_aFlag.addValue(1);
448 : : }
449 : 0 : }
450 : 0 : void SAL_CALL onTerminated()
451 : : {
452 : : // t_print("normally terminate this thread %d!\n", getIdentifier());
453 : 0 : }
454 : : public:
455 : :
456 : 0 : ~OAddThread()
457 [ # # ]: 0 : {
458 [ # # ]: 0 : if (isRunning())
459 : : {
460 : : // t_print("error: not terminated.\n");
461 : : }
462 [ # # ]: 0 : }
463 : :
464 : : };
465 : :
466 : : namespace osl_Thread
467 : : {
468 : :
469 : 0 : void resumeAndWaitThread(Thread* _pThread)
470 : : {
471 : : // This functions starts a thread, wait a second and suspends the thread
472 : : // Due to the fact, that a suspend and never run thread never really exists.
473 : :
474 : : // Note: on UNX, after createSuspended, and then terminate the thread, it performs well;
475 : : // while on Windows, after createSuspended, the thread can not terminate, wait endlessly,
476 : : // so here call resume at first, then call terminate.
477 : : #ifdef WNT
478 : : t_print("resumeAndWaitThread\n");
479 : : _pThread->resume();
480 : : ThreadHelper::thread_sleep_tenth_sec(1);
481 : : #else
482 : 0 : _pThread->resume();
483 : : #endif
484 : 0 : }
485 : :
486 : : // kill a running thread and join it, if it has terminated, do nothing
487 : 0 : void termAndJoinThread(Thread* _pThread)
488 : : {
489 : 0 : _pThread->terminate();
490 : :
491 : : // LLA: Windows feature???, a suspended thread can not terminated, so we have to weak it up
492 : : #ifdef WNT
493 : : _pThread->resume();
494 : : ThreadHelper::thread_sleep_tenth_sec(1);
495 : : #endif
496 : 0 : t_print("#wait for join.\n");
497 : 0 : _pThread->join();
498 : 0 : }
499 : : /** Test of the osl::Thread::create method
500 : : */
501 : :
502 [ # # ]: 0 : class create : public CppUnit::TestFixture
503 : : {
504 : : public:
505 : :
506 : : // initialise your test code values here.
507 : 0 : void setUp()
508 : : {
509 : 0 : }
510 : :
511 : 0 : void tearDown()
512 : : {
513 : 0 : }
514 : :
515 : : /** Simple create a thread.
516 : :
517 : : Create a simple thread, it just does add 1 to value(which initialized 0),
518 : : if the thread run, the value should be 1.
519 : : */
520 : 0 : void create_001()
521 : : {
522 [ # # ]: 0 : myThread* newthread = new myThread();
523 : 0 : sal_Bool bRes = newthread->create();
524 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!\n", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
525 : :
526 : 0 : ThreadHelper::thread_sleep_tenth_sec(1); // wait short
527 : 0 : sal_Bool isRunning = newthread->isRunning(); // check if thread is running
528 : : /// wait for the new thread to assure it has run
529 : 0 : ThreadHelper::thread_sleep_tenth_sec(3);
530 : 0 : sal_Int32 nValue = newthread->getValue();
531 : : /// to assure the new thread has terminated
532 : 0 : termAndJoinThread(newthread);
533 [ # # ]: 0 : delete newthread;
534 : :
535 : 0 : t_print(" nValue = %d\n", (int) nValue);
536 [ # # ]: 0 : t_print("isRunning = %s\n", isRunning == sal_True ? "true" : "false");
537 : :
538 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
539 : : "Creates a new thread",
540 : : nValue >= 1 && isRunning == sal_True
541 [ # # ]: 0 : );
542 : :
543 : 0 : }
544 : :
545 : : /** only one running thread per instance, return false if create secondly
546 : : */
547 : 0 : void create_002()
548 : : {
549 [ # # ]: 0 : myThread* newthread = new myThread();
550 : 0 : sal_Bool res1 = newthread->create();
551 : 0 : sal_Bool res2 = newthread->create();
552 : 0 : t_print("In non pro, an assertion should occurred. This behaviour is right.\n");
553 : 0 : termAndJoinThread(newthread);
554 [ # # ]: 0 : delete newthread;
555 : :
556 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
557 : : "Creates a new thread: can not create two threads per instance",
558 : : res1 && !res2
559 [ # # ]: 0 : );
560 : :
561 : 0 : }
562 : :
563 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(create);
[ # # ][ # # ]
[ # # ]
564 [ # # ][ # # ]: 0 : CPPUNIT_TEST(create_001);
[ # # ][ # # ]
[ # # ][ # # ]
565 [ # # ][ # # ]: 0 : CPPUNIT_TEST(create_002);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
566 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
567 : : }; // class create
568 : :
569 : :
570 : :
571 : : /** Test of the osl::Thread::createSuspended method
572 : : */
573 [ # # ]: 0 : class createSuspended : public CppUnit::TestFixture
574 : : {
575 : : public:
576 : : // initialise your test code values here.
577 : 0 : void setUp()
578 : : {
579 : 0 : }
580 : :
581 : 0 : void tearDown()
582 : : {
583 : 0 : }
584 : :
585 : : /** Create a suspended thread, use the same class as create_001
586 : :
587 : : after create, wait enough time, check the value, if it's still the initial value, pass
588 : : */
589 : 0 : void createSuspended_001()
590 : : {
591 [ # # ]: 0 : myThread* newthread = new myThread();
592 : 0 : sal_Bool bRes = newthread->createSuspended();
593 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
594 : :
595 : 0 : ThreadHelper::thread_sleep_tenth_sec(1);
596 : 0 : sal_Bool isRunning = newthread->isRunning();
597 : 0 : ThreadHelper::thread_sleep_tenth_sec(3);
598 : 0 : sal_Int32 nValue = newthread->getValue();
599 : :
600 : 0 : resumeAndWaitThread(newthread);
601 : :
602 : 0 : termAndJoinThread(newthread);
603 [ # # ]: 0 : delete newthread;
604 : :
605 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
606 : : "Creates a new suspended thread",
607 : : nValue == 0 && isRunning
608 [ # # ]: 0 : );
609 : 0 : }
610 : :
611 : 0 : void createSuspended_002()
612 : : {
613 [ # # ]: 0 : myThread* newthread = new myThread();
614 : 0 : sal_Bool res1 = newthread->createSuspended();
615 : 0 : sal_Bool res2 = newthread->createSuspended();
616 : :
617 : 0 : resumeAndWaitThread(newthread);
618 : :
619 : 0 : termAndJoinThread(newthread);
620 : :
621 [ # # ]: 0 : delete newthread;
622 : :
623 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
624 : : "Creates a new thread: can not create two threads per instance",
625 : : res1 && !res2
626 [ # # ]: 0 : );
627 : 0 : }
628 : :
629 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(createSuspended);
[ # # ][ # # ]
[ # # ]
630 [ # # ][ # # ]: 0 : CPPUNIT_TEST(createSuspended_001);
[ # # ][ # # ]
[ # # ][ # # ]
631 : : // LLA: Deadlocked!!!
632 [ # # ][ # # ]: 0 : CPPUNIT_TEST(createSuspended_002);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
633 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
634 : : }; // class createSuspended
635 : :
636 : : /** when the count value equal to or more than 3, suspend the thread.
637 : : */
638 : 0 : void suspendCountThread(OCountThread* _pCountThread)
639 : : {
640 : 0 : sal_Int32 nValue = 0;
641 : 0 : while (1)
642 : : {
643 : 0 : nValue = _pCountThread->getValue();
644 [ # # ]: 0 : if (nValue >= 3)
645 : : {
646 : 0 : _pCountThread->suspend();
647 : 0 : break;
648 : : }
649 : : }
650 : 0 : }
651 : :
652 : : /** Test of the osl::Thread::suspend method
653 : : */
654 [ # # ]: 0 : class suspend : public CppUnit::TestFixture
655 : : {
656 : : public:
657 : : // initialise your test code values here.
658 : 0 : void setUp()
659 : : {
660 : 0 : }
661 : :
662 : 0 : void tearDown()
663 : : {
664 : 0 : }
665 : :
666 : : /** Use a thread which has a flag added 1 every second
667 : :
668 : : ALGORITHM:
669 : : create the thread, after running special time, record value of flag, then suspend it,
670 : : wait a long time, check the flag, if it remains unchanged during suspending
671 : : */
672 : 0 : void suspend_001()
673 : : {
674 [ # # ]: 0 : OCountThread* aCountThread = new OCountThread();
675 : 0 : sal_Bool bRes = aCountThread->create();
676 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
677 : : // the thread run for some seconds, but not terminate
678 : 0 : suspendCountThread( aCountThread );
679 : :
680 : : // the value just after calling suspend
681 : 0 : sal_Int32 nValue = aCountThread->getValue(); // (2)
682 : :
683 : 0 : ThreadHelper::thread_sleep_tenth_sec(3);
684 : :
685 : : // the value after waiting 3 seconds
686 : 0 : sal_Int32 nLaterValue = aCountThread->getValue(); // (3)
687 : :
688 : 0 : resumeAndWaitThread(aCountThread);
689 : 0 : termAndJoinThread(aCountThread);
690 [ # # ]: 0 : delete aCountThread;
691 : :
692 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
693 : : "Suspend the thread",
694 : : bRes == sal_True && nValue == nLaterValue
695 [ # # ]: 0 : );
696 : :
697 : 0 : }
698 : : /** suspend a thread in it's worker-function, the ALGORITHM is same as suspend_001
699 : : reason of deadlocked I think: no schedule can schedule other threads to go on excuting
700 : : */
701 : : void suspend_002()
702 : : {
703 : : OSuspendThread* aThread = new OSuspendThread();
704 : : sal_Bool bRes = aThread->create();
705 : : CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
706 : : // first the thread run for some seconds, but not terminate
707 : : sal_Int32 nValue = 0;
708 : : //while (1)
709 : : //{
710 : : ThreadHelper::thread_sleep_tenth_sec(3);
711 : : nValue = aThread->getValue(); // (1)
712 : : t_print(" getValue is %d !", (int) nValue );
713 : : if (nValue >= 2)
714 : : {
715 : : aThread->setSuspend();
716 : : //break;
717 : : }
718 : : //}
719 : : t_print(" after while!");
720 : : // the value just after calling suspend
721 : : nValue = aThread->getValue(); // (2)
722 : :
723 : : ThreadHelper::thread_sleep_tenth_sec(3);
724 : : t_print(" after sleep!");
725 : : // the value after waiting 3 seconds
726 : : sal_Int32 nLaterValue = aThread->getValue(); // (3)
727 : :
728 : : //resumeAndWaitThread(aThread);
729 : : aThread->resume();
730 : : termAndJoinThread(aThread);
731 : : delete aThread;
732 : :
733 : : CPPUNIT_ASSERT_MESSAGE(
734 : : "Suspend the thread",
735 : : bRes == sal_True && nValue == nLaterValue
736 : : );
737 : : }
738 : :
739 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(suspend);
[ # # ][ # # ]
[ # # ]
740 [ # # ][ # # ]: 0 : CPPUNIT_TEST(suspend_001);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
741 : : // LLA: Deadlocked!!!
742 : : // CPPUNIT_TEST(createSuspended_002);
743 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
744 : : }; // class suspend
745 : :
746 : : /** Test of the osl::Thread::resume method
747 : : */
748 [ # # ]: 0 : class resume : public CppUnit::TestFixture
749 : : {
750 : : public:
751 : : // initialise your test code values here.
752 : 0 : void setUp()
753 : : {
754 : 0 : }
755 : :
756 : 0 : void tearDown()
757 : : {
758 : 0 : }
759 : :
760 : : /** check if the thread run samely as usual after suspend and resume
761 : :
762 : : ALGORITHM:
763 : : compare the values before and after suspend, they should be same,
764 : : then compare values before and after resume, the difference should be same as the sleep seconds number
765 : : */
766 : 0 : void resume_001()
767 : : {
768 [ # # ]: 0 : OCountThread* pCountThread = new OCountThread();
769 : 0 : sal_Bool bRes = pCountThread->create();
770 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
771 : :
772 : 0 : suspendCountThread(pCountThread);
773 : :
774 : 0 : sal_Int32 nSuspendValue = pCountThread->getValue(); // (2)
775 : : // suspend for 3 seconds
776 : 0 : ThreadHelper::thread_sleep_tenth_sec(3);
777 : 0 : pCountThread->resume();
778 : :
779 : 0 : ThreadHelper::thread_sleep_tenth_sec(3);
780 : 0 : sal_Int32 nResumeValue = pCountThread->getValue();
781 : :
782 : 0 : ThreadHelper::thread_sleep_tenth_sec(3);
783 : 0 : sal_Int32 nLaterValue = pCountThread->getValue();
784 : :
785 : 0 : termAndJoinThread(pCountThread);
786 [ # # ]: 0 : delete pCountThread;
787 : :
788 : 0 : t_print("SuspendValue: %d\n", (int) nSuspendValue);
789 : 0 : t_print("ResumeValue: %d\n", (int) nResumeValue);
790 : 0 : t_print("LaterValue: %d\n", (int) nLaterValue);
791 : :
792 : : /* LLA: this assumption is no longer relevant: nResumeValue == nSuspendValue && */
793 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
794 : : "Suspend then resume the thread",
795 : : nLaterValue >= 9 &&
796 : : nResumeValue > nSuspendValue &&
797 : : nLaterValue > nResumeValue
798 [ # # ]: 0 : );
799 : :
800 : 0 : }
801 : :
802 : : /** Create a suspended thread then resume, check if the thread has run
803 : : */
804 : 0 : void resume_002()
805 : : {
806 [ # # ]: 0 : myThread* newthread = new myThread();
807 : 0 : sal_Bool bRes = newthread->createSuspended();
808 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE ( "Can't create thread!", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
809 : :
810 : 0 : newthread->resume();
811 : 0 : ThreadHelper::thread_sleep_tenth_sec(2);
812 : 0 : sal_Int32 nValue = newthread->getValue();
813 : :
814 : 0 : termAndJoinThread(newthread);
815 [ # # ]: 0 : delete newthread;
816 : :
817 : 0 : t_print(" nValue = %d\n", (int) nValue);
818 : :
819 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
820 : : "Creates a suspended thread, then resume",
821 : : nValue >= 1
822 [ # # ]: 0 : );
823 : 0 : }
824 : :
825 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(resume);
[ # # ][ # # ]
[ # # ]
826 [ # # ][ # # ]: 0 : CPPUNIT_TEST(resume_001);
[ # # ][ # # ]
[ # # ][ # # ]
827 [ # # ][ # # ]: 0 : CPPUNIT_TEST(resume_002);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
828 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
829 : : }; // class resume
830 : :
831 : : /** Test of the osl::Thread::terminate method
832 : : */
833 [ # # ]: 0 : class terminate : public CppUnit::TestFixture
834 : : {
835 : : public:
836 : : // initialise your test code values here.
837 : 0 : void setUp()
838 : : {
839 : 0 : }
840 : :
841 : 0 : void tearDown()
842 : : {
843 : 0 : }
844 : :
845 : : /** Check after call terminate if the running thread running go on executing
846 : :
847 : : ALGORITHM:
848 : : before and after call terminate, the values should be the same
849 : : */
850 : 0 : void terminate_001()
851 : : {
852 [ # # ]: 0 : OCountThread* aCountThread = new OCountThread();
853 : 0 : sal_Bool bRes = aCountThread->create();
854 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
855 : :
856 : 0 : ThreadHelper::thread_sleep_tenth_sec(2);
857 : 0 : sal_Int32 nValue = aCountThread->getValue();
858 : 0 : aCountThread->terminate();
859 : 0 : ThreadHelper::thread_sleep_tenth_sec(2);
860 : 0 : sal_Int32 nLaterValue = aCountThread->getValue();
861 : :
862 : : // isRunning should be false after terminate
863 : 0 : sal_Bool isRunning = aCountThread->isRunning();
864 : 0 : aCountThread->join();
865 [ # # ]: 0 : delete aCountThread;
866 : :
867 : 0 : t_print(" nValue = %d\n", (int) nValue);
868 : 0 : t_print("nLaterValue = %d\n", (int) nLaterValue);
869 : :
870 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
871 : : "Terminate the thread",
872 : : isRunning == sal_False && nLaterValue >= nValue
873 [ # # ]: 0 : );
874 : 0 : }
875 : : /** Check if a suspended thread will terminate after call terminate, different on w32 and on UNX
876 : : */
877 : 0 : void terminate_002()
878 : : {
879 [ # # ]: 0 : OCountThread* aCountThread = new OCountThread();
880 : 0 : sal_Bool bRes = aCountThread->create();
881 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
882 : :
883 : 0 : ThreadHelper::thread_sleep_tenth_sec(1);
884 : 0 : suspendCountThread(aCountThread);
885 : 0 : sal_Int32 nValue = aCountThread->getValue();
886 : :
887 : : // seems a suspended thread can not be terminated on W32, while on Solaris can
888 : 0 : resumeAndWaitThread(aCountThread);
889 : :
890 : 0 : ThreadHelper::thread_sleep_tenth_sec(2);
891 : :
892 : 0 : termAndJoinThread(aCountThread);
893 : 0 : sal_Int32 nLaterValue = aCountThread->getValue();
894 [ # # ]: 0 : delete aCountThread;
895 : :
896 : 0 : t_print(" nValue = %d\n", (int) nValue);
897 : 0 : t_print("nLaterValue = %d\n", (int) nLaterValue);
898 : :
899 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
900 : : "Suspend then resume the thread",
901 [ # # ]: 0 : nLaterValue > nValue );
902 : 0 : }
903 : :
904 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(terminate);
[ # # ][ # # ]
[ # # ]
905 [ # # ][ # # ]: 0 : CPPUNIT_TEST(terminate_001);
[ # # ][ # # ]
[ # # ][ # # ]
906 [ # # ][ # # ]: 0 : CPPUNIT_TEST(terminate_002);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
907 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
908 : : }; // class terminate
909 : :
910 : : /** Test of the osl::Thread::join method
911 : : */
912 [ # # ]: 0 : class join : public CppUnit::TestFixture
913 : : {
914 : : public:
915 : : // initialise your test code values here.
916 : 0 : void setUp()
917 : : {
918 : 0 : }
919 : :
920 : 0 : void tearDown()
921 : : {
922 : 0 : }
923 : :
924 : : /** Check after call terminate if the thread running function will not go on executing
925 : :
926 : : the next statement after join will not exec before the thread terminate
927 : : ALGORITHM:
928 : : recode system time at the beginning of the thread run, call join, then record system time again,
929 : : the difference of the two time should be equal or more than 20 seconds, the CountThead normally terminate
930 : : */
931 : 0 : void join_001()
932 : : {
933 [ # # ]: 0 : OCountThread *aCountThread = new OCountThread();
934 [ # # ]: 0 : sal_Bool bRes = aCountThread->create();
935 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
936 : :
937 : 0 : StopWatch aStopWatch;
938 [ # # ]: 0 : aStopWatch.start();
939 : : // TimeValue aTimeVal_befor;
940 : : // osl_getSystemTime( &aTimeVal_befor );
941 : : //t_print("#join:the system time is %d,%d\n", pTimeVal_befor->Seconds,pTimeVal_befor->Nanosec);
942 : :
943 [ # # ]: 0 : aCountThread->join();
944 : :
945 : : //the below line will be executed after aCountThread terminate
946 : : // TimeValue aTimeVal_after;
947 : : // osl_getSystemTime( &aTimeVal_after );
948 [ # # ]: 0 : aStopWatch.stop();
949 : : // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
950 : 0 : double nSec = aStopWatch.getSeconds();
951 [ # # ]: 0 : t_print("join_001 nSec=%f\n", nSec);
952 [ # # ][ # # ]: 0 : delete aCountThread;
953 : :
954 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
955 : : "Join the thread: after the thread terminate",
956 : : nSec >= 2
957 [ # # ]: 0 : );
958 : :
959 : 0 : }
960 : : /** after terminated by another thread, join exited immediately
961 : :
962 : : ALGORITHM:
963 : : terminate the thread when value>=3, call join, check the beginning time and time after join,
964 : : the difference should be 3 seconds, join costs little time
965 : : */
966 : 0 : void join_002()
967 : : {
968 [ # # ]: 0 : OCountThread *aCountThread = new OCountThread();
969 [ # # ]: 0 : sal_Bool bRes = aCountThread->create();
970 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
971 : :
972 : : //record the time when the running begin
973 : : // TimeValue aTimeVal_befor;
974 : : // osl_getSystemTime( &aTimeVal_befor );
975 : 0 : StopWatch aStopWatch;
976 [ # # ]: 0 : aStopWatch.start();
977 : :
978 [ # # ]: 0 : ThreadHelper::thread_sleep_tenth_sec(10);
979 [ # # ]: 0 : termAndJoinThread(aCountThread);
980 : :
981 : : //the below line will be executed after aCountThread terminate
982 : : // TimeValue aTimeVal_after;
983 : : // osl_getSystemTime( &aTimeVal_after );
984 : : // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
985 [ # # ]: 0 : aStopWatch.stop();
986 : 0 : double nSec = aStopWatch.getSeconds();
987 [ # # ]: 0 : t_print("join_002 nSec=%f\n", nSec);
988 : :
989 [ # # ][ # # ]: 0 : delete aCountThread;
990 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
991 : : "Join the thread: after thread terminate by another thread",
992 : : nSec >= 1
993 [ # # ]: 0 : );
994 : 0 : }
995 : :
996 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(join);
[ # # ][ # # ]
[ # # ]
997 [ # # ][ # # ]: 0 : CPPUNIT_TEST(join_001);
[ # # ][ # # ]
[ # # ][ # # ]
998 [ # # ][ # # ]: 0 : CPPUNIT_TEST(join_002);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
999 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1000 : : }; // class join
1001 : :
1002 : : /** Test of the osl::Thread::isRunning method
1003 : : */
1004 [ # # ]: 0 : class isRunning : public CppUnit::TestFixture
1005 : : {
1006 : : public:
1007 : : // initialise your test code values here.
1008 : 0 : void setUp()
1009 : : {
1010 : 0 : }
1011 : :
1012 : 0 : void tearDown()
1013 : : {
1014 : 0 : }
1015 : :
1016 : : /**
1017 : : */
1018 : 0 : void isRunning_001()
1019 : : {
1020 [ # # ]: 0 : OCountThread *aCountThread = new OCountThread();
1021 : 0 : sal_Bool bRes = aCountThread->create();
1022 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1023 : :
1024 : 0 : sal_Bool bRun = aCountThread->isRunning();
1025 : :
1026 : 0 : ThreadHelper::thread_sleep_tenth_sec(2);
1027 : 0 : termAndJoinThread(aCountThread);
1028 : 0 : sal_Bool bTer = aCountThread->isRunning();
1029 [ # # ]: 0 : delete aCountThread;
1030 : :
1031 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1032 : : "Test isRunning",
1033 : : bRun == sal_True && bTer == sal_False
1034 [ # # ]: 0 : );
1035 : 0 : }
1036 : : /** check the value of isRunning when suspending and after resume
1037 : : */
1038 : 0 : void isRunning_002()
1039 : : {
1040 [ # # ]: 0 : OCountThread *aCountThread = new OCountThread();
1041 : 0 : sal_Bool bRes = aCountThread->create();
1042 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1043 : :
1044 : : // sal_Bool bRunning = aCountThread->isRunning();
1045 : : // sal_Int32 nValue = 0;
1046 : 0 : suspendCountThread(aCountThread);
1047 : :
1048 : 0 : sal_Bool bRunning_sup = aCountThread->isRunning();
1049 : 0 : ThreadHelper::thread_sleep_tenth_sec(2);
1050 : 0 : aCountThread->resume();
1051 : 0 : ThreadHelper::thread_sleep_tenth_sec(2);
1052 : 0 : sal_Bool bRunning_res = aCountThread->isRunning();
1053 : 0 : termAndJoinThread(aCountThread);
1054 : 0 : sal_Bool bRunning_ter = aCountThread->isRunning();
1055 [ # # ]: 0 : delete aCountThread;
1056 : :
1057 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1058 : : "Test isRunning",
1059 : : bRes == sal_True &&
1060 : : bRunning_sup == sal_True &&
1061 : : bRunning_res == sal_True &&
1062 : : bRunning_ter == sal_False
1063 [ # # ]: 0 : );
1064 : :
1065 : 0 : }
1066 : :
1067 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(isRunning);
[ # # ][ # # ]
[ # # ]
1068 [ # # ][ # # ]: 0 : CPPUNIT_TEST(isRunning_001);
[ # # ][ # # ]
[ # # ][ # # ]
1069 [ # # ][ # # ]: 0 : CPPUNIT_TEST(isRunning_002);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1070 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1071 : : }; // class isRunning
1072 : :
1073 : :
1074 : : /// check osl::Thread::setPriority
1075 [ # # ]: 0 : class setPriority : public CppUnit::TestFixture
1076 : : {
1077 : : public:
1078 : : // initialise your test code values here.
1079 : 0 : void setUp()
1080 : : {
1081 : 0 : }
1082 : :
1083 : 0 : void tearDown()
1084 : : {
1085 : 0 : }
1086 : :
1087 : : // insert your test code here.
1088 : 0 : rtl::OString getPrioName(oslThreadPriority _aPriority)
1089 : : {
1090 : 0 : rtl::OString sPrioStr;
1091 [ # # # # : 0 : switch (_aPriority)
# # ]
1092 : : {
1093 : : case osl_Thread_PriorityHighest:
1094 : 0 : sPrioStr = "Highest";
1095 : 0 : break;
1096 : :
1097 : : case osl_Thread_PriorityAboveNormal:
1098 : 0 : sPrioStr = "AboveNormal";
1099 : 0 : break;
1100 : :
1101 : : case osl_Thread_PriorityNormal:
1102 : 0 : sPrioStr = "Normal";
1103 : 0 : break;
1104 : :
1105 : : case osl_Thread_PriorityBelowNormal:
1106 : 0 : sPrioStr = "BelowNormal";
1107 : 0 : break;
1108 : :
1109 : : case osl_Thread_PriorityLowest:
1110 : 0 : sPrioStr = "Lowest";
1111 : 0 : break;
1112 : : default:
1113 : 0 : sPrioStr = "unknown";
1114 : : }
1115 : 0 : return sPrioStr;
1116 : : }
1117 : :
1118 : :
1119 : : /** check 2 threads.
1120 : :
1121 : : ALGORITHM:
1122 : : Here the function should show, that 2 different threads,
1123 : : which only increase a value, should run at the same time with same prio.
1124 : : The test fails, if the difference between the two values is more than 5%
1125 : : but IMHO this isn't a failure, it's only a feature of the OS.
1126 : : */
1127 : :
1128 : 0 : void check2Threads(oslThreadPriority _aPriority)
1129 : : {
1130 : : // initial 5 threads with different priorities
1131 [ # # ]: 0 : OAddThread* pThread = new OAddThread();
1132 [ # # ]: 0 : OAddThread* p2Thread = new OAddThread();
1133 : :
1134 : : //Create them and start running at the same time
1135 [ # # ]: 0 : pThread->create();
1136 [ # # ]: 0 : pThread->setPriority(_aPriority);
1137 [ # # ]: 0 : p2Thread->create();
1138 [ # # ]: 0 : p2Thread->setPriority(_aPriority);
1139 : :
1140 [ # # ]: 0 : ThreadHelper::thread_sleep_tenth_sec(5);
1141 : :
1142 [ # # ]: 0 : pThread->terminate();
1143 [ # # ]: 0 : p2Thread->terminate();
1144 : :
1145 : 0 : sal_Int32 nValueNormal = 0;
1146 [ # # ]: 0 : nValueNormal = pThread->getValue();
1147 : :
1148 : 0 : sal_Int32 nValueNormal2 = 0;
1149 [ # # ]: 0 : nValueNormal2 = p2Thread->getValue();
1150 : :
1151 [ # # ]: 0 : rtl::OString sPrio = getPrioName(_aPriority);
1152 [ # # ]: 0 : t_print("After 10 tenth seconds\n");
1153 : :
1154 [ # # ]: 0 : t_print("nValue in %s Prio Thread is %d\n",sPrio.getStr(), (int) nValueNormal);
1155 [ # # ]: 0 : t_print("nValue in %s Prio Thread is %d\n", sPrio.getStr(), (int) nValueNormal2);
1156 : :
1157 : : // ThreadHelper::thread_sleep_tenth_sec(1);
1158 [ # # ]: 0 : pThread->join();
1159 [ # # ]: 0 : p2Thread->join();
1160 : :
1161 [ # # ][ # # ]: 0 : delete pThread;
1162 [ # # ][ # # ]: 0 : delete p2Thread;
1163 : :
1164 : 0 : sal_Int32 nDelta = abs(nValueNormal - nValueNormal2);
1165 [ # # ]: 0 : double nQuotient = std::max(nValueNormal, nValueNormal2);
1166 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1167 : : "Quotient is zero, which means, there exist no right values.",
1168 : : nQuotient != 0
1169 [ # # ]: 0 : );
1170 : 0 : double nDeltaPercent = nDelta / nQuotient * 100;
1171 : :
1172 [ # # ]: 0 : t_print("Delta value %d, percent %f\n", (int) nDelta, nDeltaPercent);
1173 : :
1174 : : // LLA: it's not a bug if the current OS is not able to handle thread scheduling right and good.
1175 : : // like Windows XP
1176 : : // LLA: CPPUNIT_ASSERT_MESSAGE(
1177 : : // LLA: "Run 2 normal threads, the count diff more than 5 percent.",
1178 : : // LLA: nDeltaPercent <= 5
1179 : : // LLA: );
1180 : 0 : }
1181 : :
1182 : 0 : void setPriority_001_1()
1183 : : {
1184 : 0 : check2Threads(osl_Thread_PriorityHighest);
1185 : 0 : }
1186 : 0 : void setPriority_001_2()
1187 : : {
1188 : 0 : check2Threads(osl_Thread_PriorityAboveNormal);
1189 : 0 : }
1190 : 0 : void setPriority_001_3()
1191 : : {
1192 : 0 : check2Threads(osl_Thread_PriorityNormal);
1193 : 0 : }
1194 : 0 : void setPriority_001_4()
1195 : : {
1196 : 0 : check2Threads(osl_Thread_PriorityBelowNormal);
1197 : 0 : }
1198 : 0 : void setPriority_001_5()
1199 : : {
1200 : 0 : check2Threads(osl_Thread_PriorityLowest);
1201 : 0 : }
1202 : :
1203 : 0 : void setPriority_002()
1204 : : {
1205 : : // initial 5 threads with different priorities
1206 : :
1207 [ # # ]: 0 : OAddThread aHighestThread;
1208 [ # # ]: 0 : OAddThread aAboveNormalThread;
1209 [ # # ]: 0 : OAddThread aNormalThread;
1210 : : //OAddThread *aBelowNormalThread = new OAddThread();
1211 : : //OAddThread *aLowestThread = new OAddThread();
1212 : :
1213 : : //Create them and start running at the same time
1214 [ # # ]: 0 : aHighestThread.createSuspended();
1215 [ # # ]: 0 : aHighestThread.setPriority(osl_Thread_PriorityHighest);
1216 : :
1217 [ # # ]: 0 : aAboveNormalThread.createSuspended();
1218 [ # # ]: 0 : aAboveNormalThread.setPriority(osl_Thread_PriorityAboveNormal);
1219 : :
1220 [ # # ]: 0 : aNormalThread.createSuspended();
1221 [ # # ]: 0 : aNormalThread.setPriority(osl_Thread_PriorityNormal);
1222 : : /*aBelowNormalThread->create();
1223 : : aBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1224 : : aLowestThread->create();
1225 : : aLowestThread->setPriority(osl_Thread_PriorityLowest);
1226 : : */
1227 : :
1228 [ # # ]: 0 : aHighestThread.resume();
1229 [ # # ]: 0 : aAboveNormalThread.resume();
1230 [ # # ]: 0 : aNormalThread.resume();
1231 : :
1232 [ # # ]: 0 : ThreadHelper::thread_sleep_tenth_sec(5);
1233 : :
1234 [ # # ]: 0 : aHighestThread.suspend();
1235 [ # # ]: 0 : aAboveNormalThread.suspend();
1236 [ # # ]: 0 : aNormalThread.suspend();
1237 : :
1238 [ # # ]: 0 : termAndJoinThread(&aNormalThread);
1239 [ # # ]: 0 : termAndJoinThread(&aAboveNormalThread);
1240 [ # # ]: 0 : termAndJoinThread(&aHighestThread);
1241 : : //aBelowNormalThread->terminate();
1242 : : //aLowestThread->terminate();
1243 : :
1244 : 0 : sal_Int32 nValueHighest = 0;
1245 [ # # ]: 0 : nValueHighest = aHighestThread.getValue();
1246 : :
1247 : 0 : sal_Int32 nValueAboveNormal = 0;
1248 [ # # ]: 0 : nValueAboveNormal = aAboveNormalThread.getValue();
1249 : :
1250 : 0 : sal_Int32 nValueNormal = 0;
1251 [ # # ]: 0 : nValueNormal = aNormalThread.getValue();
1252 : :
1253 [ # # ]: 0 : t_print("After 10 tenth seconds\n");
1254 [ # # ]: 0 : t_print("nValue in Highest Prio Thread is %d\n", (int) nValueHighest);
1255 [ # # ]: 0 : t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal);
1256 [ # # ]: 0 : t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal);
1257 : :
1258 : : #ifndef WNT
1259 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1260 : : "SetPriority",
1261 : : nValueHighest > 0 &&
1262 : : nValueAboveNormal > 0 &&
1263 : : nValueNormal > 0
1264 [ # # # # ]: 0 : );
[ # # ][ # # ]
1265 : : #endif
1266 : 0 : }
1267 : :
1268 : 0 : void setPriority_003()
1269 : : {
1270 : : // initial 5 threads with different priorities
1271 [ # # ]: 0 : OAddThread *pHighestThread = new OAddThread();
1272 [ # # ]: 0 : OAddThread *pAboveNormalThread = new OAddThread();
1273 [ # # ]: 0 : OAddThread *pNormalThread = new OAddThread();
1274 [ # # ]: 0 : OAddThread *pBelowNormalThread = new OAddThread();
1275 [ # # ]: 0 : OAddThread *pLowestThread = new OAddThread();
1276 : :
1277 : : //Create them and start running at the same time
1278 : 0 : pHighestThread->createSuspended();
1279 : 0 : pHighestThread->setPriority(osl_Thread_PriorityHighest);
1280 : :
1281 : 0 : pAboveNormalThread->createSuspended();
1282 : 0 : pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1283 : :
1284 : 0 : pNormalThread->createSuspended();
1285 : 0 : pNormalThread->setPriority(osl_Thread_PriorityNormal);
1286 : :
1287 : 0 : pBelowNormalThread->createSuspended();
1288 : 0 : pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1289 : :
1290 : 0 : pLowestThread->createSuspended();
1291 : 0 : pLowestThread->setPriority(osl_Thread_PriorityLowest);
1292 : :
1293 : 0 : pHighestThread->resume();
1294 : 0 : pAboveNormalThread->resume();
1295 : 0 : pNormalThread->resume();
1296 : 0 : pBelowNormalThread->resume();
1297 : 0 : pLowestThread->resume();
1298 : :
1299 : 0 : ThreadHelper::thread_sleep_tenth_sec(5);
1300 : :
1301 : 0 : pHighestThread->suspend();
1302 : 0 : pAboveNormalThread->suspend();
1303 : 0 : pNormalThread->suspend();
1304 : 0 : pBelowNormalThread->suspend();
1305 : 0 : pLowestThread->suspend();
1306 : :
1307 : 0 : termAndJoinThread(pHighestThread);
1308 : 0 : termAndJoinThread(pAboveNormalThread);
1309 : 0 : termAndJoinThread(pNormalThread);
1310 : 0 : termAndJoinThread(pBelowNormalThread);
1311 : 0 : termAndJoinThread(pLowestThread);
1312 : :
1313 : 0 : sal_Int32 nValueHighest = 0;
1314 : 0 : nValueHighest = pHighestThread->getValue();
1315 : :
1316 : 0 : sal_Int32 nValueAboveNormal = 0;
1317 : 0 : nValueAboveNormal = pAboveNormalThread->getValue();
1318 : :
1319 : 0 : sal_Int32 nValueNormal = 0;
1320 : 0 : nValueNormal = pNormalThread->getValue();
1321 : :
1322 : 0 : sal_Int32 nValueBelowNormal = 0;
1323 : 0 : nValueBelowNormal = pBelowNormalThread->getValue();
1324 : :
1325 : 0 : sal_Int32 nValueLowest = 0;
1326 : 0 : nValueLowest = pLowestThread->getValue();
1327 : :
1328 : 0 : t_print("After 10 tenth seconds\n");
1329 : 0 : t_print("nValue in Highest Prio Thread is %d\n", (int) nValueHighest);
1330 : 0 : t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal);
1331 : 0 : t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal);
1332 : 0 : t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal);
1333 : 0 : t_print("nValue in Lowest Prio Thread is %d\n", (int) nValueLowest);
1334 : :
1335 [ # # ]: 0 : delete pHighestThread;
1336 [ # # ]: 0 : delete pAboveNormalThread;
1337 [ # # ]: 0 : delete pNormalThread;
1338 [ # # ]: 0 : delete pBelowNormalThread;
1339 [ # # ]: 0 : delete pLowestThread;
1340 : :
1341 : : #ifndef WNT
1342 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1343 : : "SetPriority",
1344 : : nValueHighest > 0 &&
1345 : : nValueAboveNormal > 0 &&
1346 : : nValueNormal > 0 &&
1347 : : nValueBelowNormal > 0 &&
1348 : : nValueLowest > 0
1349 [ # # ]: 0 : );
1350 : : #endif
1351 : 0 : }
1352 : :
1353 : 0 : void setPriority_004()
1354 : : {
1355 : : // initial 5 threads with different priorities
1356 : : // OAddThread *pHighestThread = new OAddThread();
1357 [ # # ]: 0 : OAddThread *pAboveNormalThread = new OAddThread();
1358 [ # # ]: 0 : OAddThread *pNormalThread = new OAddThread();
1359 [ # # ]: 0 : OAddThread *pBelowNormalThread = new OAddThread();
1360 [ # # ]: 0 : OAddThread *pLowestThread = new OAddThread();
1361 : :
1362 : : //Create them and start running at the same time
1363 : : // pHighestThread->createSuspended();
1364 : : // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1365 : :
1366 : 0 : pAboveNormalThread->createSuspended();
1367 : 0 : pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1368 : :
1369 : 0 : pNormalThread->createSuspended();
1370 : 0 : pNormalThread->setPriority(osl_Thread_PriorityNormal);
1371 : :
1372 : 0 : pBelowNormalThread->createSuspended();
1373 : 0 : pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1374 : :
1375 : 0 : pLowestThread->createSuspended();
1376 : 0 : pLowestThread->setPriority(osl_Thread_PriorityLowest);
1377 : :
1378 : : // pHighestThread->resume();
1379 : 0 : pAboveNormalThread->resume();
1380 : 0 : pNormalThread->resume();
1381 : 0 : pBelowNormalThread->resume();
1382 : 0 : pLowestThread->resume();
1383 : :
1384 : 0 : ThreadHelper::thread_sleep_tenth_sec(5);
1385 : :
1386 : : // pHighestThread->suspend();
1387 : 0 : pAboveNormalThread->suspend();
1388 : 0 : pNormalThread->suspend();
1389 : 0 : pBelowNormalThread->suspend();
1390 : 0 : pLowestThread->suspend();
1391 : :
1392 : : // termAndJoinThread(pHighestThread);
1393 : 0 : termAndJoinThread(pAboveNormalThread);
1394 : 0 : termAndJoinThread(pNormalThread);
1395 : 0 : termAndJoinThread(pBelowNormalThread);
1396 : 0 : termAndJoinThread(pLowestThread);
1397 : :
1398 : : // sal_Int32 nValueHighest = 0;
1399 : : // nValueHighest = pHighestThread->getValue();
1400 : :
1401 : 0 : sal_Int32 nValueAboveNormal = 0;
1402 : 0 : nValueAboveNormal = pAboveNormalThread->getValue();
1403 : :
1404 : 0 : sal_Int32 nValueNormal = 0;
1405 : 0 : nValueNormal = pNormalThread->getValue();
1406 : :
1407 : 0 : sal_Int32 nValueBelowNormal = 0;
1408 : 0 : nValueBelowNormal = pBelowNormalThread->getValue();
1409 : :
1410 : 0 : sal_Int32 nValueLowest = 0;
1411 : 0 : nValueLowest = pLowestThread->getValue();
1412 : :
1413 : 0 : t_print("After 5 tenth seconds\n");
1414 : 0 : t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal);
1415 : 0 : t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal);
1416 : 0 : t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal);
1417 : 0 : t_print("nValue in Lowest Prio Thread is %d\n", (int) nValueLowest);
1418 : :
1419 : : // delete pHighestThread;
1420 [ # # ]: 0 : delete pAboveNormalThread;
1421 [ # # ]: 0 : delete pNormalThread;
1422 [ # # ]: 0 : delete pBelowNormalThread;
1423 [ # # ]: 0 : delete pLowestThread;
1424 : :
1425 : : #ifndef WNT
1426 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1427 : : "SetPriority",
1428 : : /* nValueHighest > 0 && */
1429 : : nValueAboveNormal > 0 &&
1430 : : nValueNormal > 0 &&
1431 : : nValueBelowNormal > 0 &&
1432 : : nValueLowest > 0
1433 [ # # ]: 0 : );
1434 : : #endif
1435 : 0 : }
1436 : 0 : void setPriority_005()
1437 : : {
1438 : : // initial 5 threads with different priorities
1439 : : // OAddThread *pHighestThread = new OAddThread();
1440 : : // OAddThread *pAboveNormalThread = new OAddThread();
1441 [ # # ]: 0 : OAddThread *pNormalThread = new OAddThread();
1442 [ # # ]: 0 : OAddThread *pBelowNormalThread = new OAddThread();
1443 [ # # ]: 0 : OAddThread *pLowestThread = new OAddThread();
1444 : :
1445 : : //Create them and start running at the same time
1446 : : // pHighestThread->createSuspended();
1447 : : // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1448 : :
1449 : : // pAboveNormalThread->createSuspended();
1450 : : // pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1451 : :
1452 : 0 : pNormalThread->createSuspended();
1453 : 0 : pNormalThread->setPriority(osl_Thread_PriorityNormal);
1454 : :
1455 : 0 : pBelowNormalThread->createSuspended();
1456 : 0 : pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1457 : :
1458 : 0 : pLowestThread->createSuspended();
1459 : 0 : pLowestThread->setPriority(osl_Thread_PriorityLowest);
1460 : :
1461 : : // pHighestThread->resume();
1462 : : // pAboveNormalThread->resume();
1463 : 0 : pNormalThread->resume();
1464 : 0 : pBelowNormalThread->resume();
1465 : 0 : pLowestThread->resume();
1466 : :
1467 : 0 : ThreadHelper::thread_sleep_tenth_sec(5);
1468 : :
1469 : : // pHighestThread->suspend();
1470 : : // pAboveNormalThread->suspend();
1471 : 0 : pNormalThread->suspend();
1472 : 0 : pBelowNormalThread->suspend();
1473 : 0 : pLowestThread->suspend();
1474 : :
1475 : : // termAndJoinThread(pHighestThread);
1476 : : // termAndJoinThread(pAboveNormalThread);
1477 : 0 : termAndJoinThread(pNormalThread);
1478 : 0 : termAndJoinThread(pBelowNormalThread);
1479 : 0 : termAndJoinThread(pLowestThread);
1480 : :
1481 : : // sal_Int32 nValueHighest = 0;
1482 : : // nValueHighest = pHighestThread->getValue();
1483 : :
1484 : : // sal_Int32 nValueAboveNormal = 0;
1485 : : // nValueAboveNormal = pAboveNormalThread->getValue();
1486 : :
1487 : 0 : sal_Int32 nValueNormal = 0;
1488 : 0 : nValueNormal = pNormalThread->getValue();
1489 : :
1490 : 0 : sal_Int32 nValueBelowNormal = 0;
1491 : 0 : nValueBelowNormal = pBelowNormalThread->getValue();
1492 : :
1493 : 0 : sal_Int32 nValueLowest = 0;
1494 : 0 : nValueLowest = pLowestThread->getValue();
1495 : :
1496 : 0 : t_print("After 5 tenth seconds\n");
1497 : 0 : t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal);
1498 : 0 : t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal);
1499 : 0 : t_print("nValue in Lowest Prio Thread is %d\n", (int) nValueLowest);
1500 : :
1501 [ # # ]: 0 : delete pNormalThread;
1502 [ # # ]: 0 : delete pBelowNormalThread;
1503 [ # # ]: 0 : delete pLowestThread;
1504 : :
1505 : : #ifndef WNT
1506 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1507 : : "SetPriority",
1508 : : /* nValueHighest > 0 && */
1509 : : /* nValueAboveNormal > 0 && */
1510 : : nValueNormal > 0 &&
1511 : : nValueBelowNormal > 0 &&
1512 : : nValueLowest > 0
1513 [ # # ]: 0 : );
1514 : : #endif
1515 : 0 : }
1516 : :
1517 : :
1518 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(setPriority);
[ # # ][ # # ]
[ # # ]
1519 : : #ifndef SOLARIS
1520 [ # # ][ # # ]: 0 : CPPUNIT_TEST(setPriority_002);
[ # # ][ # # ]
[ # # ][ # # ]
1521 [ # # ][ # # ]: 0 : CPPUNIT_TEST(setPriority_003);
[ # # ][ # # ]
[ # # ][ # # ]
1522 [ # # ][ # # ]: 0 : CPPUNIT_TEST(setPriority_004);
[ # # ][ # # ]
[ # # ][ # # ]
1523 [ # # ][ # # ]: 0 : CPPUNIT_TEST(setPriority_005);
[ # # ][ # # ]
[ # # ][ # # ]
1524 : : #endif
1525 [ # # ][ # # ]: 0 : CPPUNIT_TEST(setPriority_001_1);
[ # # ][ # # ]
[ # # ][ # # ]
1526 [ # # ][ # # ]: 0 : CPPUNIT_TEST(setPriority_001_2);
[ # # ][ # # ]
[ # # ][ # # ]
1527 [ # # ][ # # ]: 0 : CPPUNIT_TEST(setPriority_001_3);
[ # # ][ # # ]
[ # # ][ # # ]
1528 [ # # ][ # # ]: 0 : CPPUNIT_TEST(setPriority_001_4);
[ # # ][ # # ]
[ # # ][ # # ]
1529 [ # # ][ # # ]: 0 : CPPUNIT_TEST(setPriority_001_5);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1530 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1531 : : }; // class setPriority
1532 : :
1533 : : /** Test of the osl::Thread::getPriority method
1534 : : */
1535 [ # # ]: 0 : class getPriority : public CppUnit::TestFixture
1536 : : {
1537 : : public:
1538 : : // initialise your test code values here.
1539 : 0 : void setUp()
1540 : : {
1541 : 0 : }
1542 : :
1543 : 0 : void tearDown()
1544 : : {
1545 : 0 : }
1546 : :
1547 : : // insert your test code here.
1548 : 0 : void getPriority_001()
1549 : : {
1550 [ # # ]: 0 : OAddThread *pHighestThread = new OAddThread();
1551 : :
1552 : : //Create them and start running at the same time
1553 [ # # ]: 0 : pHighestThread->create();
1554 [ # # ]: 0 : pHighestThread->setPriority(osl_Thread_PriorityHighest);
1555 : :
1556 [ # # ]: 0 : oslThreadPriority aPriority = pHighestThread->getPriority();
1557 [ # # ]: 0 : termAndJoinThread(pHighestThread);
1558 [ # # ][ # # ]: 0 : delete pHighestThread;
1559 : :
1560 [ # # ]: 0 : ThreadHelper::outputPriority(aPriority);
1561 : :
1562 : : // LLA: Priority settings may not work within some OS versions.
1563 : : #if ( defined WNT ) || ( defined SOLARIS )
1564 : : CPPUNIT_ASSERT_MESSAGE(
1565 : : "getPriority",
1566 : : aPriority == osl_Thread_PriorityHighest
1567 : : );
1568 : : #else
1569 : : // LLA: Linux
1570 : : // NO_PTHREAD_PRIORITY ???
1571 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1572 : : "getPriority",
1573 : : aPriority == osl_Thread_PriorityNormal
1574 [ # # ]: 0 : );
1575 : : #endif
1576 : 0 : }
1577 : :
1578 : 0 : void getPriority_002()
1579 : : {
1580 : :
1581 : 0 : }
1582 : :
1583 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(getPriority);
[ # # ][ # # ]
[ # # ]
1584 [ # # ][ # # ]: 0 : CPPUNIT_TEST(getPriority_001);
[ # # ][ # # ]
[ # # ][ # # ]
1585 [ # # ][ # # ]: 0 : CPPUNIT_TEST(getPriority_002);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1586 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1587 : : }; // class getPriority
1588 : :
1589 : :
1590 [ # # ]: 0 : class getIdentifier : public CppUnit::TestFixture
1591 : : {
1592 : : public:
1593 : : // initialise your test code values here.
1594 : 0 : void setUp()
1595 : : {
1596 : 0 : }
1597 : :
1598 : 0 : void tearDown()
1599 : : {
1600 : 0 : }
1601 : :
1602 : : // insert your test code here.
1603 : 0 : void getIdentifier_001()
1604 : : {
1605 : :
1606 : 0 : }
1607 : :
1608 : 0 : void getIdentifier_002()
1609 : : {
1610 : :
1611 : 0 : }
1612 : :
1613 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(getIdentifier);
[ # # ][ # # ]
[ # # ]
1614 [ # # ][ # # ]: 0 : CPPUNIT_TEST(getIdentifier_001);
[ # # ][ # # ]
[ # # ][ # # ]
1615 [ # # ][ # # ]: 0 : CPPUNIT_TEST(getIdentifier_002);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1616 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1617 : : }; // class getIdentifier
1618 : :
1619 : : /** Test of the osl::Thread::getCurrentIdentifier method
1620 : : */
1621 [ # # ]: 0 : class getCurrentIdentifier : public CppUnit::TestFixture
1622 : : {
1623 : : public:
1624 : : // initialise your test code values here.
1625 : 0 : void setUp()
1626 : : {
1627 : 0 : }
1628 : :
1629 : 0 : void tearDown()
1630 : : {
1631 : 0 : }
1632 : :
1633 : : // insert your test code here.
1634 : 0 : void getCurrentIdentifier_001()
1635 : : {
1636 : : oslThreadIdentifier oId;
1637 [ # # ]: 0 : OCountThread* pCountThread = new OCountThread;
1638 : 0 : pCountThread->create();
1639 : 0 : pCountThread->setWait(3);
1640 : 0 : oId = Thread::getCurrentIdentifier();
1641 : 0 : oslThreadIdentifier oIdChild = pCountThread->getIdentifier();
1642 : 0 : termAndJoinThread(pCountThread);
1643 [ # # ]: 0 : delete pCountThread;
1644 : :
1645 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1646 : : "Get the identifier for the current active thread.",
1647 : : oId != oIdChild
1648 [ # # ]: 0 : );
1649 : :
1650 : 0 : }
1651 : :
1652 : : void getCurrentIdentifier_002()
1653 : : {
1654 : : }
1655 : :
1656 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(getCurrentIdentifier);
[ # # ][ # # ]
[ # # ]
1657 [ # # ][ # # ]: 0 : CPPUNIT_TEST(getCurrentIdentifier_001);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1658 : : //CPPUNIT_TEST(getCurrentIdentifier_002);
1659 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1660 : : }; // class getCurrentIdentifier
1661 : :
1662 : : /** Test of the osl::Thread::wait method
1663 : : */
1664 [ # # ]: 0 : class wait : public CppUnit::TestFixture
1665 : : {
1666 : : public:
1667 : : // initialise your test code values here.
1668 : 0 : void setUp()
1669 : : {
1670 : 0 : }
1671 : :
1672 : 0 : void tearDown()
1673 : : {
1674 : 0 : }
1675 : :
1676 : : /** call wait in the run method
1677 : :
1678 : : ALGORITHM:
1679 : : tested thread wait nWaitSec seconds, main thread sleep (2) seconds,
1680 : : then terminate the tested thread, due to the fact that the thread do a sleep(1) + wait(5)
1681 : : it's finish after 6 seconds.
1682 : : */
1683 : 0 : void wait_001()
1684 : : {
1685 [ # # ]: 0 : OCountThread *aCountThread = new OCountThread();
1686 : 0 : sal_Int32 nWaitSec = 5;
1687 : 0 : aCountThread->setWait(nWaitSec);
1688 : : // thread runs at least 5 seconds.
1689 [ # # ]: 0 : sal_Bool bRes = aCountThread->create();
1690 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1691 : :
1692 : : //record the time when the running begin
1693 : 0 : StopWatch aStopWatch;
1694 [ # # ]: 0 : aStopWatch.start();
1695 : :
1696 : : // wait a little bit, to let the thread the time, to start
1697 [ # # ]: 0 : ThreadHelper::thread_sleep_tenth_sec( 4 );
1698 : :
1699 : : // if wait works,
1700 : : // this function returns, after 4 sec. later
1701 [ # # ]: 0 : termAndJoinThread(aCountThread);
1702 : :
1703 : : // value should be one.
1704 [ # # ]: 0 : sal_Int32 nValue = aCountThread->getValue();
1705 : :
1706 [ # # ]: 0 : aStopWatch.stop();
1707 : :
1708 : : // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
1709 : 0 : double nTenthSec = aStopWatch.getTenthSec();
1710 : 0 : double nSec = aStopWatch.getSeconds();
1711 [ # # ][ # # ]: 0 : delete aCountThread;
1712 [ # # ]: 0 : t_print("nTenthSec = %f \n", nTenthSec);
1713 [ # # ]: 0 : t_print("nSec = %f \n", nSec);
1714 [ # # ]: 0 : t_print("nValue = %d \n", (int) nValue);
1715 : :
1716 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1717 : : "Wait: Blocks the calling thread for the given number of time.",
1718 : : nTenthSec >= 5 && nValue == 1
1719 [ # # ]: 0 : );
1720 : :
1721 : 0 : }
1722 : :
1723 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(wait);
[ # # ][ # # ]
[ # # ]
1724 [ # # ][ # # ]: 0 : CPPUNIT_TEST(wait_001);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1725 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1726 : : }; // class wait
1727 : :
1728 : : /** osl::Thread::yield method: can not design good test scenario to test up to now
1729 : : */
1730 [ # # ]: 0 : class yield : public CppUnit::TestFixture
1731 : : {
1732 : : public:
1733 : 0 : void setUp()
1734 : : {
1735 : 0 : }
1736 : :
1737 : 0 : void tearDown()
1738 : : {
1739 : 0 : }
1740 : :
1741 : : // insert your test code here.
1742 : 0 : void yield_001()
1743 : : {
1744 : 0 : }
1745 : :
1746 : :
1747 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(yield);
[ # # ][ # # ]
[ # # ]
1748 [ # # ][ # # ]: 0 : CPPUNIT_TEST(yield_001);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1749 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1750 : : }; // class yield
1751 : :
1752 : : /** Test of the osl::Thread::schedule method
1753 : : */
1754 [ # # ]: 0 : class schedule : public CppUnit::TestFixture
1755 : : {
1756 : : public:
1757 : : // initialise your test code values here.
1758 : 0 : void setUp()
1759 : : {
1760 : 0 : }
1761 : :
1762 : 0 : void tearDown()
1763 : : {
1764 : 0 : }
1765 : :
1766 : : /** The requested thread will get terminate the next time schedule() is called.
1767 : :
1768 : : Note: on UNX, if call suspend thread is not the to be suspended thread, the to be
1769 : : suspended thread will get suspended the next time schedule() is called,
1770 : : while on w32, it's nothing with schedule.
1771 : :
1772 : : check if suspend and terminate work well via schedule
1773 : : */
1774 : 0 : void schedule_001()
1775 : : {
1776 [ # # ]: 0 : OAddThread* aThread = new OAddThread();
1777 : 0 : sal_Bool bRes = aThread->create();
1778 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1779 : :
1780 : 0 : ThreadHelper::thread_sleep_tenth_sec(2);
1781 : 0 : aThread->suspend();
1782 : 0 : ThreadHelper::thread_sleep_tenth_sec(1);
1783 : 0 : sal_Int32 nValue = aThread->getValue();
1784 : 0 : ThreadHelper::thread_sleep_tenth_sec(3);
1785 : 0 : sal_Int32 nLaterValue = aThread->getValue();
1786 : : // resumeAndWaitThread(aThread);
1787 : 0 : t_print(" value = %d\n", (int) nValue);
1788 : 0 : t_print("later value = %d\n", (int) nLaterValue);
1789 : : // if value and latervalue not equal, than the thread would not suspended
1790 : :
1791 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1792 : : "Schedule: suspend works.",
1793 : : nLaterValue == nValue
1794 [ # # ]: 0 : );
1795 : :
1796 : 0 : aThread->resume();
1797 : 0 : ThreadHelper::thread_sleep_tenth_sec(2);
1798 : :
1799 : 0 : aThread->terminate();
1800 : 0 : sal_Int32 nValue_term = aThread->getValue();
1801 : :
1802 : 0 : aThread->join();
1803 : 0 : sal_Int32 nValue_join = aThread->getValue();
1804 : :
1805 : 0 : t_print("value after term = %d\n", (int) nValue_term);
1806 : 0 : t_print("value after join = %d\n", (int) nValue_join);
1807 : :
1808 : : // nValue_term and nValue_join should be the same
1809 : : // but should be differ from nValue
1810 : :
1811 [ # # ]: 0 : delete aThread;
1812 : : //check if thread really terminate after call terminate, if join immediatlly return
1813 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1814 : : "Schedule: Returns False if the thread should terminate.",
1815 : : nValue_join - nValue_term <= 1 && nValue_join - nValue_term >= 0
1816 [ # # ]: 0 : );
1817 : :
1818 : 0 : }
1819 : :
1820 : : /** design a thread that has not call schedule in the workfunction--run method
1821 : : */
1822 : 0 : void schedule_002()
1823 : : {
1824 [ # # ]: 0 : ONoScheduleThread aThread; // this thread runs 10 sec. (no schedule() used)
1825 [ # # ]: 0 : sal_Bool bRes = aThread.create();
1826 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1827 : :
1828 [ # # ]: 0 : ThreadHelper::thread_sleep_tenth_sec(2);
1829 [ # # ]: 0 : aThread.suspend();
1830 [ # # ]: 0 : sal_Int32 nValue = aThread.getValue();
1831 : :
1832 [ # # ]: 0 : ThreadHelper::thread_sleep_tenth_sec(3);
1833 [ # # ]: 0 : sal_Int32 nLaterValue = aThread.getValue();
1834 [ # # ]: 0 : ThreadHelper::thread_sleep_tenth_sec(5);
1835 : :
1836 [ # # ]: 0 : resumeAndWaitThread(&aThread);
1837 : :
1838 [ # # ]: 0 : t_print(" value = %d\n", (int) nValue);
1839 [ # # ]: 0 : t_print("later value = %d\n", (int) nLaterValue);
1840 : :
1841 : : //On windows, suspend works, so the values are same
1842 : : #ifdef WNT
1843 : : CPPUNIT_ASSERT_MESSAGE(
1844 : : "Schedule: don't schedule in thread run method, suspend works.",
1845 : : nLaterValue == nValue
1846 : : );
1847 : : #endif
1848 : :
1849 : : //On UNX, suspend does not work, so the difference of the values equals to sleep seconds number
1850 : : #ifdef UNX
1851 [ # # ]: 0 : aThread.resume();
1852 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1853 : : "Schedule: don't schedule in thread run method, suspend does not work too.",
1854 : : nLaterValue > nValue
1855 [ # # ]: 0 : );
1856 : : #endif
1857 : :
1858 : : // terminate will not work if no schedule in thread's work function
1859 [ # # ]: 0 : termAndJoinThread(&aThread);
1860 [ # # ]: 0 : sal_Int32 nValue_term = aThread.getValue();
1861 : :
1862 [ # # ]: 0 : t_print(" value term = %d\n", (int) nValue_term);
1863 : :
1864 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1865 : : "Schedule: don't schedule in thread run method, terminate failed.",
1866 : : nValue_term == 10
1867 [ # # # # ]: 0 : );
1868 : 0 : }
1869 : :
1870 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE(schedule);
[ # # ][ # # ]
[ # # ]
1871 [ # # ][ # # ]: 0 : CPPUNIT_TEST(schedule_001);
[ # # ][ # # ]
[ # # ][ # # ]
1872 [ # # ][ # # ]: 0 : CPPUNIT_TEST(schedule_002);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1873 [ # # ][ # # ]: 0 : CPPUNIT_TEST_SUITE_END();
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1874 : : }; // class schedule
1875 : :
1876 : : // -----------------------------------------------------------------------------
1877 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::create, "osl_Thread");
1878 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::createSuspended, "osl_Thread");
1879 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::suspend, "osl_Thread");
1880 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::resume, "osl_Thread");
1881 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::terminate, "osl_Thread");
1882 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::join, "osl_Thread");
1883 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::isRunning, "osl_Thread");
1884 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::setPriority, "osl_Thread");
1885 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getPriority, "osl_Thread");
1886 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getIdentifier, "osl_Thread");
1887 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getCurrentIdentifier, "osl_Thread");
1888 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::wait, "osl_Thread");
1889 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::yield, "osl_Thread");
1890 [ + - ][ + - ]: 5 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::schedule, "osl_Thread");
1891 : : } // namespace osl_Thread
1892 : :
1893 : :
1894 : : // -----------------------------------------------------------------------------
1895 : : // destroy function when the binding thread terminate
1896 : 60 : void SAL_CALL destroyCallback(void * data)
1897 : : {
1898 [ + - ]: 60 : delete[] (char *) data;
1899 : 59 : }
1900 : :
1901 : 5 : static ThreadData myThreadData(destroyCallback);
1902 : :
1903 : : /**
1904 : : */
1905 : : class myKeyThread : public Thread
1906 : : {
1907 : : public:
1908 : : // a public char member for test result checking
1909 : : char m_Char_Test;
1910 : : // for pass thread-special data to thread
1911 : 40 : myKeyThread(const char cData)
1912 : 40 : {
1913 : 40 : m_nData = cData;
1914 : 40 : }
1915 : : private:
1916 : : char m_nData;
1917 : :
1918 : 40 : void SAL_CALL run()
1919 : : {
1920 : 40 : char * pc = new char[2];
1921 : : // strcpy(pc, &m_nData);
1922 : 40 : memcpy(pc, &m_nData, 1);
1923 : 40 : pc[1] = '\0';
1924 : :
1925 : 40 : myThreadData.setData(pc);
1926 : 40 : char* pData = (char*)myThreadData.getData();
1927 : 40 : m_Char_Test = *pData;
1928 : : // wait for long time to check the data value in main thread
1929 : 40 : ThreadHelper::thread_sleep_tenth_sec(3);
1930 : 39 : }
1931 : : public:
1932 : 40 : ~myKeyThread()
1933 : 40 : {
1934 [ + - ][ - + ]: 40 : if (isRunning())
1935 : : {
1936 [ # # ]: 0 : t_print("error: not terminated.\n");
1937 : : }
1938 [ - + ]: 40 : }
1939 : : };
1940 : :
1941 : 5 : static ThreadData idData;
1942 : :
1943 : 10 : class idThread: public Thread
1944 : : {
1945 : : public:
1946 : : oslThreadIdentifier m_Id;
1947 : : private:
1948 : 10 : void SAL_CALL run()
1949 : : {
1950 : 10 : oslThreadIdentifier* pId = new oslThreadIdentifier;
1951 : 10 : *pId = getIdentifier();
1952 : 10 : idData.setData(pId);
1953 : 10 : oslThreadIdentifier* pIdData = (oslThreadIdentifier*)idData.getData();
1954 : : //t_print("Thread %d has Data %d\n", getIdentifier(), *pIdData);
1955 : 10 : m_Id = *pIdData;
1956 : 10 : delete pId;
1957 : 10 : }
1958 : :
1959 : : public:
1960 : 10 : ~idThread()
1961 : 10 : {
1962 [ + - ][ - + ]: 10 : if (isRunning())
1963 : : {
1964 [ # # ]: 0 : t_print("error: not terminated.\n");
1965 : : }
1966 [ - + ]: 10 : }
1967 : : };
1968 : :
1969 : : namespace osl_ThreadData
1970 : : {
1971 : :
1972 [ - + ]: 15 : class ctors : public CppUnit::TestFixture
1973 : : {
1974 : : public:
1975 : : // initialise your test code values here.
1976 : 5 : void setUp()
1977 : : {
1978 : 5 : }
1979 : :
1980 : 5 : void tearDown()
1981 : : {
1982 : 5 : }
1983 : :
1984 : : // insert your test code here.
1985 : 5 : void ctor_001()
1986 : : {
1987 : :
1988 : 5 : }
1989 : :
1990 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE(ctors);
[ + - ][ + - ]
[ # # ]
1991 [ + - ][ + - ]: 5 : CPPUNIT_TEST(ctor_001);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
1992 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END();
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
1993 : : }; // class ctors
1994 : :
1995 : :
1996 [ - + ]: 45 : class setData : public CppUnit::TestFixture
1997 : : {
1998 : : public:
1999 : : // initialise your test code values here.
2000 : 15 : void setUp()
2001 : : {
2002 : 15 : }
2003 : :
2004 : 15 : void tearDown()
2005 : : {
2006 : 15 : }
2007 : :
2008 : : /** the same instance of the class can have different values in different threads
2009 : : */
2010 : 5 : void setData_001()
2011 : : {
2012 [ + - ]: 5 : idThread aThread1;
2013 [ + - ]: 5 : aThread1.create();
2014 [ + - ]: 5 : idThread aThread2;
2015 [ + - ]: 5 : aThread2.create();
2016 : :
2017 [ + - ]: 5 : aThread1.join();
2018 [ + - ]: 5 : aThread2.join();
2019 : :
2020 [ + - ]: 5 : oslThreadIdentifier aThreadId1 = aThread1.getIdentifier();
2021 [ + - ]: 5 : oslThreadIdentifier aThreadId2 = aThread2.getIdentifier();
2022 : :
2023 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE(
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
[ + - ][ + - ]
2024 : : "ThreadData setData: ",
2025 : : aThread1.m_Id == aThreadId1 && aThread2.m_Id == aThreadId2
2026 [ + - + - ]: 10 : );
[ + - ]
2027 : :
2028 : 5 : }
2029 : :
2030 : 5 : void setData_002()
2031 : : {
2032 : : // at first, set the data a value
2033 [ + - ]: 5 : char* pc = new char[2];
2034 : 5 : char m_nData = 'm';
2035 : : // LLA: this is a copy functions only and really only for \0 terminated strings
2036 : : // m_nData is not a string, it's a character
2037 : : // strcpy(pc, &m_nData);
2038 : 5 : memcpy(pc, &m_nData, 1);
2039 : 5 : pc[1] = '\0';
2040 : :
2041 [ + - ]: 5 : myThreadData.setData(pc);
2042 : :
2043 [ + - ]: 5 : myKeyThread aThread1('a');
2044 [ + - ]: 5 : aThread1.create();
2045 [ + - ]: 5 : myKeyThread aThread2('b');
2046 [ + - ]: 5 : aThread2.create();
2047 : : // aThread1 and aThread2 should have not terminated yet, check current data, not 'a' 'b'
2048 [ + - ]: 5 : char* pChar = (char*)myThreadData.getData();
2049 : 5 : char aChar = *pChar;
2050 : :
2051 [ + - ]: 5 : aThread1.join();
2052 [ + - ]: 5 : aThread2.join();
2053 : :
2054 : : // the saved thread data of aThread1 & aThread2, different
2055 : 5 : char cData1 = aThread1.m_Char_Test;
2056 : 5 : char cData2 = aThread2.m_Char_Test;
2057 : :
2058 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE(
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ]
2059 : : "ThreadData setData: ",
2060 : : cData1 == 'a' && cData2 == 'b' && aChar == 'm'
2061 [ + - + - ]: 10 : );
[ + - ]
2062 : :
2063 : 5 : }
2064 : : /** setData the second time, and then getData
2065 : : */
2066 : 5 : void setData_003()
2067 : : {
2068 : : // at first, set the data a value
2069 [ + - ]: 5 : char* pc = new char[2];
2070 : 5 : char m_nData = 'm';
2071 : 5 : memcpy(pc, &m_nData, 1);
2072 : 5 : pc[1] = '\0';
2073 [ + - ]: 5 : myThreadData.setData(pc);
2074 : :
2075 [ + - ]: 5 : myKeyThread aThread1('a');
2076 [ + - ]: 5 : aThread1.create();
2077 [ + - ]: 5 : myKeyThread aThread2('b');
2078 [ + - ]: 5 : aThread2.create();
2079 : : // aThread1 and aThread2 should have not terminated yet
2080 : : // setData the second time
2081 [ + - ]: 5 : char* pc2 = new char[2];
2082 : 5 : m_nData = 'o';
2083 : 5 : memcpy(pc2, &m_nData, 1);
2084 : 5 : pc2[1] = '\0';
2085 : :
2086 [ + - ]: 5 : myThreadData.setData(pc2);
2087 [ + - ]: 5 : char* pChar = (char*)myThreadData.getData();
2088 : 5 : char aChar = *pChar;
2089 : :
2090 [ + - ]: 5 : aThread1.join();
2091 [ + - ]: 5 : aThread2.join();
2092 : :
2093 : : // the saved thread data of aThread1 & aThread2, different
2094 : 5 : char cData1 = aThread1.m_Char_Test;
2095 : 5 : char cData2 = aThread2.m_Char_Test;
2096 : :
2097 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE(
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ]
2098 : : "ThreadData setData: ",
2099 : : cData1 == 'a' && cData2 == 'b' && aChar == 'o'
2100 [ + - + - ]: 10 : );
[ + - ]
2101 : 5 : }
2102 : :
2103 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE(setData);
[ + - ][ + - ]
[ # # ]
2104 [ + - ][ + - ]: 5 : CPPUNIT_TEST(setData_001);
[ + - ][ + - ]
[ + - ][ + - ]
2105 [ + - ][ + - ]: 5 : CPPUNIT_TEST(setData_002);
[ + - ][ + - ]
[ + - ][ + - ]
2106 [ + - ][ + - ]: 5 : CPPUNIT_TEST(setData_003);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
2107 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END();
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
2108 : : }; // class setData
2109 : :
2110 [ - + ]: 30 : class getData : public CppUnit::TestFixture
2111 : : {
2112 : : public:
2113 : : // initialise your test code values here.
2114 : 10 : void setUp()
2115 : : {
2116 : 10 : }
2117 : :
2118 : 10 : void tearDown()
2119 : : {
2120 : 10 : }
2121 : :
2122 : : // After setData in child threads, get Data in the main thread, should be independent
2123 : 5 : void getData_001()
2124 : : {
2125 [ + - ]: 5 : char* pc = new char[2];
2126 : 5 : char m_nData[] = "i";
2127 : 5 : strcpy(pc, m_nData);
2128 [ + - ]: 5 : myThreadData.setData(pc);
2129 : :
2130 [ + - ]: 5 : myKeyThread aThread1('c');
2131 [ + - ]: 5 : aThread1.create();
2132 [ + - ]: 5 : myKeyThread aThread2('d');
2133 [ + - ]: 5 : aThread2.create();
2134 : :
2135 [ + - ]: 5 : aThread1.join();
2136 [ + - ]: 5 : aThread2.join();
2137 : :
2138 : 5 : char cData1 = aThread1.m_Char_Test;
2139 : 5 : char cData2 = aThread2.m_Char_Test;
2140 : :
2141 [ + - ]: 5 : char* pChar = (char*)myThreadData.getData();
2142 : 5 : char aChar = *pChar;
2143 : :
2144 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE(
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ]
2145 : : "ThreadData setData: ",
2146 : : cData1 == 'c' && cData2 == 'd' && aChar == 'i'
2147 [ + - + - ]: 10 : );
[ + - ]
2148 : 5 : }
2149 : :
2150 : : // setData then change the value in the address data pointer points,
2151 : : // and then getData, should get the new value
2152 : 5 : void getData_002()
2153 : : {
2154 [ + - ]: 5 : char* pc = new char[2];
2155 : 5 : char m_nData = 'i';
2156 : 5 : memcpy(pc, &m_nData, 1);
2157 : 5 : pc[1] = '\0';
2158 : :
2159 [ + - ]: 5 : myThreadData.setData(pc);
2160 : :
2161 [ + - ]: 5 : myKeyThread aThread1('a');
2162 [ + - ]: 5 : aThread1.create();
2163 [ + - ]: 5 : myKeyThread aThread2('b');
2164 [ + - ]: 5 : aThread2.create();
2165 : :
2166 : : // change the value which pc points
2167 : 5 : char m_nData2 = 'j';
2168 : 5 : memcpy(pc, &m_nData2, 1);
2169 : 5 : pc[1] = '\0';
2170 : :
2171 [ + - ]: 5 : void* pChar = myThreadData.getData();
2172 : 5 : char aChar = *(char*)pChar;
2173 : :
2174 [ + - ]: 5 : aThread1.join();
2175 [ + - ]: 5 : aThread2.join();
2176 : :
2177 : 5 : char cData1 = aThread1.m_Char_Test;
2178 : 5 : char cData2 = aThread2.m_Char_Test;
2179 : :
2180 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE(
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ]
2181 : : "ThreadData setData: ",
2182 : : cData1 == 'a' && cData2 == 'b' && aChar == 'j'
2183 [ + - + - ]: 10 : );
[ + - ]
2184 : :
2185 : 5 : }
2186 : :
2187 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE(getData);
[ + - ][ + - ]
[ # # ]
2188 [ + - ][ + - ]: 5 : CPPUNIT_TEST(getData_001);
[ + - ][ + - ]
[ + - ][ + - ]
2189 [ + - ][ + - ]: 5 : CPPUNIT_TEST(getData_002);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
2190 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END();
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
2191 : : }; // class getData
2192 : :
2193 : : // -----------------------------------------------------------------------------
2194 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::ctors);
2195 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::setData);
2196 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::getData);
2197 : : } // namespace osl_ThreadData
2198 : :
2199 : : // this macro creates an empty function, which will called by the RegisterAllFunctions()
2200 : : // to let the user the possibility to also register some functions by hand.
2201 [ + - ][ + - ]: 20 : CPPUNIT_PLUGIN_IMPLEMENT();
[ + - ][ + - ]
[ + - ][ # # ]
2202 : :
2203 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|