Make Qt4 work
[ipdf/code.git] / src / tests / arb.cpp
index 29c6325..f8a74d3 100644 (file)
 #include <cstdlib>
 #include <cstdio>
 #include <iostream>
-
-#include "arbint.cpp"
+#include <cassert>
+#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", arb_a < arb_b);
+               assert((arb_a < arb_b) == (a < 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());
+               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)
 {
-       Arbint a(4294967296L);
-       printf("%s\n", a.Str().c_str());
+       
+       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;
 }

UCC git Repository :: git.ucc.asn.au