From f4bb236c0463a393c1a2d70ceb50c506514a23fa Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 21 Oct 2011 11:21:52 +0100 Subject: [PATCH] Add FUZZY_COMPARE to me tar unit-test, tolerate lower-order imprecision in FPUs --- simgear/environment/test_metar.cxx | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/simgear/environment/test_metar.cxx b/simgear/environment/test_metar.cxx index bcdc7725..814094b2 100644 --- a/simgear/environment/test_metar.cxx +++ b/simgear/environment/test_metar.cxx @@ -30,12 +30,20 @@ using std::string; exit(1); \ } +#define FUZZY_COMPARE(a, b, epsilon) \ + if (fabs(a - b) > epsilon) { \ + cerr << "failed:" << #a << " != " << #b << endl; \ + cerr << "\tgot:" << a << endl; \ + cerr << "\tepsilon:" << epsilon << endl; \ + } + #define VERIFY(a) \ if (!(a)) { \ cerr << "failed:" << #a << endl; \ exit(1); \ } - + +const double TEST_EPSILON = 1e-9; void test_basic() { @@ -48,11 +56,11 @@ void test_basic() COMPARE(m1.getReportType(), -1); // should default to NIL? COMPARE(m1.getWindDir(), 270); - COMPARE(m1.getWindSpeed_kt(), 12); + FUZZY_COMPARE(m1.getWindSpeed_kt(), 12, TEST_EPSILON); - COMPARE(m1.getTemperature_C(), 10); - COMPARE(m1.getDewpoint_C(), 5); - COMPARE(m1.getPressure_hPa(), 1025); + FUZZY_COMPARE(m1.getTemperature_C(), 10, TEST_EPSILON); + FUZZY_COMPARE(m1.getDewpoint_C(), 5, TEST_EPSILON); + FUZZY_COMPARE(m1.getPressure_hPa(), 1025, TEST_EPSILON); } int main(int argc, char* argv[]) -- 2.39.5