Avoid potential negative array index access to cached text.
[LibreOffice.git] / cppuhelper / qa / ifcontainer / cppu_ifcontainer.cxx
blob0dcebbbac16ac5e5047568b8de90e105d3231253
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/types.h>
22 #include <cppunit/TestFixture.h>
23 #include <cppunit/extensions/HelperMacros.h>
24 #include <cppunit/plugin/TestPlugIn.h>
26 #include <com/sun/star/lang/XEventListener.hpp>
27 #include <cppuhelper/interfacecontainer.hxx>
28 #include <cppuhelper/implbase.hxx>
29 #include <cppuhelper/propshlp.hxx>
31 using namespace com::sun::star;
32 using namespace com::sun::star::uno;
33 using namespace com::sun::star::lang;
35 namespace {
37 struct ContainerStats {
38 int m_nAlive;
39 int m_nDisposed;
40 ContainerStats() : m_nAlive(0), m_nDisposed(0) {}
43 class ContainerListener : public ::cppu::WeakImplHelper< XEventListener >
45 ContainerStats *m_pStats;
46 public:
47 explicit ContainerListener(ContainerStats *pStats)
48 : m_pStats(pStats) { m_pStats->m_nAlive++; }
49 virtual ~ContainerListener() override { m_pStats->m_nAlive--; }
50 virtual void SAL_CALL disposing( const EventObject& ) override
52 m_pStats->m_nDisposed++;
58 namespace cppu_ifcontainer
60 class IfTest : public CppUnit::TestFixture
62 osl::Mutex m_aGuard;
63 static const int nTests = 10;
64 public:
65 void testCreateDispose()
67 ContainerStats aStats;
68 cppu::OInterfaceContainerHelper *pContainer;
70 pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
72 CPPUNIT_ASSERT_EQUAL_MESSAGE("Empty container not empty",
73 static_cast<sal_Int32>(0), pContainer->getLength());
75 int i;
76 for (i = 0; i < nTests; i++)
78 Reference<XEventListener> xRef = new ContainerListener(&aStats);
79 int nNewLen = pContainer->addInterface(xRef);
81 CPPUNIT_ASSERT_EQUAL_MESSAGE("addition length mismatch",
82 i + 1, nNewLen);
83 CPPUNIT_ASSERT_EQUAL_MESSAGE("addition length mismatch",
84 static_cast<sal_Int32>(i + 1), pContainer->getLength());
86 CPPUNIT_ASSERT_MESSAGE("alive count mismatch",
87 bool(aStats.m_nAlive == nTests));
89 EventObject aObj;
90 pContainer->disposeAndClear(aObj);
92 CPPUNIT_ASSERT_MESSAGE("dispose count mismatch",
93 bool(aStats.m_nDisposed == nTests));
94 CPPUNIT_ASSERT_EQUAL_MESSAGE("leaked container left alive",
95 0, aStats.m_nAlive);
97 delete pContainer;
100 void testEnumerate()
102 int i;
103 ContainerStats aStats;
104 cppu::OInterfaceContainerHelper *pContainer;
105 pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
107 std::vector< Reference< XEventListener > > aListeners;
108 for (i = 0; i < nTests; i++)
110 Reference<XEventListener> xRef = new ContainerListener(&aStats);
111 pContainer->addInterface(xRef);
112 aListeners.push_back(xRef);
114 Sequence< Reference< XInterface > > aElements = pContainer->getElements();
116 CPPUNIT_ASSERT_MESSAGE("query contents",
117 bool(static_cast<int>(aElements.getLength()) == nTests));
118 if (static_cast<int>(aElements.getLength()) == nTests)
120 for (i = 0; i < nTests; i++)
122 CPPUNIT_ASSERT_MESSAGE("mismatching elements",
123 bool(aElements[i] == aListeners[i]));
126 pContainer->clear();
128 CPPUNIT_ASSERT_EQUAL_MESSAGE("non-empty container post clear",
129 static_cast<sal_Int32>(0), pContainer->getLength());
130 delete pContainer;
133 template < typename ContainerType, typename ContainedType >
134 void doContainerTest(const ContainedType *pTypes)
136 ContainerStats aStats;
137 ContainerType *pContainer;
138 pContainer = new ContainerType(m_aGuard);
140 int i;
141 Reference<XEventListener> xRefs[nTests * 2];
143 // add these interfaces
144 for (i = 0; i < nTests * 2; i++)
146 xRefs[i] = new ContainerListener(&aStats);
147 pContainer->addInterface(pTypes[i / 2], xRefs[i]);
150 // check it is all there
151 for (i = 0; i < nTests; i++)
153 cppu::OInterfaceContainerHelper *pHelper;
155 pHelper = pContainer->getContainer(pTypes[i]);
157 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != nullptr);
158 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
159 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong num elements", static_cast<sal_Int32>(2), aSeq.getLength());
160 CPPUNIT_ASSERT_MESSAGE("match", bool(aSeq[0] == xRefs[i*2]));
161 CPPUNIT_ASSERT_MESSAGE("match", bool(aSeq[1] == xRefs[i*2+1]));
164 // remove every other interface
165 for (i = 0; i < nTests; i++)
166 pContainer->removeInterface(pTypes[i], xRefs[i*2+1]);
168 // check it is half there
169 for (i = 0; i < nTests; i++)
171 cppu::OInterfaceContainerHelper *pHelper;
173 pHelper = pContainer->getContainer(pTypes[i]);
175 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != nullptr);
176 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
177 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong num elements", static_cast<sal_Int32>(1), aSeq.getLength());
178 CPPUNIT_ASSERT_MESSAGE("match", bool(aSeq[0] == xRefs[i*2]));
181 // remove the 1st half of the rest
182 for (i = 0; i < nTests / 2; i++)
183 pContainer->removeInterface(pTypes[i], xRefs[i*2]);
185 // check it is half there
186 for (i = 0; i < nTests / 2; i++)
188 cppu::OInterfaceContainerHelper *pHelper;
190 pHelper = pContainer->getContainer(pTypes[i]);
191 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != nullptr);
192 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
193 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong num elements", static_cast<sal_Int32>(0), aSeq.getLength());
196 delete pContainer;
199 void testOMultiTypeInterfaceContainerHelper()
201 uno::Type pTypes[nTests] =
203 ::cppu::UnoType< bool >::get(),
204 ::cppu::UnoType< float >::get(),
205 ::cppu::UnoType< double >::get(),
206 ::cppu::UnoType< ::sal_uInt64 >::get(),
207 ::cppu::UnoType< ::sal_Int64 >::get(),
208 ::cppu::UnoType< ::sal_uInt32 >::get(),
209 ::cppu::UnoType< ::sal_Int32 >::get(),
210 ::cppu::UnoType< ::sal_Int16 >::get(),
211 ::cppu::UnoType< OUString >::get(),
212 ::cppu::UnoType< ::sal_Int8 >::get()
214 doContainerTest< cppu::OMultiTypeInterfaceContainerHelper,
215 uno::Type> (pTypes);
218 void testOMultiTypeInterfaceContainerHelperInt32()
220 sal_Int32 const pTypes[nTests] =
225 256,
226 1024,
233 doContainerTest< cppu::OMultiTypeInterfaceContainerHelperInt32, sal_Int32> (pTypes);
236 void testOMultiTypeInterfaceContainerHelperVar()
238 typedef cppu::OMultiTypeInterfaceContainerHelperVar<
239 char const *, void, rtl::CStringEqual> StrContainer;
241 const char * const pTypes[nTests] =
243 "this_is", "such", "fun", "writing", "unit", "tests", "when", "it", "works", "anyway"
245 doContainerTest< StrContainer, const char *> (pTypes);
248 // Automatic registration code
249 CPPUNIT_TEST_SUITE(IfTest);
250 CPPUNIT_TEST(testCreateDispose);
251 CPPUNIT_TEST(testEnumerate);
252 CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelper);
253 CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelperVar);
254 CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelperInt32);
255 CPPUNIT_TEST_SUITE_END();
257 } // namespace cppu_ifcontainer
259 CPPUNIT_TEST_SUITE_REGISTRATION(cppu_ifcontainer::IfTest);
261 CPPUNIT_PLUGIN_IMPLEMENT();
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */