From 322af3e49ae800f45eae39e4f081197cba0b9a3f Mon Sep 17 00:00:00 2001 From: David Fioramonti Date: Thu, 11 Apr 2019 17:19:57 -0700 Subject: TESTS: Use relative difference for math tests Also add double template variants and use already defined delta assert macro. --- test/common/math.h | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/test/common/math.h b/test/common/math.h index 0d70264c73..5010ca5ed2 100644 --- a/test/common/math.h +++ b/test/common/math.h @@ -2,13 +2,7 @@ #include "common/math.h" -// Macro function for asserting that the compared values x and y -// aren't further apart from each other than z. - -#define TS_ASSERT_ALMOST_EQUALS(x, y, z) \ - TS_ASSERT_LESS_THAN_EQUALS(((x) - (y)) >= 0 ? (x) - (y) : (y) - (x), z) - -const float MAX_ERROR = 0.001; +const float MAX_ERROR = 1e-7; class MathTestSuite : public CxxTest::TestSuite { public: @@ -24,16 +18,28 @@ class MathTestSuite : public CxxTest::TestSuite } void test_rad2deg() { - TS_ASSERT_ALMOST_EQUALS(Common::rad2deg(0), 0, MAX_ERROR); - TS_ASSERT_ALMOST_EQUALS(Common::rad2deg(M_PI), 180.0, MAX_ERROR); - TS_ASSERT_ALMOST_EQUALS(Common::rad2deg(2.0 * M_PI), 360.0, MAX_ERROR); - TS_ASSERT_ALMOST_EQUALS(Common::rad2deg(M_PI / 2.0), 90.0, MAX_ERROR); + //float verion + TS_ASSERT_DELTA(Common::rad2deg(0), 0, MAX_ERROR); + TS_ASSERT_DELTA(Common::rad2deg(M_PI), 180.0, 180.0 * MAX_ERROR); + TS_ASSERT_DELTA(Common::rad2deg(2.0 * M_PI), 360.0, 360.0 * MAX_ERROR); + TS_ASSERT_DELTA(Common::rad2deg(M_PI / 2.0), 90.0, 90.0 * MAX_ERROR); + //double version + TS_ASSERT_DELTA(Common::rad2deg(0), 0, MAX_ERROR); + TS_ASSERT_DELTA(Common::rad2deg(M_PI), 180.0, 180.0 * MAX_ERROR); + TS_ASSERT_DELTA(Common::rad2deg(2.0 * M_PI), 360.0, 360.0 * MAX_ERROR); + TS_ASSERT_DELTA(Common::rad2deg(M_PI / 2.0), 90.0, 90.0 * MAX_ERROR); } void test_deg2rad() { - TS_ASSERT_ALMOST_EQUALS(Common::deg2rad(0), 0, MAX_ERROR); - TS_ASSERT_ALMOST_EQUALS(Common::deg2rad(180.0), M_PI, MAX_ERROR); - TS_ASSERT_ALMOST_EQUALS(Common::deg2rad(360.0), 2.0 * M_PI, MAX_ERROR); - TS_ASSERT_ALMOST_EQUALS(Common::deg2rad(90.0), M_PI / 2.0, MAX_ERROR); + //float verion + TS_ASSERT_DELTA(Common::deg2rad(0), 0, MAX_ERROR); + TS_ASSERT_DELTA(Common::deg2rad(180.0), M_PI, M_PI * MAX_ERROR); + TS_ASSERT_DELTA(Common::deg2rad(360.0), 2.0 * M_PI, 2.0 * M_PI * MAX_ERROR); + TS_ASSERT_DELTA(Common::deg2rad(90.0), M_PI / 2.0, M_PI / 2.0 * MAX_ERROR); + //double version + TS_ASSERT_DELTA(Common::deg2rad(0), 0, MAX_ERROR); + TS_ASSERT_DELTA(Common::deg2rad(180.0), M_PI, M_PI * MAX_ERROR); + TS_ASSERT_DELTA(Common::deg2rad(360.0), 2.0 * M_PI, 2.0 * M_PI * MAX_ERROR); + TS_ASSERT_DELTA(Common::deg2rad(90.0), M_PI / 2.0, M_PI / 2.0 * MAX_ERROR); } }; -- cgit v1.2.3