core/testlib/testlib_test.cxx
Go to the documentation of this file.
00001 // This is core/testlib/testlib_test.cxx
00002 #include "testlib_test.h"
00003 //
00004 // Copyright (C) 1991 Texas Instruments Incorporated.
00005 //
00006 // Permission is granted to any individual or institution to use, copy, modify,
00007 // and distribute this software, provided that this complete copyright and
00008 // permission notice is maintained, intact, in all copies and supporting
00009 // documentation.
00010 //
00011 // Texas Instruments Incorporated provides this software "as is" without
00012 // express or implied warranty.
00013 //
00014 // Created: 11-Mar-2001: TFC Copy of vnl_test
00015 // Created: 25-Apr-2002: AGAP Modified copy of testlib_test
00016 //
00017 #include <vcl_cmath.h>
00018 #include <vcl_cstdlib.h> // for abs(long)
00019 #include <vcl_iostream.h>
00020 #include <vcl_iomanip.h> // for setfill, setw
00021 #include <vcl_complex.h>
00022 
00023 static int num_test;
00024 static int tests_passed;
00025 static int tests_failed;
00026 static const char* test_name;
00027 
00028 void testlib_test_start(const char* name)
00029 {
00030   num_test = 0;
00031   tests_passed = 0;
00032   tests_failed = 0;
00033   test_name = name;
00034   vcl_cout << "-----------------------------------------------------------------------------\n"
00035            << "Start Testing";
00036   if (test_name != NULL) vcl_cout << ' ' << test_name;
00037   vcl_cout << ":\n-----------------------------------------------------------------------------\n" << vcl_flush;
00038  }
00039 
00040 void testlib_test_begin(const char* msg)
00041 {
00042   num_test++;
00043   vcl_cout <<" Test "<< vcl_setw(3) << vcl_right << vcl_setfill('0') << num_test
00044            <<": "<< vcl_setw(53) << vcl_left << vcl_setfill(' ')<< msg <<" --> "
00045            << vcl_flush;
00046 }
00047 
00048 // NOTE: We don't pass in the message (see test_begin) because
00049 //       we want to ensure that the message is printed BEFORE
00050 //       the test is executed.  This way when a test crashes
00051 //       we can tell if it was during a test, or between tests.
00052 void testlib_test_perform(bool success)
00053 {
00054   if (success) {
00055     tests_passed++;
00056     vcl_cout << "  PASSED\n" << vcl_flush;
00057   } else {
00058     tests_failed++;
00059     vcl_cout << "**FAILED**\n" << vcl_flush;
00060   }
00061 }
00062 
00063 int testlib_test_summary()
00064 {
00065   vcl_cout << "-----------------------------------------------------------------------------\n";
00066   if (test_name) vcl_cout << test_name << ' ';
00067   vcl_cout << "Test Summary: ";
00068   if (tests_failed > 0)
00069   {
00070     if (tests_passed == 0)
00071       vcl_cout << "No tests succeeded";
00072     else if (tests_passed == 1)
00073       vcl_cout << "1 test succeeded";
00074     else
00075       vcl_cout << tests_passed <<" tests succeeded";
00076     if (tests_failed == 1)
00077       vcl_cout <<", 1 test failed";
00078     else
00079       vcl_cout <<", "<< tests_failed <<" tests failed";
00080     vcl_cout<<"\t\t*****";
00081   }
00082   else
00083   {
00084     if (tests_passed > 1)
00085       vcl_cout << "All "<< tests_passed <<" tests succeeded";
00086     else if (tests_passed == 1)
00087       vcl_cout << "1 test succeeded";
00088     else
00089       vcl_cout << "Test succeeded";
00090   }
00091   vcl_cout << "\n-----------------------------------------------------------------------------\n" << vcl_flush;
00092   return tests_failed;
00093 }
00094 
00095 void testlib_test_assert(const vcl_string& msg, bool expr)
00096 {
00097   vcl_cout << msg << " - " << vcl_flush;
00098   testlib_test_perform(expr);
00099 }
00100 
00101 void testlib_test_assert_near(const vcl_string& msg, double expr, double target, double tol)
00102 {
00103   vcl_cout << msg << " should be " << target << ", is " << expr << ", " << vcl_flush;
00104   double diff = vcl_abs(expr - target);
00105   if (target != 0.0 && diff != 0.0)
00106     vcl_cout << "difference " << diff << ", " << vcl_flush;
00107   testlib_test_perform(diff <= tol);
00108 }
00109 
00110 void testlib_test_assert_near(const vcl_string& msg, vcl_complex<double> expr, vcl_complex<double> target, double tol)
00111 {
00112   vcl_cout << msg << " should be " << target << ", is " << expr << ", " << vcl_flush;
00113   double diff = vcl_abs(expr - target);
00114   if (target != vcl_complex<double>(0,0) && diff != 0.0)
00115     vcl_cout << "difference " << diff << ", " << vcl_flush;
00116   testlib_test_perform(diff <= tol);
00117 }
00118 
00119 void testlib_test_assert_near_relative(const vcl_string& msg, double expr, double target, double tol)
00120 {
00121   vcl_cout << msg << " should be " << target << ", is " << expr << ", " << vcl_flush;
00122   double max = vcl_abs(target); if (vcl_abs(expr) > max) max = vcl_abs(expr);
00123   if (max==0.0 || target==0.0) max=1.0;
00124   double diff = vcl_abs(expr - target) / max;
00125   if (target != 0.0 && diff != 0.0)
00126     vcl_cout << "relative difference " << diff << ", " << vcl_flush;
00127   testlib_test_perform(diff <= tol);
00128 }
00129 
00130 void testlib_test_assert_near_relative(const vcl_string& msg, vcl_complex<double> expr, vcl_complex<double> target, double tol)
00131 {
00132   vcl_cout << msg << " should be " << target << ", is " << expr << ", " << vcl_flush;
00133   double max = vcl_abs(target); if (vcl_abs(expr) > max) max = vcl_abs(expr);
00134   if (max==0.0 || target==vcl_complex<double>(0,0)) max=1.0;
00135   double diff = vcl_abs(expr - target) / max;
00136   if (target != vcl_complex<double>(0,0) && diff != 0.0)
00137     vcl_cout << "relative difference " << diff << ", " << vcl_flush;
00138   testlib_test_perform(diff <= tol);
00139 }
00140 
00141 void testlib_test_assert_far(const vcl_string& msg, double expr, double target, double tol)
00142 {
00143   vcl_cout << msg << " should not be " << target << ", is " << expr << ", " << vcl_flush;
00144   double diff = vcl_abs(expr - target);
00145   if (target != 0.0 && diff != 0.0)
00146     vcl_cout << "difference " << diff << ", " << vcl_flush;
00147   testlib_test_perform(diff > tol);
00148 }
00149 
00150 void testlib_test_assert_far(const vcl_string& msg, vcl_complex<double> expr, vcl_complex<double> target, double tol)
00151 {
00152   vcl_cout << msg << " should not be " << target << ", is " << expr << ", " << vcl_flush;
00153   double diff = vcl_abs(expr - target);
00154   if (target != vcl_complex<double>(0,0) && diff != 0.0)
00155     vcl_cout << "difference " << diff << ", " << vcl_flush;
00156   testlib_test_perform(diff > tol);
00157 }
00158 
00159 void testlib_test_assert_equal(const vcl_string& msg, long expr, long target)
00160 {
00161   vcl_cout << msg << " should be " << target << ", is " << expr << ", " << vcl_flush;
00162   long diff = vcl_abs(expr - target);
00163   if (target != 0 && diff != 0)
00164     vcl_cout << "difference " << diff << ", " << vcl_flush;
00165   testlib_test_perform(diff == 0);
00166 }
00167