1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 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. 14 * 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). 20 * 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. 25 * 26 ************************************************************************/ 27 28 29 // MARKER(update_precomp.py): autogen include statement, do not remove 30 #include "precompiled_sal.hxx" 31 // autogenerated file with codegen.pl 32 33 #include <algorithm> // STL 34 35 #include <testshl/simpleheader.hxx> 36 #include <rtl/random.h> 37 38 namespace rtl_random 39 { 40 41 class createPool : public CppUnit::TestFixture 42 { 43 public: 44 // initialise your test code values here. 45 void setUp() 46 { 47 } 48 49 void tearDown() 50 { 51 } 52 53 // insert your test code here. 54 // this is only demonstration code 55 void createPool_001() 56 { 57 // this is demonstration code 58 59 rtlRandomPool aPool = rtl_random_createPool(); 60 61 // LLA: seems to be that an other test is not possible for createPool() 62 CPPUNIT_ASSERT_MESSAGE("create failed", aPool != NULL); 63 64 rtl_random_destroyPool(aPool); 65 } 66 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. 70 71 CPPUNIT_TEST_SUITE(createPool); 72 CPPUNIT_TEST(createPool_001); 73 CPPUNIT_TEST_SUITE_END(); 74 }; // class createPool 75 76 77 class destroyPool : public CppUnit::TestFixture 78 { 79 public: 80 // initialise your test code values here. 81 void setUp() 82 { 83 } 84 85 void tearDown() 86 { 87 } 88 89 // insert your test code here. 90 void destroyPool_000() 91 { 92 // GPF, if failed 93 rtl_random_destroyPool(NULL); 94 } 95 96 void destroyPool_001() 97 { 98 rtlRandomPool aPool = rtl_random_createPool(); 99 100 // LLA: seems to be that an other test is not possible for createPool() 101 CPPUNIT_ASSERT_MESSAGE("create failed", aPool != NULL); 102 103 rtl_random_destroyPool(aPool); 104 } 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. 108 109 CPPUNIT_TEST_SUITE(destroyPool); 110 CPPUNIT_TEST(destroyPool_000); 111 CPPUNIT_TEST(destroyPool_001); 112 CPPUNIT_TEST_SUITE_END(); 113 }; // class destroyPool 114 115 116 class addBytes : public CppUnit::TestFixture 117 { 118 public: 119 // initialise your test code values here. 120 void setUp() 121 { 122 } 123 124 void tearDown() 125 { 126 } 127 128 // insert your test code here. 129 // this is only demonstration code 130 void addBytes_000() 131 { 132 rtlRandomPool aPool = rtl_random_createPool(); 133 134 sal_uInt32 nBufLen = 4; 135 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; 136 memset(pBuffer, 0, nBufLen); 137 138 rtlRandomError aError = rtl_random_addBytes(NULL, NULL, 0); 139 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument); 140 141 /* rtlRandomError */ aError = rtl_random_addBytes(aPool, NULL, 0); 142 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument); 143 144 /* rtlRandomError */ aError = rtl_random_addBytes(aPool, pBuffer, nBufLen); 145 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); 146 147 rtl_random_destroyPool(aPool); 148 delete [] pBuffer; 149 150 } 151 152 void addBytes_001() 153 { 154 rtlRandomPool aPool = rtl_random_createPool(); 155 156 sal_uInt32 nBufLen = 4; 157 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; 158 159 memset(pBuffer, 0, nBufLen); 160 161 rtl_random_addBytes(aPool, pBuffer, nBufLen); 162 163 t_print("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]); 164 165 rtl_random_destroyPool(aPool); 166 delete [] pBuffer; 167 } 168 169 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. 173 174 CPPUNIT_TEST_SUITE(addBytes); 175 CPPUNIT_TEST(addBytes_000); 176 CPPUNIT_TEST(addBytes_001); 177 CPPUNIT_TEST_SUITE_END(); 178 }; // class addBytes 179 180 181 class Statistics 182 { 183 int m_nDispensation[256]; 184 185 int m_nMin; 186 int m_nMax; 187 int m_nAverage; 188 int m_nMinDeviation; 189 int m_nMaxDeviation; 190 191 public: 192 void clearDispensation() 193 { 194 for (int i = 0;i < 256;i ++) // clear array 195 { 196 m_nDispensation[i] = 0; 197 } 198 } 199 Statistics() 200 { 201 clearDispensation(); 202 } 203 ~Statistics(){} 204 205 void addValue(sal_Int16 _nIndex, sal_Int32 _nValue) 206 { 207 OSL_ASSERT(_nIndex >= 0 && _nIndex < 256); 208 m_nDispensation[_nIndex] += _nValue; 209 } 210 211 void build(sal_Int32 _nCountMax) 212 { 213 m_nMin = _nCountMax; 214 m_nMax = 0; 215 216 m_nAverage = _nCountMax / 256; 217 218 m_nMinDeviation = _nCountMax; 219 m_nMaxDeviation = 0; 220 221 for (int i = 0;i < 256;i ++) // show dispensation 222 { 223 m_nMin = std::min(m_nMin, m_nDispensation[i]); 224 m_nMax = std::max(m_nMax, m_nDispensation[i]); 225 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])); 228 } 229 } 230 231 void print() 232 { 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); 240 } 241 242 sal_Int32 getAverage() {return m_nAverage;} 243 sal_Int32 getMaxDeviation() {return m_nMaxDeviation;} 244 245 }; 246 247 class getBytes : public CppUnit::TestFixture 248 { 249 public: 250 // initialise your test code values here. 251 void setUp() 252 { 253 } 254 255 void tearDown() 256 { 257 } 258 259 // insert your test code here. 260 void getBytes_000() 261 { 262 rtlRandomPool aPool = rtl_random_createPool(); 263 264 sal_uInt32 nBufLen = 4; 265 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; 266 memset(pBuffer, 0, nBufLen); 267 268 rtlRandomError aError = rtl_random_getBytes(NULL, NULL, 0); 269 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument); 270 271 /* rtlRandomError */ aError = rtl_random_getBytes(aPool, NULL, 0); 272 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument); 273 274 /* rtlRandomError */ aError = rtl_random_getBytes(aPool, pBuffer, nBufLen); 275 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); 276 277 rtl_random_destroyPool(aPool); 278 delete [] pBuffer; 279 } 280 281 void getBytes_001() 282 { 283 rtlRandomPool aPool = rtl_random_createPool(); 284 285 sal_uInt32 nBufLen = 4; 286 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; 287 memset(pBuffer, 0, nBufLen); 288 289 rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen); 290 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); 291 292 t_print("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]); 293 294 rtl_random_destroyPool(aPool); 295 delete [] pBuffer; 296 } 297 298 void getBytes_002() 299 { 300 rtlRandomPool aPool = rtl_random_createPool(); 301 302 sal_uInt32 nBufLen = 4; 303 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen << 1 ]; 304 memset(pBuffer, 0, nBufLen << 1); 305 306 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0); 307 308 rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen); 309 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); 310 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]); 312 313 CPPUNIT_ASSERT_MESSAGE("internal memory overwrite", pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0); 314 315 rtl_random_destroyPool(aPool); 316 delete [] pBuffer; 317 } 318 319 void getBytes_003() 320 { 321 rtlRandomPool aPool = rtl_random_createPool(); 322 323 sal_uInt32 nBufLen = 1; 324 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; 325 memset(pBuffer, 0, nBufLen); 326 327 Statistics aStat; 328 329 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[0] == 0); 330 331 int nCount = 0; 332 333 int nCountMax = 1000000; 334 for(nCount = 0;nCount < nCountMax; nCount ++) // run 100000000 through getBytes(...) 335 { 336 /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen); 337 /* CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); */ 338 339 aStat.addValue(pBuffer[0], 1); 340 } 341 342 aStat.build(nCountMax); 343 aStat.print(); 344 345 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage()); 346 347 rtl_random_destroyPool(aPool); 348 delete [] pBuffer; 349 } 350 351 void getBytes_003_1() 352 { 353 rtlRandomPool aPool = rtl_random_createPool(); 354 355 sal_uInt32 nBufLen = 256; 356 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; 357 memset(pBuffer, 0, nBufLen); 358 359 Statistics aStat; 360 361 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[0] == 0); 362 363 int nCount = 0; 364 365 int nCountMax = 10000; 366 for(nCount = 0;nCount < nCountMax; nCount ++) // run 100000000 through getBytes(...) 367 { 368 /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen); 369 // CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); 370 371 for (sal_uInt32 i=0;i<nBufLen;i++) 372 { 373 aStat.addValue(pBuffer[i], 1); 374 } 375 } 376 377 aStat.build(nCountMax * nBufLen); 378 aStat.print(); 379 380 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage()); 381 382 rtl_random_destroyPool(aPool); 383 delete [] pBuffer; 384 } 385 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. 389 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(); 397 }; // class getBytes 398 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 405 406 407 // ----------------------------------------------------------------------------- 408 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. 411 NOADDITIONAL; 412 413