8076a1416fea8cc303c767e00fc18b51df116197
[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 bool NotEqual(double a, double b, double threshold=1e-2)
10 {
11         return (fabs(a-b) > threshold);
12 }
13
14 int main(int argc, char ** argv)
15 {
16         srand(time(NULL));
17         
18         unsigned failures = 0;
19         for (unsigned i = 0; i < TEST_CASES; ++i)
20         {
21                 double da = (double)(rand()%100 + 1) / (double)(rand()%100 + 1);
22                 double db = (double)(rand()%100 + 1) / (double)(rand()%100 + 1);
23                 
24                 if (rand() % 2 == 0)
25                         da = -da;
26                 if (rand() % 2 == 0)
27                         db = -db;
28                 
29                 Real a(da);
30                 Real b(db);
31                 Real aa(a);
32                 Real bb(b);
33                 unsigned old_failures = failures;
34                 if (NotEqual(Double(a), Double(aa)))
35                 {
36                         failures++;
37                         Warn("a != Real(a); %f vs %f", Double(a), Double(aa));
38                 }
39                 if (NotEqual(Double(b), Double(bb)))
40                 {
41                         failures++;
42                         Warn("b != Real(b); %f vs %f", Double(b), Double(bb));
43                 }
44                 
45                 if (NotEqual(Double(a), da))
46                 {
47                         failures++;
48                         Warn("a != da; %f vs %f", Double(a), da);
49                 }
50                 if (NotEqual(Double(b), db))
51                 {
52                         failures++;                     
53                         Warn("b != db; %f vs %f", Double(b), db);
54                 }
55                 if (NotEqual(Double(a+b), da+db))
56                 {
57                         failures++;                     
58                         Warn("a + b = %f should be %f", Double(a+b), da+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
76                 if (NotEqual(Double(a), da))
77                 {
78                         failures++;                     
79                         Warn("a has changed after +-*/ from %f to %f", da, Double(a));
80                 }
81                 if (NotEqual(Double(b), db))
82                 {
83                         failures++;                     
84                         Warn("b has changed after +-*/ from %f to %f", db, Double(b));
85                 }
86                 Real abeforeop(a);
87                 if (NotEqual(Double(a+=b), da+=db))
88                 {
89                         failures++;                     
90                         Warn("a += b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
91                 }
92                 abeforeop = a;
93                 if (NotEqual(Double(a-=b), da-=db))
94                 {
95                         failures++;                     
96                         Warn("a -= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
97                 }
98                 abeforeop = a;
99                 if (NotEqual(Double(a*=b), da*=db))
100                 {
101                         failures++;                     
102                         Warn("a *= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
103                 }
104                 abeforeop = a;
105                 if (NotEqual(Double(a/=b), da/=db))
106                 {
107                         failures++;
108                         Warn("a /= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
109                 }               
110                 
111                 if (failures > old_failures)
112                 {
113                         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));
114                         #if REAL == REAL_RATIONAL || REAL == REAL_RATIONAL_ARBINT
115                                 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());
116                         #endif
117                 }
118         }
119         Debug("Completed %u test cases with total of %u operations, %u failures", TEST_CASES, 12*TEST_CASES, failures);
120
121 }

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