#include "unitnumber.h" #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::divide1), "divide1", 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() == "121932631356500531347203169112635269" ); } void UnitNumber::multiply1() { unitTest(Number("123456789") * Number("987654321") == "121932631112635269"); unitTest( Number("123456789123456789") * Number("987654321987654321") == "121932631356500531347203169112635269" ); } void UnitNumber::divide1() { unitTest(Number("4") / Number("10") == "0"); unitTest(Number("4") % Number("10") == "4"); unitTest(Number("200") / Number("2") == "100"); unitTest(Number("10", 5) / Number("0.01", 5) == "1000"); unitTest(Number("552", 5) / Number("0.051", 5) == "10823.52941"); } #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"); } #undef mathTest #define mathTest( anum, op, bnum, scale, answ ) \ unitTest( (Number(anum, scale) op Number(bnum, scale)).toString() == answ ) void UnitNumber::number2() { mathTest("55", +, "-100", 0, "-45"); mathTest("30.01", +, "-31.21", 2, "-1.20"); mathTest("55", -, "100", 0, "-45"); mathTest("30.01", -, "31.21", 2, "-1.20"); // mathTest( "1", /, "17", 10, "0.0588235294" ); // mathTest( "1", /, "177", 10, "0.0056497175" ); } /* // clic 1/17 0.0588235294 1/177 0.0005649717 // bc 1/17 .0588235294 1/177 .0056497175 1/17 bc: .0588235294 clic: 0.0588235294 1/177 bc: .0056497175 clic: 0.0005649717 */ //} #undef mathTest #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() { 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" ); mathTestS( 10, "1", /, "17", "0.0588235294" ); mathTestS( 10, "1", /, "177", "0.0056497175" ); mathTestS( 10, "1", /, "177.1", "0.0056465273" ); }