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