testlib_test.cxx
Go to the documentation of this file.
1 // This is core/testlib/testlib_test.cxx
2 #include <cmath>
3 #include <cstdlib>
4 #include <iostream>
5 #include <iomanip>
6 #include <complex>
7 #include "testlib_test.h"
8 //
9 // Copyright (C) 1991 Texas Instruments Incorporated.
10 //
11 // Permission is granted to any individual or institution to use, copy, modify,
12 // and distribute this software, provided that this complete copyright and
13 // permission notice is maintained, intact, in all copies and supporting
14 // documentation.
15 //
16 // Texas Instruments Incorporated provides this software "as is" without
17 // express or implied warranty.
18 //
19 // Created: 11-Mar-2001: TFC Copy of vnl_test
20 // Created: 25-Apr-2002: AGAP Modified copy of testlib_test
21 //
22 #ifdef _MSC_VER
23 # include <vcl_msvc_warnings.h>
24 #endif
25 
26 static int num_test;
27 static int tests_passed;
28 static int tests_failed;
29 static const char* test_name;
30 
31 void testlib_test_start(const char* name)
32 {
33  num_test = 0;
34  tests_passed = 0;
35  tests_failed = 0;
36  test_name = name;
37  std::cout << "-----------------------------------------------------------------------------\n"
38  << "Start Testing";
39  if (test_name != nullptr) std::cout << ' ' << test_name;
40  std::cout << ":\n-----------------------------------------------------------------------------\n" << std::flush;
41  }
42 
43 void testlib_test_begin(const char* msg)
44 {
45  num_test++;
46  std::cout <<" Test "<< std::setw(3) << std::right << std::setfill('0') << num_test
47  <<": "<< std::setw(53) << std::left << std::setfill(' ')<< msg <<" --> "
48  << std::flush;
49 }
50 
51 // NOTE: We don't pass in the message (see test_begin) because
52 // we want to ensure that the message is printed BEFORE
53 // the test is executed. This way when a test crashes
54 // we can tell if it was during a test, or between tests.
55 void testlib_test_perform(bool success)
56 {
57  if (success) {
58  tests_passed++;
59  std::cout << " PASSED\n" << std::flush;
60  } else {
61  tests_failed++;
62  std::cout << "**FAILED**\n" << std::flush;
63  }
64 }
65 
67 {
68  std::cout << "-----------------------------------------------------------------------------\n";
69  if (test_name) std::cout << test_name << ' ';
70  std::cout << "Test Summary: ";
71  if (tests_failed > 0)
72  {
73  if (tests_passed == 0)
74  std::cout << "No tests succeeded";
75  else if (tests_passed == 1)
76  std::cout << "1 test succeeded";
77  else
78  std::cout << tests_passed <<" tests succeeded";
79  if (tests_failed == 1)
80  std::cout <<", 1 test failed";
81  else
82  std::cout <<", "<< tests_failed <<" tests failed";
83  std::cout<<"\t\t*****";
84  }
85  else
86  {
87  if (tests_passed > 1)
88  std::cout << "All "<< tests_passed <<" tests succeeded";
89  else if (tests_passed == 1)
90  std::cout << "1 test succeeded";
91  else
92  std::cout << "Test succeeded";
93  }
94  std::cout << "\n-----------------------------------------------------------------------------\n" << std::flush;
95  return tests_failed;
96 }
97 
98 void testlib_test_assert(const std::string& msg, bool expr)
99 {
100  std::cout << msg << " - " << std::flush;
101  testlib_test_perform(expr);
102 }
103 
104 void testlib_test_assert_near(const std::string& msg, double expr, double target, double tol)
105 {
106  std::cout << msg << " should be " << target << ", is " << expr << ", " << std::flush;
107  double diff = std::abs(expr - target);
108  if (target != 0.0 && diff != 0.0)
109  std::cout << "difference " << diff << ", " << std::flush;
110  testlib_test_perform(diff <= tol);
111 }
112 
113 void testlib_test_assert_near(const std::string& msg, std::complex<double> expr, std::complex<double> target, double tol)
114 {
115  std::cout << msg << " should be " << target << ", is " << expr << ", " << std::flush;
116  double diff = std::abs(expr - target);
117  if (target != std::complex<double>(0,0) && diff != 0.0)
118  std::cout << "difference " << diff << ", " << std::flush;
119  testlib_test_perform(diff <= tol);
120 }
121 
122 void testlib_test_assert_near_relative(const std::string& msg, double expr, double target, double tol)
123 {
124  std::cout << msg << " should be " << target << ", is " << expr << ", " << std::flush;
125  double max = std::abs(target); if (std::abs(expr) > max) max = std::abs(expr);
126  if (max==0.0 || target==0.0) max=1.0;
127  double diff = std::abs(expr - target) / max;
128  if (target != 0.0 && diff != 0.0)
129  std::cout << "relative difference " << diff << ", " << std::flush;
130  testlib_test_perform(diff <= tol);
131 }
132 
133 void testlib_test_assert_near_relative(const std::string& msg, std::complex<double> expr, std::complex<double> target, double tol)
134 {
135  std::cout << msg << " should be " << target << ", is " << expr << ", " << std::flush;
136  double max = std::abs(target); if (std::abs(expr) > max) max = std::abs(expr);
137  if (max==0.0 || target==std::complex<double>(0,0)) max=1.0;
138  double diff = std::abs(expr - target) / max;
139  if (target != std::complex<double>(0,0) && diff != 0.0)
140  std::cout << "relative difference " << diff << ", " << std::flush;
141  testlib_test_perform(diff <= tol);
142 }
143 
144 void testlib_test_assert_far(const std::string& msg, double expr, double target, double tol)
145 {
146  std::cout << msg << " should not be " << target << ", is " << expr << ", " << std::flush;
147  double diff = std::abs(expr - target);
148  if (target != 0.0 && diff != 0.0)
149  std::cout << "difference " << diff << ", " << std::flush;
150  testlib_test_perform(diff > tol);
151 }
152 
153 void testlib_test_assert_far(const std::string& msg, std::complex<double> expr, std::complex<double> target, double tol)
154 {
155  std::cout << msg << " should not be " << target << ", is " << expr << ", " << std::flush;
156  double diff = std::abs(expr - target);
157  if (target != std::complex<double>(0,0) && diff != 0.0)
158  std::cout << "difference " << diff << ", " << std::flush;
159  testlib_test_perform(diff > tol);
160 }
161 
162 void testlib_test_assert_equal(const std::string& msg, long expr, long target)
163 {
164  std::cout << msg << " should be " << target << ", is " << expr << ", " << std::flush;
165  long diff = std::abs(expr - target);
166  if (target != 0 && diff != 0)
167  std::cout << "difference " << diff << ", " << std::flush;
168  testlib_test_perform(diff == 0);
169 }
void testlib_test_start(const char *name)
initialise test counters, check test name 'name' exists.
void testlib_test_assert_equal(const std::string &msg, long expr, long target)
output msg, then perform test to see if expr is equal to target.
void testlib_test_perform(bool success)
increment success/failure counters.
void testlib_test_begin(const char *msg)
increment number of tests, then output msg.
int testlib_test_summary()
output summary of tests performed.
void testlib_test_assert_near_relative(const std::string &msg, double expr, double target, double tol)
output msg, then test to see if expr is within relative tol of target.
void testlib_test_assert_near(const std::string &msg, double expr, double target, double tol)
output msg, then perform test to see if expr is within tol of target.
void testlib_test_assert(const std::string &msg, bool expr)
output msg, then perform test in expr.
void testlib_test_assert_far(const std::string &msg, double expr, double target, double tol)
output msg, then perform test to see if expr is not within tol of target.
Testing software.