X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Ftests%2Farb.cpp;h=f8a74d3f3f44fc542cc7b9b84a969d25fcf42786;hp=00bfefb301992ccd68e7c0509c647ba95793485d;hb=e35bf651e7ebfe4932e877780bb00397c41a7ec2;hpb=a7da45c62d5a0604785479f5dfeb1c2a65d0f9de diff --git a/src/tests/arb.cpp b/src/tests/arb.cpp index 00bfefb..f8a74d3 100644 --- a/src/tests/arb.cpp +++ b/src/tests/arb.cpp @@ -7,49 +7,115 @@ using namespace std; using namespace IPDF; -#define TEST_CASES 100 +#define TEST_CASES 10 -int main(int argc, char ** argv) +void TestShifting(unsigned cases) { - + for (unsigned c = 0; c < cases; ++c) + { + Arbint aa(1u, rand()); + Arbint bb(aa); + for (unsigned i = 0; i < 100*sizeof(Arbint::digit_t); ++i) + { + bb <<= i; + //Debug("i is %u bb is %c%s, a was %c%s", i, bb.SignChar(), bb.DigitStr().c_str(), aa.SignChar(), aa.DigitStr().c_str()); + bb >>= i; + if (bb != aa) + { + Fatal("i is %u bb is %c%s, a was %c%s", i, bb.SignChar(), bb.DigitStr().c_str(), aa.SignChar(), aa.DigitStr().c_str()); + } + } + } +} - for (unsigned i = 0; i < TEST_CASES; ++i) +void TestMaths(unsigned cases) +{ + for (unsigned i = 0; i < cases; ++i) { - Arbint::digit_t a = rand(); - Arbint::digit_t b = rand(); + int64_t a = rand(); + int64_t b = rand(); Arbint arb_a(a); Arbint arb_b(b); - Debug("Test %u: a = %li, b = %li", i, a, b); + //Debug("Test %u: a = %li, b = %li", i, a, b); - Debug("a < b = %d", a < b); - Debug("Arb a b = %d", a > b); - Debug("Arb a>b = %d", arb_a > arb_b); + //Debug("a > b = %d", a > b); + //Debug("Arb a>b = %d", arb_a > arb_b); assert((arb_a > arb_b) == (a > b)); - Debug("a + b = %.16lx", a+b); - Debug("Arb a+b = %s", (arb_a+arb_b).DigitStr().c_str()); + //Debug("a + b = %.16lx", a+b); + //Debug("Arb a+b = %s", (arb_a+arb_b).DigitStr().c_str()); assert((arb_a+arb_b).AsDigit() == a + b); - Debug("a - b = %li %.16lx", (a-b), (a-b)); - Debug("Arb a-b = %s %.16lx", (arb_a-arb_b).DigitStr().c_str(), (arb_a-arb_b).AsDigit()); + //Debug("a - b = %li %.16lx", (a-b), (a-b)); + //Debug("Arb a-b = %s %.16lx", (arb_a-arb_b).DigitStr().c_str(), (arb_a-arb_b).AsDigit()); assert((arb_a-arb_b).AsDigit() == a - b); - Debug("a * b = %.16lx", a*b); - Debug("Arb a*b = %s", (arb_a*arb_b).DigitStr().c_str()); + //Debug("a * b = %.16lx", a*b); + //Debug("Arb a*b = %s", (arb_a*arb_b).DigitStr().c_str()); assert((arb_a*arb_b).AsDigit() == a * b); - Debug("a / b = %.16lx", a/b); - Debug("Arb a/b = %s", (arb_a/arb_b).DigitStr().c_str()); + //Debug("a / b = %.16lx", a/b); + //Debug("Arb a/b = %s", (arb_a/arb_b).DigitStr().c_str()); assert((arb_a/arb_b).AsDigit() == a / b); assert((arb_a%arb_b).AsDigit() == a % b); + } +} + +void TestConstructFromDouble(unsigned cases) +{ + for (unsigned c = 0; c < cases; ++c) + { + int64_t test = (int64_t)(rand()*1e6); + double d = (double)(test); + Arbint a(d); + Arbint b(test); + if (a != b) + { + Fatal("test is %li, d is %f, a is %s, b is %s", test, d, a.DigitStr().c_str(), b.DigitStr().c_str()); + } + + } - printf("All single digit tests successful!\n"); +} + +void TestSubtraction() +{ + Arbint b(2u, 45L, 10L); + Arbint a(1u, 128L, 0L); + Arbint c(a - b); + Arbint test = -Arbint(2u, 18446744073709551533LU,9L); + Debug("%c%s - %c%s = %c%s", a.SignChar(), a.DigitStr().c_str(), b.SignChar(), b.DigitStr().c_str(), c.SignChar(), c.DigitStr().c_str()); + if (c != test) + { + Fatal("Expected %c%s, got %c%s", test.SignChar(), test.DigitStr().c_str(), c.SignChar(), c.DigitStr().c_str()); + } +} + +int main(int argc, char ** argv) +{ + + Debug("Shift testing..."); + TestShifting(TEST_CASES); + Debug("Left/Right shift testing succeeded"); + + Debug("Testing +-*/%"); + TestMaths(TEST_CASES); + Debug("All (single digit) maths operator tests successful!"); + + + Debug("Testing construct from double"); + TestConstructFromDouble(TEST_CASES); + Debug("Construct from double successful"); + Debug("Testing subtractions"); + TestSubtraction(); + Debug("Testing subtractions successful"); return 0; }