| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <memory> |
| 21 |
#include <algorithm> |
| 22 |
|
| 23 |
#include <sal/types.h> |
| 24 |
#include <cppunit/TestAssert.h> |
| 25 |
#include <cppunit/TestFixture.h> |
| 26 |
#include <cppunit/extensions/HelperMacros.h> |
| 27 |
|
| 28 |
#include <rtl/random.h> |
| 29 |
|
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
namespace rtl_random |
| 33 |
{ |
| 34 |
|
| 35 |
class createPool : public CppUnit::TestFixture |
| 36 |
{ |
| 37 |
public: |
| 38 |
// insert your test code here. |
| 39 |
// this is only demonstration code |
| 40 |
void createPool_001() |
| 41 |
{ |
| 42 |
// this is demonstration code |
| 43 |
|
| 44 |
rtlRandomPool aPool = rtl_random_createPool(); |
| 45 |
|
| 46 |
// LLA: seems to be that another test is not possible for createPool() |
| 47 |
CPPUNIT_ASSERT_MESSAGE("create failed", aPool != nullptr); |
| 48 |
|
| 49 |
rtl_random_destroyPool(aPool); |
| 50 |
} |
| 51 |
|
| 52 |
// Change the following lines only, if you add, remove or rename |
| 53 |
// member functions of the current class, |
| 54 |
// because these macros are need by auto register mechanism. |
| 55 |
|
| 56 |
CPPUNIT_TEST_SUITE(createPool); |
| 57 |
CPPUNIT_TEST(createPool_001); |
| 58 |
CPPUNIT_TEST_SUITE_END(); |
| 59 |
}; // class createPool |
| 60 |
|
| 61 |
class destroyPool : public CppUnit::TestFixture |
| 62 |
{ |
| 63 |
public: |
| 64 |
// insert your test code here. |
| 65 |
void destroyPool_000() |
| 66 |
{ |
| 67 |
// GPF, if failed |
| 68 |
rtl_random_destroyPool(nullptr); |
| 69 |
} |
| 70 |
|
| 71 |
void destroyPool_001() |
| 72 |
{ |
| 73 |
rtlRandomPool aPool = rtl_random_createPool(); |
| 74 |
|
| 75 |
// LLA: seems to be that another test is not possible for createPool() |
| 76 |
CPPUNIT_ASSERT_MESSAGE("create failed", aPool != nullptr); |
| 77 |
|
| 78 |
rtl_random_destroyPool(aPool); |
| 79 |
} |
| 80 |
// Change the following lines only, if you add, remove or rename |
| 81 |
// member functions of the current class, |
| 82 |
// because these macros are need by auto register mechanism. |
| 83 |
|
| 84 |
CPPUNIT_TEST_SUITE(destroyPool); |
| 85 |
CPPUNIT_TEST(destroyPool_000); |
| 86 |
CPPUNIT_TEST(destroyPool_001); |
| 87 |
CPPUNIT_TEST_SUITE_END(); |
| 88 |
}; // class destroyPool |
| 89 |
|
| 90 |
class addBytes : public CppUnit::TestFixture |
| 91 |
{ |
| 92 |
public: |
| 93 |
// insert your test code here. |
| 94 |
// this is only demonstration code |
| 95 |
void addBytes_000() |
| 96 |
{ |
| 97 |
rtlRandomPool aPool = rtl_random_createPool(); |
| 98 |
|
| 99 |
sal_uInt32 nBufLen = 4; |
| 100 |
std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen ] ); |
| 101 |
memset(pBuffer.get(), 0, nBufLen); |
| 102 |
|
| 103 |
rtlRandomError aError = rtl_random_addBytes(nullptr, nullptr, 0); |
| 104 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_Argument, aError); |
| 105 |
|
| 106 |
/* rtlRandomError */ aError = rtl_random_addBytes(aPool, nullptr, 0); |
| 107 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_Argument, aError); |
| 108 |
|
| 109 |
/* rtlRandomError */ aError = rtl_random_addBytes(aPool, pBuffer.get(), nBufLen); |
| 110 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_None, aError); |
| 111 |
|
| 112 |
rtl_random_destroyPool(aPool); |
| 113 |
} |
| 114 |
|
| 115 |
void addBytes_001() |
| 116 |
{ |
| 117 |
rtlRandomPool aPool = rtl_random_createPool(); |
| 118 |
|
| 119 |
sal_uInt32 nBufLen = 4; |
| 120 |
std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen ] ); |
| 121 |
|
| 122 |
memset(pBuffer.get(), 0, nBufLen); |
| 123 |
|
| 124 |
rtl_random_addBytes(aPool, pBuffer.get(), nBufLen); |
| 125 |
|
| 126 |
printf("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]); |
| 127 |
|
| 128 |
rtl_random_destroyPool(aPool); |
| 129 |
} |
| 130 |
|
| 131 |
// Change the following lines only, if you add, remove or rename |
| 132 |
// member functions of the current class, |
| 133 |
// because these macros are need by auto register mechanism. |
| 134 |
|
| 135 |
CPPUNIT_TEST_SUITE(addBytes); |
| 136 |
CPPUNIT_TEST(addBytes_000); |
| 137 |
CPPUNIT_TEST(addBytes_001); |
| 138 |
CPPUNIT_TEST_SUITE_END(); |
| 139 |
}; // class addBytes |
| 140 |
|
| 141 |
namespace { |
| 142 |
|
| 143 |
class Statistics |
| 144 |
{ |
| 145 |
int m_nDispensation[256]; |
| 146 |
|
| 147 |
int m_nMin; |
| 148 |
int m_nMax; |
| 149 |
int m_nAverage; |
| 150 |
int m_nMinDeviation; |
| 151 |
int m_nMaxDeviation; |
| 152 |
|
| 153 |
public: |
| 154 |
void clearDispensation() |
| 155 |
{ |
| 156 |
for (int i = 0;i < 256;++i) // clear array |
| 157 |
{ |
| 158 |
m_nDispensation[i] = 0; |
| 159 |
} |
| 160 |
} |
| 161 |
Statistics() |
| 162 |
: m_nMin(0) |
| 163 |
, m_nMax(0) |
| 164 |
, m_nAverage(0) |
| 165 |
, m_nMinDeviation(0) |
| 166 |
, m_nMaxDeviation(0) |
| 167 |
{ |
| 168 |
clearDispensation(); |
| 169 |
} |
| 170 |
|
| 171 |
void addValue(sal_uInt8 _nIndex, sal_Int32 _nValue) |
| 172 |
{ |
| 173 |
m_nDispensation[_nIndex] += _nValue; |
| 174 |
} |
| 175 |
|
| 176 |
void build(sal_Int32 _nCountMax) |
| 177 |
{ |
| 178 |
m_nMin = _nCountMax; |
| 179 |
m_nMax = 0; |
| 180 |
|
| 181 |
m_nAverage = _nCountMax / 256; |
| 182 |
|
| 183 |
m_nMinDeviation = _nCountMax; |
| 184 |
m_nMaxDeviation = 0; |
| 185 |
|
| 186 |
for (int i = 0;i < 256;++i) // show dispensation |
| 187 |
{ |
| 188 |
m_nMin = std::min(m_nMin, m_nDispensation[i]); |
| 189 |
m_nMax = std::max(m_nMax, m_nDispensation[i]); |
| 190 |
|
| 191 |
m_nMinDeviation = std::min(m_nMinDeviation, abs(m_nAverage - m_nDispensation[i])); |
| 192 |
m_nMaxDeviation = std::max(m_nMaxDeviation, abs(m_nAverage - m_nDispensation[i])); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
void print() |
| 197 |
{ |
| 198 |
// LLA: these are only info values |
| 199 |
printf("\nSome statistics\n"); |
| 200 |
printf("Min: %d\n", m_nMin); |
| 201 |
printf("Max: %d\n", m_nMax); |
| 202 |
printf("Average: %d\n", m_nAverage); |
| 203 |
printf("Min abs deviation: %d\n", m_nMinDeviation); |
| 204 |
printf("Max abs deviation: %d\n", m_nMaxDeviation); |
| 205 |
} |
| 206 |
|
| 207 |
sal_Int32 getAverage() const {return m_nAverage;} |
| 208 |
sal_Int32 getMaxDeviation() const {return m_nMaxDeviation;} |
| 209 |
|
| 210 |
}; |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
class getBytes : public CppUnit::TestFixture |
| 215 |
{ |
| 216 |
public: |
| 217 |
// insert your test code here. |
| 218 |
void getBytes_000() |
| 219 |
{ |
| 220 |
rtlRandomPool aPool = rtl_random_createPool(); |
| 221 |
|
| 222 |
sal_uInt32 nBufLen = 4; |
| 223 |
std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen ] ); |
| 224 |
memset(pBuffer.get(), 0, nBufLen); |
| 225 |
|
| 226 |
rtlRandomError aError = rtl_random_getBytes(nullptr, nullptr, 0); |
| 227 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_Argument, aError); |
| 228 |
|
| 229 |
/* rtlRandomError */ aError = rtl_random_getBytes(aPool, nullptr, 0); |
| 230 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_Argument, aError); |
| 231 |
|
| 232 |
/* rtlRandomError */ aError = rtl_random_getBytes(aPool, pBuffer.get(), nBufLen); |
| 233 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_None, aError); |
| 234 |
|
| 235 |
rtl_random_destroyPool(aPool); |
| 236 |
} |
| 237 |
|
| 238 |
void getBytes_001() |
| 239 |
{ |
| 240 |
rtlRandomPool aPool = rtl_random_createPool(); |
| 241 |
|
| 242 |
sal_uInt32 nBufLen = 4; |
| 243 |
std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen ] ); |
| 244 |
memset(pBuffer.get(), 0, nBufLen); |
| 245 |
|
| 246 |
rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer.get(), nBufLen); |
| 247 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_None, aError); |
| 248 |
|
| 249 |
printf("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]); |
| 250 |
|
| 251 |
rtl_random_destroyPool(aPool); |
| 252 |
} |
| 253 |
|
| 254 |
void getBytes_002() |
| 255 |
{ |
| 256 |
rtlRandomPool aPool = rtl_random_createPool(); |
| 257 |
|
| 258 |
sal_uInt32 nBufLen = 4; |
| 259 |
std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen << 1 ] ); |
| 260 |
memset(pBuffer.get(), 0, nBufLen << 1); |
| 261 |
|
| 262 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("memset failed", sal_uInt8(0), pBuffer[4]); |
| 263 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("memset failed", sal_uInt8(0), pBuffer[5]); |
| 264 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("memset failed", sal_uInt8(0), pBuffer[6]); |
| 265 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("memset failed", sal_uInt8(0), pBuffer[7]); |
| 266 |
|
| 267 |
rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer.get(), nBufLen); |
| 268 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong parameter", rtl_Random_E_None, aError); |
| 269 |
|
| 270 |
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]); |
| 271 |
|
| 272 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("internal memory overwrite", sal_uInt8(0), pBuffer[4]); |
| 273 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("internal memory overwrite", sal_uInt8(0), pBuffer[5]); |
| 274 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("internal memory overwrite", sal_uInt8(0), pBuffer[6]); |
| 275 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("internal memory overwrite", sal_uInt8(0), pBuffer[7]); |
| 276 |
|
| 277 |
rtl_random_destroyPool(aPool); |
| 278 |
} |
| 279 |
|
| 280 |
void getBytes_003() |
| 281 |
{ |
| 282 |
rtlRandomPool aPool = rtl_random_createPool(); |
| 283 |
|
| 284 |
sal_uInt32 nBufLen = 1; |
| 285 |
std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen ] ); |
| 286 |
memset(pBuffer.get(), 0, nBufLen); |
| 287 |
|
| 288 |
Statistics aStat; |
| 289 |
|
| 290 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("memset failed", static_cast<sal_uInt8>(0), pBuffer[0]); |
| 291 |
|
| 292 |
int nCount = 0; |
| 293 |
|
| 294 |
int nCountMax = 1000000; |
| 295 |
for(nCount = 0;nCount < nCountMax; ++nCount) // run 100000000 through getBytes(...) |
| 296 |
{ |
| 297 |
/* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer.get(), nBufLen); |
| 298 |
/* CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); */ |
| 299 |
|
| 300 |
aStat.addValue(pBuffer[0], 1); |
| 301 |
} |
| 302 |
|
| 303 |
aStat.build(nCountMax); |
| 304 |
aStat.print(); |
| 305 |
|
| 306 |
CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage()); |
| 307 |
|
| 308 |
rtl_random_destroyPool(aPool); |
| 309 |
} |
| 310 |
|
| 311 |
void getBytes_003_1() |
| 312 |
{ |
| 313 |
rtlRandomPool aPool = rtl_random_createPool(); |
| 314 |
|
| 315 |
sal_uInt32 nBufLen = 256; |
| 316 |
std::unique_ptr<sal_uInt8[]> pBuffer( new sal_uInt8[ nBufLen ] ); |
| 317 |
memset(pBuffer.get(), 0, nBufLen); |
| 318 |
|
| 319 |
Statistics aStat; |
| 320 |
|
| 321 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("memset failed", static_cast<sal_uInt8>(0), pBuffer[0]); |
| 322 |
|
| 323 |
int nCount = 0; |
| 324 |
|
| 325 |
int nCountMax = 10000; |
| 326 |
for(nCount = 0;nCount < nCountMax; ++nCount) // run 100000000 through getBytes(...) |
| 327 |
{ |
| 328 |
/* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer.get(), nBufLen); |
| 329 |
// CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); |
| 330 |
|
| 331 |
for (sal_uInt32 i=0;i<nBufLen;++i) |
| 332 |
aStat.addValue(pBuffer[i], 1); |
| 333 |
} |
| 334 |
|
| 335 |
aStat.build(nCountMax * nBufLen); |
| 336 |
aStat.print(); |
| 337 |
|
| 338 |
CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage()); |
| 339 |
|
| 340 |
rtl_random_destroyPool(aPool); |
| 341 |
} |
| 342 |
|
| 343 |
// Change the following lines only, if you add, remove or rename |
| 344 |
// member functions of the current class, |
| 345 |
// because these macros are need by auto register mechanism. |
| 346 |
|
| 347 |
CPPUNIT_TEST_SUITE(getBytes); |
| 348 |
CPPUNIT_TEST(getBytes_000); |
| 349 |
CPPUNIT_TEST(getBytes_001); |
| 350 |
CPPUNIT_TEST(getBytes_002); |
| 351 |
CPPUNIT_TEST(getBytes_003); |
| 352 |
CPPUNIT_TEST(getBytes_003_1); |
| 353 |
CPPUNIT_TEST_SUITE_END(); |
| 354 |
}; // class getBytes |
| 355 |
|
| 356 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_random::createPool); |
| 357 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_random::destroyPool); |
| 358 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_random::addBytes); |
| 359 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_random::getBytes); |
| 360 |
} // namespace rtl_random |
| 361 |
|
| 362 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |