Line data Source code
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 <rtl/alloc.h>
21 : #include <sal/types.h>
22 : #include <cppunit/TestFixture.h>
23 : #include <cppunit/extensions/HelperMacros.h>
24 : #include <cppunit/plugin/TestPlugIn.h>
25 :
26 : #include <memory.h>
27 :
28 : namespace rtl_alloc
29 : {
30 :
31 : // small memory check routine, which return false, if there is a problem
32 :
33 8 : bool checkMemory(char* _pMemory, sal_uInt32 _nSize, char _n)
34 : {
35 8 : bool bOk = true;
36 :
37 209721352 : for (sal_uInt32 i=0;i<_nSize;i++)
38 : {
39 209721344 : if (_pMemory[i] != _n)
40 : {
41 0 : bOk = false;
42 : }
43 : }
44 8 : return bOk;
45 : }
46 :
47 8 : class Memory : public CppUnit::TestFixture
48 : {
49 : // for normal alloc functions
50 : char *m_pMemory;
51 : sal_uInt32 m_nSizeOfMemory;
52 :
53 : public:
54 4 : Memory()
55 : : m_pMemory(NULL)
56 4 : , m_nSizeOfMemory(1024)
57 : {
58 4 : }
59 :
60 : // initialise your test code values here.
61 4 : void setUp() SAL_OVERRIDE
62 : {
63 4 : m_pMemory = (char*) rtl_allocateMemory( m_nSizeOfMemory );
64 4 : }
65 :
66 4 : void tearDown() SAL_OVERRIDE
67 : {
68 4 : rtl_freeMemory(m_pMemory);
69 4 : m_pMemory = NULL;
70 4 : }
71 :
72 2 : void rtl_allocateMemory_001()
73 : {
74 2 : CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", m_pMemory != NULL);
75 2 : memset(m_pMemory, 1, m_nSizeOfMemory);
76 2 : CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pMemory, m_nSizeOfMemory, 1) == true);
77 2 : }
78 :
79 2 : void rtl_reallocateMemory_001()
80 : {
81 2 : sal_uInt32 nSize = 2 * 1024;
82 2 : m_pMemory = (char*)rtl_reallocateMemory(m_pMemory, nSize);
83 :
84 2 : CPPUNIT_ASSERT_MESSAGE( "Can reallocate memory.", m_pMemory != NULL);
85 2 : memset(m_pMemory, 2, nSize);
86 2 : CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pMemory, nSize, 2) == true);
87 2 : }
88 :
89 4 : CPPUNIT_TEST_SUITE(Memory);
90 2 : CPPUNIT_TEST(rtl_allocateMemory_001);
91 2 : CPPUNIT_TEST(rtl_reallocateMemory_001);
92 4 : CPPUNIT_TEST_SUITE_END();
93 : }; // class test
94 :
95 4 : class TestZeroMemory : public CppUnit::TestFixture
96 : {
97 : // for zero functions
98 : char *m_pZeroMemory;
99 : sal_uInt32 m_nSizeOfZeroMemory;
100 :
101 : public:
102 2 : TestZeroMemory()
103 : : m_pZeroMemory(NULL)
104 2 : , m_nSizeOfZeroMemory( 50 * 1024 * 1024 )
105 : {
106 2 : }
107 :
108 : // initialise your test code values here.
109 2 : void setUp() SAL_OVERRIDE
110 : {
111 2 : m_pZeroMemory = (char*) rtl_allocateZeroMemory( m_nSizeOfZeroMemory );
112 2 : }
113 :
114 2 : void tearDown() SAL_OVERRIDE
115 : {
116 2 : 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 2 : }
120 :
121 : // insert your test code here.
122 :
123 2 : void rtl_allocateZeroMemory_001()
124 : {
125 2 : CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", m_pZeroMemory != NULL);
126 2 : CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pZeroMemory, m_nSizeOfZeroMemory, 0) == true);
127 :
128 2 : memset(m_pZeroMemory, 3, m_nSizeOfZeroMemory);
129 2 : CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pZeroMemory, m_nSizeOfZeroMemory, 3) == true);
130 2 : }
131 :
132 4 : CPPUNIT_TEST_SUITE(TestZeroMemory);
133 2 : CPPUNIT_TEST(rtl_allocateZeroMemory_001);
134 4 : CPPUNIT_TEST_SUITE_END();
135 : };
136 :
137 2 : CPPUNIT_TEST_SUITE_REGISTRATION(rtl_alloc::Memory);
138 2 : CPPUNIT_TEST_SUITE_REGISTRATION(rtl_alloc::TestZeroMemory);
139 : } // namespace rtl_alloc
140 :
141 8 : CPPUNIT_PLUGIN_IMPLEMENT();
142 :
143 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|