Improved "realops" tester
[ipdf/code.git] / src / tests / realtofloat.cpp
index b09bd49..c475ea9 100644 (file)
@@ -1,9 +1,26 @@
 #include "main.h"
-float test = 1e4;
+#include <cmath>
+float test_min = 0;
+float test_max = 1e-6;
+unsigned test_count = 100;
+float error_thresh = (test_max - test_min)/1e1;
 int main(int argc, char ** argv)
 {
-       Real r(test);
-       Debug("test float %.20f", test);
-       Debug("test real %.20f", Float(r));
+       Debug("TEST STARTING - Comparing Float(Real(test)) for %u trials between %.20f and %.20f", test_count, test_min, test_max);
+       float error;
+       for (unsigned i = 0; i < test_count; ++i)
+       {
+               float test = test_min + (test_max-test_min)*((float)(rand() % (int)1e6)/1e6);
+               Real real(test);
+               float thiserror = abs(test - Float(real));
+               error += thiserror;
+               Debug("#%u: |test %.20f - real %.20f| = %.20f [mean %f]", i, test, Float(real), thiserror, error/(i+1));
+       }
+
+       if (error/test_count > error_thresh)
+       {
+               Fatal("TEST FAILED - Average error %.20f exceeds threshold %.20f", error/test_count, error_thresh);
+       }
+       Debug("TEST SUCCEEDED - Average error %.20f is %f percent of range", error/test_count, 1e2*(error/test_count) / (test_max - test_min));
        return 0;
 }

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