merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / qa / rtl / random / rtl_random.cxx
blob47ff63e480c4a34a6f11b66d8a4eca9d396a1702
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_sal.hxx"
31 // autogenerated file with codegen.pl
33 #include <algorithm> // STL
35 #include <testshl/simpleheader.hxx>
36 #include <rtl/random.h>
38 namespace rtl_random
41 class createPool : public CppUnit::TestFixture
43 public:
44 // initialise your test code values here.
45 void setUp()
49 void tearDown()
53 // insert your test code here.
54 // this is only demonstration code
55 void createPool_001()
57 // this is demonstration code
59 rtlRandomPool aPool = rtl_random_createPool();
61 // LLA: seems to be that an other test is not possible for createPool()
62 CPPUNIT_ASSERT_MESSAGE("create failed", aPool != NULL);
64 rtl_random_destroyPool(aPool);
67 // Change the following lines only, if you add, remove or rename
68 // member functions of the current class,
69 // because these macros are need by auto register mechanism.
71 CPPUNIT_TEST_SUITE(createPool);
72 CPPUNIT_TEST(createPool_001);
73 CPPUNIT_TEST_SUITE_END();
74 }; // class createPool
77 class destroyPool : public CppUnit::TestFixture
79 public:
80 // initialise your test code values here.
81 void setUp()
85 void tearDown()
89 // insert your test code here.
90 void destroyPool_000()
92 // GPF, if failed
93 rtl_random_destroyPool(NULL);
96 void destroyPool_001()
98 rtlRandomPool aPool = rtl_random_createPool();
100 // LLA: seems to be that an other test is not possible for createPool()
101 CPPUNIT_ASSERT_MESSAGE("create failed", aPool != NULL);
103 rtl_random_destroyPool(aPool);
105 // Change the following lines only, if you add, remove or rename
106 // member functions of the current class,
107 // because these macros are need by auto register mechanism.
109 CPPUNIT_TEST_SUITE(destroyPool);
110 CPPUNIT_TEST(destroyPool_000);
111 CPPUNIT_TEST(destroyPool_001);
112 CPPUNIT_TEST_SUITE_END();
113 }; // class destroyPool
116 class addBytes : public CppUnit::TestFixture
118 public:
119 // initialise your test code values here.
120 void setUp()
124 void tearDown()
128 // insert your test code here.
129 // this is only demonstration code
130 void addBytes_000()
132 rtlRandomPool aPool = rtl_random_createPool();
134 sal_uInt32 nBufLen = 4;
135 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
136 memset(pBuffer, 0, nBufLen);
138 rtlRandomError aError = rtl_random_addBytes(NULL, NULL, 0);
139 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument);
141 /* rtlRandomError */ aError = rtl_random_addBytes(aPool, NULL, 0);
142 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument);
144 /* rtlRandomError */ aError = rtl_random_addBytes(aPool, pBuffer, nBufLen);
145 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None);
147 rtl_random_destroyPool(aPool);
148 delete [] pBuffer;
152 void addBytes_001()
154 rtlRandomPool aPool = rtl_random_createPool();
156 sal_uInt32 nBufLen = 4;
157 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
159 memset(pBuffer, 0, nBufLen);
161 rtl_random_addBytes(aPool, pBuffer, nBufLen);
163 t_print("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]);
165 rtl_random_destroyPool(aPool);
166 delete [] pBuffer;
170 // Change the following lines only, if you add, remove or rename
171 // member functions of the current class,
172 // because these macros are need by auto register mechanism.
174 CPPUNIT_TEST_SUITE(addBytes);
175 CPPUNIT_TEST(addBytes_000);
176 CPPUNIT_TEST(addBytes_001);
177 CPPUNIT_TEST_SUITE_END();
178 }; // class addBytes
181 class Statistics
183 int m_nDispensation[256];
185 int m_nMin;
186 int m_nMax;
187 int m_nAverage;
188 int m_nMinDeviation;
189 int m_nMaxDeviation;
191 public:
192 void clearDispensation()
194 for (int i = 0;i < 256;i ++) // clear array
196 m_nDispensation[i] = 0;
199 Statistics()
201 clearDispensation();
203 ~Statistics(){}
205 void addValue(sal_Int16 _nIndex, sal_Int32 _nValue)
207 OSL_ASSERT(_nIndex >= 0 && _nIndex < 256);
208 m_nDispensation[_nIndex] += _nValue;
211 void build(sal_Int32 _nCountMax)
213 m_nMin = _nCountMax;
214 m_nMax = 0;
216 m_nAverage = _nCountMax / 256;
218 m_nMinDeviation = _nCountMax;
219 m_nMaxDeviation = 0;
221 for (int i = 0;i < 256;i ++) // show dispensation
223 m_nMin = std::min(m_nMin, m_nDispensation[i]);
224 m_nMax = std::max(m_nMax, m_nDispensation[i]);
226 m_nMinDeviation = std::min(m_nMinDeviation, abs(m_nAverage - m_nDispensation[i]));
227 m_nMaxDeviation = std::max(m_nMaxDeviation, abs(m_nAverage - m_nDispensation[i]));
231 void print()
233 // LLA: these are only info values
234 t_print("\nSome statistics\n");
235 t_print("Min: %d\n", m_nMin);
236 t_print("Max: %d\n", m_nMax);
237 t_print("Average: %d\n", m_nAverage);
238 t_print("Min abs deviation: %d\n", m_nMinDeviation);
239 t_print("Max abs deviation: %d\n", m_nMaxDeviation);
242 sal_Int32 getAverage() {return m_nAverage;}
243 sal_Int32 getMaxDeviation() {return m_nMaxDeviation;}
247 class getBytes : public CppUnit::TestFixture
249 public:
250 // initialise your test code values here.
251 void setUp()
255 void tearDown()
259 // insert your test code here.
260 void getBytes_000()
262 rtlRandomPool aPool = rtl_random_createPool();
264 sal_uInt32 nBufLen = 4;
265 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
266 memset(pBuffer, 0, nBufLen);
268 rtlRandomError aError = rtl_random_getBytes(NULL, NULL, 0);
269 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument);
271 /* rtlRandomError */ aError = rtl_random_getBytes(aPool, NULL, 0);
272 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument);
274 /* rtlRandomError */ aError = rtl_random_getBytes(aPool, pBuffer, nBufLen);
275 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None);
277 rtl_random_destroyPool(aPool);
278 delete [] pBuffer;
281 void getBytes_001()
283 rtlRandomPool aPool = rtl_random_createPool();
285 sal_uInt32 nBufLen = 4;
286 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
287 memset(pBuffer, 0, nBufLen);
289 rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen);
290 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None);
292 t_print("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]);
294 rtl_random_destroyPool(aPool);
295 delete [] pBuffer;
298 void getBytes_002()
300 rtlRandomPool aPool = rtl_random_createPool();
302 sal_uInt32 nBufLen = 4;
303 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen << 1 ];
304 memset(pBuffer, 0, nBufLen << 1);
306 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0);
308 rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen);
309 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None);
311 t_print("%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]);
313 CPPUNIT_ASSERT_MESSAGE("internal memory overwrite", pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0);
315 rtl_random_destroyPool(aPool);
316 delete [] pBuffer;
319 void getBytes_003()
321 rtlRandomPool aPool = rtl_random_createPool();
323 sal_uInt32 nBufLen = 1;
324 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
325 memset(pBuffer, 0, nBufLen);
327 Statistics aStat;
329 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[0] == 0);
331 int nCount = 0;
333 int nCountMax = 1000000;
334 for(nCount = 0;nCount < nCountMax; nCount ++) // run 100000000 through getBytes(...)
336 /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen);
337 /* CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); */
339 aStat.addValue(pBuffer[0], 1);
342 aStat.build(nCountMax);
343 aStat.print();
345 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage());
347 rtl_random_destroyPool(aPool);
348 delete [] pBuffer;
351 void getBytes_003_1()
353 rtlRandomPool aPool = rtl_random_createPool();
355 sal_uInt32 nBufLen = 256;
356 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
357 memset(pBuffer, 0, nBufLen);
359 Statistics aStat;
361 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[0] == 0);
363 int nCount = 0;
365 int nCountMax = 10000;
366 for(nCount = 0;nCount < nCountMax; nCount ++) // run 100000000 through getBytes(...)
368 /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen);
369 // CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None);
371 for (sal_uInt32 i=0;i<nBufLen;i++)
373 aStat.addValue(pBuffer[i], 1);
377 aStat.build(nCountMax * nBufLen);
378 aStat.print();
380 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage());
382 rtl_random_destroyPool(aPool);
383 delete [] pBuffer;
386 // Change the following lines only, if you add, remove or rename
387 // member functions of the current class,
388 // because these macros are need by auto register mechanism.
390 CPPUNIT_TEST_SUITE(getBytes);
391 CPPUNIT_TEST(getBytes_000);
392 CPPUNIT_TEST(getBytes_001);
393 CPPUNIT_TEST(getBytes_002);
394 CPPUNIT_TEST(getBytes_003);
395 CPPUNIT_TEST(getBytes_003_1);
396 CPPUNIT_TEST_SUITE_END();
397 }; // class getBytes
399 // -----------------------------------------------------------------------------
400 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_random::createPool, "rtl_random");
401 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_random::destroyPool, "rtl_random");
402 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_random::addBytes, "rtl_random");
403 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_random::getBytes, "rtl_random");
404 } // namespace rtl_random
407 // -----------------------------------------------------------------------------
409 // this macro creates an empty function, which will called by the RegisterAllFunctions()
410 // to let the user the possibility to also register some functions by hand.
411 NOADDITIONAL;