400ce8660d03f5a4e04ea580906f0b1e5b437dea
[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 100
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                 double da = (double)(rand()%100 + 1) / (double)(rand()%100 + 1);
26                 double db = (double)(rand()%100 + 1) / (double)(rand()%100 + 1);
27                 
28                 if (rand() % 2 == 0)
29                         da = -da;
30                 if (rand() % 2 == 0)
31                         db = -db;
32                 
33                 Real a(da);
34                 Real b(db);
35                 Real aa(a);
36                 Real bb(b);
37                 unsigned old_failures = failures;
38                 if (NotEqual(Double(a), Double(aa)))
39                 {
40                         failures++;
41                         Warn("a != Real(a); %f vs %f", Double(a), Double(aa));
42                 }
43                 if (NotEqual(Double(b), Double(bb)))
44                 {
45                         failures++;
46                         Warn("b != Real(b); %f vs %f", Double(b), Double(bb));
47                 }
48                 
49                 if (NotEqual(Double(a), da))
50                 {
51                         failures++;
52                         Warn("a != da; %f vs %f", Double(a), da);
53                 }
54                 if (NotEqual(Double(b), db))
55                 {
56                         failures++;                     
57                         Warn("b != db; %f vs %f", Double(b), db);
58                 }
59                 if (NotEqual(Double(a+b), da+db))
60                 {
61                         failures++;                     
62                         Warn("a + b = %f should be %f", Double(a+b), da+db);
63                 }
64                 if (NotEqual(Double(a-b), da-db))
65                 {
66                         failures++;                     
67                         Warn("a - b = %f should be %f", Double(a-b), da-db);
68                 }
69                 if (NotEqual(Double(a*b), da*db))
70                 {
71                         failures++;                     
72                         Warn("a * b = %f should be %f", Double(a*b), da*db);
73                 }
74                 if (NotEqual(Double(a/b), da/db))
75                 {
76                         failures++;                     
77                         Warn("a / b = %f should be %f", Double(a/b), da/db);
78                 }               
79
80                 if (NotEqual(Double(a), da))
81                 {
82                         failures++;                     
83                         Warn("a has changed after +-*/ from %f to %f", da, Double(a));
84                 }
85                 if (NotEqual(Double(b), db))
86                 {
87                         failures++;                     
88                         Warn("b has changed after +-*/ from %f to %f", db, Double(b));
89                 }
90                 Real abeforeop(a);
91                 if (NotEqual(Double(a+=b), da+=db))
92                 {
93                         failures++;                     
94                         Warn("a += b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
95                 }
96                 abeforeop = a;
97                 if (NotEqual(Double(a-=b), da-=db))
98                 {
99                         failures++;                     
100                         Warn("a -= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
101                 }
102                 abeforeop = a;
103                 if (NotEqual(Double(a*=b), da*=db))
104                 {
105                         failures++;                     
106                         Warn("a *= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
107                 }
108                 abeforeop = a;
109                 if (NotEqual(Double(a/=b), da/=db))
110                 {
111                         failures++;
112                         Warn("a /= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
113                 }               
114                 
115                 if (failures > old_failures)
116                 {
117                         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));
118                         #if REAL == REAL_RATIONAL || REAL == REAL_RATIONAL_ARBINT
119                                 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());
120                         #endif
121                 }
122         }
123         Debug("Completed %u test cases with total of %u operations, %u failures", TEST_CASES, 12*TEST_CASES, failures);
124         Debug("Total accumulated difference between Real and Double operations was %f", g_totalerror);
125
126 }

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