0aeeeb6189fca90a9d3e65a0f8f9d5534d3b5db0
[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-4)
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()) / (double)(rand());
22                 double db = (double)(rand()) / (double)(rand());
23                 
24                 Real a(da);
25                 Real b(db);
26                 
27                 
28                 
29                 if (NotEqual(Double(a), da))
30                 {
31                         failures++;
32                         Warn("a != da; %f vs %f", Double(a), da);
33                 }
34                 if (NotEqual(Double(b), db))
35                 {
36                         failures++;                     
37                         Warn("b != db; %f vs %f", Double(b), db);
38                 }
39                 if (NotEqual(Double(a+b), da+db))
40                 {
41                         failures++;                     
42                         Warn("a + b = %f should be %f", Double(a+b), da+db);
43                 }
44                 if (NotEqual(Double(a-b), da-db))
45                 {
46                         failures++;                     
47                         Warn("a - b = %f should be %f", Double(a-b), da-db);
48                 }
49                 if (NotEqual(Double(a*b), da*db))
50                 {
51                         failures++;                     
52                         Warn("a * b = %f should be %f", Double(a*b), da*db);
53                 }
54                 if (NotEqual(Double(a/b), da/db))
55                 {
56                         failures++;                     
57                         Warn("a / b = %f should be %f", Double(a/b), da/db);
58                 }               
59
60                 if (NotEqual(Double(a), da))
61                 {
62                         failures++;                     
63                         Warn("a has changed after +-*/ from %f to %f", da, Double(a));
64                 }
65                 if (NotEqual(Double(b), db))
66                 {
67                         failures++;                     
68                         Warn("b has changed after +-*/ from %f to %f", db, Double(b));
69                 }
70
71                 if (NotEqual(Double(a+=b), da+=db))
72                 {
73                         failures++;                     
74                         Warn("a += b = %f should be %f", Double(a), da);
75                 }
76                 if (NotEqual(Double(a-=b), da-=db))
77                 {
78                         failures++;                     
79                         Warn("a -= b = %f should be %f", Double(a), da);
80                 }
81                 if (NotEqual(Double(a*=b), da*=db))
82                 {
83                         failures++;                     
84                         Warn("a *= b = %f should be %f", Double(a), da);
85                 }
86                 if (NotEqual(Double(a/=b), da/=db))
87                 {
88                         failures++;
89                         Warn("a /= b = %f should be %f", Double(a), da);
90                 }               
91         }
92         Debug("Completed %u test cases with total of %u operations, %u failures", TEST_CASES, 12*TEST_CASES, failures);
93
94 }

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