Branch libreoffice-5-0-4
[LibreOffice.git] / salhelper / qa / test_api.cxx
blobf016a88d55dbcadfb9ea1254e0251e9faf00d672
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sal/config.h"
22 #include <typeinfo>
24 namespace salhelper {
25 class Condition;
26 class ConditionModifier;
27 class ConditionWaiter;
28 class ORealDynamicLoader;
29 class SimpleReferenceObject;
32 namespace {
34 std::type_info const & getConditionTypeInfo()
35 { return typeid (salhelper::Condition *); }
37 std::type_info const & getConditionModifierTypeInfo()
38 { return typeid (salhelper::ConditionModifier *); }
40 std::type_info const & getConditionWaiterTypeInfo()
41 { return typeid (salhelper::ConditionWaiter *); }
43 std::type_info const & getORealDynamicLoaderTypeInfo()
44 { return typeid (salhelper::ORealDynamicLoader *); }
46 std::type_info const & getSimpleReferenceObjectTypeInfo()
47 { return typeid (salhelper::SimpleReferenceObject *); }
51 #include "osl/mutex.hxx"
52 #include "salhelper/condition.hxx"
53 #include "salhelper/dynload.hxx"
54 #include "salhelper/simplereferenceobject.hxx"
55 #include <cppunit/TestFixture.h>
56 #include <cppunit/extensions/HelperMacros.h>
57 #include <cppunit/plugin/TestPlugIn.h>
58 #include <boost/scoped_ptr.hpp>
60 namespace {
62 class DerivedCondition: public salhelper::Condition {
63 public:
64 explicit DerivedCondition(osl::Mutex & mutex): Condition(mutex) {}
66 protected:
67 virtual bool applies() const SAL_OVERRIDE { return false; }
70 class DerivedConditionWaiterTimedout:
71 public salhelper::ConditionWaiter::timedout
72 {};
74 class DerivedSimpleReferenceObject: public salhelper::SimpleReferenceObject {};
76 class Test: public CppUnit::TestFixture {
77 public:
78 void testCondition();
80 void testConditionModifier();
82 void testConditionWaiter();
84 void testConditionWaiterTimedout();
86 void testORealDynamicLoader();
88 void testSimpleReferenceObject();
90 void testDerivedCondition();
92 void testDerivedConditionWaiterTimedout();
94 void testDerivedSimpleReferenceObject();
96 CPPUNIT_TEST_SUITE(Test);
97 CPPUNIT_TEST(testCondition);
98 CPPUNIT_TEST(testConditionModifier);
99 CPPUNIT_TEST(testConditionWaiter);
100 CPPUNIT_TEST(testConditionWaiterTimedout);
101 CPPUNIT_TEST(testORealDynamicLoader);
102 CPPUNIT_TEST(testSimpleReferenceObject);
103 CPPUNIT_TEST(testDerivedCondition);
104 CPPUNIT_TEST(testDerivedConditionWaiterTimedout);
105 CPPUNIT_TEST(testDerivedSimpleReferenceObject);
106 CPPUNIT_TEST_SUITE_END();
109 void Test::testCondition() {
110 osl::Mutex mutex;
111 boost::scoped_ptr< salhelper::Condition > p(new DerivedCondition(mutex));
112 CPPUNIT_ASSERT(typeid (*p.get()) != typeid (salhelper::Condition));
113 CPPUNIT_ASSERT(typeid (p.get()) == typeid (salhelper::Condition *));
114 CPPUNIT_ASSERT(
115 typeid (const_cast< salhelper::Condition const * >(p.get()))
116 == typeid (salhelper::Condition const *));
117 CPPUNIT_ASSERT(
118 typeid (const_cast< salhelper::Condition volatile * >(p.get()))
119 == typeid (salhelper::Condition volatile *));
120 CPPUNIT_ASSERT(typeid (salhelper::Condition *) == getConditionTypeInfo());
123 #ifdef _MSC_VER
124 // MSVC 2012 warns about the "p" being unused
125 #pragma warning (push, 1)
126 #pragma warning (disable: 4189)
127 #endif
130 void Test::testConditionModifier() {
131 salhelper::ConditionModifier * p = 0;
132 CPPUNIT_ASSERT(typeid (*p) == typeid (salhelper::ConditionModifier));
133 CPPUNIT_ASSERT(typeid (p) == typeid (salhelper::ConditionModifier *));
134 CPPUNIT_ASSERT(
135 typeid (const_cast< salhelper::ConditionModifier const * >(p))
136 == typeid (salhelper::ConditionModifier const *));
137 CPPUNIT_ASSERT(
138 typeid (const_cast< salhelper::ConditionModifier volatile * >(p))
139 == typeid (salhelper::ConditionModifier volatile *));
140 CPPUNIT_ASSERT(
141 typeid (salhelper::ConditionModifier *)
142 == getConditionModifierTypeInfo());
145 void Test::testConditionWaiter() {
146 salhelper::ConditionWaiter * p = 0;
147 CPPUNIT_ASSERT(typeid (*p) == typeid (salhelper::ConditionWaiter));
148 CPPUNIT_ASSERT(typeid (p) == typeid (salhelper::ConditionWaiter *));
149 CPPUNIT_ASSERT(
150 typeid (const_cast< salhelper::ConditionWaiter const * >(p))
151 == typeid (salhelper::ConditionWaiter const *));
152 CPPUNIT_ASSERT(
153 typeid (const_cast< salhelper::ConditionWaiter volatile * >(p))
154 == typeid (salhelper::ConditionWaiter volatile *));
155 CPPUNIT_ASSERT(
156 typeid (salhelper::ConditionWaiter *) == getConditionWaiterTypeInfo());
159 void Test::testConditionWaiterTimedout() {
160 salhelper::ConditionWaiter::timedout x;
161 CPPUNIT_ASSERT(typeid (x) == typeid (salhelper::ConditionWaiter::timedout));
162 CPPUNIT_ASSERT(
163 typeid (&x) == typeid (salhelper::ConditionWaiter::timedout *));
164 CPPUNIT_ASSERT(
165 typeid (const_cast< salhelper::ConditionWaiter::timedout const * >(&x))
166 == typeid (salhelper::ConditionWaiter::timedout const *));
167 CPPUNIT_ASSERT(
168 (typeid
169 (const_cast< salhelper::ConditionWaiter::timedout volatile * >(&x)))
170 == typeid (salhelper::ConditionWaiter::timedout volatile *));
171 try {
172 throw salhelper::ConditionWaiter::timedout();
173 } catch (salhelper::ConditionWaiter::timedout &) {
174 } catch (...) {
175 CPPUNIT_FAIL("not caught");
179 void Test::testORealDynamicLoader() {
180 salhelper::ORealDynamicLoader * p = 0;
181 CPPUNIT_ASSERT(typeid (p) != typeid (salhelper::ORealDynamicLoader));
182 CPPUNIT_ASSERT(typeid (p) == typeid (salhelper::ORealDynamicLoader *));
183 CPPUNIT_ASSERT(
184 typeid (const_cast< salhelper::ORealDynamicLoader const * >(p))
185 == typeid (salhelper::ORealDynamicLoader const *));
186 CPPUNIT_ASSERT(
187 typeid (const_cast< salhelper::ORealDynamicLoader volatile * >(p))
188 == typeid (salhelper::ORealDynamicLoader volatile *));
189 CPPUNIT_ASSERT(
190 typeid (salhelper::ORealDynamicLoader *)
191 == getORealDynamicLoaderTypeInfo());
194 #ifdef _MSC_VER
195 #pragma warning (pop)
196 #endif
198 void Test::testSimpleReferenceObject() {
199 salhelper::SimpleReferenceObject * p = new DerivedSimpleReferenceObject;
200 try {
201 CPPUNIT_ASSERT(
202 typeid (*p) != typeid (salhelper::SimpleReferenceObject));
203 CPPUNIT_ASSERT(
204 typeid (p) == typeid (salhelper::SimpleReferenceObject *));
205 CPPUNIT_ASSERT(
206 typeid (const_cast< salhelper::SimpleReferenceObject const * >(p))
207 == typeid (salhelper::SimpleReferenceObject const *));
208 CPPUNIT_ASSERT(
209 (typeid
210 (const_cast< salhelper::SimpleReferenceObject volatile * >(p)))
211 == typeid (salhelper::SimpleReferenceObject volatile *));
212 CPPUNIT_ASSERT(
213 typeid (salhelper::SimpleReferenceObject *)
214 == getSimpleReferenceObjectTypeInfo());
215 } catch (...) {
216 delete static_cast< DerivedSimpleReferenceObject * >(p);
217 throw;
219 delete static_cast< DerivedSimpleReferenceObject * >(p);
222 void Test::testDerivedCondition() {
223 osl::Mutex mutex;
224 boost::scoped_ptr< salhelper::Condition > p(new DerivedCondition(mutex));
225 CPPUNIT_ASSERT(dynamic_cast< DerivedCondition * >(p.get()) != 0);
228 void Test::testDerivedConditionWaiterTimedout() {
229 boost::scoped_ptr< salhelper::ConditionWaiter::timedout > p(
230 new DerivedConditionWaiterTimedout);
231 CPPUNIT_ASSERT(
232 dynamic_cast< DerivedConditionWaiterTimedout * >(p.get()) != 0);
233 try {
234 throw DerivedConditionWaiterTimedout();
235 } catch (salhelper::ConditionWaiter::timedout &) {
236 } catch (...) {
237 CPPUNIT_FAIL("not caught");
241 void Test::testDerivedSimpleReferenceObject() {
242 salhelper::SimpleReferenceObject * p = new DerivedSimpleReferenceObject;
243 try {
244 CPPUNIT_ASSERT(dynamic_cast< DerivedSimpleReferenceObject * >(p) != 0);
245 } catch (...) {
246 delete static_cast< DerivedSimpleReferenceObject * >(p);
247 throw;
249 delete static_cast< DerivedSimpleReferenceObject * >(p);
252 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
256 CPPUNIT_PLUGIN_IMPLEMENT();
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */