| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 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> |
| 27 |
|
| 28 |
#include "../../../rtl/strimp.hxx" |
| 29 |
|
| 30 |
namespace rtl_alloc |
| 31 |
{ |
| 32 |
|
| 33 |
// small memory check routine, which return false, if there is a problem |
| 34 |
|
| 35 |
static bool checkMemory(const char* _pMemory, sal_uInt32 _nSize, char _n) |
| 36 |
{ |
| 37 |
bool bOk = true; |
| 38 |
|
| 39 |
for (sal_uInt32 i=0;i<_nSize;i++) |
| 40 |
{ |
| 41 |
if (_pMemory[i] != _n) |
| 42 |
{ |
| 43 |
bOk = false; |
| 44 |
} |
| 45 |
} |
| 46 |
return bOk; |
| 47 |
} |
| 48 |
|
| 49 |
class Memory : public CppUnit::TestFixture |
| 50 |
{ |
| 51 |
// for normal alloc functions |
| 52 |
char *m_pMemory; |
| 53 |
static const sal_uInt32 m_nSizeOfMemory = 1024; |
| 54 |
|
| 55 |
public: |
| 56 |
Memory() |
| 57 |
: m_pMemory(nullptr) |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
// initialise your test code values here. |
| 62 |
void setUp() override |
| 63 |
{ |
| 64 |
m_pMemory = static_cast<char*>(rtl_allocateMemory( m_nSizeOfMemory )); |
| 65 |
} |
| 66 |
|
| 67 |
void tearDown() override |
| 68 |
{ |
| 69 |
rtl_freeMemory(m_pMemory); |
| 70 |
m_pMemory = nullptr; |
| 71 |
} |
| 72 |
|
| 73 |
void rtl_allocateMemory_001() |
| 74 |
{ |
| 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)); |
| 78 |
} |
| 79 |
|
| 80 |
void rtl_reallocateMemory_001() |
| 81 |
{ |
| 82 |
sal_uInt32 nSize = 2 * 1024; |
| 83 |
m_pMemory = static_cast<char*>(rtl_reallocateMemory(m_pMemory, nSize)); |
| 84 |
|
| 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)); |
| 88 |
} |
| 89 |
|
| 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 |
| 95 |
|
| 96 |
class TestZeroMemory : public CppUnit::TestFixture |
| 97 |
{ |
| 98 |
// for zero functions |
| 99 |
char *m_pZeroMemory; |
| 100 |
static const sal_uInt32 m_nSizeOfZeroMemory = 50 * 1024 * 1024; |
| 101 |
|
| 102 |
public: |
| 103 |
TestZeroMemory() |
| 104 |
: m_pZeroMemory(nullptr) |
| 105 |
{ |
| 106 |
} |
| 107 |
|
| 108 |
// initialise your test code values here. |
| 109 |
void setUp() override |
| 110 |
{ |
| 111 |
m_pZeroMemory = static_cast<char*>(rtl_allocateZeroMemory( m_nSizeOfZeroMemory )); |
| 112 |
} |
| 113 |
|
| 114 |
void tearDown() override |
| 115 |
{ |
| 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); |
| 119 |
} |
| 120 |
|
| 121 |
// insert your test code here. |
| 122 |
|
| 123 |
void rtl_allocateZeroMemory_001() |
| 124 |
{ |
| 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)); |
| 127 |
|
| 128 |
memset(m_pZeroMemory, 3, m_nSizeOfZeroMemory); |
| 129 |
CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pZeroMemory, m_nSizeOfZeroMemory, 3)); |
| 130 |
} |
| 131 |
|
| 132 |
CPPUNIT_TEST_SUITE(TestZeroMemory); |
| 133 |
CPPUNIT_TEST(rtl_allocateZeroMemory_001); |
| 134 |
CPPUNIT_TEST_SUITE_END(); |
| 135 |
}; |
| 136 |
|
| 137 |
class TestPreinit : public CppUnit::TestFixture |
| 138 |
{ |
| 139 |
public: |
| 140 |
TestPreinit() |
| 141 |
{ |
| 142 |
} |
| 143 |
|
| 144 |
// initialise your test code values here. |
| 145 |
void setUp() override |
| 146 |
{ |
| 147 |
} |
| 148 |
|
| 149 |
void tearDown() override |
| 150 |
{ |
| 151 |
} |
| 152 |
|
| 153 |
// insert your test code here. |
| 154 |
|
| 155 |
void test() |
| 156 |
{ |
| 157 |
const char sample[] = "Hello World"; |
| 158 |
std::vector<OUString> aStrings; |
| 159 |
|
| 160 |
rtl_alloc_preInit(1); |
| 161 |
|
| 162 |
OUString aFoo(o3tl::nonStaticString(u"foo")); |
| 163 |
|
| 164 |
// fill some cache bits |
| 165 |
for (int iter = 0; iter < 4; iter++) |
| 166 |
{ |
| 167 |
for (int i = 1; i < 4096; i += 8) |
| 168 |
{ |
| 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); |
| 173 |
} |
| 174 |
// free some pieces to make holes |
| 175 |
for (size_t i = iter; i < aStrings.size(); i += 17) |
| 176 |
aStrings[i] = aFoo; |
| 177 |
} |
| 178 |
|
| 179 |
for (size_t i = 0; i < aStrings.size(); ++i) |
| 180 |
{ |
| 181 |
CPPUNIT_ASSERT_MESSAGE( "not static before.", !(aStrings[i].pData->refCount & SAL_STRING_STATIC_FLAG) ); |
| 182 |
} |
| 183 |
|
| 184 |
// should static-ize all the strings. |
| 185 |
rtl_alloc_preInit(0); |
| 186 |
|
| 187 |
for (size_t i = 0; i < aStrings.size(); ++i) |
| 188 |
CPPUNIT_ASSERT_MESSAGE( "static after.", (aStrings[i].pData->refCount & SAL_STRING_STATIC_FLAG) ); |
| 189 |
} |
| 190 |
|
| 191 |
void test2() |
| 192 |
{ |
| 193 |
// should never happen but let's try it again. |
| 194 |
test(); |
| 195 |
} |
| 196 |
|
| 197 |
CPPUNIT_TEST_SUITE(TestPreinit); |
| 198 |
CPPUNIT_TEST(test); |
| 199 |
CPPUNIT_TEST(test2); |
| 200 |
CPPUNIT_TEST_SUITE_END(); |
| 201 |
}; |
| 202 |
|
| 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 |
| 207 |
|
| 208 |
CPPUNIT_PLUGIN_IMPLEMENT(); |
| 209 |
|
| 210 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |