Define for Transformations on Path only, also fixed segfault due to GraphicsBuffer
[ipdf/code.git] / src / tests / realops.cpp
index 400ce86..4669f59 100644 (file)
@@ -1,16 +1,21 @@
+/**
+ * Test mathematical operations on the Real type and consistency with double
+ */
+
 #include "main.h"
 #include "real.h"
+#include "progressbar.h"
 
 using namespace std;
 using namespace IPDF;
 
-#define TEST_CASES 100
+#define TEST_CASES 1000
 
 static double g_totalerror = 0;
 
-bool NotEqual(double a, double b, double threshold=1e-1)
+bool NotEqual(double a, double b, double threshold=1e-4)
 {
-       double error = (fabs(a-b) > threshold);
+       double error = fabs(a-b);
        g_totalerror += error;
        return (error > threshold);
 }
@@ -18,12 +23,18 @@ bool NotEqual(double a, double b, double threshold=1e-1)
 int main(int argc, char ** argv)
 {
        srand(time(NULL));
+       DebugRealInfo();
        
        unsigned failures = 0;
+       Real acumulate(0);
+       double dacumulate = 0;
+       
        for (unsigned i = 0; i < TEST_CASES; ++i)
        {
-               double da = (double)(rand()%100 + 1) / (double)(rand()%100 + 1);
-               double db = (double)(rand()%100 + 1) / (double)(rand()%100 + 1);
+               ProgressBar(i, TEST_CASES, 50);
+               //Debug("Test %u of %u", i, TEST_CASES);
+               double da = (double)(rand() + 1) / (double)(rand() + 1);
+               double db = (double)(rand() + 1) / (double)(rand() + 1);
                
                if (rand() % 2 == 0)
                        da = -da;
@@ -110,8 +121,62 @@ int main(int argc, char ** argv)
                {
                        failures++;
                        Warn("a /= b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
+               }
+               if (NotEqual(Double(a*0.0 + 1.0), da*0.0 + 1.0))
+               {
+                       failures++;
+                       Warn("a * 0 = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
+               }               
+
+               if (NotEqual(Double(a=b), da=db))
+               {
+                       failures++;
+                       Warn("a = b = %f should be %f, a before op was %f", Double(a), da, Double(abeforeop));
+               }
+               
+               if (NotEqual(Double(-a), -da))
+               {
+                       failures++;
+                       Warn("-a = %f should be %f, a before op was %f", Double(-a), -da, Double(abeforeop));
+               }
+               
+               if (NotEqual(Double(Sqrt(a)), Sqrt(da)))
+               {
+                       failures++;
+                       Warn("Sqrt(a) = %f should be %f, a before op was %f", Double(Sqrt(a)), Sqrt(da), Double(abeforeop));
                }               
                
+               if (NotEqual(Double(a), da))
+               {
+                       failures++;
+                       Warn("a = %f, should be %f, a before ops was %f", Double(a), da, Double(abeforeop));
+               }
+               
+               switch (rand() % 4)
+               {
+                       case 0:
+                               acumulate += a;
+                               dacumulate += da;
+                               break;
+                       case 1:
+                               acumulate -= a;
+                               dacumulate -= da;
+                               break;
+                       case 2:
+                               acumulate *= a;
+                               dacumulate *= da;
+                               break;
+                       case 3:
+                               acumulate /= a;
+                               dacumulate /= da;
+                               break;
+               }
+               if (NotEqual(Double(acumulate), dacumulate))
+               {
+                       Warn("Accumulated result %.30lf vs %.30lf is wrong", Double(acumulate), dacumulate);
+                       failures++;
+               }
+               
                if (failures > old_failures)
                {
                        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));
@@ -120,7 +185,10 @@ int main(int argc, char ** argv)
                        #endif
                }
        }
-       Debug("Completed %u test cases with total of %u operations, %u failures", TEST_CASES, 12*TEST_CASES, failures);
+       Debug("Completed %u test cases with total of %u operations, %u failures", TEST_CASES, 18*TEST_CASES, failures);
        Debug("Total accumulated difference between Real and Double operations was %f", g_totalerror);
+       Debug("Real: %.40lf", Double(acumulate));
+       Debug("Doub: %.40lf", dacumulate);
+       Debug("Diff: %.40lf", Double(acumulate) - dacumulate);
 
 }

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