00001
00332 #include <time.h>
00333 #include "ctl/ctldef.h"
00334
00335 #ifndef CTL_UNIT
00336
00340 ppC(bool _ctl_dummy_true(void) { return true; })
00341 ppC(bool _ctl_dummy_false(void) { return false; })
00342
00343
00349 size_t int32_NumPlaces( int32 val )
00350 {
00351 size_t ret = 0;
00352 if( val < 0 )
00353 {
00354 val = -val;
00355 ret = 1;
00356 if( val < 0 )
00357 return 11;
00358 }
00359 return ret + uint32_NumPlaces( val );
00360 }
00366 size_t uint32_NumPlaces( uint32 val )
00367 {
00368 if( val >= 100000u )
00369 {
00370 if( val >= 100000000u )
00371 {
00372 if( val >= 1000000000u )
00373 {
00374 return 10;
00375 }
00376 return 9;
00377 }
00378 else if( val >= 1000000u )
00379 {
00380 if( val >= 10000000u )
00381 return 8;
00382 return 7;
00383 }
00384 return 6;
00385 }
00386 else if( val >= 1000u )
00387 {
00388 if( val >= 10000u )
00389 return 5;
00390 return 4;
00391 }
00392 else if( val >= 10u )
00393 {
00394 if( val >= 100u )
00395 return 3;
00396 return 2;
00397 }
00398 return 1;
00399 }
00400
00406 size_t int64_NumPlaces( int64 val )
00407 {
00408 size_t ret = 0;
00409 if( val < 0 )
00410 {
00411 val = -val;
00412 ret = 1;
00413 if( val < 0 )
00414 return 20;
00415 }
00416 return ret + uint64_NumPlaces( val );
00417 }
00418
00424 size_t uint64_NumPlaces( uint64 val )
00425 {
00426
00427 if( val > uint64const(billion(1,000,000,000)) )
00428 {
00429 if( val >= uint64const(quadrillion(1,000,000,000,000,000)) )
00430 {
00431 if( val >= uint64const(quintillion(1,000,000,000,000,000,000)) )
00432 {
00433 if( val >= uint64const(quintillion(10,000,000,000,000,000,000)) )
00434 return 20;
00435
00436 return 19;
00437 }
00438 else
00439 {
00440 if( val >= uint64const(quadrillion(100,000,000,000,000,000)) )
00441 return 18;
00442 if( val >= uint64const(quadrillion(10,000,000,000,000,000)) )
00443 return 17;
00444
00445 return 16;
00446 }
00447 }
00448 else if( val >= uint64const(trillion(1,000,000,000,000)) )
00449 {
00450 if( val >= uint64const(trillion(100,000,000,000,000)) )
00451 return 15;
00452 if( val >= uint64const(trillion(10,000,000,000,000)) )
00453 return 14;
00454
00455 return 13;
00456 }
00457 else
00458 {
00459 if( val >= uint64const(billion(100,000,000,000)) )
00460 return 12;
00461 if( val >= uint64const(billion(10,000,000,000)) )
00462 return 11;
00463
00464 return 10;
00465 }
00466 }
00467 return uint32_NumPlaces( (uint32)val );
00468
00469 }
00470
00476 size_t uint32_XNumPlaces( uint32 val )
00477 {
00478 size_t ret = 1;
00479 if( val >= 0xffff0000 )
00480 {
00481 ret += 4;
00482 val >>= 16;
00483 }
00484 if( val >= 0xff00 )
00485 {
00486 ret += 2;
00487 val >>= 8;
00488 }
00489 if( val >= 0xf0 )
00490 {
00491 ret++;
00492 }
00493 return ret;
00494 }
00500 size_t uint64_XNumPlaces( uint64 val )
00501 {
00502 if( val > 0xffffffff )
00503 {
00504 val >>= 32;
00505 return 32+uint32_XNumPlaces( (uint32)val );
00506 }
00507 return uint32_XNumPlaces( (uint32)val );
00508 }
00509
00510
00511 #else
00512 #include "unit/unit.h"
00513
00514
00521 void Test_UnitShifts(void)
00522 {
00523
00524 #define Test_Sizeof(type,size) { int ppConcat(val_,type) = (int)sizeof(type); Test_Error( ppConcat(val_,type) == size ); }
00525 #define Test_Constant(type,val,shouldbe) { type ppConcat(val_,type) = (val); Test_Error( ppConcat(val_,type) == (shouldbe) ); }
00526 Test_Sizeof( int8, 1 );
00527 Test_Sizeof( uint8, 1 );
00528 Test_Sizeof( int16, 2 );
00529 Test_Sizeof( uint16, 2 );
00530 Test_Sizeof( int32, 4 );
00531 Test_Sizeof( uint32, 4 );
00532 Test_Sizeof( int64, 8 );
00533 Test_Sizeof( uint64, 8 );
00534 Test_Sizeof( float32, 4 );
00535 Test_Sizeof( float64, 8 );
00536
00537 {
00538 const uint16 c16_msb = ppBits16_msb( 1,0,0,1, 1,0,1,0, 1,0,1,1, 1,1,0,0 );
00539 const uint16 c16_lsb = ppBits16_lsb( 1,0,0,1, 1,0,1,0, 1,0,1,1, 1,1,0,0 );
00540 Test_Error( c16_msb == 0x9abc );
00541 Test_Error( c16_lsb == 0x3d59 );
00542 Test_Constant(uint32, ppBits32_msb( 1,0,0,0, 1,0,0,1, 1,0,1,0, 1,0,1,1, 1,1,0,0, 1,1,0,1, 1,1,1,0, 1,1,1,1 ), 0x89abcdefu );
00543 Test_Constant(uint32, ppBits32_lsb( 1,0,0,0, 1,0,0,1, 1,0,1,0, 1,0,1,1, 1,1,0,0, 1,1,0,1, 1,1,1,0, 1,1,1,1 ), 0xf7b3d591u );
00544 }
00545 {
00546 const uint32 val = 1 ppOps( * 10, 3 );
00547 Test_Error( val == 1000 );
00548 Test_Error( ppExponent( 10, 4 ) == 10000 );
00549 Test_Error( ppExponent( 2, 8 ) == 256 );
00550 Test_Error( ppExponent( 3, 3 ) == 27 );
00551 Test_Error( ppExponent( 10, 0 ) == 1 );
00552 }
00553
00554 {
00555 int curr;
00556 curr = 0;
00557 for( curr = 0; curr < 1000; ++curr )
00558 {
00559 int tally = 0;
00560 ppDuff( 4, curr, tally++ );
00561 Test_Error( tally == curr );
00562
00563 tally = 0;
00564 ppDuff( 8, curr, tally++ );
00565 Test_Error( tally == curr );
00566
00567 tally = 0;
00568 ppDuff( 32, curr, tally++ );
00569 Test_Error( tally == curr );
00570
00571 tally = 0;
00572 ppDuff( 64, curr, tally++ );
00573 Test_Error( tally == curr );
00574
00575 tally = 0;
00576 ppDuff( 128, curr, tally++ );
00577 Test_Error( tally == curr );
00578
00579 tally = 0;
00580 ppDuff( 256, curr, tally++ );
00581 Test_Error( tally == curr );
00582 }
00583 }
00584
00585 {
00586 int tally;
00587 int p1,p2,p3,p4,p5;
00588 p1=p2=p3=p4=p5=tally = 0;
00589
00590 #define BeginCheck()\
00591 {\
00592 tally=0;\
00593 \
00594 }
00595 #define DoCheck()\
00596 {\
00597 tally++;\
00598 \
00599 }
00600 BeginCheck();
00601 exponents_2( p1,p2, 0,4 )
00602 DoCheck();
00603 Test_Error( tally == exponents_2_calc(0,4) );
00604 BeginCheck();
00605 arrangements_2( p1,p2, 0,4 )
00606 DoCheck();
00607 Test_Error( tally == arrangements_2_calc(0,4) );
00608 BeginCheck();
00609 combinations_2( p1,p2, 0,4 )
00610 DoCheck();
00611 Test_Error( tally == combinations_2_calc(0,4) );
00612
00613 BeginCheck()
00614 exponents_3( p1,p2,p3, 0,5 )
00615 DoCheck();
00616 Test_Error( tally == exponents_3_calc(0,5) );
00617 BeginCheck();
00618 arrangements_3( p1,p2,p3,0,5 )
00619 DoCheck();
00620 Test_Error( tally == arrangements_3_calc(0,5) );
00621 BeginCheck();
00622 combinations_3( p1,p2,p3, 0,5 )
00623 DoCheck();
00624 Test_Error( tally == combinations_3_calc(0,5) );
00625
00626 BeginCheck()
00627 exponents_4( p1,p2,p3,p4, 0,6 )
00628 DoCheck();
00629 Test_Error( tally == exponents_4_calc(0,6) );
00630 BeginCheck();
00631 arrangements_4( p1,p2,p3,p4, 0,6 )
00632 DoCheck();
00633 Test_Error( tally == arrangements_4_calc(0,6) );
00634 BeginCheck();
00635 combinations_4( p1,p2,p3,p4, 0,6 )
00636 DoCheck();
00637 Test_Error( tally == combinations_4_calc(0,6) );
00638
00639 BeginCheck()
00640 exponents_5( p1,p2,p3,p4,p5, 0,7 )
00641 DoCheck();
00642 Test_Error( tally == exponents_5_calc(0,7) );
00643 BeginCheck();
00644 arrangements_5( p1,p2,p3,p4,p5, 0,7 )
00645 DoCheck();
00646 Test_Error( tally == arrangements_5_calc(0,7) );
00647 BeginCheck();
00648 combinations_5( p1,p2,p3,p4,p5, 0,7 )
00649 DoCheck();
00650 Test_Error( tally == combinations_5_calc(0,7) );
00651 }
00652 };
00653
00657 void Test_ppRand(void)
00658 {
00659 ppRandSeed_T seed = 1;
00660 int remain = 10000;
00661 while( remain-- )
00662 {
00663 #if 1
00664 ppRandSeed_T val = ppRand(seed);
00665 Test_Error( val >= 0 );
00666 #else
00667 double val = ppfRand(seed);
00668 Test_Error( val >= 0.0 && val <= 1.0 );
00669 #endif
00670 }
00671 Test_Error( 1043618065 == seed );
00672 }
00673
00674
00675 #endif
00676