1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 25 // MARKER(update_precomp.py): autogen include statement, do not remove 26 #include "precompiled_sal.hxx" 27 // autogenerated file with codegen.pl 28 29 #include <algorithm> // STL 30 31 #include <testshl/simpleheader.hxx> 32 #include <rtl/random.h> 33 34 namespace rtl_random 35 { 36 37 class createPool : public CppUnit::TestFixture 38 { 39 public: 40 // initialise your test code values here. 41 void setUp() 42 { 43 } 44 45 void tearDown() 46 { 47 } 48 49 // insert your test code here. 50 // this is only demonstration code 51 void createPool_001() 52 { 53 // this is demonstration code 54 55 rtlRandomPool aPool = rtl_random_createPool(); 56 57 // LLA: seems to be that an other test is not possible for createPool() 58 CPPUNIT_ASSERT_MESSAGE("create failed", aPool != NULL); 59 60 rtl_random_destroyPool(aPool); 61 } 62 63 // Change the following lines only, if you add, remove or rename 64 // member functions of the current class, 65 // because these macros are need by auto register mechanism. 66 67 CPPUNIT_TEST_SUITE(createPool); 68 CPPUNIT_TEST(createPool_001); 69 CPPUNIT_TEST_SUITE_END(); 70 }; // class createPool 71 72 73 class destroyPool : public CppUnit::TestFixture 74 { 75 public: 76 // initialise your test code values here. 77 void setUp() 78 { 79 } 80 81 void tearDown() 82 { 83 } 84 85 // insert your test code here. 86 void destroyPool_000() 87 { 88 // GPF, if failed 89 rtl_random_destroyPool(NULL); 90 } 91 92 void destroyPool_001() 93 { 94 rtlRandomPool aPool = rtl_random_createPool(); 95 96 // LLA: seems to be that an other test is not possible for createPool() 97 CPPUNIT_ASSERT_MESSAGE("create failed", aPool != NULL); 98 99 rtl_random_destroyPool(aPool); 100 } 101 // Change the following lines only, if you add, remove or rename 102 // member functions of the current class, 103 // because these macros are need by auto register mechanism. 104 105 CPPUNIT_TEST_SUITE(destroyPool); 106 CPPUNIT_TEST(destroyPool_000); 107 CPPUNIT_TEST(destroyPool_001); 108 CPPUNIT_TEST_SUITE_END(); 109 }; // class destroyPool 110 111 112 class addBytes : public CppUnit::TestFixture 113 { 114 public: 115 // initialise your test code values here. 116 void setUp() 117 { 118 } 119 120 void tearDown() 121 { 122 } 123 124 // insert your test code here. 125 // this is only demonstration code 126 void addBytes_000() 127 { 128 rtlRandomPool aPool = rtl_random_createPool(); 129 130 sal_uInt32 nBufLen = 4; 131 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; 132 memset(pBuffer, 0, nBufLen); 133 134 rtlRandomError aError = rtl_random_addBytes(NULL, NULL, 0); 135 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument); 136 137 /* rtlRandomError */ aError = rtl_random_addBytes(aPool, NULL, 0); 138 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument); 139 140 /* rtlRandomError */ aError = rtl_random_addBytes(aPool, pBuffer, nBufLen); 141 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); 142 143 rtl_random_destroyPool(aPool); 144 delete [] pBuffer; 145 146 } 147 148 void addBytes_001() 149 { 150 rtlRandomPool aPool = rtl_random_createPool(); 151 152 sal_uInt32 nBufLen = 4; 153 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; 154 155 memset(pBuffer, 0, nBufLen); 156 157 rtl_random_addBytes(aPool, pBuffer, nBufLen); 158 159 t_print("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]); 160 161 rtl_random_destroyPool(aPool); 162 delete [] pBuffer; 163 } 164 165 166 // Change the following lines only, if you add, remove or rename 167 // member functions of the current class, 168 // because these macros are need by auto register mechanism. 169 170 CPPUNIT_TEST_SUITE(addBytes); 171 CPPUNIT_TEST(addBytes_000); 172 CPPUNIT_TEST(addBytes_001); 173 CPPUNIT_TEST_SUITE_END(); 174 }; // class addBytes 175 176 177 class Statistics 178 { 179 int m_nDispensation[256]; 180 181 int m_nMin; 182 int m_nMax; 183 int m_nAverage; 184 int m_nMinDeviation; 185 int m_nMaxDeviation; 186 187 public: 188 void clearDispensation() 189 { 190 for (int i = 0;i < 256;i ++) // clear array 191 { 192 m_nDispensation[i] = 0; 193 } 194 } 195 Statistics() 196 { 197 clearDispensation(); 198 } 199 ~Statistics(){} 200 201 void addValue(sal_Int16 _nIndex, sal_Int32 _nValue) 202 { 203 OSL_ASSERT(_nIndex >= 0 && _nIndex < 256); 204 m_nDispensation[_nIndex] += _nValue; 205 } 206 207 void build(sal_Int32 _nCountMax) 208 { 209 m_nMin = _nCountMax; 210 m_nMax = 0; 211 212 m_nAverage = _nCountMax / 256; 213 214 m_nMinDeviation = _nCountMax; 215 m_nMaxDeviation = 0; 216 217 for (int i = 0;i < 256;i ++) // show dispensation 218 { 219 m_nMin = std::min(m_nMin, m_nDispensation[i]); 220 m_nMax = std::max(m_nMax, m_nDispensation[i]); 221 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])); 224 } 225 } 226 227 void print() 228 { 229 // LLA: these are only info values 230 t_print("\nSome statistics\n"); 231 t_print("Min: %d\n", m_nMin); 232 t_print("Max: %d\n", m_nMax); 233 t_print("Average: %d\n", m_nAverage); 234 t_print("Min abs deviation: %d\n", m_nMinDeviation); 235 t_print("Max abs deviation: %d\n", m_nMaxDeviation); 236 } 237 238 sal_Int32 getAverage() {return m_nAverage;} 239 sal_Int32 getMaxDeviation() {return m_nMaxDeviation;} 240 241 }; 242 243 class getBytes : public CppUnit::TestFixture 244 { 245 public: 246 // initialise your test code values here. 247 void setUp() 248 { 249 } 250 251 void tearDown() 252 { 253 } 254 255 // insert your test code here. 256 void getBytes_000() 257 { 258 rtlRandomPool aPool = rtl_random_createPool(); 259 260 sal_uInt32 nBufLen = 4; 261 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; 262 memset(pBuffer, 0, nBufLen); 263 264 rtlRandomError aError = rtl_random_getBytes(NULL, NULL, 0); 265 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument); 266 267 /* rtlRandomError */ aError = rtl_random_getBytes(aPool, NULL, 0); 268 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_Argument); 269 270 /* rtlRandomError */ aError = rtl_random_getBytes(aPool, pBuffer, nBufLen); 271 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); 272 273 rtl_random_destroyPool(aPool); 274 delete [] pBuffer; 275 } 276 277 void getBytes_001() 278 { 279 rtlRandomPool aPool = rtl_random_createPool(); 280 281 sal_uInt32 nBufLen = 4; 282 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; 283 memset(pBuffer, 0, nBufLen); 284 285 rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen); 286 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); 287 288 t_print("%2x %2x %2x %2x\n", pBuffer[0], pBuffer[1], pBuffer[2], pBuffer[3]); 289 290 rtl_random_destroyPool(aPool); 291 delete [] pBuffer; 292 } 293 294 void getBytes_002() 295 { 296 rtlRandomPool aPool = rtl_random_createPool(); 297 298 sal_uInt32 nBufLen = 4; 299 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen << 1 ]; 300 memset(pBuffer, 0, nBufLen << 1); 301 302 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0); 303 304 rtlRandomError aError = rtl_random_getBytes(aPool, pBuffer, nBufLen); 305 CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); 306 307 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]); 308 309 CPPUNIT_ASSERT_MESSAGE("internal memory overwrite", pBuffer[4] == 0 && pBuffer[5] == 0 && pBuffer[6] == 0 && pBuffer[7] == 0); 310 311 rtl_random_destroyPool(aPool); 312 delete [] pBuffer; 313 } 314 315 void getBytes_003() 316 { 317 rtlRandomPool aPool = rtl_random_createPool(); 318 319 sal_uInt32 nBufLen = 1; 320 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; 321 memset(pBuffer, 0, nBufLen); 322 323 Statistics aStat; 324 325 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[0] == 0); 326 327 int nCount = 0; 328 329 int nCountMax = 1000000; 330 for(nCount = 0;nCount < nCountMax; nCount ++) // run 100000000 through getBytes(...) 331 { 332 /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen); 333 /* CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); */ 334 335 aStat.addValue(pBuffer[0], 1); 336 } 337 338 aStat.build(nCountMax); 339 aStat.print(); 340 341 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage()); 342 343 rtl_random_destroyPool(aPool); 344 delete [] pBuffer; 345 } 346 347 void getBytes_003_1() 348 { 349 rtlRandomPool aPool = rtl_random_createPool(); 350 351 sal_uInt32 nBufLen = 256; 352 sal_uInt8 *pBuffer = new sal_uInt8[ nBufLen ]; 353 memset(pBuffer, 0, nBufLen); 354 355 Statistics aStat; 356 357 CPPUNIT_ASSERT_MESSAGE("memset failed", pBuffer[0] == 0); 358 359 int nCount = 0; 360 361 int nCountMax = 10000; 362 for(nCount = 0;nCount < nCountMax; nCount ++) // run 100000000 through getBytes(...) 363 { 364 /* rtlRandomError aError = */ rtl_random_getBytes(aPool, pBuffer, nBufLen); 365 // CPPUNIT_ASSERT_MESSAGE("wrong parameter", aError == rtl_Random_E_None); 366 367 for (sal_uInt32 i=0;i<nBufLen;i++) 368 { 369 aStat.addValue(pBuffer[i], 1); 370 } 371 } 372 373 aStat.build(nCountMax * nBufLen); 374 aStat.print(); 375 376 CPPUNIT_ASSERT_MESSAGE("deviation should be less average", aStat.getMaxDeviation() < aStat.getAverage()); 377 378 rtl_random_destroyPool(aPool); 379 delete [] pBuffer; 380 } 381 382 // Change the following lines only, if you add, remove or rename 383 // member functions of the current class, 384 // because these macros are need by auto register mechanism. 385 386 CPPUNIT_TEST_SUITE(getBytes); 387 CPPUNIT_TEST(getBytes_000); 388 CPPUNIT_TEST(getBytes_001); 389 CPPUNIT_TEST(getBytes_002); 390 CPPUNIT_TEST(getBytes_003); 391 CPPUNIT_TEST(getBytes_003_1); 392 CPPUNIT_TEST_SUITE_END(); 393 }; // class getBytes 394 395 // ----------------------------------------------------------------------------- 396 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_random::createPool, "rtl_random"); 397 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_random::destroyPool, "rtl_random"); 398 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_random::addBytes, "rtl_random"); 399 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_random::getBytes, "rtl_random"); 400 } // namespace rtl_random 401 402 403 // ----------------------------------------------------------------------------- 404 405 // this macro creates an empty function, which will called by the RegisterAllFunctions() 406 // to let the user the possibility to also register some functions by hand. 407 NOADDITIONAL; 408 409