Bump version to 6.4-15
[LibreOffice.git] / sal / qa / rtl / random / rtl_random.cxx
blob1e0eb625d5f584781c4107d2227375ba1f939e48
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 <memory>
21 #include <algorithm>
23 #include <sal/types.h>
24 #include <cppunit/TestAssert.h>
25 #include <cppunit/TestFixture.h>
26 #include <cppunit/extensions/HelperMacros.h>
27 #include <cppunit/plugin/TestPlugIn.h>
29 #include <rtl/random.h>
31 #include <string.h>
33 namespace rtl_random
36 class createPool : public CppUnit::TestFixture
38 public:
39 // insert your test code here.
40 // this is only demonstration code
41 void createPool_001()
43 // this is demonstration code
45 rtlRandomPool aPool = rtl_random_createPool();
47 // LLA: seems to be that another test is not possible for createPool()
48 CPPUNIT_ASSERT_MESSAGE("create failed", aPool != nullptr);
50 rtl_random_destroyPool(aPool);
53 // Change the following lines only, if you add, remove or rename
54 // member functions of the current class,
55 // because these macros are need by auto register mechanism.
57 CPPUNIT_TEST_SUITE(createPool);
58 CPPUNIT_TEST(createPool_001);
59 CPPUNIT_TEST_SUITE_END();
60 }; // class createPool
62 class destroyPool : public CppUnit::TestFixture
64 public:
65 // insert your test code here.
66 void destroyPool_000()
68 // GPF, if failed
69 rtl_random_destroyPool(nullptr);
72 void destroyPool_001()
74 rtlRandomPool aPool = rtl_random_createPool();
76 // LLA: seems to be that another test is not possible for createPool()
77 CPPUNIT_ASSERT_MESSAGE("create failed", aPool != nullptr);
79 rtl_random_destroyPool(aPool);
81 // Change the following lines only, if you add, remove or rename
82 // member functions of the current class,
83 // because these macros are need by auto register mechanism.
85 CPPUNIT_TEST_SUITE(destroyPool);
86 CPPUNIT_TEST(destroyPool_000);
87 CPPUNIT_TEST(destroyPool_001);
88 CPPUNIT_TEST_SUITE_END();
89 }; // class destroyPool
91 class addBytes : public CppUnit::TestFixture
93 public:
94 // insert your test code here.
95 // this is only demonstration code
96 void addBytes_000()
98 rtlRandomPool aPool = rtl_random_createPool();
100 sal_uInt32 nBufLen = 4;
101 std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen ] );
102 memset(pBuffer.get(), 0, nBufLen);
104 rtlRandomError aError = rtl_random_addBytes(nullptr, nullptr, 0);
105 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_Argument, aError);
107 /* rtlRandomError */ aError = rtl_random_addBytes(aPool, nullptr, 0);
108 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_Argument, aError);
110 /* rtlRandomError */ aError = rtl_random_addBytes(aPool, pBuffer.get(), nBufLen);
111 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_None, aError);
113 rtl_random_destroyPool(aPool);
116 void addBytes_001()
118 rtlRandomPool aPool = rtl_random_createPool();
120 sal_uInt32 nBufLen = 4;
121 std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen ] );
123 memset(pBuffer.get(), 0, nBufLen);
125 rtl_random_addBytes(aPool, pBuffer.get(), nBufLen);
127 printf("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]);
129 rtl_random_destroyPool(aPool);
132 // Change the following lines only, if you add, remove or rename
133 // member functions of the current class,
134 // because these macros are need by auto register mechanism.
136 CPPUNIT_TEST_SUITE(addBytes);
137 CPPUNIT_TEST(addBytes_000);
138 CPPUNIT_TEST(addBytes_001);
139 CPPUNIT_TEST_SUITE_END();
140 }; // class addBytes
142 class Statistics
144 int m_nDispensation[256];
146 int m_nMin;
147 int m_nMax;
148 int m_nAverage;
149 int m_nMinDeviation;
150 int m_nMaxDeviation;
152 public:
153 void clearDispensation()
155 for (int i = 0;i < 256;++i) // clear array
157 m_nDispensation[i] = 0;
160 Statistics()
161 : m_nMin(0)
162 , m_nMax(0)
163 , m_nAverage(0)
164 , m_nMinDeviation(0)
165 , m_nMaxDeviation(0)
167 clearDispensation();
170 void addValue(sal_uInt8 _nIndex, sal_Int32 _nValue)
172 m_nDispensation[_nIndex] += _nValue;
175 void build(sal_Int32 _nCountMax)
177 m_nMin = _nCountMax;
178 m_nMax = 0;
180 m_nAverage = _nCountMax / 256;
182 m_nMinDeviation = _nCountMax;
183 m_nMaxDeviation = 0;
185 for (int i = 0;i < 256;++i) // show dispensation
187 m_nMin = std::min(m_nMin, m_nDispensation[i]);
188 m_nMax = std::max(m_nMax, m_nDispensation[i]);
190 m_nMinDeviation = std::min(m_nMinDeviation, abs(m_nAverage - m_nDispensation[i]));
191 m_nMaxDeviation = std::max(m_nMaxDeviation, abs(m_nAverage - m_nDispensation[i]));
195 void print()
197 // LLA: these are only info values
198 printf("\nSome statistics\n");
199 printf("Min: %d\n", m_nMin);
200 printf("Max: %d\n", m_nMax);
201 printf("Average: %d\n", m_nAverage);
202 printf("Min abs deviation: %d\n", m_nMinDeviation);
203 printf("Max abs deviation: %d\n", m_nMaxDeviation);
206 sal_Int32 getAverage() const {return m_nAverage;}
207 sal_Int32 getMaxDeviation() const {return m_nMaxDeviation;}
211 class getBytes : public CppUnit::TestFixture
213 public:
214 // insert your test code here.
215 void getBytes_000()
217 rtlRandomPool aPool = rtl_random_createPool();
219 sal_uInt32 nBufLen = 4;
220 std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen ] );
221 memset(pBuffer.get(), 0, nBufLen);
223 rtlRandomError aError = rtl_random_getBytes(nullptr, nullptr, 0);
224 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_Argument, aError);
226 /* rtlRandomError */ aError = rtl_random_getBytes(aPool, nullptr, 0);
227 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_Argument, aError);
229 /* rtlRandomError */ aError = rtl_random_getBytes(aPool, pBuffer.get(), nBufLen);
230 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_None, aError);
232 rtl_random_destroyPool(aPool);
235 void getBytes_001()
237 rtlRandomPool aPool = rtl_random_createPool();
239 sal_uInt32 nBufLen = 4;
240 std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen ] );
241 memset(pBuffer.get(), 0, nBufLen);
243 rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer.get(), nBufLen);
244 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_None, aError);
246 printf("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]);
248 rtl_random_destroyPool(aPool);
251 void getBytes_002()
253 rtlRandomPool aPool = rtl_random_createPool();
255 sal_uInt32 nBufLen = 4;
256 std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen << 1 ] );
257 memset(pBuffer.get(), 0, nBufLen << 1);
259 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0);
261 rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer.get(), nBufLen);
262 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_None, aError);
264 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]);
266 CPPUNIT_ASSERT_MESSAGE("internal memory overwrite", pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0);
268 rtl_random_destroyPool(aPool);
271 void getBytes_003()
273 rtlRandomPool aPool = rtl_random_createPool();
275 sal_uInt32 nBufLen = 1;
276 std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen ] );
277 memset(pBuffer.get(), 0, nBufLen);
279 Statistics aStat;
281 CPPUNIT_ASSERT_EQUAL_MESSAGE("memset failed", static_cast<sal_uInt8>(0), pBuffer[0]);
283 int nCount = 0;
285 int nCountMax = 1000000;
286 for(nCount = 0;nCount < nCountMax; ++nCount) // run 100000000 through getBytes(...)
288 /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer.get(), nBufLen);
289 /* CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); */
291 aStat.addValue(pBuffer[0], 1);
294 aStat.build(nCountMax);
295 aStat.print();
297 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage());
299 rtl_random_destroyPool(aPool);
302 void getBytes_003_1()
304 rtlRandomPool aPool = rtl_random_createPool();
306 sal_uInt32 nBufLen = 256;
307 std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen ] );
308 memset(pBuffer.get(), 0, nBufLen);
310 Statistics aStat;
312 CPPUNIT_ASSERT_EQUAL_MESSAGE("memset failed", static_cast<sal_uInt8>(0), pBuffer[0]);
314 int nCount = 0;
316 int nCountMax = 10000;
317 for(nCount = 0;nCount < nCountMax; ++nCount) // run 100000000 through getBytes(...)
319 /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer.get(), nBufLen);
320 // CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None);
322 for (sal_uInt32 i=0;i<nBufLen;++i)
323 aStat.addValue(pBuffer[i], 1);
326 aStat.build(nCountMax * nBufLen);
327 aStat.print();
329 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage());
331 rtl_random_destroyPool(aPool);
334 // Change the following lines only, if you add, remove or rename
335 // member functions of the current class,
336 // because these macros are need by auto register mechanism.
338 CPPUNIT_TEST_SUITE(getBytes);
339 CPPUNIT_TEST(getBytes_000);
340 CPPUNIT_TEST(getBytes_001);
341 CPPUNIT_TEST(getBytes_002);
342 CPPUNIT_TEST(getBytes_003);
343 CPPUNIT_TEST(getBytes_003_1);
344 CPPUNIT_TEST_SUITE_END();
345 }; // class getBytes
347 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_random::createPool);
348 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_random::destroyPool);
349 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_random::addBytes);
350 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_random::getBytes);
351 } // namespace rtl_random
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */