update dev300-m57
[ooovba.git] / sal / qa / rtl / random / rtl_random.cxx
blob26cab8bf5b4c9ee265e5a10ccbf9a5cb8b6bb25e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtl_random.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sal.hxx"
34 // autogenerated file with codegen.pl
36 #include <algorithm> // STL
38 #include <cppunit/simpleheader.hxx>
39 #include <rtl/random.h>
41 namespace rtl_random
44 class createPool : public CppUnit::TestFixture
46 public:
47 // initialise your test code values here.
48 void setUp()
52 void tearDown()
56 // insert your test code here.
57 // this is only demonstration code
58 void createPool_001()
60 // this is demonstration code
62 rtlRandomPool aPool = rtl_random_createPool();
64 // LLA: seems to be that an other test is not possible for createPool()
65 CPPUNIT_ASSERT_MESSAGE("create failed", aPool != NULL);
67 rtl_random_destroyPool(aPool);
70 // Change the following lines only, if you add, remove or rename
71 // member functions of the current class,
72 // because these macros are need by auto register mechanism.
74 CPPUNIT_TEST_SUITE(createPool);
75 CPPUNIT_TEST(createPool_001);
76 CPPUNIT_TEST_SUITE_END();
77 }; // class createPool
80 class destroyPool : public CppUnit::TestFixture
82 public:
83 // initialise your test code values here.
84 void setUp()
88 void tearDown()
92 // insert your test code here.
93 void destroyPool_000()
95 // GPF, if failed
96 rtl_random_destroyPool(NULL);
99 void destroyPool_001()
101 rtlRandomPool aPool = rtl_random_createPool();
103 // LLA: seems to be that an other test is not possible for createPool()
104 CPPUNIT_ASSERT_MESSAGE("create failed", aPool != NULL);
106 rtl_random_destroyPool(aPool);
108 // Change the following lines only, if you add, remove or rename
109 // member functions of the current class,
110 // because these macros are need by auto register mechanism.
112 CPPUNIT_TEST_SUITE(destroyPool);
113 CPPUNIT_TEST(destroyPool_000);
114 CPPUNIT_TEST(destroyPool_001);
115 CPPUNIT_TEST_SUITE_END();
116 }; // class destroyPool
119 class addBytes : public CppUnit::TestFixture
121 public:
122 // initialise your test code values here.
123 void setUp()
127 void tearDown()
131 // insert your test code here.
132 // this is only demonstration code
133 void addBytes_000()
135 rtlRandomPool aPool = rtl_random_createPool();
137 sal_uInt32 nBufLen = 4;
138 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
139 memset(pBuffer, 0, nBufLen);
141 rtlRandomError aError = rtl_random_addBytes(NULL, NULL, 0);
142 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument);
144 /* rtlRandomError */ aError = rtl_random_addBytes(aPool, NULL, 0);
145 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument);
147 /* rtlRandomError */ aError = rtl_random_addBytes(aPool, pBuffer, nBufLen);
148 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None);
150 rtl_random_destroyPool(aPool);
151 delete [] pBuffer;
155 void addBytes_001()
157 rtlRandomPool aPool = rtl_random_createPool();
159 sal_uInt32 nBufLen = 4;
160 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
162 memset(pBuffer, 0, nBufLen);
164 rtl_random_addBytes(aPool, pBuffer, nBufLen);
166 t_print("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]);
168 rtl_random_destroyPool(aPool);
169 delete [] pBuffer;
173 // Change the following lines only, if you add, remove or rename
174 // member functions of the current class,
175 // because these macros are need by auto register mechanism.
177 CPPUNIT_TEST_SUITE(addBytes);
178 CPPUNIT_TEST(addBytes_000);
179 CPPUNIT_TEST(addBytes_001);
180 CPPUNIT_TEST_SUITE_END();
181 }; // class addBytes
184 class Statistics
186 int m_nDispensation[256];
188 int m_nMin;
189 int m_nMax;
190 int m_nAverage;
191 int m_nMinDeviation;
192 int m_nMaxDeviation;
194 public:
195 void clearDispensation()
197 for (int i = 0;i < 256;i ++) // clear array
199 m_nDispensation[i] = 0;
202 Statistics()
204 clearDispensation();
206 ~Statistics(){}
208 void addValue(sal_Int16 _nIndex, sal_Int32 _nValue)
210 OSL_ASSERT(_nIndex >= 0 && _nIndex < 256);
211 m_nDispensation[_nIndex] += _nValue;
214 void build(sal_Int32 _nCountMax)
216 m_nMin = _nCountMax;
217 m_nMax = 0;
219 m_nAverage = _nCountMax / 256;
221 m_nMinDeviation = _nCountMax;
222 m_nMaxDeviation = 0;
224 for (int i = 0;i < 256;i ++) // show dispensation
226 m_nMin = std::min(m_nMin, m_nDispensation[i]);
227 m_nMax = std::max(m_nMax, m_nDispensation[i]);
229 m_nMinDeviation = std::min(m_nMinDeviation, abs(m_nAverage - m_nDispensation[i]));
230 m_nMaxDeviation = std::max(m_nMaxDeviation, abs(m_nAverage - m_nDispensation[i]));
234 void print()
236 // LLA: these are only info values
237 t_print("\nSome statistics\n");
238 t_print("Min: %d\n", m_nMin);
239 t_print("Max: %d\n", m_nMax);
240 t_print("Average: %d\n", m_nAverage);
241 t_print("Min abs deviation: %d\n", m_nMinDeviation);
242 t_print("Max abs deviation: %d\n", m_nMaxDeviation);
245 sal_Int32 getAverage() {return m_nAverage;}
246 sal_Int32 getMaxDeviation() {return m_nMaxDeviation;}
250 class getBytes : public CppUnit::TestFixture
252 public:
253 // initialise your test code values here.
254 void setUp()
258 void tearDown()
262 // insert your test code here.
263 void getBytes_000()
265 rtlRandomPool aPool = rtl_random_createPool();
267 sal_uInt32 nBufLen = 4;
268 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
269 memset(pBuffer, 0, nBufLen);
271 rtlRandomError aError = rtl_random_getBytes(NULL, NULL, 0);
272 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument);
274 /* rtlRandomError */ aError = rtl_random_getBytes(aPool, NULL, 0);
275 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument);
277 /* rtlRandomError */ aError = rtl_random_getBytes(aPool, pBuffer, nBufLen);
278 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None);
280 rtl_random_destroyPool(aPool);
281 delete [] pBuffer;
284 void getBytes_001()
286 rtlRandomPool aPool = rtl_random_createPool();
288 sal_uInt32 nBufLen = 4;
289 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
290 memset(pBuffer, 0, nBufLen);
292 rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen);
293 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None);
295 t_print("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]);
297 rtl_random_destroyPool(aPool);
298 delete [] pBuffer;
301 void getBytes_002()
303 rtlRandomPool aPool = rtl_random_createPool();
305 sal_uInt32 nBufLen = 4;
306 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen << 1 ];
307 memset(pBuffer, 0, nBufLen << 1);
309 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0);
311 rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen);
312 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None);
314 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]);
316 CPPUNIT_ASSERT_MESSAGE("internal memory overwrite", pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0);
318 rtl_random_destroyPool(aPool);
319 delete [] pBuffer;
322 void getBytes_003()
324 rtlRandomPool aPool = rtl_random_createPool();
326 sal_uInt32 nBufLen = 1;
327 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
328 memset(pBuffer, 0, nBufLen);
330 Statistics aStat;
332 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[0] == 0);
334 int nCount = 0;
336 int nCountMax = 1000000;
337 for(nCount = 0;nCount < nCountMax; nCount ++) // run 100000000 through getBytes(...)
339 /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen);
340 /* CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); */
342 aStat.addValue(pBuffer[0], 1);
345 aStat.build(nCountMax);
346 aStat.print();
348 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage());
350 rtl_random_destroyPool(aPool);
351 delete [] pBuffer;
354 void getBytes_003_1()
356 rtlRandomPool aPool = rtl_random_createPool();
358 sal_uInt32 nBufLen = 256;
359 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
360 memset(pBuffer, 0, nBufLen);
362 Statistics aStat;
364 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[0] == 0);
366 int nCount = 0;
368 int nCountMax = 10000;
369 for(nCount = 0;nCount < nCountMax; nCount ++) // run 100000000 through getBytes(...)
371 /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen);
372 // CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None);
374 for (sal_uInt32 i=0;i<nBufLen;i++)
376 aStat.addValue(pBuffer[i], 1);
380 aStat.build(nCountMax * nBufLen);
381 aStat.print();
383 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage());
385 rtl_random_destroyPool(aPool);
386 delete [] pBuffer;
389 // Change the following lines only, if you add, remove or rename
390 // member functions of the current class,
391 // because these macros are need by auto register mechanism.
393 CPPUNIT_TEST_SUITE(getBytes);
394 CPPUNIT_TEST(getBytes_000);
395 CPPUNIT_TEST(getBytes_001);
396 CPPUNIT_TEST(getBytes_002);
397 CPPUNIT_TEST(getBytes_003);
398 CPPUNIT_TEST(getBytes_003_1);
399 CPPUNIT_TEST_SUITE_END();
400 }; // class getBytes
402 // -----------------------------------------------------------------------------
403 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_random::createPool, "rtl_random");
404 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_random::destroyPool, "rtl_random");
405 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_random::addBytes, "rtl_random");
406 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_random::getBytes, "rtl_random");
407 } // namespace rtl_random
410 // -----------------------------------------------------------------------------
412 // this macro creates an empty function, which will called by the RegisterAllFunctions()
413 // to let the user the possibility to also register some functions by hand.
414 NOADDITIONAL;