xref: /aoo4110/main/salhelper/qa/test_api.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "sal/config.h"
25 
26 #include <typeinfo>
27 
28 namespace salhelper {
29     class Condition;
30     class ConditionModifier;
31     class ConditionWaiter;
32     class ORealDynamicLoader;
33     class SimpleReferenceObject;
34 }
35 
36 namespace {
37 
getConditionTypeInfo()38 std::type_info const & getConditionTypeInfo()
39 { return typeid (salhelper::Condition *); }
40 
getConditionModifierTypeInfo()41 std::type_info const & getConditionModifierTypeInfo()
42 { return typeid (salhelper::ConditionModifier *); }
43 
getConditionWaiterTypeInfo()44 std::type_info const & getConditionWaiterTypeInfo()
45 { return typeid (salhelper::ConditionWaiter *); }
46 
getORealDynamicLoaderTypeInfo()47 std::type_info const & getORealDynamicLoaderTypeInfo()
48 { return typeid (salhelper::ORealDynamicLoader *); }
49 
getSimpleReferenceObjectTypeInfo()50 std::type_info const & getSimpleReferenceObjectTypeInfo()
51 { return typeid (salhelper::SimpleReferenceObject *); }
52 
53 }
54 
55 #include "testshl/simpleheader.hxx"
56 #include "osl/mutex.hxx"
57 #include "salhelper/condition.hxx"
58 #include "salhelper/dynload.hxx"
59 #include "salhelper/simplereferenceobject.hxx"
60 
61 #include <memory>
62 
63 namespace {
64 
65 class DerivedCondition: public salhelper::Condition {
66 public:
DerivedCondition(osl::Mutex & mutex)67     explicit DerivedCondition(osl::Mutex & mutex): Condition(mutex) {}
68 
69 protected:
applies() const70     virtual bool applies() const { return false; }
71 };
72 
73 class DerivedConditionWaiterTimedout:
74     public salhelper::ConditionWaiter::timedout
75 {};
76 
77 class DerivedSimpleReferenceObject: public salhelper::SimpleReferenceObject {};
78 
79 class Test: public CppUnit::TestFixture {
80 public:
81     void testCondition();
82 
83     void testConditionModifier();
84 
85     void testConditionWaiter();
86 
87     void testConditionWaiterTimedout();
88 
89     void testORealDynamicLoader();
90 
91     void testSimpleReferenceObject();
92 
93     void testDerivedCondition();
94 
95     void testDerivedConditionWaiterTimedout();
96 
97     void testDerivedSimpleReferenceObject();
98 
99     CPPUNIT_TEST_SUITE(Test);
100     CPPUNIT_TEST(testCondition);
101     CPPUNIT_TEST(testConditionModifier);
102     CPPUNIT_TEST(testConditionWaiter);
103     CPPUNIT_TEST(testConditionWaiterTimedout);
104     CPPUNIT_TEST(testORealDynamicLoader);
105     CPPUNIT_TEST(testSimpleReferenceObject);
106     CPPUNIT_TEST(testDerivedCondition);
107     CPPUNIT_TEST(testDerivedConditionWaiterTimedout);
108     CPPUNIT_TEST(testDerivedSimpleReferenceObject);
109     CPPUNIT_TEST_SUITE_END();
110 };
111 
testCondition()112 void Test::testCondition() {
113     osl::Mutex mutex;
114     std::auto_ptr< salhelper::Condition > p(new DerivedCondition(mutex));
115     CPPUNIT_ASSERT(typeid (*p.get()) != typeid (salhelper::Condition));
116     CPPUNIT_ASSERT(typeid (p.get()) == typeid (salhelper::Condition *));
117     CPPUNIT_ASSERT(
118         typeid (const_cast< salhelper::Condition const * >(p.get()))
119         == typeid (salhelper::Condition const *));
120     CPPUNIT_ASSERT(
121         typeid (const_cast< salhelper::Condition volatile * >(p.get()))
122         == typeid (salhelper::Condition volatile *));
123     CPPUNIT_ASSERT(typeid (salhelper::Condition *) == getConditionTypeInfo());
124 }
125 
testConditionModifier()126 void Test::testConditionModifier() {
127     salhelper::ConditionModifier * p = 0;
128     CPPUNIT_ASSERT(typeid (*p) == typeid (salhelper::ConditionModifier));
129     CPPUNIT_ASSERT(typeid (p) == typeid (salhelper::ConditionModifier *));
130     CPPUNIT_ASSERT(
131         typeid (const_cast< salhelper::ConditionModifier const * >(p))
132         == typeid (salhelper::ConditionModifier const *));
133     CPPUNIT_ASSERT(
134         typeid (const_cast< salhelper::ConditionModifier volatile * >(p))
135         == typeid (salhelper::ConditionModifier volatile *));
136     CPPUNIT_ASSERT(
137         typeid (salhelper::ConditionModifier *)
138         == getConditionModifierTypeInfo());
139 }
140 
testConditionWaiter()141 void Test::testConditionWaiter() {
142     salhelper::ConditionWaiter * p = 0;
143     CPPUNIT_ASSERT(typeid (*p) == typeid (salhelper::ConditionWaiter));
144     CPPUNIT_ASSERT(typeid (p) == typeid (salhelper::ConditionWaiter *));
145     CPPUNIT_ASSERT(
146         typeid (const_cast< salhelper::ConditionWaiter const * >(p))
147         == typeid (salhelper::ConditionWaiter const *));
148     CPPUNIT_ASSERT(
149         typeid (const_cast< salhelper::ConditionWaiter volatile * >(p))
150         == typeid (salhelper::ConditionWaiter volatile *));
151     CPPUNIT_ASSERT(
152         typeid (salhelper::ConditionWaiter *) == getConditionWaiterTypeInfo());
153 }
154 
testConditionWaiterTimedout()155 void Test::testConditionWaiterTimedout() {
156     salhelper::ConditionWaiter::timedout x;
157     CPPUNIT_ASSERT(typeid (x) == typeid (salhelper::ConditionWaiter::timedout));
158     CPPUNIT_ASSERT(
159         typeid (&x) == typeid (salhelper::ConditionWaiter::timedout *));
160     CPPUNIT_ASSERT(
161         typeid (const_cast< salhelper::ConditionWaiter::timedout const * >(&x))
162         == typeid (salhelper::ConditionWaiter::timedout const *));
163     CPPUNIT_ASSERT(
164         (typeid
165          (const_cast< salhelper::ConditionWaiter::timedout volatile * >(&x)))
166         == typeid (salhelper::ConditionWaiter::timedout volatile *));
167     try {
168         throw salhelper::ConditionWaiter::timedout();
169     } catch (salhelper::ConditionWaiter::timedout &) {
170     } catch (...) {
171         CPPUNIT_FAIL("not caught");
172     }
173 }
174 
testORealDynamicLoader()175 void Test::testORealDynamicLoader() {
176     salhelper::ORealDynamicLoader * p = 0;
177     CPPUNIT_ASSERT(typeid (p) != typeid (salhelper::ORealDynamicLoader));
178     CPPUNIT_ASSERT(typeid (p) == typeid (salhelper::ORealDynamicLoader *));
179     CPPUNIT_ASSERT(
180         typeid (const_cast< salhelper::ORealDynamicLoader const * >(p))
181         == typeid (salhelper::ORealDynamicLoader const *));
182     CPPUNIT_ASSERT(
183         typeid (const_cast< salhelper::ORealDynamicLoader volatile * >(p))
184         == typeid (salhelper::ORealDynamicLoader volatile *));
185     CPPUNIT_ASSERT(
186         typeid (salhelper::ORealDynamicLoader *)
187         == getORealDynamicLoaderTypeInfo());
188 }
189 
testSimpleReferenceObject()190 void Test::testSimpleReferenceObject() {
191     salhelper::SimpleReferenceObject * p = new DerivedSimpleReferenceObject;
192     try {
193         CPPUNIT_ASSERT(
194             typeid (*p) != typeid (salhelper::SimpleReferenceObject));
195         CPPUNIT_ASSERT(
196             typeid (p) == typeid (salhelper::SimpleReferenceObject *));
197         CPPUNIT_ASSERT(
198             typeid (const_cast< salhelper::SimpleReferenceObject const * >(p))
199             == typeid (salhelper::SimpleReferenceObject const *));
200         CPPUNIT_ASSERT(
201             (typeid
202              (const_cast< salhelper::SimpleReferenceObject volatile * >(p)))
203             == typeid (salhelper::SimpleReferenceObject volatile *));
204         CPPUNIT_ASSERT(
205             typeid (salhelper::SimpleReferenceObject *)
206             == getSimpleReferenceObjectTypeInfo());
207     } catch (...) {
208         delete static_cast< DerivedSimpleReferenceObject * >(p);
209         throw;
210     }
211 }
212 
testDerivedCondition()213 void Test::testDerivedCondition() {
214     osl::Mutex mutex;
215     std::auto_ptr< salhelper::Condition > p(new DerivedCondition(mutex));
216     CPPUNIT_ASSERT(dynamic_cast< DerivedCondition * >(p.get()) != 0);
217 }
218 
testDerivedConditionWaiterTimedout()219 void Test::testDerivedConditionWaiterTimedout() {
220     std::auto_ptr< salhelper::ConditionWaiter::timedout > p(
221         new DerivedConditionWaiterTimedout);
222     CPPUNIT_ASSERT(
223         dynamic_cast< DerivedConditionWaiterTimedout * >(p.get()) != 0);
224     try {
225         throw DerivedConditionWaiterTimedout();
226     } catch (salhelper::ConditionWaiter::timedout &) {
227     } catch (...) {
228         CPPUNIT_FAIL("not caught");
229     }
230 }
231 
testDerivedSimpleReferenceObject()232 void Test::testDerivedSimpleReferenceObject() {
233     salhelper::SimpleReferenceObject * p = new DerivedSimpleReferenceObject;
234     try {
235         CPPUNIT_ASSERT(dynamic_cast< DerivedSimpleReferenceObject * >(p) != 0);
236     } catch (...) {
237         delete static_cast< DerivedSimpleReferenceObject * >(p);
238         throw;
239     }
240 }
241 
242 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test, "alltests");
243 
244 }
245 
246 NOADDITIONAL;
247