1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <rtl/alloc.h>
21 #include <rtl/ustrbuf.hxx>
22 #include <sal/types.h>
23 #include <cppunit/TestFixture.h>
24 #include <cppunit/extensions/HelperMacros.h>
25 #include <cppunit/plugin/TestPlugIn.h>
27 #include "../../../rtl/strimp.hxx"
32 // small memory check routine, which return false, if there is a problem
34 static bool checkMemory(const char* _pMemory
, sal_uInt32 _nSize
, char _n
)
38 for (sal_uInt32 i
=0;i
<_nSize
;i
++)
40 if (_pMemory
[i
] != _n
)
48 class Memory
: public CppUnit::TestFixture
50 // for normal alloc functions
52 static const sal_uInt32 m_nSizeOfMemory
= 1024;
60 // initialise your test code values here.
63 m_pMemory
= static_cast<char*>(rtl_allocateMemory( m_nSizeOfMemory
));
66 void tearDown() override
68 rtl_freeMemory(m_pMemory
);
72 void rtl_allocateMemory_001()
74 CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", m_pMemory
!= nullptr);
75 memset(m_pMemory
, 1, m_nSizeOfMemory
);
76 CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pMemory
, m_nSizeOfMemory
, 1));
79 void rtl_reallocateMemory_001()
81 sal_uInt32 nSize
= 2 * 1024;
82 m_pMemory
= static_cast<char*>(rtl_reallocateMemory(m_pMemory
, nSize
));
84 CPPUNIT_ASSERT_MESSAGE( "Can reallocate memory.", m_pMemory
!= nullptr);
85 memset(m_pMemory
, 2, nSize
);
86 CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pMemory
, nSize
, 2));
89 CPPUNIT_TEST_SUITE(Memory
);
90 CPPUNIT_TEST(rtl_allocateMemory_001
);
91 CPPUNIT_TEST(rtl_reallocateMemory_001
);
92 CPPUNIT_TEST_SUITE_END();
95 class TestZeroMemory
: public CppUnit::TestFixture
99 static const sal_uInt32 m_nSizeOfZeroMemory
= 50 * 1024 * 1024;
103 : m_pZeroMemory(nullptr)
107 // initialise your test code values here.
108 void setUp() override
110 m_pZeroMemory
= static_cast<char*>(rtl_allocateZeroMemory( m_nSizeOfZeroMemory
));
113 void tearDown() override
115 rtl_freeZeroMemory(m_pZeroMemory
, m_nSizeOfZeroMemory
);
116 // LLA: no check possible, may GPF if there is something wrong.
117 // CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", pZeroMemory != NULL);
120 // insert your test code here.
122 void rtl_allocateZeroMemory_001()
124 CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", m_pZeroMemory
!= nullptr);
125 CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pZeroMemory
, m_nSizeOfZeroMemory
, 0));
127 memset(m_pZeroMemory
, 3, m_nSizeOfZeroMemory
);
128 CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pZeroMemory
, m_nSizeOfZeroMemory
, 3));
131 CPPUNIT_TEST_SUITE(TestZeroMemory
);
132 CPPUNIT_TEST(rtl_allocateZeroMemory_001
);
133 CPPUNIT_TEST_SUITE_END();
136 class TestPreinit
: public CppUnit::TestFixture
143 // initialise your test code values here.
144 void setUp() override
148 void tearDown() override
152 // insert your test code here.
156 const char sample
[] = "Hello World";
157 std::vector
<OUString
> aStrings
;
159 rtl_alloc_preInit(true);
161 OUString
aFoo("foo");
163 // fill some cache bits
164 for (int iter
= 0; iter
< 4; iter
++)
166 for (int i
= 1; i
< 4096; i
+= 8)
168 OUStringBuffer
aBuf(i
);
169 aBuf
.appendAscii(sample
, (i
/8) % (SAL_N_ELEMENTS(sample
)-1));
170 OUString aStr
= aBuf
.makeStringAndClear();
171 aStrings
.push_back(aStr
);
173 // free some pieces to make holes
174 for (size_t i
= iter
; i
< aStrings
.size(); i
+= 17)
178 for (size_t i
= 0; i
< aStrings
.size(); ++i
)
180 CPPUNIT_ASSERT_MESSAGE( "not static before.", !(aStrings
[i
].pData
->refCount
& SAL_STRING_STATIC_FLAG
) );
183 // should static-ize all the strings.
184 rtl_alloc_preInit(false);
186 for (size_t i
= 0; i
< aStrings
.size(); ++i
)
187 CPPUNIT_ASSERT_MESSAGE( "static after.", (aStrings
[i
].pData
->refCount
& SAL_STRING_STATIC_FLAG
) );
192 // should never happen but lets try it again.
196 CPPUNIT_TEST_SUITE(TestPreinit
);
199 CPPUNIT_TEST_SUITE_END();
202 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_alloc::Memory
);
203 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_alloc::TestZeroMemory
);
204 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_alloc::TestPreinit
);
205 } // namespace rtl_alloc
207 CPPUNIT_PLUGIN_IMPLEMENT();
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */