1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24
25 // MARKER(update_precomp.py): autogen include statement, do not remove
26 #include "precompiled_sal.hxx"
27 // autogenerated file with codegen.pl
28
29 #include <rtl/alloc.h>
30 #include "gtest/gtest.h"
31
32 namespace rtl_alloc
33 {
34
35 // small memory check routine, which return false, if there is a problem
36
checkMemory(char * _pMemory,sal_uInt32 _nSize,char _n)37 bool checkMemory(char* _pMemory, sal_uInt32 _nSize, char _n)
38 {
39 bool bOk = true;
40
41 for (sal_uInt32 i=0;i<_nSize;i++)
42 {
43 if (_pMemory[i] != _n)
44 {
45 bOk = false;
46 }
47 }
48 return bOk;
49 }
50
51 class Memory : public ::testing::Test
52 {
53 protected:
54 // for normal alloc functions
55 char *m_pMemory;
56 sal_uInt32 m_nSizeOfMemory;
57
58 public:
Memory()59 Memory()
60 :m_pMemory(NULL),
61 m_nSizeOfMemory(50 * 1024 * 1024)
62 {
63 }
64
65 // initialise your test code values here.
SetUp()66 void SetUp()
67 {
68 printf("allocate memory\n");
69 m_pMemory = (char*) rtl_allocateMemory( m_nSizeOfMemory );
70 }
71
TearDown()72 void TearDown()
73 {
74 printf("free memory\n");
75 rtl_freeMemory(m_pMemory);
76 m_pMemory = NULL;
77 }
78 }; // class test
79
TEST_F(Memory,rtl_allocateMemory_001)80 TEST_F(Memory, rtl_allocateMemory_001)
81 {
82 ASSERT_TRUE(m_pMemory != NULL) << "Can get zero memory.";
83 memset(m_pMemory, 1, m_nSizeOfMemory);
84 ASSERT_TRUE(checkMemory(m_pMemory, m_nSizeOfMemory, 1) == true) << "memory contains wrong value.";
85 }
86
TEST_F(Memory,rtl_reallocateMemory_001)87 TEST_F(Memory, rtl_reallocateMemory_001)
88 {
89 printf("reallocate memory\n");
90 sal_uInt32 nSize = 10 * 1024 * 1024;
91 m_pMemory = (char*)rtl_reallocateMemory(m_pMemory, nSize);
92
93 ASSERT_TRUE(m_pMemory != NULL) << "Can reallocate memory.";
94 memset(m_pMemory, 2, nSize);
95 ASSERT_TRUE(checkMemory(m_pMemory, nSize, 2) == true) << "memory contains wrong value.";
96 }
97
98 class ZeroMemory : public ::testing::Test
99 {
100 protected:
101 // for zero functions
102 char *m_pZeroMemory;
103 sal_uInt32 m_nSizeOfZeroMemory;
104
105 public:
ZeroMemory()106 ZeroMemory()
107 :m_pZeroMemory(NULL),
108 m_nSizeOfZeroMemory( 50 * 1024 * 1024 )
109 {
110 }
111
112 // initialise your test code values here.
SetUp()113 void SetUp()
114 {
115 printf("allocate zero memory\n");
116 m_pZeroMemory = (char*) rtl_allocateZeroMemory( m_nSizeOfZeroMemory );
117 }
118
TearDown()119 void TearDown()
120 {
121 printf("free zero memory\n");
122 rtl_freeZeroMemory(m_pZeroMemory, m_nSizeOfZeroMemory);
123 // LLA: no check possible, may GPF if there is something wrong.
124 // ASSERT_TRUE(pZeroMemory != NULL) << "Can get zero memory.";
125 }
126
127 }; // class test
128
TEST_F(ZeroMemory,rtl_allocateZeroMemory_001)129 TEST_F(ZeroMemory, rtl_allocateZeroMemory_001)
130 {
131 ASSERT_TRUE(m_pZeroMemory != NULL) << "Can get zero memory.";
132 ASSERT_TRUE(checkMemory(m_pZeroMemory, m_nSizeOfZeroMemory, 0) == true) << "memory contains wrong value.";
133
134 memset(m_pZeroMemory, 3, m_nSizeOfZeroMemory);
135 ASSERT_TRUE(checkMemory(m_pZeroMemory, m_nSizeOfZeroMemory, 3) == true) << "memory contains wrong value.";
136 }
137
138 } // namespace rtl_alloc
139
main(int argc,char ** argv)140 int main(int argc, char **argv)
141 {
142 ::testing::InitGoogleTest(&argc, argv);
143 return RUN_ALL_TESTS();
144 }
145