bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / rtl / random / rtl_random.cxx
blob752769fe782b00ee6396680d0ef23e6252ee2b62
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <algorithm>
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>
31 #include <string.h>
33 namespace rtl_random
36 class createPool : public CppUnit::TestFixture
38 public:
39 // initialise your test code values here.
40 void setUp()
44 void tearDown()
48 // insert your test code here.
49 // this is only demonstration code
50 void createPool_001()
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
73 public:
74 // initialise your test code values here.
75 void setUp()
79 void tearDown()
83 // insert your test code here.
84 void destroyPool_000()
86 // GPF, if failed
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
111 public:
112 // initialise your test code values here.
113 void setUp()
117 void tearDown()
121 // insert your test code here.
122 // this is only demonstration code
123 void addBytes_000()
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);
141 delete [] pBuffer;
145 void addBytes_001()
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);
159 delete [] pBuffer;
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();
170 }; // class addBytes
172 class Statistics
174 int m_nDispensation[256];
176 int m_nMin;
177 int m_nMax;
178 int m_nAverage;
179 int m_nMinDeviation;
180 int m_nMaxDeviation;
182 public:
183 void clearDispensation()
185 for (int i = 0;i < 256;++i) // clear array
187 m_nDispensation[i] = 0;
190 Statistics()
191 : m_nMin(0)
192 , m_nMax(0)
193 , m_nAverage(0)
194 , m_nMinDeviation(0)
195 , m_nMaxDeviation(0)
197 clearDispensation();
199 ~Statistics(){}
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)
209 m_nMin = _nCountMax;
210 m_nMax = 0;
212 m_nAverage = _nCountMax / 256;
214 m_nMinDeviation = _nCountMax;
215 m_nMaxDeviation = 0;
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]));
227 void print()
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
245 public:
246 // initialise your test code values here.
247 void setUp()
251 void tearDown()
255 // insert your test code here.
256 void getBytes_000()
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);
274 delete [] pBuffer;
277 void getBytes_001()
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);
291 delete [] pBuffer;
294 void getBytes_002()
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);
312 delete [] pBuffer;
315 void getBytes_003()
317 rtlRandomPool aPool = rtl_random_createPool();
319 sal_uInt32 nBufLen = 1;
320 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ];
321 memset(pBuffer, 0, nBufLen);
323 Statistics aStat;
325 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[0] == 0);
327 int nCount = 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);
339 aStat.print();
341 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage());
343 rtl_random_destroyPool(aPool);
344 delete [] pBuffer;
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);
355 Statistics aStat;
357 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[0] == 0);
359 int nCount = 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);
372 aStat.print();
374 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage());
376 rtl_random_destroyPool(aPool);
377 delete [] pBuffer;
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();
391 }; // class getBytes
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: */