Arbint subtraction/addition deal with borrow/carry correctly, maybe
[ipdf/code.git] / src / tests / realops.cpp
1 #include "main.h"
2 #include "real.h"
3
4 using namespace std;
5 using namespace IPDF;
6
7 #define TEST_CASES 1000
8
9 static double g_totalerror = 0;
10
11 bool NotEqual(double a, double b, double threshold=1e-1)
12 {
13         double error = (fabs(a-b) > threshold);
14         g_totalerror += error;
15         return (error > threshold);
16 }
17
18 int main(int argc, char ** argv)
19 {
20         srand(time(NULL));
21         
22         unsigned failures = 0;
23         for (unsigned i = 0; i < TEST_CASES; ++i)
24         {
25                 //Debug("Test %u of %u", i, TEST_CASES);
26                 double da = (double)(rand() + 1) / (double)(rand() + 1);
27                 double db = (double)(rand() + 1) / (double)(rand() + 1);
28                 
29                 if (rand() % 2 == 0)
30                         da = -da;
31                 if (rand() % 2 == 0)
32                         db = -db;
33                 
34                 Real a(da);
35                 Real b(db);
36                 Real aa(a);
37                 Real bb(b);
38                 unsigned old_failures = failures;
39                 if (NotEqual(Double(a), Double(aa)))
40                 {
41                         failures++;
42                         Warn("a != Real(a); %f vs %f", Double(a), Double(aa));
43                 }
44                 if (NotEqual(Double(b), Double(bb)))
45                 {
46                         failures++;
47                         Warn("b != Real(b); %f vs %f", Double(b), Double(bb));
48                 }
49                 
50                 if (NotEqual(Double(a), da))
51                 {
52                         failures++;
53                         Warn("a != da; %f vs %f", Double(a), da);
54                 }
55                 if (NotEqual(Double(b), db))
56                 {
57                         failures++;                     
58                         Warn("b != db; %f vs %f", Double(b), db);
59                 }
60                 if (NotEqual(Double(a+b), da+db))
61                 {
62                         failures++;                     
63                         Warn("a + b = %f should be %f", Double(a+b), da+db);
64                 }
65                 if (NotEqual(Double(a-b), da-db))
66                 {
67                         failures++;                     
68                         Warn("a - b = %f should be %f", Double(a-b), da-db);
69                 }
70                 if (NotEqual(Double(a*b), da*db))
71                 {
72                         failures++;                     
73                         Warn("a * b = %f should be %f", Double(a*b), da*db);
74                 }
75                 if (NotEqual(Double(a/b), da/db))
76                 {
77                         failures++;                     
78                         Warn("a / b = %f should be %f", Double(a/b), da/db);
79                 }               
80
81                 if (NotEqual(Double(a), da))
82                 {
83                         failures++;                     
84                         Warn("a has changed after +-*/ from %f to %f", da, Double(a));
85                 }
86                 if (NotEqual(Double(b), db))
87                 {
88                         failures++;                     
89                         Warn("b has changed after +-*/ from %f to %f", db, Double(b));
90                 }
91                 Real abeforeop(a);
92                 if (NotEqual(Double(a+=b), da+=db))
93                 {
94                         failures++;                     
95                         Warn("a += b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
96                 }
97                 abeforeop = a;
98                 if (NotEqual(Double(a-=b), da-=db))
99                 {
100                         failures++;                     
101                         Warn("a -= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
102                 }
103                 abeforeop = a;
104                 if (NotEqual(Double(a*=b), da*=db))
105                 {
106                         failures++;                     
107                         Warn("a *= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
108                 }
109                 abeforeop = a;
110                 if (NotEqual(Double(a/=b), da/=db))
111                 {
112                         failures++;
113                         Warn("a /= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
114                 }               
115                 
116                 if (failures > old_failures)
117                 {
118                         Error("%u failures on case %u da = %f, db = %f, a = %f, b = %f, aa = %f, bb = %f", failures-old_failures, i, da, db, Double(a), Double(b), Double(aa), Double(bb));
119                         #if REAL == REAL_RATIONAL || REAL == REAL_RATIONAL_ARBINT
120                                 Debug("\tStrings are a = %s, b = %s, aa = %s, bb = %s", a.Str().c_str(), b.Str().c_str(), aa.Str().c_str(), bb.Str().c_str());
121                         #endif
122                 }
123         }
124         Debug("Completed %u test cases with total of %u operations, %u failures", TEST_CASES, 12*TEST_CASES, failures);
125         Debug("Total accumulated difference between Real and Double operations was %f", g_totalerror);
126
127 }

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