X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Ftests%2Farb.cpp;h=f8a74d3f3f44fc542cc7b9b84a969d25fcf42786;hp=354c337801512e362675624d0a69c3a336f9662d;hb=68dfceb76cbc78e1e0dd529dd0c14be116528353;hpb=6ad7fc3c833837713405b64a50e62dd128cb7a30 diff --git a/src/tests/arb.cpp b/src/tests/arb.cpp index 354c337..f8a74d3 100644 --- a/src/tests/arb.cpp +++ b/src/tests/arb.cpp @@ -1,25 +1,121 @@ #include #include #include - +#include #include "arbint.h" using namespace std; using namespace IPDF; +#define TEST_CASES 10 + + +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()); + } + } + } +} + +void TestMaths(unsigned cases) +{ + for (unsigned i = 0; i < cases; ++i) + { + 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("a < b = %d", a < b); + //Debug("Arb 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()); + 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()); + 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()); + 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()); + 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()); + } + + + } +} + +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) { - int64_t test[] = {12L, 5L}; - test[1] = 0xE000000000000000; - int64_t size = sizeof(test)/sizeof(int64_t); - int64_t mul = 2L; + 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!"); + - int64_t overflow = mul_digits(test, mul, size); + Debug("Testing construct from double"); + TestConstructFromDouble(TEST_CASES); + Debug("Construct from double successful"); - for (int64_t i = 0; i < size; ++i) - { - printf("digit[%li] = %.16lx\n", i, test[i]); - } - printf("Overflow is %.16lx\n", overflow); + Debug("Testing subtractions"); + TestSubtraction(); + Debug("Testing subtractions successful"); + return 0; }