vbox的更動 11347 路徑 trunk/src/VBox/Runtime/testcase
- 時間撮記:
- 2008-8-11 下午09:12:55 (16 年 以前)
- 檔案:
-
- 修改 1 筆資料
圖例:
- 未更動
- 新增
- 刪除
-
trunk/src/VBox/Runtime/testcase/tstRand.cpp
r11064 r11347 101 101 102 102 103 int main()103 static int tstRandAdv(RTRAND hRand) 104 104 { 105 RTR3Init(false);106 RTPrintf("tstRand: TESTING...\n");107 108 /*109 * Do some smoke tests first?110 */111 /** @todo RTRand smoke testing. */112 113 105 /* 114 106 * Test distribution. … … 139 131 uint32_t const uRange = uLast - uFirst; Assert(uLast >= uFirst); 140 132 uint32_t const uDivisor = uRange / TST_RAND_SAMPLE_RANGES + 1; 141 RTPrintf("tstRand: TESTING RTRandU32Ex(%#RX32, %#RX32) distribution... [div=%#RX32 range=%#RX32]\n", uFirst, uLast, uDivisor, uRange);142 for (unsigned iSample = 0; iSample < TST_RAND_SAMPLE_RANGES * 10240; iSample++) 143 { 144 uint32_t uRand = RTRand U32Ex(uFirst, uLast);133 RTPrintf("tstRand: TESTING RTRandAdvU32Ex(,%#RX32, %#RX32) distribution... [div=%#RX32 range=%#RX32]\n", uFirst, uLast, uDivisor, uRange); 134 for (unsigned iSample = 0; iSample < TST_RAND_SAMPLE_RANGES * 10240; iSample++) 135 { 136 uint32_t uRand = RTRandAdvU32Ex(hRand, uFirst, uLast); 145 137 CHECK_EXPR_MSG(uRand >= uFirst, ("%#RX32 %#RX32\n", uRand, uFirst)); 146 138 CHECK_EXPR_MSG(uRand <= uLast, ("%#RX32 %#RX32\n", uRand, uLast)); … … 184 176 uint64_t const uRange = uLast - uFirst; Assert(uLast >= uFirst); 185 177 uint64_t const uDivisor = uRange / TST_RAND_SAMPLE_RANGES + 1; 186 RTPrintf("tstRand: TESTING RTRandU64Ex(%#RX64, %#RX64) distribution... [div=%#RX64 range=%#RX64]\n", uFirst, uLast, uDivisor, uRange);187 for (unsigned iSample = 0; iSample < TST_RAND_SAMPLE_RANGES * 10240; iSample++) 188 { 189 uint64_t uRand = RTRand U64Ex(uFirst, uLast);178 RTPrintf("tstRand: TESTING RTRandAdvU64Ex(,%#RX64, %#RX64) distribution... [div=%#RX64 range=%#RX64]\n", uFirst, uLast, uDivisor, uRange); 179 for (unsigned iSample = 0; iSample < TST_RAND_SAMPLE_RANGES * 10240; iSample++) 180 { 181 uint64_t uRand = RTRandAdvU64Ex(hRand, uFirst, uLast); 190 182 CHECK_EXPR_MSG(uRand >= uFirst, ("%#RX64 %#RX64\n", uRand, uFirst)); 191 183 CHECK_EXPR_MSG(uRand <= uLast, ("%#RX64 %#RX64\n", uRand, uLast)); … … 242 234 uint32_t const uRange = iLast - iFirst; AssertMsg(iLast >= iFirst, ("%d\n", iTest)); 243 235 uint32_t const uDivisor = (uRange ? uRange : UINT32_MAX) / TST_RAND_SAMPLE_RANGES + 1; 244 RTPrintf("tstRand: TESTING RTRandS32Ex(%#RI32, %#RI32) distribution... [div=%#RX32 range=%#RX32]\n", iFirst, iLast, uDivisor, uRange);245 for (unsigned iSample = 0; iSample < TST_RAND_SAMPLE_RANGES * 10240; iSample++) 246 { 247 int32_t iRand = RTRand S32Ex(iFirst, iLast);236 RTPrintf("tstRand: TESTING RTRandAdvS32Ex(,%#RI32, %#RI32) distribution... [div=%#RX32 range=%#RX32]\n", iFirst, iLast, uDivisor, uRange); 237 for (unsigned iSample = 0; iSample < TST_RAND_SAMPLE_RANGES * 10240; iSample++) 238 { 239 int32_t iRand = RTRandAdvS32Ex(hRand, iFirst, iLast); 248 240 CHECK_EXPR_MSG(iRand >= iFirst, ("%#RI32 %#RI32\n", iRand, iFirst)); 249 241 CHECK_EXPR_MSG(iRand <= iLast, ("%#RI32 %#RI32\n", iRand, iLast)); … … 311 303 uint64_t const uRange = iLast - iFirst; AssertMsg(iLast >= iFirst, ("%d\n", iTest)); 312 304 uint64_t const uDivisor = (uRange ? uRange : UINT64_MAX) / TST_RAND_SAMPLE_RANGES + 1; 305 RTPrintf("tstRand: TESTING RTRandAdvS64Ex(,%#RI64, %#RI64) distribution... [div=%#RX64 range=%#016RX64]\n", iFirst, iLast, uDivisor, uRange); 306 for (unsigned iSample = 0; iSample < TST_RAND_SAMPLE_RANGES * 10240; iSample++) 307 { 308 int64_t iRand = RTRandAdvS64Ex(hRand, iFirst, iLast); 309 CHECK_EXPR_MSG(iRand >= iFirst, ("%#RI64 %#RI64\n", iRand, iFirst)); 310 CHECK_EXPR_MSG(iRand <= iLast, ("%#RI64 %#RI64\n", iRand, iLast)); 311 uint64_t off = iRand - iFirst; 312 acHits[off / uDivisor]++; 313 } 314 tstRandCheckDist(acHits, iTest); 315 } 316 #endif 317 318 /* 319 * Destroy it. 320 */ 321 int rc = RTRandAdvDestroy(hRand); 322 CHECK_EXPR_MSG(rc == VINF_SUCCESS, ("RTRandAdvDestroy(%p) -> %Rrc (%d)\n", (uintptr_t)hRand, rc, rc)); 323 324 return 0; 325 } 326 327 328 329 int main() 330 { 331 RTR3Init(false); 332 RTPrintf("tstRand: TESTING...\n"); 333 334 /* 335 * Do some smoke tests first? 336 */ 337 /** @todo RTRand smoke testing. */ 338 339 #if 1 340 /* 341 * Test distribution. 342 */ 343 #if 1 344 /* unsigned 32-bit */ 345 static const struct 346 { 347 uint32_t u32First; 348 uint32_t u32Last; 349 } s_aU32Tests[] = 350 { 351 { 0, UINT32_MAX }, 352 { 0, UINT32_MAX / 2 + UINT32_MAX / 4 }, 353 { 0, UINT32_MAX / 2 + UINT32_MAX / 8 }, 354 { 0, UINT32_MAX / 2 + UINT32_MAX / 16 }, 355 { 0, UINT32_MAX / 2 + UINT32_MAX / 64 }, 356 { 0, UINT32_MAX / 2 }, 357 { UINT32_MAX / 4, UINT32_MAX / 4 * 3 }, 358 { 0, TST_RAND_SAMPLE_RANGES - 1 }, 359 { 1234, 1234 + TST_RAND_SAMPLE_RANGES - 1 }, 360 }; 361 for (unsigned iTest = 0; iTest < RT_ELEMENTS(s_aU32Tests); iTest++) 362 { 363 uint32_t acHits[TST_RAND_SAMPLE_RANGES] = {0}; 364 uint32_t const uFirst = s_aU32Tests[iTest].u32First; 365 uint32_t const uLast = s_aU32Tests[iTest].u32Last; 366 uint32_t const uRange = uLast - uFirst; Assert(uLast >= uFirst); 367 uint32_t const uDivisor = uRange / TST_RAND_SAMPLE_RANGES + 1; 368 RTPrintf("tstRand: TESTING RTRandU32Ex(%#RX32, %#RX32) distribution... [div=%#RX32 range=%#RX32]\n", uFirst, uLast, uDivisor, uRange); 369 for (unsigned iSample = 0; iSample < TST_RAND_SAMPLE_RANGES * 10240; iSample++) 370 { 371 uint32_t uRand = RTRandU32Ex(uFirst, uLast); 372 CHECK_EXPR_MSG(uRand >= uFirst, ("%#RX32 %#RX32\n", uRand, uFirst)); 373 CHECK_EXPR_MSG(uRand <= uLast, ("%#RX32 %#RX32\n", uRand, uLast)); 374 uint32_t off = uRand - uFirst; 375 acHits[off / uDivisor]++; 376 } 377 tstRandCheckDist(acHits, iTest); 378 } 379 #endif 380 381 #if 1 382 /* unsigned 64-bit */ 383 static const struct 384 { 385 uint64_t u64First; 386 uint64_t u64Last; 387 } s_aU64Tests[] = 388 { 389 { 0, UINT64_MAX }, 390 { 0, UINT64_MAX / 2 + UINT64_MAX / 4 }, 391 { 0, UINT64_MAX / 2 + UINT64_MAX / 8 }, 392 { 0, UINT64_MAX / 2 + UINT64_MAX / 16 }, 393 { 0, UINT64_MAX / 2 + UINT64_MAX / 64 }, 394 { 0, UINT64_MAX / 2 }, 395 { UINT64_MAX / 4, UINT64_MAX / 4 * 3 }, 396 { 0, UINT32_MAX }, 397 { 0, UINT32_MAX / 2 + UINT32_MAX / 4 }, 398 { 0, UINT32_MAX / 2 + UINT32_MAX / 8 }, 399 { 0, UINT32_MAX / 2 + UINT32_MAX / 16 }, 400 { 0, UINT32_MAX / 2 + UINT32_MAX / 64 }, 401 { 0, UINT32_MAX / 2 }, 402 { UINT32_MAX / 4, UINT32_MAX / 4 * 3 }, 403 { 0, TST_RAND_SAMPLE_RANGES - 1 }, 404 { 1234, 1234 + TST_RAND_SAMPLE_RANGES - 1 }, 405 }; 406 for (unsigned iTest = 0; iTest < RT_ELEMENTS(s_aU64Tests); iTest++) 407 { 408 uint32_t acHits[TST_RAND_SAMPLE_RANGES] = {0}; 409 uint64_t const uFirst = s_aU64Tests[iTest].u64First; 410 uint64_t const uLast = s_aU64Tests[iTest].u64Last; 411 uint64_t const uRange = uLast - uFirst; Assert(uLast >= uFirst); 412 uint64_t const uDivisor = uRange / TST_RAND_SAMPLE_RANGES + 1; 413 RTPrintf("tstRand: TESTING RTRandU64Ex(%#RX64, %#RX64) distribution... [div=%#RX64 range=%#RX64]\n", uFirst, uLast, uDivisor, uRange); 414 for (unsigned iSample = 0; iSample < TST_RAND_SAMPLE_RANGES * 10240; iSample++) 415 { 416 uint64_t uRand = RTRandU64Ex(uFirst, uLast); 417 CHECK_EXPR_MSG(uRand >= uFirst, ("%#RX64 %#RX64\n", uRand, uFirst)); 418 CHECK_EXPR_MSG(uRand <= uLast, ("%#RX64 %#RX64\n", uRand, uLast)); 419 uint64_t off = uRand - uFirst; 420 acHits[off / uDivisor]++; 421 } 422 tstRandCheckDist(acHits, iTest); 423 } 424 #endif 425 426 #if 1 427 /* signed 32-bit */ 428 static const struct 429 { 430 int32_t i32First; 431 int32_t i32Last; 432 } s_aS32Tests[] = 433 { 434 { -429496729, 429496729 }, 435 { INT32_MIN, INT32_MAX }, 436 { INT32_MIN, INT32_MAX / 2 }, 437 { -0x20000000, INT32_MAX }, 438 { -0x10000000, INT32_MAX }, 439 { -0x080000000, INT32_MAX }, 440 { -0x008000000, INT32_MAX }, 441 { -0x000800000, INT32_MAX }, 442 { -0x000080000, INT32_MAX }, 443 { -0x000008000, INT32_MAX }, 444 { 2, INT32_MAX / 2 }, 445 { 4000000, INT32_MAX / 2 }, 446 { -4000000, INT32_MAX / 2 }, 447 { INT32_MIN / 2, INT32_MAX / 2 }, 448 { INT32_MIN / 3, INT32_MAX / 2 }, 449 { INT32_MIN / 3, INT32_MAX / 3 }, 450 { INT32_MIN / 3, INT32_MAX / 4 }, 451 { INT32_MIN / 4, INT32_MAX / 4 }, 452 { INT32_MIN / 5, INT32_MAX / 5 }, 453 { INT32_MIN / 6, INT32_MAX / 6 }, 454 { INT32_MIN / 7, INT32_MAX / 6 }, 455 { INT32_MIN / 7, INT32_MAX / 7 }, 456 { INT32_MIN / 7, INT32_MAX / 8 }, 457 { INT32_MIN / 8, INT32_MAX / 8 }, 458 { INT32_MIN / 9, INT32_MAX / 9 }, 459 { INT32_MIN / 9, INT32_MAX / 12 }, 460 { INT32_MIN / 12, INT32_MAX / 12 }, 461 { 0, TST_RAND_SAMPLE_RANGES - 1 }, 462 { -TST_RAND_SAMPLE_RANGES / 2, TST_RAND_SAMPLE_RANGES / 2 - 1 }, 463 }; 464 for (unsigned iTest = 0; iTest < RT_ELEMENTS(s_aS32Tests); iTest++) 465 { 466 uint32_t acHits[TST_RAND_SAMPLE_RANGES] = {0}; 467 int32_t const iFirst = s_aS32Tests[iTest].i32First; 468 int32_t const iLast = s_aS32Tests[iTest].i32Last; 469 uint32_t const uRange = iLast - iFirst; AssertMsg(iLast >= iFirst, ("%d\n", iTest)); 470 uint32_t const uDivisor = (uRange ? uRange : UINT32_MAX) / TST_RAND_SAMPLE_RANGES + 1; 471 RTPrintf("tstRand: TESTING RTRandS32Ex(%#RI32, %#RI32) distribution... [div=%#RX32 range=%#RX32]\n", iFirst, iLast, uDivisor, uRange); 472 for (unsigned iSample = 0; iSample < TST_RAND_SAMPLE_RANGES * 10240; iSample++) 473 { 474 int32_t iRand = RTRandS32Ex(iFirst, iLast); 475 CHECK_EXPR_MSG(iRand >= iFirst, ("%#RI32 %#RI32\n", iRand, iFirst)); 476 CHECK_EXPR_MSG(iRand <= iLast, ("%#RI32 %#RI32\n", iRand, iLast)); 477 uint32_t off = iRand - iFirst; 478 acHits[off / uDivisor]++; 479 } 480 tstRandCheckDist(acHits, iTest); 481 } 482 #endif 483 484 #if 1 485 /* signed 64-bit */ 486 static const struct 487 { 488 int64_t i64First; 489 int64_t i64Last; 490 } s_aS64Tests[] = 491 { 492 { INT64_MIN, INT64_MAX }, 493 { INT64_MIN, INT64_MAX / 2 }, 494 { INT64_MIN / 2, INT64_MAX / 2 }, 495 { INT64_MIN / 2 + INT64_MIN / 4, INT64_MAX / 2 }, 496 { INT64_MIN / 2 + INT64_MIN / 8, INT64_MAX / 2 }, 497 { INT64_MIN / 2 + INT64_MIN / 16, INT64_MAX / 2 }, 498 { INT64_MIN / 2 + INT64_MIN / 64, INT64_MAX / 2 }, 499 { INT64_MIN / 2 + INT64_MIN / 64, INT64_MAX / 2 + INT64_MAX / 64 }, 500 { INT64_MIN / 2, INT64_MAX / 2 + INT64_MAX / 64 }, 501 { INT64_MIN / 2, INT64_MAX / 2 + INT64_MAX / 8 }, 502 { INT64_MIN / 2, INT64_MAX / 2 - INT64_MAX / 8 }, 503 { INT64_MIN / 2 - INT64_MIN / 4, INT64_MAX / 2 - INT64_MAX / 4 }, 504 { INT64_MIN / 2 - INT64_MIN / 4, INT64_MAX / 2 - INT64_MAX / 8 }, 505 { INT64_MIN / 2 - INT64_MIN / 8, INT64_MAX / 2 - INT64_MAX / 8 }, 506 { INT64_MIN / 2 - INT64_MIN / 16, INT64_MAX / 2 - INT64_MAX / 8 }, 507 { INT64_MIN / 2 - INT64_MIN / 16, INT64_MAX / 2 - INT64_MAX / 16 }, 508 { INT64_MIN / 2 - INT64_MIN / 32, INT64_MAX / 2 - INT64_MAX / 16 }, 509 { INT64_MIN / 2 - INT64_MIN / 32, INT64_MAX / 2 - INT64_MAX / 32 }, 510 { INT64_MIN / 2 - INT64_MIN / 64, INT64_MAX / 2 - INT64_MAX / 64 }, 511 { INT64_MIN / 2 - INT64_MIN / 8, INT64_MAX / 2 }, 512 { INT64_MIN / 4, INT64_MAX / 4 }, 513 { INT64_MIN / 5, INT64_MAX / 5 }, 514 { INT64_MIN / 6, INT64_MAX / 6 }, 515 { INT64_MIN / 7, INT64_MAX / 7 }, 516 { INT64_MIN / 8, INT64_MAX / 8 }, 517 { INT32_MIN, INT32_MAX }, 518 { INT32_MIN, INT32_MAX / 2 }, 519 { -0x20000000, INT32_MAX }, 520 { -0x10000000, INT32_MAX }, 521 { -0x7f000000, INT32_MAX }, 522 { -0x08000000, INT32_MAX }, 523 { -0x00800000, INT32_MAX }, 524 { -0x00080000, INT32_MAX }, 525 { -0x00008000, INT32_MAX }, 526 { 2, INT32_MAX / 2 }, 527 { 4000000, INT32_MAX / 2 }, 528 { -4000000, INT32_MAX / 2 }, 529 { INT32_MIN / 2, INT32_MAX / 2 }, 530 { 0, TST_RAND_SAMPLE_RANGES - 1 }, 531 { -TST_RAND_SAMPLE_RANGES / 2, TST_RAND_SAMPLE_RANGES / 2 - 1 } 532 }; 533 for (unsigned iTest = 0; iTest < RT_ELEMENTS(s_aS64Tests); iTest++) 534 { 535 uint32_t acHits[TST_RAND_SAMPLE_RANGES] = {0}; 536 int64_t const iFirst = s_aS64Tests[iTest].i64First; 537 int64_t const iLast = s_aS64Tests[iTest].i64Last; 538 uint64_t const uRange = iLast - iFirst; AssertMsg(iLast >= iFirst, ("%d\n", iTest)); 539 uint64_t const uDivisor = (uRange ? uRange : UINT64_MAX) / TST_RAND_SAMPLE_RANGES + 1; 313 540 RTPrintf("tstRand: TESTING RTRandS64Ex(%#RI64, %#RI64) distribution... [div=%#RX64 range=%#016RX64]\n", iFirst, iLast, uDivisor, uRange); 314 541 for (unsigned iSample = 0; iSample < TST_RAND_SAMPLE_RANGES * 10240; iSample++) … … 323 550 } 324 551 #endif 552 #endif /* Testing RTRand */ 553 554 #if 1 555 /* 556 * Test the various random generators. 557 */ 558 RTPrintf("tstRand: TESTING RTRandAdvCreateParkerMiller\n"); 559 RTRAND hRand; 560 int rc = RTRandAdvCreateParkMiller(&hRand); 561 CHECK_EXPR_MSG(rc == VINF_SUCCESS, ("rc=%Rrc\n", rc)); 562 if (RT_SUCCESS(rc)) 563 if (tstRandAdv(hRand)) 564 return 1; 565 566 #endif /* Testing RTRandAdv */ 325 567 326 568 /*
注意:
瀏覽 TracChangeset
來幫助您使用更動檢視器