1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtl_random.cxx,v $
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>
44 class createPool
: public CppUnit::TestFixture
47 // initialise your test code values here.
56 // insert your test code here.
57 // this is only demonstration code
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
83 // initialise your test code values here.
92 // insert your test code here.
93 void destroyPool_000()
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
122 // initialise your test code values here.
131 // insert your test code here.
132 // this is only demonstration code
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
);
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
);
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();
186 int m_nDispensation
[256];
195 void clearDispensation()
197 for (int i
= 0;i
< 256;i
++) // clear array
199 m_nDispensation
[i
] = 0;
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
)
219 m_nAverage
= _nCountMax
/ 256;
221 m_nMinDeviation
= _nCountMax
;
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
]));
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
253 // initialise your test code values here.
262 // insert your test code here.
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
);
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
);
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
);
324 rtlRandomPool aPool
= rtl_random_createPool();
326 sal_uInt32 nBufLen
= 1;
327 sal_uInt8
*pBuffer
= new sal_uInt8
[ nBufLen
];
328 memset(pBuffer
, 0, nBufLen
);
332 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer
[0] == 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
);
348 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat
.getMaxDeviation() < aStat
.getAverage());
350 rtl_random_destroyPool(aPool
);
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
);
364 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer
[0] == 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
);
383 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat
.getMaxDeviation() < aStat
.getAverage());
385 rtl_random_destroyPool(aPool
);
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();
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.