X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Ftests%2Farb.cpp;h=ffbf74efeb195ef34f6247d823c444f6a5ccc3a1;hp=00bfefb301992ccd68e7c0509c647ba95793485d;hb=b02dcbab39b8c28b9baa41436842ca9fe4ae7ffd;hpb=a7da45c62d5a0604785479f5dfeb1c2a65d0f9de;ds=sidebyside diff --git a/src/tests/arb.cpp b/src/tests/arb.cpp index 00bfefb..ffbf74e 100644 --- a/src/tests/arb.cpp +++ b/src/tests/arb.cpp @@ -10,46 +10,99 @@ using namespace IPDF; #define TEST_CASES 100 -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() +{ + +} + +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"); return 0; }