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>
41 class createPool
: public CppUnit::TestFixture
44 // initialise your test code values here.
53 // insert your test code here.
54 // this is only demonstration code
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
80 // initialise your test code values here.
89 // insert your test code here.
90 void destroyPool_000()
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
119 // initialise your test code values here.
128 // insert your test code here.
129 // this is only demonstration code
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
);
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
);
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();
183 int m_nDispensation
[256];
192 void clearDispensation()
194 for (int i
= 0;i
< 256;i
++) // clear array
196 m_nDispensation
[i
] = 0;
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
)
216 m_nAverage
= _nCountMax
/ 256;
218 m_nMinDeviation
= _nCountMax
;
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
]));
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
250 // initialise your test code values here.
259 // insert your test code here.
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
);
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
);
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
);
321 rtlRandomPool aPool
= rtl_random_createPool();
323 sal_uInt32 nBufLen
= 1;
324 sal_uInt8
*pBuffer
= new sal_uInt8
[ nBufLen
];
325 memset(pBuffer
, 0, nBufLen
);
329 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer
[0] == 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
);
345 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat
.getMaxDeviation() < aStat
.getAverage());
347 rtl_random_destroyPool(aPool
);
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
);
361 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer
[0] == 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
);
380 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat
.getMaxDeviation() < aStat
.getAverage());
382 rtl_random_destroyPool(aPool
);
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();
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.