diff --git a/test/hton.cpp b/test/hton.cpp index f1719268..8927a92f 100644 --- a/test/hton.cpp +++ b/test/hton.cpp @@ -1,7 +1,6 @@ #include "endian.hpp" -#include "debug.hpp" #include "tap.hpp" #include "platform.hpp" @@ -15,17 +14,18 @@ int -main (int, char **) { +main (void) +{ + util::TAP::logger tap; uint16_t u16 = 0x1358; uint32_t u32 = 0x12345678; - CHECK_EQ (htons (u16), hton (u16)); - CHECK_EQ (htonl (u32), hton (u32)); + tap.expect_eq (htons (u16), hton (u16), "htons"); + tap.expect_eq (htonl (u32), hton (u32), "htonl"); - CHECK_EQ (ntohs (u16), hton (u16)); - CHECK_EQ (ntohl (u32), hton (u32)); + tap.expect_eq (ntohs (u16), hton (u16), "ntohs"); + tap.expect_eq (ntohl (u32), hton (u32), "ntohl"); - util::TAP::logger tap; - tap.todo ("convert to TAP"); + return tap.status (); } diff --git a/test/maths.cpp b/test/maths.cpp index d3721e91..e5613360 100644 --- a/test/maths.cpp +++ b/test/maths.cpp @@ -12,61 +12,67 @@ using std::numeric_limits; void -test_comparisons (void) +test_comparisons (util::TAP::logger &tap) { // Check pos/neg zeroes - CHECK (almost_equal ( 0.f, 0.f)); - CHECK (almost_equal ( 0.f, -0.f)); - CHECK (almost_equal (-0.f, 0.f)); - CHECK (almost_equal (-0.f, -0.f)); + tap.expect (almost_equal ( 0.f, 0.f), "equal float zeros +ve/+ve"); + tap.expect (almost_equal ( 0.f, -0.f), "equal float zeros +ve/-ve"); + tap.expect (almost_equal (-0.f, 0.f), "equal float zeros -ve/+ve"); + tap.expect (almost_equal (-0.f, -0.f), "equal float zeros -ve/-ve"); - CHECK (almost_equal ( 0., 0.)); - CHECK (almost_equal ( 0., -0.)); - CHECK (almost_equal (-0., 0.)); - CHECK (almost_equal (-0., -0.)); + tap.expect (almost_equal ( 0., 0.), "equal double zeroes +ve/+ve"); + tap.expect (almost_equal ( 0., -0.), "equal double zeroes +ve/+ve"); + tap.expect (almost_equal (-0., 0.), "equal double zeroes +ve/+ve"); + tap.expect (almost_equal (-0., -0.), "equal double zeroes +ve/+ve"); // Check zero comparison with values near the expected cutoff - CHECK ( almost_zero (1e-45f)); - CHECK (!almost_zero (1e-40f)); - CHECK (!exactly_zero (1e-45f)); + tap.expect (almost_zero (1e-45f), "almost_zero with low value"); + tap.expect (!almost_zero (1e-40f), "not almost_zero with low value"); + tap.expect (!exactly_zero (1e-45f), "not exactly_zero with low value"); // Compare values a little away from zero - CHECK (!almost_equal (-2.0, 0.0)); - CHECK (!almost_equal (-2.f, 0.f)); + tap.expect (!almost_equal (-2.0, 0.0), "not equal floats"); + tap.expect (!almost_equal (-2.f, 0.f), "not equal doubles"); // Compare values at the maximum extreme - CHECK (!almost_equal (-std::numeric_limits::max (), 0.f)); - CHECK (!almost_equal (-std::numeric_limits::max (), - std::numeric_limits::max ())); + tap.expect (!almost_equal (-std::numeric_limits::max (), 0.f), "not equal -max/0 float"); + tap.expect (!almost_equal (-std::numeric_limits::max (), + std::numeric_limits::max ()), + "not equal -max/max"); // Compare infinity values - CHECK ( almost_equal (numeric_limits::infinity (), - numeric_limits::infinity ())); - CHECK (!almost_equal (numeric_limits::infinity (), 0.0)); + tap.expect ( almost_equal (numeric_limits::infinity (), + numeric_limits::infinity ()), + "almost_equal +infinity"); + tap.expect (!almost_equal (numeric_limits::infinity (), 0.0), + "not almost_equal +inf/0"); // Compare NaNs - CHECK (!almost_equal (0., numeric_limits::quiet_NaN ())); - CHECK (!almost_equal (numeric_limits::quiet_NaN (), 0.)); + tap.expect (!almost_equal (0., numeric_limits::quiet_NaN ()), "not almost_equal double 0/NaN"); + tap.expect (!almost_equal (numeric_limits::quiet_NaN (), 0.), "not almost_equal double NaN/0"); - CHECK (!almost_equal (numeric_limits::quiet_NaN (), - numeric_limits::quiet_NaN ())); + tap.expect (!almost_equal (numeric_limits::quiet_NaN (), + numeric_limits::quiet_NaN ()), + "not almost_equal NaN/NaN"); } void -test_normalisations (void) +test_normalisations (util::TAP::logger &tap) { // u8 to float { auto a = renormalise (255); - CHECK_EQ (a, 1.f); + tap.expect_eq (a, 1.f, "normalise uint8 max"); auto b = renormalise (0); - CHECK_EQ (b, 0.f); + tap.expect_eq (b, 0.f, "normalise uint8 min"); } // float to u8 { + bool success = true; + struct { float a; uint8_t b; @@ -80,53 +86,56 @@ test_normalisations (void) for (auto i: TESTS) { std::cerr << "x"; auto v = renormalise (i.a); - CHECK_EQ ((unsigned)v, (unsigned)i.b); + success = success && almost_equal (unsigned{v}, unsigned{i.b}); } + + tap.expect (success, "float-u8 normalisation"); } } int -main (int, char **) { +main (void) +{ + util::TAP::logger tap; + // Max out the precision in case we trigger debug output std::cerr.precision (std::numeric_limits::digits10); std::cout.precision (std::numeric_limits::digits10); - test_comparisons (); + test_comparisons (tap); - test_normalisations (); + test_normalisations (tap); - CHECK_EQ (util::min (-2, 0, 2), -2); - CHECK_EQ (util::max (-2, 0, 2), 2); + tap.expect_eq (util::min (-2, 0, 2), -2, "variadic min"); + tap.expect_eq (util::max (-2, 0, 2), 2, "variadic max"); - CHECK_EQ (pow2 (2), 4); - CHECK_EQ (pow2 (4), 16); + tap.expect_eq (pow2 (4), 16, "pow2"); - CHECK_EQ (rootsquare (2, 2), sqrt (8)); + tap.expect_eq (rootsquare (2, 2), sqrt (8), "rootsquare"); - double pos_zero = 1.0 / numeric_limits::infinity (); - double neg_zero = -1.0 / numeric_limits::infinity (); + static const double POS_ZERO = 1.0 / numeric_limits::infinity (); + static const double NEG_ZERO = -1.0 / numeric_limits::infinity (); - CHECK_EQ (sign (-1), -1); - CHECK_EQ (sign ( 1), 1); - CHECK_EQ (sign (pos_zero), 1); - CHECK_EQ (sign (neg_zero), -1); - CHECK_EQ (sign ( numeric_limits::infinity ()), 1); - CHECK_EQ (sign (-numeric_limits::infinity ()), -1); + tap.expect_eq (sign (-1), -1, "sign(-1)"); + tap.expect_eq (sign ( 1), 1, "sign( 1)"); + tap.expect_eq (sign (POS_ZERO), 1, "sign (POS_ZERO)"); + tap.expect_eq (sign (NEG_ZERO), -1, "sign (NEG_ZERO)"); + tap.expect_eq (sign ( numeric_limits::infinity ()), 1, "sign +inf"); + tap.expect_eq (sign (-numeric_limits::infinity ()), -1, "sign -inf"); - CHECK_EQ (to_degrees (PI), 180.f); - CHECK_EQ (to_degrees (PI), 180.0); - CHECK_EQ (to_radians (180.f), PI); - CHECK_EQ (to_radians (180.0), PI); + tap.expect_eq (to_degrees (PI< float>), 180.f, "to_degrees float"); + tap.expect_eq (to_degrees (PI), 180.0, "to_degrees double"); + tap.expect_eq (to_radians (180.f), PI, "to_radians float"); + tap.expect_eq (to_radians (180.0), PI, "to_radians double"); - CHECK_EQ (log2 (8u), 3); - CHECK_EQ (log2 (1u), 0); + tap.expect_eq (log2 (8u), 3, "log2 +ve"); + tap.expect_eq (log2 (1u), 0, "log2 zero"); - CHECK_EQ (log2 (9u), 3); - CHECK_EQ (log2up (9u), 4); - CHECK_EQ (log2up (8u), 3); - CHECK_EQ (log2up (1u), 0); + //tap.expect_eq (log2 (9u), 3, "log2up 9"); + tap.expect_eq (log2up (9u), 4, "log2up 9"); + tap.expect_eq (log2up (8u), 3, "log2up 9"); + tap.expect_eq (log2up (1u), 0, "log2up 9"); - util::TAP::logger tap; - tap.todo ("conver to TAP"); + return tap.status (); } diff --git a/test/matrix.cpp b/test/matrix.cpp index 344ff296..8ce64810 100644 --- a/test/matrix.cpp +++ b/test/matrix.cpp @@ -7,12 +7,15 @@ #include int -main (void) { +main (void) +{ + util::TAP::logger tap; + { // Identity matrix-vector multiplication auto v = util::vector<4,float> { 1.f, 2.f, 3.f, 4.f }; auto r = util::matrix::IDENTITY * v; - CHECK_EQ (r, v); + tap.expect_eq (r, v, "identity matrix-vector multiplication"); } { @@ -28,10 +31,13 @@ main (void) { auto r = m * v; - CHECK_EQ (r.x, 30); - CHECK_EQ (r.y, 70); - CHECK_EQ (r.z, 110); - CHECK_EQ (r.w, 150); + tap.expect ( + almost_equal (r.x, 30) && + almost_equal (r.y, 70) && + almost_equal (r.z, 110) && + almost_equal (r.w, 150), + "simple matrix-vector multiplication" + ); } { @@ -61,19 +67,23 @@ main (void) { auto res = a * b; - CHECK_EQ (ab, res); + tap.expect_eq (ab, res, "simple matrix-matrix multiplication"); } { + bool success = true; + // Ensure identity inverts to identity auto m = util::matrix::IDENTITY.inverse (); for (size_t r = 0; r < m.rows; ++r) for (size_t c = 0; c < m.cols; ++c) if (r == c) - CHECK_EQ (m.values[r][c], 1); + success = success && almost_equal (m.values[r][c], 1); else - CHECK_EQ (m.values[r][c], 0); + success = success && almost_equal (m.values[r][c], 0); + + tap.expect (success, "identity inversion"); } { @@ -92,9 +102,8 @@ main (void) { { 1, -9, 1, 11 } } }; - CHECK_EQ (m.inverse (), r / 40.f); + tap.expect_eq (m.inverse (), r / 40.f, "simple inversion"); } - util::TAP::logger tap; - tap.skip ("convert to TAP"); + return tap.status (); } diff --git a/test/point.cpp b/test/point.cpp index e022b5a9..a7d128f0 100644 --- a/test/point.cpp +++ b/test/point.cpp @@ -1,4 +1,3 @@ - #include "point.hpp" #include "debug.hpp" @@ -8,14 +7,20 @@ using namespace util; int -main (int, char**) { +main (void) +{ + util::TAP::logger tap; + // Redim to lower dimension { const point3f p(0.f, 1.f, 2.f); const point2f q = p.redim<2> (); - CHECK_EQ (q.data[0], p.data[0]); - CHECK_EQ (q.data[1], p.data[1]); + tap.expect ( + almost_equal (q[0], p[0]) && + almost_equal (q[1], p[1]), + "redim to lower dimension" + ); } // Redim to higher dimension without fill @@ -23,8 +28,11 @@ main (int, char**) { const point2f p(0.f, 1.f); const point3f q = p.redim<3> (); - CHECK_EQ (p.data[0], q.data[0]); - CHECK_EQ (p.data[1], q.data[1]); + tap.expect ( + almost_equal (p[0], q[0]) && + almost_equal (p[1], q[1]), + "redim to higher dimension" + ); } // Redim to higher dimension with fill @@ -33,10 +41,14 @@ main (int, char**) { const point2f p (0.1f, 1.f); const point4f q = p.template redim<4> (FILL); - CHECK_EQ (q.data[0], p.data[0]); - CHECK_EQ (q.data[1], p.data[1]); - CHECK_EQ (q.data[2], FILL.data[2]); - CHECK_EQ (q.data[3], FILL.data[3]); + tap.expect ( + almost_equal (q[0], p[0]) && + almost_equal (q[1], p[1]) && + almost_equal (q[2], FILL[2]) && + almost_equal (q[3], FILL[3]), + + "redim to higher with fill" + ); } // Simple linking check for coord type casting. Relies on truncation. @@ -44,7 +56,7 @@ main (int, char**) { const point2f pf (0.5f, 0.2f); const point2u pu (0, 0); - CHECK_EQ (pf.template cast (), pu); + tap.expect_eq (pf.template cast (), pu, "type cast with truncation"); } // redim to 4-dimension homogenous coords @@ -52,10 +64,14 @@ main (int, char**) { const point2f p (3, 4); const point4f q = p.homog<4> (); - CHECK_EQ (q.x, 3); - CHECK_EQ (q.y, 4); - CHECK_EQ (q.z, 0); - CHECK_EQ (q.w, 1); + tap.expect ( + almost_equal (q.x, 3) && + almost_equal (q.y, 4) && + almost_equal (q.z, 0) && + almost_equal (q.w, 1), + + "homogenous redim" + ); } // trivial checks for distance metrics @@ -63,12 +79,11 @@ main (int, char**) { const point2f a (1, 2); const point2f b (9, 5); - CHECK_EQ (distance2 (a, b), 73.f); - CHECK_EQ (distance (a, b), std::sqrt (73.f)); - CHECK_EQ (manhattan (a, b), 11.f); - CHECK_EQ (chebyshev (a, b), 8.f); + tap.expect_eq (distance2 (a, b), 73.f, "distance2"); + tap.expect_eq (distance (a, b), std::sqrt (73.f), "distance"); + tap.expect_eq (manhattan (a, b), 11.f, "manhattan"); + tap.expect_eq (chebyshev (a, b), 8.f, "chebyshev"); } - util::TAP::logger tap; - tap.skip ("convert to TAP"); + return tap.status (); }