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 <algorithm> // STL
30
31 #include "gtest/gtest.h"
32 #include <rtl/random.h>
33
34 namespace rtl_random
35 {
36
37 class createPool : public ::testing::Test
38 {
39 public:
40 // initialise your test code values here.
SetUp()41 void SetUp()
42 {
43 }
44
TearDown()45 void TearDown()
46 {
47 }
48 }; // class createPool
49
TEST_F(createPool,createPool_001)50 TEST_F(createPool, createPool_001)
51 {
52 // this is demonstration code
53
54 rtlRandomPool aPool = rtl_random_createPool();
55
56 // LLA: seems to be that an other test is not possible for createPool()
57 ASSERT_TRUE(aPool != NULL) << "create failed";
58 rtl_random_destroyPool(aPool);
59 }
60
61 class destroyPool : public ::testing::Test
62 {
63 public:
64 // initialise your test code values here.
SetUp()65 void SetUp()
66 {
67 }
68
TearDown()69 void TearDown()
70 {
71 }
72 }; // class destroyPool
73
TEST_F(destroyPool,destroyPool_000)74 TEST_F(destroyPool, destroyPool_000)
75 {
76 // GPF, if failed
77 rtl_random_destroyPool(NULL);
78 }
79
TEST_F(destroyPool,destroyPool_001)80 TEST_F(destroyPool, destroyPool_001)
81 {
82 rtlRandomPool aPool = rtl_random_createPool();
83
84 // LLA: seems to be that an other test is not possible for createPool()
85 ASSERT_TRUE(aPool != NULL) << "create failed";
86
87 rtl_random_destroyPool(aPool);
88 }
89
90 class addBytes : public ::testing::Test
91 {
92 public:
93 // initialise your test code values here.
SetUp()94 void SetUp()
95 {
96 }
97
TearDown()98 void TearDown()
99 {
100 }
101 }; // class addBytes
102
TEST_F(addBytes,addBytes_000)103 TEST_F(addBytes, addBytes_000)
104 {
105 rtlRandomPool aPool = rtl_random_createPool();
106
107 sal_uInt32 nBufLen = 4;
108 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
109 memset(pBuffer, 0, nBufLen);
110
111 rtlRandomError aError = rtl_random_addBytes(NULL, NULL, 0);
112 ASSERT_TRUE(aError == rtl_Random_E_Argument) << "wrong parameter";
113
114 /* rtlRandomError */ aError = rtl_random_addBytes(aPool, NULL, 0);
115 ASSERT_TRUE(aError == rtl_Random_E_Argument) << "wrong parameter";
116
117 /* rtlRandomError */ aError = rtl_random_addBytes(aPool, pBuffer, nBufLen);
118 ASSERT_TRUE(aError == rtl_Random_E_None) << "wrong parameter";
119
120 rtl_random_destroyPool(aPool);
121 delete [] pBuffer;
122
123 }
124
TEST_F(addBytes,addBytes_001)125 TEST_F(addBytes, addBytes_001)
126 {
127 rtlRandomPool aPool = rtl_random_createPool();
128
129 sal_uInt32 nBufLen = 4;
130 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
131
132 memset(pBuffer, 0, nBufLen);
133
134 rtl_random_addBytes(aPool, pBuffer, nBufLen);
135
136 printf("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]);
137
138 rtl_random_destroyPool(aPool);
139 delete [] pBuffer;
140 }
141
142 class Statistics
143 {
144 protected:
145 int m_nDispensation[256];
146
147 int m_nMin;
148 int m_nMax;
149 int m_nAverage;
150 int m_nMinDeviation;
151 int m_nMaxDeviation;
152
153 public:
clearDispensation()154 void clearDispensation()
155 {
156 for (int i = 0;i < 256;i ++) // clear array
157 {
158 m_nDispensation[i] = 0;
159 }
160 }
Statistics()161 Statistics()
162 {
163 clearDispensation();
164 }
~Statistics()165 ~Statistics(){}
166
addValue(sal_Int16 _nIndex,sal_Int32 _nValue)167 void addValue(sal_Int16 _nIndex, sal_Int32 _nValue)
168 {
169 ASSERT_TRUE(_nIndex >= 0 && _nIndex < 256);
170 m_nDispensation[_nIndex] += _nValue;
171 }
172
build(sal_Int32 _nCountMax)173 void build(sal_Int32 _nCountMax)
174 {
175 m_nMin = _nCountMax;
176 m_nMax = 0;
177
178 m_nAverage = _nCountMax / 256;
179
180 m_nMinDeviation = _nCountMax;
181 m_nMaxDeviation = 0;
182
183 for (int i = 0;i < 256;i ++) // show dispensation
184 {
185 m_nMin = std::min(m_nMin, m_nDispensation[i]);
186 m_nMax = std::max(m_nMax, m_nDispensation[i]);
187
188 m_nMinDeviation = std::min(m_nMinDeviation, abs(m_nAverage - m_nDispensation[i]));
189 m_nMaxDeviation = std::max(m_nMaxDeviation, abs(m_nAverage - m_nDispensation[i]));
190 }
191 }
192
print()193 void print()
194 {
195 // LLA: these are only info values
196 printf("\nSome statistics\n");
197 printf("Min: %d\n", m_nMin);
198 printf("Max: %d\n", m_nMax);
199 printf("Average: %d\n", m_nAverage);
200 printf("Min abs deviation: %d\n", m_nMinDeviation);
201 printf("Max abs deviation: %d\n", m_nMaxDeviation);
202 }
203
getAverage()204 sal_Int32 getAverage() {return m_nAverage;}
getMaxDeviation()205 sal_Int32 getMaxDeviation() {return m_nMaxDeviation;}
206
207 };
208
209 class getBytes : public ::testing::Test
210 {
211 public:
212 // initialise your test code values here.
SetUp()213 void SetUp()
214 {
215 }
216
TearDown()217 void TearDown()
218 {
219 }
220 }; // class getBytes
221
TEST_F(getBytes,getBytes_000)222 TEST_F(getBytes, getBytes_000)
223 {
224 rtlRandomPool aPool = rtl_random_createPool();
225
226 sal_uInt32 nBufLen = 4;
227 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
228 memset(pBuffer, 0, nBufLen);
229
230 rtlRandomError aError = rtl_random_getBytes(NULL, NULL, 0);
231 ASSERT_TRUE(aError == rtl_Random_E_Argument) << "wrong parameter";
232
233 /* rtlRandomError */ aError = rtl_random_getBytes(aPool, NULL, 0);
234 ASSERT_TRUE(aError == rtl_Random_E_Argument) << "wrong parameter";
235
236 /* rtlRandomError */ aError = rtl_random_getBytes(aPool, pBuffer, nBufLen);
237 ASSERT_TRUE(aError == rtl_Random_E_None) << "wrong parameter";
238
239 rtl_random_destroyPool(aPool);
240 delete [] pBuffer;
241 }
242
TEST_F(getBytes,getBytes_001)243 TEST_F(getBytes, getBytes_001)
244 {
245 rtlRandomPool aPool = rtl_random_createPool();
246
247 sal_uInt32 nBufLen = 4;
248 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
249 memset(pBuffer, 0, nBufLen);
250
251 rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen);
252 ASSERT_TRUE(aError == rtl_Random_E_None) << "wrong parameter";
253
254 printf("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]);
255
256 rtl_random_destroyPool(aPool);
257 delete [] pBuffer;
258 }
259
TEST_F(getBytes,getBytes_002)260 TEST_F(getBytes, getBytes_002)
261 {
262 rtlRandomPool aPool = rtl_random_createPool();
263
264 sal_uInt32 nBufLen = 4;
265 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen << 1 ];
266 memset(pBuffer, 0, nBufLen << 1);
267
268 ASSERT_TRUE(pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0) << "memset failed";
269
270 rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen);
271 ASSERT_TRUE(aError == rtl_Random_E_None) << "wrong parameter";
272
273 printf("%2x %2x %2x %2x %2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3], pBuffer[4], pBuffer[5], pBuffer[6], pBuffer[7]);
274
275 ASSERT_TRUE(pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0) << "internal memory overwrite";
276
277 rtl_random_destroyPool(aPool);
278 delete [] pBuffer;
279 }
280
TEST_F(getBytes,getBytes_003)281 TEST_F(getBytes, getBytes_003)
282 {
283 rtlRandomPool aPool = rtl_random_createPool();
284
285 sal_uInt32 nBufLen = 1;
286 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
287 memset(pBuffer, 0, nBufLen);
288
289 Statistics aStat;
290
291 ASSERT_TRUE(pBuffer[0] == 0) << "memset failed";
292
293 int nCount = 0;
294
295 int nCountMax = 1000000;
296 for(nCount = 0;nCount < nCountMax; nCount ++) // run 100000000 through getBytes(...)
297 {
298 /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen);
299 /* ASSERT_TRUE(aError == rtl_Random_E_None); << "wrong parameter" */
300
301 aStat.addValue(pBuffer[0], 1);
302 }
303
304 aStat.build(nCountMax);
305 aStat.print();
306
307 ASSERT_TRUE(aStat.getMaxDeviation() < aStat.getAverage()) << "deviation should be less average";
308
309 rtl_random_destroyPool(aPool);
310 delete [] pBuffer;
311 }
312
TEST_F(getBytes,getBytes_003_1)313 TEST_F(getBytes, getBytes_003_1)
314 {
315 rtlRandomPool aPool = rtl_random_createPool();
316
317 sal_uInt32 nBufLen = 256;
318 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
319 memset(pBuffer, 0, nBufLen);
320
321 Statistics aStat;
322
323 ASSERT_TRUE(pBuffer[0] == 0) << "memset failed";
324
325 int nCount = 0;
326
327 int nCountMax = 10000;
328 for(nCount = 0;nCount < nCountMax; nCount ++) // run 100000000 through getBytes(...)
329 {
330 /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen);
331 // ASSERT_TRUE(aError == rtl_Random_E_None) << "wrong parameter"";
332
333 for (sal_uInt32 i=0;i<nBufLen;i++)
334 {
335 aStat.addValue(pBuffer[i], 1);
336 }
337 }
338
339 aStat.build(nCountMax * nBufLen);
340 aStat.print();
341
342 ASSERT_TRUE(aStat.getMaxDeviation() < aStat.getAverage()) << "deviation should be less average";
343
344 rtl_random_destroyPool(aPool);
345 delete [] pBuffer;
346 }
347
348 // -----------------------------------------------------------------------------
349 } // namespace rtl_random
350
main(int argc,char ** argv)351 int main(int argc, char **argv)
352 {
353 ::testing::InitGoogleTest(&argc, argv);
354 return RUN_ALL_TESTS();
355 }
356