xref: /trunk/main/salhelper/qa/test_api.cxx (revision 9abd56f0)
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 "osl/mutex.hxx"
56 #include "salhelper/condition.hxx"
57 #include "salhelper/dynload.hxx"
58 #include "salhelper/simplereferenceobject.hxx"
59 #include "gtest/gtest.h"
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 ::testing::Test {
80 public:
81 };
82 
TEST_F(Test,testCondition)83 TEST_F(Test, testCondition) {
84     osl::Mutex mutex;
85     std::auto_ptr< salhelper::Condition > p(new DerivedCondition(mutex));
86     ASSERT_TRUE(typeid (*p.get()) != typeid (salhelper::Condition));
87     ASSERT_TRUE(typeid (p.get()) == typeid (salhelper::Condition *));
88     ASSERT_TRUE(
89         typeid (const_cast< salhelper::Condition const * >(p.get()))
90         == typeid (salhelper::Condition const *));
91     ASSERT_TRUE(
92         typeid (const_cast< salhelper::Condition volatile * >(p.get()))
93         == typeid (salhelper::Condition volatile *));
94     ASSERT_TRUE(typeid (salhelper::Condition *) == getConditionTypeInfo());
95 }
96 
TEST_F(Test,testConditionModifier)97 TEST_F(Test, testConditionModifier) {
98     salhelper::ConditionModifier * p = 0;
99     ASSERT_TRUE(typeid (*p) == typeid (salhelper::ConditionModifier));
100     ASSERT_TRUE(typeid (p) == typeid (salhelper::ConditionModifier *));
101     ASSERT_TRUE(
102         typeid (const_cast< salhelper::ConditionModifier const * >(p))
103         == typeid (salhelper::ConditionModifier const *));
104     ASSERT_TRUE(
105         typeid (const_cast< salhelper::ConditionModifier volatile * >(p))
106         == typeid (salhelper::ConditionModifier volatile *));
107     ASSERT_TRUE(
108         typeid (salhelper::ConditionModifier *)
109         == getConditionModifierTypeInfo());
110 }
111 
TEST_F(Test,testConditionWaiter)112 TEST_F(Test, testConditionWaiter) {
113     salhelper::ConditionWaiter * p = 0;
114     ASSERT_TRUE(typeid (*p) == typeid (salhelper::ConditionWaiter));
115     ASSERT_TRUE(typeid (p) == typeid (salhelper::ConditionWaiter *));
116     ASSERT_TRUE(
117         typeid (const_cast< salhelper::ConditionWaiter const * >(p))
118         == typeid (salhelper::ConditionWaiter const *));
119     ASSERT_TRUE(
120         typeid (const_cast< salhelper::ConditionWaiter volatile * >(p))
121         == typeid (salhelper::ConditionWaiter volatile *));
122     ASSERT_TRUE(
123         typeid (salhelper::ConditionWaiter *) == getConditionWaiterTypeInfo());
124 }
125 
TEST_F(Test,testConditionWaiterTimedout)126 TEST_F(Test, testConditionWaiterTimedout) {
127     salhelper::ConditionWaiter::timedout x;
128     ASSERT_TRUE(typeid (x) == typeid (salhelper::ConditionWaiter::timedout));
129     ASSERT_TRUE(
130         typeid (&x) == typeid (salhelper::ConditionWaiter::timedout *));
131     ASSERT_TRUE(
132         typeid (const_cast< salhelper::ConditionWaiter::timedout const * >(&x))
133         == typeid (salhelper::ConditionWaiter::timedout const *));
134     ASSERT_TRUE(
135         (typeid
136          (const_cast< salhelper::ConditionWaiter::timedout volatile * >(&x)))
137         == typeid (salhelper::ConditionWaiter::timedout volatile *));
138     try {
139         throw salhelper::ConditionWaiter::timedout();
140     } catch (salhelper::ConditionWaiter::timedout &) {
141     } catch (...) {
142         FAIL() << "not caught";
143     }
144 }
145 
TEST_F(Test,testORealDynamicLoader)146 TEST_F(Test, testORealDynamicLoader) {
147     salhelper::ORealDynamicLoader * p = 0;
148     ASSERT_TRUE(typeid (p) != typeid (salhelper::ORealDynamicLoader));
149     ASSERT_TRUE(typeid (p) == typeid (salhelper::ORealDynamicLoader *));
150     ASSERT_TRUE(
151         typeid (const_cast< salhelper::ORealDynamicLoader const * >(p))
152         == typeid (salhelper::ORealDynamicLoader const *));
153     ASSERT_TRUE(
154         typeid (const_cast< salhelper::ORealDynamicLoader volatile * >(p))
155         == typeid (salhelper::ORealDynamicLoader volatile *));
156     ASSERT_TRUE(
157         typeid (salhelper::ORealDynamicLoader *)
158         == getORealDynamicLoaderTypeInfo());
159 }
160 
TEST_F(Test,testSimpleReferenceObject)161 TEST_F(Test, testSimpleReferenceObject) {
162     salhelper::SimpleReferenceObject * p = new DerivedSimpleReferenceObject;
163     try {
164         ASSERT_TRUE(
165             typeid (*p) != typeid (salhelper::SimpleReferenceObject));
166         ASSERT_TRUE(
167             typeid (p) == typeid (salhelper::SimpleReferenceObject *));
168         ASSERT_TRUE(
169             typeid (const_cast< salhelper::SimpleReferenceObject const * >(p))
170             == typeid (salhelper::SimpleReferenceObject const *));
171         ASSERT_TRUE(
172             (typeid
173              (const_cast< salhelper::SimpleReferenceObject volatile * >(p)))
174             == typeid (salhelper::SimpleReferenceObject volatile *));
175         ASSERT_TRUE(
176             typeid (salhelper::SimpleReferenceObject *)
177             == getSimpleReferenceObjectTypeInfo());
178     } catch (...) {
179         delete static_cast< DerivedSimpleReferenceObject * >(p);
180         throw;
181     }
182 }
183 
TEST_F(Test,testDerivedCondition)184 TEST_F(Test, testDerivedCondition) {
185     osl::Mutex mutex;
186     std::auto_ptr< salhelper::Condition > p(new DerivedCondition(mutex));
187     ASSERT_TRUE(dynamic_cast< DerivedCondition * >(p.get()) != 0);
188 }
189 
TEST_F(Test,testDerivedConditionWaiterTimedout)190 TEST_F(Test, testDerivedConditionWaiterTimedout) {
191     std::auto_ptr< salhelper::ConditionWaiter::timedout > p(
192         new DerivedConditionWaiterTimedout);
193     ASSERT_TRUE(
194         dynamic_cast< DerivedConditionWaiterTimedout * >(p.get()) != 0);
195     try {
196         throw DerivedConditionWaiterTimedout();
197     } catch (salhelper::ConditionWaiter::timedout &) {
198     } catch (...) {
199         FAIL() << "not caught";
200     }
201 }
202 
TEST_F(Test,testDerivedSimpleReferenceObject)203 TEST_F(Test, testDerivedSimpleReferenceObject) {
204     salhelper::SimpleReferenceObject * p = new DerivedSimpleReferenceObject;
205     try {
206         ASSERT_TRUE(dynamic_cast< DerivedSimpleReferenceObject * >(p) != 0);
207     } catch (...) {
208         delete static_cast< DerivedSimpleReferenceObject * >(p);
209         throw;
210     }
211 }
212 
213 
214 }
215