FROM_LIST_NO_WARN was handled above
[LibreOffice.git] / salhelper / qa / test_api.cxx
blobdd45f4c730f2d42df6ab677e44395d099696b317
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 #include <osl/mutex.hxx>
25 #include <salhelper/condition.hxx>
26 #include <salhelper/dynload.hxx>
27 #include <salhelper/simplereferenceobject.hxx>
28 #include <cppunit/TestFixture.h>
29 #include <cppunit/extensions/HelperMacros.h>
30 #include <cppunit/plugin/TestPlugIn.h>
31 #include <memory>
33 namespace {
35 std::type_info const & getConditionTypeInfo()
36 { return typeid (salhelper::Condition *); }
38 std::type_info const & getConditionModifierTypeInfo()
39 { return typeid (salhelper::ConditionModifier *); }
41 std::type_info const & getConditionWaiterTypeInfo()
42 { return typeid (salhelper::ConditionWaiter *); }
44 std::type_info const & getORealDynamicLoaderTypeInfo()
45 { return typeid (salhelper::ORealDynamicLoader *); }
47 std::type_info const & getSimpleReferenceObjectTypeInfo()
48 { return typeid (salhelper::SimpleReferenceObject *); }
52 namespace {
54 class DerivedCondition: public salhelper::Condition {
55 public:
56 explicit DerivedCondition(osl::Mutex & mutex): Condition(mutex) {}
58 protected:
59 virtual bool applies() const override { return false; }
62 class DerivedConditionWaiterTimedout:
63 public salhelper::ConditionWaiter::timedout
64 {};
66 class DerivedSimpleReferenceObject: public salhelper::SimpleReferenceObject {};
68 class Test: public CppUnit::TestFixture {
69 public:
70 void testCondition();
72 void testConditionModifier();
74 void testConditionWaiter();
76 void testConditionWaiterTimedout();
78 void testORealDynamicLoader();
80 void testSimpleReferenceObject();
82 void testDerivedCondition();
84 void testDerivedConditionWaiterTimedout();
86 void testDerivedSimpleReferenceObject();
88 CPPUNIT_TEST_SUITE(Test);
89 CPPUNIT_TEST(testCondition);
90 CPPUNIT_TEST(testConditionModifier);
91 CPPUNIT_TEST(testConditionWaiter);
92 CPPUNIT_TEST(testConditionWaiterTimedout);
93 CPPUNIT_TEST(testORealDynamicLoader);
94 CPPUNIT_TEST(testSimpleReferenceObject);
95 CPPUNIT_TEST(testDerivedCondition);
96 CPPUNIT_TEST(testDerivedConditionWaiterTimedout);
97 CPPUNIT_TEST(testDerivedSimpleReferenceObject);
98 CPPUNIT_TEST_SUITE_END();
101 void Test::testCondition() {
102 osl::Mutex mutex;
103 std::unique_ptr< salhelper::Condition > p(new DerivedCondition(mutex));
104 CPPUNIT_ASSERT(typeid(*p) != typeid(salhelper::Condition));
105 CPPUNIT_ASSERT(bool(typeid (p.get()) == typeid (salhelper::Condition *)));
106 CPPUNIT_ASSERT(bool(
107 typeid (const_cast< salhelper::Condition const * >(p.get()))
108 == typeid (salhelper::Condition const *)));
109 CPPUNIT_ASSERT(bool(
110 typeid (const_cast< salhelper::Condition volatile * >(p.get()))
111 == typeid (salhelper::Condition volatile *)));
112 CPPUNIT_ASSERT(bool(typeid (salhelper::Condition *) == getConditionTypeInfo()));
115 #ifdef _MSC_VER
116 #pragma warning (push)
117 #pragma warning (disable: 4189) // 'p': local variable is initialized but not referenced
118 #endif
121 void Test::testConditionModifier() {
122 salhelper::ConditionModifier * p = nullptr;
123 CPPUNIT_ASSERT(bool(typeid (*p) == typeid (salhelper::ConditionModifier)));
124 CPPUNIT_ASSERT(bool(typeid (p) == typeid (salhelper::ConditionModifier *)));
125 CPPUNIT_ASSERT(bool(
126 typeid (const_cast< salhelper::ConditionModifier const * >(p))
127 == typeid (salhelper::ConditionModifier const *)));
128 CPPUNIT_ASSERT(bool(
129 typeid (const_cast< salhelper::ConditionModifier volatile * >(p))
130 == typeid (salhelper::ConditionModifier volatile *)));
131 CPPUNIT_ASSERT(bool(
132 typeid (salhelper::ConditionModifier *)
133 == getConditionModifierTypeInfo()));
136 void Test::testConditionWaiter() {
137 salhelper::ConditionWaiter * p = nullptr;
138 CPPUNIT_ASSERT(bool(typeid (*p) == typeid (salhelper::ConditionWaiter)));
139 CPPUNIT_ASSERT(bool(typeid (p) == typeid (salhelper::ConditionWaiter *)));
140 CPPUNIT_ASSERT(bool(
141 typeid (const_cast< salhelper::ConditionWaiter const * >(p))
142 == typeid (salhelper::ConditionWaiter const *)));
143 CPPUNIT_ASSERT(bool(
144 typeid (const_cast< salhelper::ConditionWaiter volatile * >(p))
145 == typeid (salhelper::ConditionWaiter volatile *)));
146 CPPUNIT_ASSERT(bool(
147 typeid (salhelper::ConditionWaiter *) == getConditionWaiterTypeInfo()));
150 void Test::testConditionWaiterTimedout() {
151 salhelper::ConditionWaiter::timedout x;
152 CPPUNIT_ASSERT(bool(typeid (x) == typeid (salhelper::ConditionWaiter::timedout)));
153 CPPUNIT_ASSERT(bool(
154 typeid (&x) == typeid (salhelper::ConditionWaiter::timedout *)));
155 CPPUNIT_ASSERT(bool(
156 typeid (const_cast< salhelper::ConditionWaiter::timedout const * >(&x))
157 == typeid (salhelper::ConditionWaiter::timedout const *)));
158 CPPUNIT_ASSERT(bool(
159 (typeid
160 (const_cast< salhelper::ConditionWaiter::timedout volatile * >(&x)))
161 == typeid (salhelper::ConditionWaiter::timedout volatile *)));
162 try {
163 throw salhelper::ConditionWaiter::timedout();
164 } catch (salhelper::ConditionWaiter::timedout &) {
165 } catch (...) {
166 CPPUNIT_FAIL("not caught");
170 void Test::testORealDynamicLoader() {
171 salhelper::ORealDynamicLoader * p = nullptr;
172 CPPUNIT_ASSERT(typeid (p) != typeid (salhelper::ORealDynamicLoader));
173 CPPUNIT_ASSERT(bool(typeid (p) == typeid (salhelper::ORealDynamicLoader *)));
174 CPPUNIT_ASSERT(bool(
175 typeid (const_cast< salhelper::ORealDynamicLoader const * >(p))
176 == typeid (salhelper::ORealDynamicLoader const *)));
177 CPPUNIT_ASSERT(bool(
178 typeid (const_cast< salhelper::ORealDynamicLoader volatile * >(p))
179 == typeid (salhelper::ORealDynamicLoader volatile *)));
180 CPPUNIT_ASSERT(bool(
181 typeid (salhelper::ORealDynamicLoader *)
182 == getORealDynamicLoaderTypeInfo()));
185 #ifdef _MSC_VER
186 #pragma warning (pop)
187 #endif
189 void Test::testSimpleReferenceObject() {
190 salhelper::SimpleReferenceObject * p = new DerivedSimpleReferenceObject;
191 try {
192 CPPUNIT_ASSERT(
193 typeid (*p) != typeid (salhelper::SimpleReferenceObject));
194 CPPUNIT_ASSERT(bool(
195 typeid (p) == typeid (salhelper::SimpleReferenceObject *)));
196 CPPUNIT_ASSERT(bool(
197 typeid (const_cast< salhelper::SimpleReferenceObject const * >(p))
198 == typeid (salhelper::SimpleReferenceObject const *)));
199 CPPUNIT_ASSERT(bool(
200 (typeid
201 (const_cast< salhelper::SimpleReferenceObject volatile * >(p)))
202 == typeid (salhelper::SimpleReferenceObject volatile *)));
203 CPPUNIT_ASSERT(bool(
204 typeid (salhelper::SimpleReferenceObject *)
205 == getSimpleReferenceObjectTypeInfo()));
206 } catch (...) {
207 delete static_cast< DerivedSimpleReferenceObject * >(p);
208 throw;
210 delete static_cast< DerivedSimpleReferenceObject * >(p);
213 void Test::testDerivedCondition() {
214 osl::Mutex mutex;
215 // Next line tests that new doesn't throw
216 std::unique_ptr< salhelper::Condition > p(new DerivedCondition(mutex));
219 void Test::testDerivedConditionWaiterTimedout() {
220 // Next line tests that new doesn't throw
221 std::unique_ptr< salhelper::ConditionWaiter::timedout > p(
222 new DerivedConditionWaiterTimedout);
223 try {
224 throw DerivedConditionWaiterTimedout();
225 } catch (salhelper::ConditionWaiter::timedout &) {
226 } catch (...) {
227 CPPUNIT_FAIL("not caught");
231 void Test::testDerivedSimpleReferenceObject() {
232 salhelper::SimpleReferenceObject * p = new DerivedSimpleReferenceObject;
233 try {
234 CPPUNIT_ASSERT(dynamic_cast< DerivedSimpleReferenceObject * >(p) != nullptr);
235 } catch (...) {
236 delete static_cast< DerivedSimpleReferenceObject * >(p);
237 throw;
239 delete static_cast< DerivedSimpleReferenceObject * >(p);
242 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
246 CPPUNIT_PLUGIN_IMPLEMENT();
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */