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"
34 // small memory check routine, which return false, if there is a problem
36 static bool checkMemory(const char* _pMemory
, sal_uInt32 _nSize
, char _n
)
40 for (sal_uInt32 i
=0;i
<_nSize
;i
++)
42 if (_pMemory
[i
] != _n
)
50 class Memory
: public CppUnit::TestFixture
52 // for normal alloc functions
54 static const sal_uInt32 m_nSizeOfMemory
= 1024;
62 // initialise your test code values here.
65 m_pMemory
= static_cast<char*>(rtl_allocateMemory( m_nSizeOfMemory
));
68 void tearDown() override
70 rtl_freeMemory(m_pMemory
);
74 void rtl_allocateMemory_001()
76 CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", m_pMemory
!= nullptr);
77 memset(m_pMemory
, 1, m_nSizeOfMemory
);
78 CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pMemory
, m_nSizeOfMemory
, 1));
81 void rtl_reallocateMemory_001()
83 sal_uInt32 nSize
= 2 * 1024;
84 m_pMemory
= static_cast<char*>(rtl_reallocateMemory(m_pMemory
, nSize
));
86 CPPUNIT_ASSERT_MESSAGE( "Can reallocate memory.", m_pMemory
!= nullptr);
87 memset(m_pMemory
, 2, nSize
);
88 CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pMemory
, nSize
, 2));
91 CPPUNIT_TEST_SUITE(Memory
);
92 CPPUNIT_TEST(rtl_allocateMemory_001
);
93 CPPUNIT_TEST(rtl_reallocateMemory_001
);
94 CPPUNIT_TEST_SUITE_END();
97 class TestZeroMemory
: public CppUnit::TestFixture
101 static const sal_uInt32 m_nSizeOfZeroMemory
= 50 * 1024 * 1024;
105 : m_pZeroMemory(nullptr)
109 // initialise your test code values here.
110 void setUp() override
112 m_pZeroMemory
= static_cast<char*>(rtl_allocateZeroMemory( m_nSizeOfZeroMemory
));
115 void tearDown() override
117 rtl_freeZeroMemory(m_pZeroMemory
, m_nSizeOfZeroMemory
);
118 // LLA: no check possible, may GPF if there is something wrong.
119 // CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", pZeroMemory != NULL);
122 // insert your test code here.
124 void rtl_allocateZeroMemory_001()
126 CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", m_pZeroMemory
!= nullptr);
127 CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pZeroMemory
, m_nSizeOfZeroMemory
, 0));
129 memset(m_pZeroMemory
, 3, m_nSizeOfZeroMemory
);
130 CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pZeroMemory
, m_nSizeOfZeroMemory
, 3));
133 CPPUNIT_TEST_SUITE(TestZeroMemory
);
134 CPPUNIT_TEST(rtl_allocateZeroMemory_001
);
135 CPPUNIT_TEST_SUITE_END();
138 class TestPreinit
: public CppUnit::TestFixture
145 // initialise your test code values here.
146 void setUp() override
150 void tearDown() override
154 // insert your test code here.
158 const char sample
[] = "Hello World";
159 std::vector
<OUString
> aStrings
;
161 rtl_alloc_preInit(true);
163 OUString
aFoo("foo");
165 // fill some cache bits
166 for (int iter
= 0; iter
< 4; iter
++)
168 for (int i
= 1; i
< 4096; i
+= 8)
170 OUStringBuffer
aBuf(i
);
171 aBuf
.appendAscii(sample
, (i
/8) % (SAL_N_ELEMENTS(sample
)-1));
172 OUString aStr
= aBuf
.makeStringAndClear();
173 aStrings
.push_back(aStr
);
175 // free some pieces to make holes
176 for (size_t i
= iter
; i
< aStrings
.size(); i
+= 17)
180 for (size_t i
= 0; i
< aStrings
.size(); ++i
)
182 CPPUNIT_ASSERT_MESSAGE( "not static before.", !(aStrings
[i
].pData
->refCount
& SAL_STRING_STATIC_FLAG
) );
185 // should static-ize all the strings.
186 rtl_alloc_preInit(false);
188 for (size_t i
= 0; i
< aStrings
.size(); ++i
)
189 CPPUNIT_ASSERT_MESSAGE( "static after.", (aStrings
[i
].pData
->refCount
& SAL_STRING_STATIC_FLAG
) );
194 // should never happen but lets try it again.
198 CPPUNIT_TEST_SUITE(TestPreinit
);
201 CPPUNIT_TEST_SUITE_END();
204 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_alloc::Memory
);
205 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_alloc::TestZeroMemory
);
206 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_alloc::TestPreinit
);
207 } // namespace rtl_alloc
209 CPPUNIT_PLUGIN_IMPLEMENT();
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */