1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <sal/types.h>
23 #include <cppunit/TestAssert.h>
24 #include <cppunit/TestFixture.h>
25 #include <cppunit/extensions/HelperMacros.h>
26 #include <cppunit/plugin/TestPlugIn.h>
28 #include <osl/diagnose.h>
29 #include <rtl/random.h>
36 class createPool
: public CppUnit::TestFixture
39 // initialise your test code values here.
48 // insert your test code here.
49 // this is only demonstration code
52 // this is demonstration code
54 rtlRandomPool aPool
= rtl_random_createPool();
56 // LLA: seems to be that an other test is not possible for createPool()
57 CPPUNIT_ASSERT_MESSAGE("create failed", aPool
!= NULL
);
59 rtl_random_destroyPool(aPool
);
62 // Change the following lines only, if you add, remove or rename
63 // member functions of the current class,
64 // because these macros are need by auto register mechanism.
66 CPPUNIT_TEST_SUITE(createPool
);
67 CPPUNIT_TEST(createPool_001
);
68 CPPUNIT_TEST_SUITE_END();
69 }; // class createPool
71 class destroyPool
: public CppUnit::TestFixture
74 // initialise your test code values here.
83 // insert your test code here.
84 void destroyPool_000()
87 rtl_random_destroyPool(NULL
);
90 void destroyPool_001()
92 rtlRandomPool aPool
= rtl_random_createPool();
94 // LLA: seems to be that an other test is not possible for createPool()
95 CPPUNIT_ASSERT_MESSAGE("create failed", aPool
!= NULL
);
97 rtl_random_destroyPool(aPool
);
99 // Change the following lines only, if you add, remove or rename
100 // member functions of the current class,
101 // because these macros are need by auto register mechanism.
103 CPPUNIT_TEST_SUITE(destroyPool
);
104 CPPUNIT_TEST(destroyPool_000
);
105 CPPUNIT_TEST(destroyPool_001
);
106 CPPUNIT_TEST_SUITE_END();
107 }; // class destroyPool
109 class addBytes
: public CppUnit::TestFixture
112 // initialise your test code values here.
121 // insert your test code here.
122 // this is only demonstration code
125 rtlRandomPool aPool
= rtl_random_createPool();
127 sal_uInt32 nBufLen
= 4;
128 sal_uInt8
*pBuffer
= new sal_uInt8
[ nBufLen
];
129 memset(pBuffer
, 0, nBufLen
);
131 rtlRandomError aError
= rtl_random_addBytes(NULL
, NULL
, 0);
132 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError
== rtl_Random_E_Argument
);
134 /* rtlRandomError */ aError
= rtl_random_addBytes(aPool
, NULL
, 0);
135 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError
== rtl_Random_E_Argument
);
137 /* rtlRandomError */ aError
= rtl_random_addBytes(aPool
, pBuffer
, nBufLen
);
138 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError
== rtl_Random_E_None
);
140 rtl_random_destroyPool(aPool
);
147 rtlRandomPool aPool
= rtl_random_createPool();
149 sal_uInt32 nBufLen
= 4;
150 sal_uInt8
*pBuffer
= new sal_uInt8
[ nBufLen
];
152 memset(pBuffer
, 0, nBufLen
);
154 rtl_random_addBytes(aPool
, pBuffer
, nBufLen
);
156 printf("%2x %2x %2x %2x\n", pBuffer
[0], pBuffer
[1], pBuffer
[2], pBuffer
[3]);
158 rtl_random_destroyPool(aPool
);
162 // Change the following lines only, if you add, remove or rename
163 // member functions of the current class,
164 // because these macros are need by auto register mechanism.
166 CPPUNIT_TEST_SUITE(addBytes
);
167 CPPUNIT_TEST(addBytes_000
);
168 CPPUNIT_TEST(addBytes_001
);
169 CPPUNIT_TEST_SUITE_END();
174 int m_nDispensation
[256];
183 void clearDispensation()
185 for (int i
= 0;i
< 256;++i
) // clear array
187 m_nDispensation
[i
] = 0;
201 void addValue(sal_Int16 _nIndex
, sal_Int32 _nValue
)
203 OSL_ASSERT(_nIndex
>= 0 && _nIndex
< 256);
204 m_nDispensation
[_nIndex
] += _nValue
;
207 void build(sal_Int32 _nCountMax
)
212 m_nAverage
= _nCountMax
/ 256;
214 m_nMinDeviation
= _nCountMax
;
217 for (int i
= 0;i
< 256;++i
) // show dispensation
219 m_nMin
= std::min(m_nMin
, m_nDispensation
[i
]);
220 m_nMax
= std::max(m_nMax
, m_nDispensation
[i
]);
222 m_nMinDeviation
= std::min(m_nMinDeviation
, abs(m_nAverage
- m_nDispensation
[i
]));
223 m_nMaxDeviation
= std::max(m_nMaxDeviation
, abs(m_nAverage
- m_nDispensation
[i
]));
229 // LLA: these are only info values
230 printf("\nSome statistics\n");
231 printf("Min: %d\n", m_nMin
);
232 printf("Max: %d\n", m_nMax
);
233 printf("Average: %d\n", m_nAverage
);
234 printf("Min abs deviation: %d\n", m_nMinDeviation
);
235 printf("Max abs deviation: %d\n", m_nMaxDeviation
);
238 sal_Int32
getAverage() const {return m_nAverage
;}
239 sal_Int32
getMaxDeviation() const {return m_nMaxDeviation
;}
243 class getBytes
: public CppUnit::TestFixture
246 // initialise your test code values here.
255 // insert your test code here.
258 rtlRandomPool aPool
= rtl_random_createPool();
260 sal_uInt32 nBufLen
= 4;
261 sal_uInt8
*pBuffer
= new sal_uInt8
[ nBufLen
];
262 memset(pBuffer
, 0, nBufLen
);
264 rtlRandomError aError
= rtl_random_getBytes(NULL
, NULL
, 0);
265 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError
== rtl_Random_E_Argument
);
267 /* rtlRandomError */ aError
= rtl_random_getBytes(aPool
, NULL
, 0);
268 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError
== rtl_Random_E_Argument
);
270 /* rtlRandomError */ aError
= rtl_random_getBytes(aPool
, pBuffer
, nBufLen
);
271 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError
== rtl_Random_E_None
);
273 rtl_random_destroyPool(aPool
);
279 rtlRandomPool aPool
= rtl_random_createPool();
281 sal_uInt32 nBufLen
= 4;
282 sal_uInt8
*pBuffer
= new sal_uInt8
[ nBufLen
];
283 memset(pBuffer
, 0, nBufLen
);
285 rtlRandomError aError
= rtl_random_getBytes(aPool
, pBuffer
, nBufLen
);
286 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError
== rtl_Random_E_None
);
288 printf("%2x %2x %2x %2x\n", pBuffer
[0], pBuffer
[1], pBuffer
[2], pBuffer
[3]);
290 rtl_random_destroyPool(aPool
);
296 rtlRandomPool aPool
= rtl_random_createPool();
298 sal_uInt32 nBufLen
= 4;
299 sal_uInt8
*pBuffer
= new sal_uInt8
[ nBufLen
<< 1 ];
300 memset(pBuffer
, 0, nBufLen
<< 1);
302 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer
[4] == 0 && pBuffer
[5] == 0 && pBuffer
[6] == 0 && pBuffer
[7] == 0);
304 rtlRandomError aError
= rtl_random_getBytes(aPool
, pBuffer
, nBufLen
);
305 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError
== rtl_Random_E_None
);
307 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]);
309 CPPUNIT_ASSERT_MESSAGE("internal memory overwrite", pBuffer
[4] == 0 && pBuffer
[5] == 0 && pBuffer
[6] == 0 && pBuffer
[7] == 0);
311 rtl_random_destroyPool(aPool
);
317 rtlRandomPool aPool
= rtl_random_createPool();
319 sal_uInt32 nBufLen
= 1;
320 sal_uInt8
*pBuffer
= new sal_uInt8
[ nBufLen
];
321 memset(pBuffer
, 0, nBufLen
);
325 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer
[0] == 0);
329 int nCountMax
= 1000000;
330 for(nCount
= 0;nCount
< nCountMax
; ++nCount
) // run 100000000 through getBytes(...)
332 /* rtlRandomError aError = */ rtl_random_getBytes(aPool
, pBuffer
, nBufLen
);
333 /* CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); */
335 aStat
.addValue(pBuffer
[0], 1);
338 aStat
.build(nCountMax
);
341 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat
.getMaxDeviation() < aStat
.getAverage());
343 rtl_random_destroyPool(aPool
);
347 void getBytes_003_1()
349 rtlRandomPool aPool
= rtl_random_createPool();
351 sal_uInt32 nBufLen
= 256;
352 sal_uInt8
*pBuffer
= new sal_uInt8
[ nBufLen
];
353 memset(pBuffer
, 0, nBufLen
);
357 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer
[0] == 0);
361 int nCountMax
= 10000;
362 for(nCount
= 0;nCount
< nCountMax
; ++nCount
) // run 100000000 through getBytes(...)
364 /* rtlRandomError aError = */ rtl_random_getBytes(aPool
, pBuffer
, nBufLen
);
365 // CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None);
367 for (sal_uInt32 i
=0;i
<nBufLen
;++i
)
368 aStat
.addValue(pBuffer
[i
], 1);
371 aStat
.build(nCountMax
* nBufLen
);
374 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat
.getMaxDeviation() < aStat
.getAverage());
376 rtl_random_destroyPool(aPool
);
380 // Change the following lines only, if you add, remove or rename
381 // member functions of the current class,
382 // because these macros are need by auto register mechanism.
384 CPPUNIT_TEST_SUITE(getBytes
);
385 CPPUNIT_TEST(getBytes_000
);
386 CPPUNIT_TEST(getBytes_001
);
387 CPPUNIT_TEST(getBytes_002
);
388 CPPUNIT_TEST(getBytes_003
);
389 CPPUNIT_TEST(getBytes_003_1
);
390 CPPUNIT_TEST_SUITE_END();
393 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_random::createPool
);
394 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_random::destroyPool
);
395 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_random::addBytes
);
396 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_random::getBytes
);
397 } // namespace rtl_random
399 CPPUNIT_PLUGIN_IMPLEMENT();
401 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */