Now with GNU MultiPrecision (GMP) integers
[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 10
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         Arbint b(2u, 45L, 10L);
91         Arbint a(1u, 128L, 0L);
92         Arbint c(a - b);
93         Arbint test = -Arbint(2u, 18446744073709551533LU,9L);
94         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());
95         if (c != test)
96         {
97                 Fatal("Expected %c%s, got %c%s", test.SignChar(), test.DigitStr().c_str(), c.SignChar(), c.DigitStr().c_str());
98         }
99 }
100
101 int main(int argc, char ** argv)
102 {
103         
104         Debug("Shift testing...");
105         TestShifting(TEST_CASES);
106         Debug("Left/Right shift testing succeeded");
107
108         Debug("Testing +-*/%");
109         TestMaths(TEST_CASES);
110         Debug("All (single digit) maths operator tests successful!");
111         
112         
113         Debug("Testing construct from double");
114         TestConstructFromDouble(TEST_CASES);
115         Debug("Construct from double successful");
116         
117         Debug("Testing subtractions");
118         TestSubtraction();
119         Debug("Testing subtractions successful");
120         return 0;
121 }

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