summaryrefslogtreecommitdiffstats
path: root/sal/qa/osl/condition/osl_Condition.cxx
blob: 05b7fc661f6a99bec4a66b7cacfd52025bf6a68c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "osl_Condition_Const.h"
#include <stdlib.h>

using namespace osl;

namespace {

enum ConditionType
{
    thread_type_set,
    thread_type_reset,
    thread_type_wait
};

/** thread for testing Condition.
 */
class ConditionThread : public Thread
{
public:
    //get the Condition to operate
    ConditionThread( ::osl::Condition& Con, ConditionType tType): m_MyCon( Con ), m_MyType( tType ) { }

protected:
    ::osl::Condition& m_MyCon;
    ConditionType m_MyType;

    void SAL_CALL run() override
    {
        switch ( m_MyType )
        {
            case thread_type_wait:
                m_MyCon.wait(); break;
            case thread_type_set:
                m_MyCon.set(); break;
            case thread_type_reset:
                m_MyCon.reset(); break;
            default:
                break;
        }
    }
};

}

namespace osl_Condition
{
    /** testing the method:
        Condition()
    */
    class ctors : public CppUnit::TestFixture
    {
    public:
        bool bRes, bRes1;

        void ctors_create()
        {
            ::osl::Condition aCond;
            bRes = aCond.check();

            CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition its initial check state should be sal_False.",
                                   !bRes );
        }

        void ctors_createAndSet()
        {
            ::osl::Condition aCond;
            aCond.set();
            bRes = aCond.check();

            CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and set it.",
                                   bRes );
        }

        CPPUNIT_TEST_SUITE(ctors);
        CPPUNIT_TEST(ctors_create);
        CPPUNIT_TEST(ctors_createAndSet);
        CPPUNIT_TEST_SUITE_END();
    };

    /** testing the method:
        void set()
    */
    class set : public CppUnit::TestFixture
    {
    public:
        bool bRes, bRes1, bRes2;

        void set_createAndSet()
        {
            ::osl::Condition aCond;
            aCond.set();
            bRes = aCond.check();

            CPPUNIT_ASSERT_MESSAGE("#test comment#: check state should be sal_True after set.",
                                   bRes );
        }

        void set_threadWaitRelease()
        {
            ::osl::Condition aCond;
            ConditionThread myThread1(aCond, thread_type_wait);
            myThread1.create();
            bRes = myThread1.isRunning();

            ConditionThread myThread2(aCond, thread_type_set);
            myThread2.create();

            myThread1.join();
            bRes1 = myThread1.isRunning();
            bRes2 = aCond.check();
            myThread2.join();

            CPPUNIT_ASSERT_MESSAGE("#test comment#: use one thread to set the condition in order to release another thread.",
                                   bRes);
            CPPUNIT_ASSERT_MESSAGE("#test comment#: use one thread to set the condition in order to release another thread.",
                                   !bRes1);
            CPPUNIT_ASSERT_MESSAGE( "#test comment#: use one thread to set the condition in order to release another thread.",
                                   bRes2);
        }

        CPPUNIT_TEST_SUITE(set);
        CPPUNIT_TEST(set_createAndSet);
        CPPUNIT_TEST(set_threadWaitRelease);
        CPPUNIT_TEST_SUITE_END( );
    };

    /** testing the method:
        void reset()
    */
    class reset : public CppUnit::TestFixture
    {
    public:
        bool bRes, bRes1, bRes2;

        void reset_resetWaitAndSet()
        {
            ::osl::Condition aCond;
            aCond.reset();

            ConditionThread myThread(aCond, thread_type_wait);
            myThread.create();
            bRes = myThread.isRunning();
            bRes2 = aCond.check();

            aCond.set();
            myThread.join();
            bRes1 = myThread.isRunning();

            CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
                                   bRes);
            CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
                                   !bRes1);
            CPPUNIT_ASSERT_MESSAGE("#test comment#: wait will cause a reset thread block, use set to release it.",
                                   !bRes2);
        }

        void reset_resetAndSet()
        {
            ::osl::Condition aCond;
            aCond.reset();
            bRes = aCond.check();
            aCond.set();
            bRes1 = aCond.check();

            CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and reset/set it.",
                                   !bRes );
            CPPUNIT_ASSERT_MESSAGE("#test comment#: create a condition and reset/set it.",
                                   bRes1 );
        }

        CPPUNIT_TEST_SUITE(reset);
        CPPUNIT_TEST(reset_resetWaitAndSet);
        CPPUNIT_TEST(reset_resetAndSet);
        CPPUNIT_TEST_SUITE_END();
    };

    /** testing the method:
        Result wait(const TimeValue *pTimeout = 0)
    */
    class wait : public CppUnit::TestFixture
    {
    public:
        bool bRes, bRes1, bRes2;
        std::unique_ptr<TimeValue> tv1;

        void setUp() override
        {
            tv1.reset(new TimeValue);
            tv1->Seconds = 1;
            tv1->Nanosec = 0;
        }

        void tearDown() override
        {
            tv1.reset();
        }

        void wait_testAllCombos( )
        {
            ::osl::Condition cond1;
            ::osl::Condition cond2;
            ::osl::Condition cond3;

            cond1.set();
            cond2.set();

            osl::Condition::Result r1=cond1.wait(tv1.get());
            osl::Condition::Result r2=cond2.wait();
            osl::Condition::Result r3=cond3.wait(tv1.get());

            CPPUNIT_ASSERT_EQUAL_MESSAGE( "#test comment#: test three types of wait.",
                                    ::osl::Condition::result_ok, r1 );
            CPPUNIT_ASSERT_EQUAL_MESSAGE( "#test comment#: test three types of wait.",
                                    ::osl::Condition::result_ok, r2 );
            CPPUNIT_ASSERT_EQUAL_MESSAGE( "#test comment#: test three types of wait.",
                                    ::osl::Condition::result_timeout, r3 );
        }

        void wait_timeoutWaits()
        {
            ::osl::Condition aCond;
            ::osl::Condition::Result wRes, wRes1;

            aCond.reset();
            bRes = aCond.check();
            wRes = aCond.wait(tv1.get());

            aCond.set();
            wRes1 = aCond.wait(tv1.get());
            bRes1 = aCond.check();

            CPPUNIT_ASSERT_MESSAGE("#test comment#: wait a condition after set/reset.",
                                   !bRes );
            CPPUNIT_ASSERT_MESSAGE("#test comment#: wait a condition after set/reset.",
                                   bRes1 );
            CPPUNIT_ASSERT_EQUAL_MESSAGE("#test comment#: wait a condition after set/reset.",
                                   ::osl::Condition::result_timeout, wRes );
            CPPUNIT_ASSERT_EQUAL_MESSAGE("#test comment#: wait a condition after set/reset.",
                                   ::osl::Condition::result_ok, wRes1 );
        }

        CPPUNIT_TEST_SUITE(wait);
        CPPUNIT_TEST(wait_testAllCombos);
        CPPUNIT_TEST(wait_timeoutWaits);
        CPPUNIT_TEST_SUITE_END();
    };

    /** testing the method:
        sal_Bool check()
    */
    class check : public CppUnit::TestFixture
    {
    public:
        bool bRes, bRes1, bRes2;

        void check_checkStates()
        {
            ::osl::Condition aCond;

            aCond.reset();
            bRes = aCond.check();
            aCond.set();
            bRes1 = aCond.check();

            CPPUNIT_ASSERT_MESSAGE("#test comment#: check the condition states.",
                                   !bRes );
            CPPUNIT_ASSERT_MESSAGE("#test comment#: check the condition states.",
                                   bRes1 );
        }

        void check_threadedCheckStates( )
        {
            ::osl::Condition aCond;
            aCond.reset();

            ConditionThread myThread(aCond, thread_type_set);
            myThread.create();
            myThread.join();
            bRes = aCond.check();

            ConditionThread myThread1(aCond, thread_type_reset);
            myThread1.create();
            myThread1.join();
            bRes1 = aCond.check();

            CPPUNIT_ASSERT_MESSAGE("#test comment#: use threads to set/reset Condition and check it in main routine.",
                                   bRes );
            CPPUNIT_ASSERT_MESSAGE("#test comment#: use threads to set/reset Condition and check it in main routine.",
                                   !bRes1 );
        }

        CPPUNIT_TEST_SUITE(check);
        CPPUNIT_TEST(check_checkStates);
        CPPUNIT_TEST(check_threadedCheckStates);
        CPPUNIT_TEST_SUITE_END();
    };

    CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::ctors);
    CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::set);
    CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::reset);
    CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::wait);
    CPPUNIT_TEST_SUITE_REGISTRATION(osl_Condition::check);

} // namespace osl_Condition

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */