From dc0139c1880d1500b3e275b09781116613862f5f Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 10 May 2013 08:36:15 -0600 Subject: Added more checking & unit-tests. --- src/main.cpp | 376 ++--------------------------------------------------- src/number.cpp | 5 + src/options.cpp | 33 +++++ src/options.h | 1 + src/unitnumber.cpp | 236 +++++++++++++++++++++++++++++++++ src/unitnumber.h | 5 + 6 files changed, 293 insertions(+), 363 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c128dd8..4c34453 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,376 +11,26 @@ #include using namespace Bu; -void packedtest1() -{ - println("-==- Packed Int Test -==-"); - - PackedIntArray a(4); - a.append( 3 ); - a.append( 9 ); - a.append( 5 ); - - println("%1").arg( a.toString() ); - println("%1").arg( a.toBitString() ); - println("%1").arg( PackedIntArray(4, 10).toString() ); - - PackedIntArray b(5); - for( int j = 0; j < 16; j++ ) - { - b.append( 21 ); - if( b[j] != 21 ) - { - println("Error at position %1").arg( j ); - println("Raw: %1 (%2)").arg( b.toBitString() ).arg( b.toString() ); - } - } -} - -void numbertest1() -{ - println("-==- Number test -==-"); - - Number a("1000902491523000321"); - Number b("3004392012498000700"); - - println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); - - println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); - - a = "-872"; - b = "123"; - - println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); - - a = "728"; - b = "-51"; - - println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); - - a = "44"; - b = "-55"; - - println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); - - a = "44"; - b = "-66"; - - println("%1 + %2 = %3").arg( a ).arg( b ).arg( a + b ); - - a = "44"; - b = "-66"; - - println("%1 - %2 = %3").arg( a ).arg( b ).arg( a - b ); - - a = "44"; - b = "66"; - - println("%1 - %2 = %3").arg( a ).arg( b ).arg( a - b ); - - a = "7814"; - b = "24"; - - println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); - - a = "12345"; - b = "678"; - - println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); - - a = "3592846"; - b = "944634757"; - - println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); - - a = "3592846"; - b = ""; - - println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); - - a = "123"; - b = "-45"; - - println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); - - a = "-123"; - b = "45"; - - println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); - - a = "-123"; - b = "-45"; - - println("%1 * %2 = %3").arg( a ).arg( b ).arg( a * b ); - - a = "123"; - b = "45"; - - println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); - - a = "12345"; - b = "45"; - - println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); - - a = "3007103450821050020096034077958224700"; - b = "898239467"; - - println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); - println("%1 %% %2 = %3").arg( a ).arg( b ).arg( a % b ); - - a = "983429807324875233421784598754987439873472349875329853298732"; - b = "18446744073709551615"; - - println("%1 / %2 = %3").arg( a ).arg( b ).arg( a / b ); - println("%1 %% %2 = %3").arg( a ).arg( b ).arg( a % b ); -} - -#define compcheck( anum, op, bnum ) \ - a = #anum; b = #bnum; \ - println("%4: %1 " #op " %2 = %3").arg( a ).arg( b ). \ - arg( a op b ).arg( ((a op b) == (anum op bnum)) ? "pass" : "fail" ) - -void numbertestcomp() -{ - Number a, b; - - println("-==- Greater Than -==-"); - compcheck( 5, >, 10 ); - compcheck( 10, >, 5 ); - compcheck( 5, >, 5 ); - compcheck( 7, >, 5 ); - compcheck( 5, >, 7 ); - compcheck( 123, >, 122 ); - compcheck( 123, >, 123 ); - compcheck( 123, >, 124 ); - compcheck( -123, >, 122 ); - compcheck( -123, >, -122 ); - compcheck( -122, >, -123 ); - compcheck( 123, >, -122 ); - - println("-==- Less Than -==-"); - compcheck( 5, <, 10 ); - compcheck( 10, <, 5 ); - compcheck( 5, <, 5 ); - compcheck( 7, <, 5 ); - compcheck( 5, <, 7 ); - compcheck( 123, <, 122 ); - compcheck( 123, <, 123 ); - compcheck( 123, <, 124 ); - compcheck( -123, <, 122 ); - compcheck( -123, <, -122 ); - compcheck( -122, <, -123 ); - compcheck( 123, <, -122 ); - - println("-==- Greater Than or Equal To -==-"); - compcheck( 5, >=, 10 ); - compcheck( 10, >=, 5 ); - compcheck( 5, >=, 5 ); - compcheck( 7, >=, 5 ); - compcheck( 5, >=, 7 ); - compcheck( 123, >=, 122 ); - compcheck( 123, >=, 123 ); - compcheck( 123, >=, 124 ); - compcheck( -123, >=, 122 ); - compcheck( -123, >=, -122 ); - compcheck( -122, >=, -123 ); - compcheck( 123, >=, -122 ); - - println("-==- Less Than or Equal To -==-"); - compcheck( 5, <=, 10 ); - compcheck( 10, <=, 5 ); - compcheck( 5, <=, 5 ); - compcheck( 7, <=, 5 ); - compcheck( 5, <=, 7 ); - compcheck( 123, <=, 122 ); - compcheck( 123, <=, 123 ); - compcheck( 123, <=, 124 ); - compcheck( -123, <=, 122 ); - compcheck( -123, <=, -122 ); - compcheck( -122, <=, -123 ); - compcheck( 123, <=, -122 ); - - println("-==-==- Non-Integer Test -==-==-"); - - a.setScale( 8 ); - b.setScale( 8 ); - println("-==- Greater Than -==-"); - compcheck( 10.1, >, 10.4 ); - compcheck( 10.1, >, 10.1 ); - compcheck( 10.4, >, 10.1 ); - compcheck( 10.413, >, 10.413 ); - compcheck( 10.41329135, >, 10.41329134 ); - compcheck( 10.41329134, >, 10.41329135 ); - compcheck( 10.41329135, >, 10.41329135 ); - compcheck( -123.3, >, 123.2 ); - compcheck( -123.3, >, -123.2 ); - compcheck( -123.3, >, -123.3 ); - compcheck( -123.3, >, -123.2 ); - compcheck( 123.3, >, -123.2 ); - - println("-==- Less Than -==-"); - compcheck( 10.1, <, 10.4 ); - compcheck( 10.1, <, 10.1 ); - compcheck( 10.4, <, 10.1 ); - compcheck( 10.413, <, 10.413 ); - compcheck( 10.41329135, <, 10.41329134 ); - compcheck( 10.41329134, <, 10.41329135 ); - compcheck( 10.41329135, <, 10.41329135 ); - compcheck( -123.3, <, 123.2 ); - compcheck( -123.3, <, -123.2 ); - compcheck( -123.3, <, -123.3 ); - compcheck( -123.3, <, -123.2 ); - compcheck( 123.3, <, -123.2 ); - - println("-==- Greater Than or Equal To -==-"); - compcheck( 10.1, >=, 10.4 ); - compcheck( 10.1, >=, 10.1 ); - compcheck( 10.4, >=, 10.1 ); - compcheck( 10.413, >=, 10.413 ); - compcheck( 10.41329135, >=, 10.41329134 ); - compcheck( 10.41329134, >=, 10.41329135 ); - compcheck( 10.41329135, >=, 10.41329135 ); - compcheck( -123.3, >=, 123.2 ); - compcheck( -123.3, >=, -123.2 ); - compcheck( -123.3, >=, -123.3 ); - compcheck( -123.3, >=, -123.2 ); - compcheck( 123.3, >=, -123.2 ); - - println("-==- Less Than or Equal To -==-"); - compcheck( 10.1, <=, 10.4 ); - compcheck( 10.1, <=, 10.1 ); - compcheck( 10.4, <=, 10.1 ); - compcheck( 10.413, <=, 10.413 ); - compcheck( 10.41329135, <=, 10.41329134 ); - compcheck( 10.41329134, <=, 10.41329135 ); - compcheck( 10.41329135, <=, 10.41329135 ); - compcheck( -123.3, <=, 123.2 ); - compcheck( -123.3, <=, -123.2 ); - compcheck( -123.3, <=, -123.3 ); - compcheck( -123.3, <=, -123.2 ); - compcheck( 123.3, <=, -123.2 ); -} - -int getHob( int x ) -{ - for( int j = 31; j >= 0; j-- ) - { - if( x&(1< 1."); } Number::Number( const Bu::String &sData, int iScale, int iRadix ) : @@ -21,6 +23,9 @@ Number::Number( const Bu::String &sData, int iScale, int iRadix ) : aInt( RadixToBits( iRadix ) ), aFrac( aInt.getBitWidth(), iScale ) { + if( iRadix <= 1 ) + throw Bu::ExceptionBase("Invalid radix. Radix must be > 1."); + set( sData ); } diff --git a/src/options.cpp b/src/options.cpp index 59dd2b4..9ade56a 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -21,6 +21,10 @@ Options::Options( int argc, char *argv[] ) : addOption( Bu::slot(this, &Options::isPrime), 'p', "is-prime", "Tests every parameter after to see if it is prime then prints out " "the ones that are prime. Set radix first."); + addOption( Bu::slot(this, &Options::convert), 'c', "convert", + "Convert the provided number to the given radix in the format " + "number:to-radix or from-radix:number:to-radix. The radix should " + "be provided in base-10"); addHelpOption('h', "help", "This help"); parse( argc, argv ); @@ -118,3 +122,32 @@ int Options::isPrime( Bu::StringArray aArgs ) return aArgs.getSize(); } +int Options::convert( Bu::StringArray aArgs ) +{ + for( Bu::StringArray::iterator i = aArgs.begin()+1; i; i++ ) + { + Bu::StringList lBits = (*i).split(':'); + if( lBits.getSize() < 2 || lBits.getSize() > 3 ) + throw Bu::ExceptionBase("Invalid format"); + + int iFromRadix = iRadix; + int iToRadix; + Number n; + if( lBits.getSize() == 2 ) + { + iToRadix = strtol( lBits.last().getStr(), 0, 10 ); + n = Number( lBits.first(), 0, iFromRadix ); + } + else if( lBits.getSize() == 3 ) + { + iFromRadix = strtol( lBits.first().getStr(), 0, 10 ); + iToRadix = strtol( lBits.last().getStr(), 0, 10 ); + n = Number( *(lBits.begin()+1), 0, iFromRadix ); + } + Bu::println("%1").arg( n.toRadix( iToRadix ).toString() ); + } + + exit( 0 ); + return 0; +} + diff --git a/src/options.h b/src/options.h index 1085e1e..e0b7f33 100644 --- a/src/options.h +++ b/src/options.h @@ -16,6 +16,7 @@ private: int selfTest( Bu::StringArray aArgs ); int textPrimes( Bu::StringArray aArgs ); int isPrime( Bu::StringArray aArgs ); + int convert( Bu::StringArray aArgs ); int iScale; int iRadix; diff --git a/src/unitnumber.cpp b/src/unitnumber.cpp index 79797c9..92cf1b7 100644 --- a/src/unitnumber.cpp +++ b/src/unitnumber.cpp @@ -2,19 +2,57 @@ #include "number.h" +#include + +using namespace Bu; + UnitNumber::UnitNumber() { setName("Number"); + add( static_cast(&UnitNumber::packed1), + "packed1", Bu::UnitSuite::expectPass ); add( static_cast(&UnitNumber::parse1), "parse1", Bu::UnitSuite::expectPass ); add( static_cast(&UnitNumber::multiply1), "multiply1", Bu::UnitSuite::expectPass ); + add( static_cast(&UnitNumber::number1), + "number1", Bu::UnitSuite::expectPass ); + add( static_cast(&UnitNumber::compare1), + "compare1", Bu::UnitSuite::expectPass ); + add( static_cast(&UnitNumber::radix1), + "radix1", Bu::UnitSuite::expectPass ); + add( static_cast(&UnitNumber::fraction1), + "fraction1", Bu::UnitSuite::expectPass ); } UnitNumber::~UnitNumber() { } +void UnitNumber::packed1() +{ + PackedIntArray a(4); + a.append( 3 ); + a.append( 9 ); + a.append( 5 ); + + unitTest( a.toString() == "593" ); + unitTest( a.toBitString() == "0101 1001 0011" ); + unitTest( PackedIntArray( 4, 10 ).toString() == "0000000000" ); + + PackedIntArray b(5); + for( int j = 0; j < 16; j++ ) + { + b.append( 21 ); + if( b[j] != 21 ) + { + throw Bu::ExceptionBase( + Bu::String("Error at position %1").arg( j ).end().getStr() + ); + } + } +} + void UnitNumber::parse1() { unitTest( Number("121932631356500531347203169112635269").toString() == @@ -30,3 +68,201 @@ void UnitNumber::multiply1() ); } +#define mathTest( anum, op, bnum, answ ) \ + unitTest( (Number(anum) op Number(bnum)).toString() == answ ) + +void UnitNumber::number1() +{ + mathTest("1000902491523000321", +, "3004392012498000700", + "4005294504021001021"); + mathTest("1000902491523000321", *, "3004392012498000700", + "3007103450821050020096034077958224700"); + + mathTest("-872", +, "123", "-749"); + mathTest("728", +, "-51", "677"); + mathTest("44", +, "-55", "-11"); + mathTest("44", +, "-66", "-22"); + mathTest("44", -, "-66", "110"); + mathTest("44", -, "66", "-22"); + mathTest("7814", *, "24", "187536"); + mathTest("12345", *, "678", "8369910"); + mathTest("3592846", *, "944634757", "3393927208148422"); + mathTest("3592846", *, "", "0"); + mathTest("123", *, "-45", "-5535"); + mathTest("-123", *, "45", "-5535"); + mathTest("-123", *, "-45", "5535"); + mathTest("123", /, "45", "2"); + mathTest("12345", /, "45", "274"); + mathTest("3007103450821050020096034077958224700", +, "898239467", + "3007103450821050020096034078856464167"); + mathTest("3007103450821050020096034077958224700", -, "898239467", + "3007103450821050020096034077059985233"); + mathTest("3007103450821050020096034077958224700", *, "898239467", + "2701099000879360682431400938999032202794234900"); + mathTest("3007103450821050020096034077958224700", /, "898239467", + "3347774798700812397164501432"); + mathTest("3007103450821050020096034077958224700", %, "898239467", + "357807956"); + + mathTest("983429807324875233421784598754987439873472349875329853298732", +, + "18446744073709551615", + "983429807324875233421784598754987439873490796619403562850347"); + mathTest("983429807324875233421784598754987439873472349875329853298732", -, + "18446744073709551615", + "983429807324875233421784598754987439873453903131256143747117"); + mathTest("983429807324875233421784598754987439873472349875329853298732", *, + "18446744073709551615", + "1814107797017946840561430060771417697390414826725906096177022" + "9365481264368052180"); + mathTest("983429807324875233421784598754987439873472349875329853298732", /, + "18446744073709551615", + "53311836679431538487701428703997840383479"); + mathTest("983429807324875233421784598754987439873472349875329853298732", %, + "18446744073709551615", + "2034904753109530147"); +} + +#define compcheck( anum, op, bnum ) \ + a = #anum; b = #bnum; \ + unitTest( ((a op b) == (anum op bnum)) ) + +void UnitNumber::compare1() +{ + Number a, b; + + compcheck( 5, >, 10 ); + compcheck( 10, >, 5 ); + compcheck( 5, >, 5 ); + compcheck( 7, >, 5 ); + compcheck( 5, >, 7 ); + compcheck( 123, >, 122 ); + compcheck( 123, >, 123 ); + compcheck( 123, >, 124 ); + compcheck( -123, >, 122 ); + compcheck( -123, >, -122 ); + compcheck( -122, >, -123 ); + compcheck( 123, >, -122 ); + + compcheck( 5, <, 10 ); + compcheck( 10, <, 5 ); + compcheck( 5, <, 5 ); + compcheck( 7, <, 5 ); + compcheck( 5, <, 7 ); + compcheck( 123, <, 122 ); + compcheck( 123, <, 123 ); + compcheck( 123, <, 124 ); + compcheck( -123, <, 122 ); + compcheck( -123, <, -122 ); + compcheck( -122, <, -123 ); + compcheck( 123, <, -122 ); + + compcheck( 5, >=, 10 ); + compcheck( 10, >=, 5 ); + compcheck( 5, >=, 5 ); + compcheck( 7, >=, 5 ); + compcheck( 5, >=, 7 ); + compcheck( 123, >=, 122 ); + compcheck( 123, >=, 123 ); + compcheck( 123, >=, 124 ); + compcheck( -123, >=, 122 ); + compcheck( -123, >=, -122 ); + compcheck( -122, >=, -123 ); + compcheck( 123, >=, -122 ); + + compcheck( 5, <=, 10 ); + compcheck( 10, <=, 5 ); + compcheck( 5, <=, 5 ); + compcheck( 7, <=, 5 ); + compcheck( 5, <=, 7 ); + compcheck( 123, <=, 122 ); + compcheck( 123, <=, 123 ); + compcheck( 123, <=, 124 ); + compcheck( -123, <=, 122 ); + compcheck( -123, <=, -122 ); + compcheck( -122, <=, -123 ); + compcheck( 123, <=, -122 ); + + + a.setScale( 8 ); + b.setScale( 8 ); + compcheck( 10.1, >, 10.4 ); + compcheck( 10.1, >, 10.1 ); + compcheck( 10.4, >, 10.1 ); + compcheck( 10.413, >, 10.413 ); + compcheck( 10.41329135, >, 10.41329134 ); + compcheck( 10.41329134, >, 10.41329135 ); + compcheck( 10.41329135, >, 10.41329135 ); + compcheck( -123.3, >, 123.2 ); + compcheck( -123.3, >, -123.2 ); + compcheck( -123.3, >, -123.3 ); + compcheck( -123.3, >, -123.2 ); + compcheck( 123.3, >, -123.2 ); + + compcheck( 10.1, <, 10.4 ); + compcheck( 10.1, <, 10.1 ); + compcheck( 10.4, <, 10.1 ); + compcheck( 10.413, <, 10.413 ); + compcheck( 10.41329135, <, 10.41329134 ); + compcheck( 10.41329134, <, 10.41329135 ); + compcheck( 10.41329135, <, 10.41329135 ); + compcheck( -123.3, <, 123.2 ); + compcheck( -123.3, <, -123.2 ); + compcheck( -123.3, <, -123.3 ); + compcheck( -123.3, <, -123.2 ); + compcheck( 123.3, <, -123.2 ); + + compcheck( 10.1, >=, 10.4 ); + compcheck( 10.1, >=, 10.1 ); + compcheck( 10.4, >=, 10.1 ); + compcheck( 10.413, >=, 10.413 ); + compcheck( 10.41329135, >=, 10.41329134 ); + compcheck( 10.41329134, >=, 10.41329135 ); + compcheck( 10.41329135, >=, 10.41329135 ); + compcheck( -123.3, >=, 123.2 ); + compcheck( -123.3, >=, -123.2 ); + compcheck( -123.3, >=, -123.3 ); + compcheck( -123.3, >=, -123.2 ); + compcheck( 123.3, >=, -123.2 ); + + compcheck( 10.1, <=, 10.4 ); + compcheck( 10.1, <=, 10.1 ); + compcheck( 10.4, <=, 10.1 ); + compcheck( 10.413, <=, 10.413 ); + compcheck( 10.41329135, <=, 10.41329134 ); + compcheck( 10.41329134, <=, 10.41329135 ); + compcheck( 10.41329135, <=, 10.41329135 ); + compcheck( -123.3, <=, 123.2 ); + compcheck( -123.3, <=, -123.2 ); + compcheck( -123.3, <=, -123.3 ); + compcheck( -123.3, <=, -123.2 ); + compcheck( 123.3, <=, -123.2 ); +} + +void UnitNumber::radix1() +{ + Number a( 10, 16 ), b( 10, 16 ); + + a = "1f8a72bbce3"; + b = "9ea8cb3"; + unitTest( (a+b).toString() == "1f8b1164996" ); + unitTest( (a-b).toString() == "1f89d413030" ); + unitTest( (a/b).toString() == "32e4.4826b6b542" ); + unitTest( (a*b).toString() == "138c3eb3e7715f36b9" ); +} + +#define mathTestS( sc, anum, op, bnum, answ ) \ + unitTest( (Number(anum, sc) op Number(bnum, sc)).toString() == answ ) + +void UnitNumber::fraction1() +{ + Number a( 8 ), b( 8 ); + + mathTestS( 8, "123.456", +, "0.987", "124.443" ); + mathTestS( 8, "123.456", -, "0.987", "122.469" ); + mathTestS( 8, "123.456", *, "0.987", "121.851072" ); + mathTestS( 8, "123.456", /, "0.987", "125.08206686" ); + + mathTestS( 8, "12", /, "4", "3" ); + mathTestS( 100, "9", /, "1.9", "4.7368421052631578947368421052631578947368421052631578947368421052631578947368421052631578947368421052" ); +} + diff --git a/src/unitnumber.h b/src/unitnumber.h index 76496b9..83da27f 100644 --- a/src/unitnumber.h +++ b/src/unitnumber.h @@ -9,8 +9,13 @@ public: UnitNumber(); virtual ~UnitNumber(); + void packed1(); void parse1(); void multiply1(); + void number1(); + void compare1(); + void radix1(); + void fraction1(); }; #endif -- cgit v1.2.3