ffbf74efeb195ef34f6247d823c444f6a5ccc3a1
[ipdf/code.git] / src / tests / arb.cpp
1 #include <cstdlib>
2 #include <cstdio>
3 #include <iostream>
4 #include <cassert>
5 #include "arbint.h"
6
7 using namespace std;
8 using namespace IPDF;
9
10 #define TEST_CASES 100
11
12
13 void TestShifting(unsigned cases)
14 {
15         for (unsigned c = 0; c < cases; ++c)
16         {
17                 Arbint aa(1u, rand());
18                 Arbint bb(aa);
19                 for (unsigned i = 0; i < 100*sizeof(Arbint::digit_t); ++i)
20                 {
21                         bb <<= i;
22                         //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());
23                         bb >>= i;
24                         if (bb != aa)
25                         {
26                                 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());
27                         }
28                 }
29         }
30 }
31
32 void TestMaths(unsigned cases)
33 {
34         for (unsigned i = 0; i < cases; ++i)
35         {
36                 int64_t a = rand();
37                 int64_t b = rand();
38                 
39                 Arbint arb_a(a);
40                 Arbint arb_b(b);
41                 
42                 //Debug("Test %u: a = %li, b = %li", i, a, b);
43                 
44                 //Debug("a < b = %d", a < b);
45                 //Debug("Arb a<b = %d", arb_a < arb_b);
46                 assert((arb_a < arb_b) == (a < b));
47                 
48                 //Debug("a > b = %d", a > b);
49                 //Debug("Arb a>b = %d", arb_a > arb_b);
50                 assert((arb_a > arb_b) == (a > b));
51                 
52                 //Debug("a + b = %.16lx", a+b);
53                 //Debug("Arb a+b = %s", (arb_a+arb_b).DigitStr().c_str());
54                 assert((arb_a+arb_b).AsDigit() == a + b);
55                 
56                 //Debug("a - b = %li %.16lx", (a-b), (a-b));
57                 //Debug("Arb a-b = %s %.16lx", (arb_a-arb_b).DigitStr().c_str(), (arb_a-arb_b).AsDigit());
58                 assert((arb_a-arb_b).AsDigit() == a - b);
59                 
60                 //Debug("a * b = %.16lx", a*b);
61                 //Debug("Arb a*b = %s", (arb_a*arb_b).DigitStr().c_str());
62                 assert((arb_a*arb_b).AsDigit() == a * b);
63                 
64                 //Debug("a / b = %.16lx", a/b);
65                 //Debug("Arb a/b = %s", (arb_a/arb_b).DigitStr().c_str());
66                 assert((arb_a/arb_b).AsDigit() == a / b);
67                 assert((arb_a%arb_b).AsDigit() == a % b);
68         }       
69 }
70
71 void TestConstructFromDouble(unsigned cases)
72 {
73         for (unsigned c = 0; c < cases; ++c)
74         {
75                 int64_t test = (int64_t)(rand()*1e6);
76                 double d = (double)(test);
77                 Arbint a(d);
78                 Arbint b(test);
79                 if (a != b)
80                 {
81                         Fatal("test is %li, d is %f, a is %s, b is %s", test, d, a.DigitStr().c_str(), b.DigitStr().c_str());
82                 }
83                 
84                 
85         }
86 }
87
88 void TestSubtraction()
89 {
90         
91 }
92
93 int main(int argc, char ** argv)
94 {
95         Debug("Shift testing...");
96         TestShifting(TEST_CASES);
97         Debug("Left/Right shift testing succeeded");
98
99         Debug("Testing +-*/%");
100         TestMaths(TEST_CASES);
101         Debug("All (single digit) maths operator tests successful!");
102         
103         
104         Debug("Testing construct from double");
105         TestConstructFromDouble(TEST_CASES);
106         Debug("Construct from double successful");
107         return 0;
108 }

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