related tdf#162786: Add cacert.pem
[LibreOffice.git] / sal / qa / rtl / alloc / rtl_alloc.cxx
blob0e743cae3c111d6286549ba72e6458273bbf0396
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 <o3tl/nonstaticstring.hxx>
21 #include <rtl/alloc.h>
22 #include <rtl/ustrbuf.hxx>
23 #include <sal/types.h>
24 #include <cppunit/TestFixture.h>
25 #include <cppunit/extensions/HelperMacros.h>
26 #include <cppunit/plugin/TestPlugIn.h>
28 #include "../../../rtl/strimp.hxx"
30 namespace rtl_alloc
33 // small memory check routine, which return false, if there is a problem
35 static bool checkMemory(const char* _pMemory, sal_uInt32 _nSize, char _n)
37 bool bOk = true;
39 for (sal_uInt32 i=0;i<_nSize;i++)
41 if (_pMemory[i] != _n)
43 bOk = false;
46 return bOk;
49 class Memory : public CppUnit::TestFixture
51 // for normal alloc functions
52 char *m_pMemory;
53 static const sal_uInt32 m_nSizeOfMemory = 1024;
55 public:
56 Memory()
57 : m_pMemory(nullptr)
61 // initialise your test code values here.
62 void setUp() override
64 m_pMemory = static_cast<char*>(rtl_allocateMemory( m_nSizeOfMemory ));
67 void tearDown() override
69 rtl_freeMemory(m_pMemory);
70 m_pMemory = nullptr;
73 void rtl_allocateMemory_001()
75 CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", m_pMemory != nullptr);
76 memset(m_pMemory, 1, m_nSizeOfMemory);
77 CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pMemory, m_nSizeOfMemory, 1));
80 void rtl_reallocateMemory_001()
82 sal_uInt32 nSize = 2 * 1024;
83 m_pMemory = static_cast<char*>(rtl_reallocateMemory(m_pMemory, nSize));
85 CPPUNIT_ASSERT_MESSAGE( "Can reallocate memory.", m_pMemory != nullptr);
86 memset(m_pMemory, 2, nSize);
87 CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pMemory, nSize, 2));
90 CPPUNIT_TEST_SUITE(Memory);
91 CPPUNIT_TEST(rtl_allocateMemory_001);
92 CPPUNIT_TEST(rtl_reallocateMemory_001);
93 CPPUNIT_TEST_SUITE_END();
94 }; // class test
96 class TestZeroMemory : public CppUnit::TestFixture
98 // for zero functions
99 char *m_pZeroMemory;
100 static const sal_uInt32 m_nSizeOfZeroMemory = 50 * 1024 * 1024;
102 public:
103 TestZeroMemory()
104 : m_pZeroMemory(nullptr)
108 // initialise your test code values here.
109 void setUp() override
111 m_pZeroMemory = static_cast<char*>(rtl_allocateZeroMemory( m_nSizeOfZeroMemory ));
114 void tearDown() override
116 rtl_freeZeroMemory(m_pZeroMemory, m_nSizeOfZeroMemory);
117 // LLA: no check possible, may GPF if there is something wrong.
118 // CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", pZeroMemory != NULL);
121 // insert your test code here.
123 void rtl_allocateZeroMemory_001()
125 CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", m_pZeroMemory != nullptr);
126 CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pZeroMemory, m_nSizeOfZeroMemory, 0));
128 memset(m_pZeroMemory, 3, m_nSizeOfZeroMemory);
129 CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pZeroMemory, m_nSizeOfZeroMemory, 3));
132 CPPUNIT_TEST_SUITE(TestZeroMemory);
133 CPPUNIT_TEST(rtl_allocateZeroMemory_001);
134 CPPUNIT_TEST_SUITE_END();
137 class TestPreinit : public CppUnit::TestFixture
139 public:
140 TestPreinit()
144 // initialise your test code values here.
145 void setUp() override
149 void tearDown() override
153 // insert your test code here.
155 void test()
157 const char sample[] = "Hello World";
158 std::vector<OUString> aStrings;
160 rtl_alloc_preInit(true);
162 OUString aFoo(o3tl::nonStaticString(u"foo"));
164 // fill some cache bits
165 for (int iter = 0; iter < 4; iter++)
167 for (int i = 1; i < 4096; i += 8)
169 OUStringBuffer aBuf(i);
170 aBuf.appendAscii(sample, (i/8) % (SAL_N_ELEMENTS(sample)-1));
171 OUString aStr = aBuf.makeStringAndClear();
172 aStrings.push_back(aStr);
174 // free some pieces to make holes
175 for (size_t i = iter; i < aStrings.size(); i += 17)
176 aStrings[i] = aFoo;
179 for (size_t i = 0; i < aStrings.size(); ++i)
181 CPPUNIT_ASSERT_MESSAGE( "not static before.", !(aStrings[i].pData->refCount & SAL_STRING_STATIC_FLAG) );
184 // should static-ize all the strings.
185 rtl_alloc_preInit(false);
187 for (size_t i = 0; i < aStrings.size(); ++i)
188 CPPUNIT_ASSERT_MESSAGE( "static after.", (aStrings[i].pData->refCount & SAL_STRING_STATIC_FLAG) );
191 void test2()
193 // should never happen but let's try it again.
194 test();
197 CPPUNIT_TEST_SUITE(TestPreinit);
198 CPPUNIT_TEST(test);
199 CPPUNIT_TEST(test2);
200 CPPUNIT_TEST_SUITE_END();
203 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_alloc::Memory);
204 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_alloc::TestZeroMemory);
205 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_alloc::TestPreinit);
206 } // namespace rtl_alloc
208 CPPUNIT_PLUGIN_IMPLEMENT();
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */