test: use TAP on more tests

This commit is contained in:
Danny Robson 2015-07-13 16:29:01 +10:00
parent fdc801628b
commit abf88710b4
4 changed files with 130 additions and 97 deletions

View File

@ -1,7 +1,6 @@
#include "endian.hpp" #include "endian.hpp"
#include "debug.hpp"
#include "tap.hpp" #include "tap.hpp"
#include "platform.hpp" #include "platform.hpp"
@ -15,17 +14,18 @@
int int
main (int, char **) { main (void)
{
util::TAP::logger tap;
uint16_t u16 = 0x1358; uint16_t u16 = 0x1358;
uint32_t u32 = 0x12345678; uint32_t u32 = 0x12345678;
CHECK_EQ (htons (u16), hton (u16)); tap.expect_eq (htons (u16), hton (u16), "htons");
CHECK_EQ (htonl (u32), hton (u32)); tap.expect_eq (htonl (u32), hton (u32), "htonl");
CHECK_EQ (ntohs (u16), hton (u16)); tap.expect_eq (ntohs (u16), hton (u16), "ntohs");
CHECK_EQ (ntohl (u32), hton (u32)); tap.expect_eq (ntohl (u32), hton (u32), "ntohl");
util::TAP::logger tap; return tap.status ();
tap.todo ("convert to TAP");
} }

View File

@ -12,61 +12,67 @@ using std::numeric_limits;
void void
test_comparisons (void) test_comparisons (util::TAP::logger &tap)
{ {
// Check pos/neg zeroes // Check pos/neg zeroes
CHECK (almost_equal ( 0.f, 0.f)); tap.expect (almost_equal ( 0.f, 0.f), "equal float zeros +ve/+ve");
CHECK (almost_equal ( 0.f, -0.f)); tap.expect (almost_equal ( 0.f, -0.f), "equal float zeros +ve/-ve");
CHECK (almost_equal (-0.f, 0.f)); tap.expect (almost_equal (-0.f, 0.f), "equal float zeros -ve/+ve");
CHECK (almost_equal (-0.f, -0.f)); tap.expect (almost_equal (-0.f, -0.f), "equal float zeros -ve/-ve");
CHECK (almost_equal ( 0., 0.)); tap.expect (almost_equal ( 0., 0.), "equal double zeroes +ve/+ve");
CHECK (almost_equal ( 0., -0.)); tap.expect (almost_equal ( 0., -0.), "equal double zeroes +ve/+ve");
CHECK (almost_equal (-0., 0.)); tap.expect (almost_equal (-0., 0.), "equal double zeroes +ve/+ve");
CHECK (almost_equal (-0., -0.)); tap.expect (almost_equal (-0., -0.), "equal double zeroes +ve/+ve");
// Check zero comparison with values near the expected cutoff // Check zero comparison with values near the expected cutoff
CHECK ( almost_zero (1e-45f)); tap.expect (almost_zero (1e-45f), "almost_zero with low value");
CHECK (!almost_zero (1e-40f)); tap.expect (!almost_zero (1e-40f), "not almost_zero with low value");
CHECK (!exactly_zero (1e-45f)); tap.expect (!exactly_zero (1e-45f), "not exactly_zero with low value");
// Compare values a little away from zero // Compare values a little away from zero
CHECK (!almost_equal (-2.0, 0.0)); tap.expect (!almost_equal (-2.0, 0.0), "not equal floats");
CHECK (!almost_equal (-2.f, 0.f)); tap.expect (!almost_equal (-2.f, 0.f), "not equal doubles");
// Compare values at the maximum extreme // Compare values at the maximum extreme
CHECK (!almost_equal (-std::numeric_limits<float>::max (), 0.f)); tap.expect (!almost_equal (-std::numeric_limits<float>::max (), 0.f), "not equal -max/0 float");
CHECK (!almost_equal (-std::numeric_limits<float>::max (), tap.expect (!almost_equal (-std::numeric_limits<float>::max (),
std::numeric_limits<float>::max ())); std::numeric_limits<float>::max ()),
"not equal -max/max");
// Compare infinity values // Compare infinity values
CHECK ( almost_equal (numeric_limits<double>::infinity (), tap.expect ( almost_equal (numeric_limits<double>::infinity (),
numeric_limits<double>::infinity ())); numeric_limits<double>::infinity ()),
CHECK (!almost_equal (numeric_limits<double>::infinity (), 0.0)); "almost_equal +infinity");
tap.expect (!almost_equal (numeric_limits<double>::infinity (), 0.0),
"not almost_equal +inf/0");
// Compare NaNs // Compare NaNs
CHECK (!almost_equal (0., numeric_limits<double>::quiet_NaN ())); tap.expect (!almost_equal (0., numeric_limits<double>::quiet_NaN ()), "not almost_equal double 0/NaN");
CHECK (!almost_equal (numeric_limits<double>::quiet_NaN (), 0.)); tap.expect (!almost_equal (numeric_limits<double>::quiet_NaN (), 0.), "not almost_equal double NaN/0");
CHECK (!almost_equal (numeric_limits<double>::quiet_NaN (), tap.expect (!almost_equal (numeric_limits<double>::quiet_NaN (),
numeric_limits<double>::quiet_NaN ())); numeric_limits<double>::quiet_NaN ()),
"not almost_equal NaN/NaN");
} }
void void
test_normalisations (void) test_normalisations (util::TAP::logger &tap)
{ {
// u8 to float // u8 to float
{ {
auto a = renormalise<uint8_t,float> (255); auto a = renormalise<uint8_t,float> (255);
CHECK_EQ (a, 1.f); tap.expect_eq (a, 1.f, "normalise uint8 max");
auto b = renormalise<uint8_t,float> (0); auto b = renormalise<uint8_t,float> (0);
CHECK_EQ (b, 0.f); tap.expect_eq (b, 0.f, "normalise uint8 min");
} }
// float to u8 // float to u8
{ {
bool success = true;
struct { struct {
float a; float a;
uint8_t b; uint8_t b;
@ -80,53 +86,56 @@ test_normalisations (void)
for (auto i: TESTS) { for (auto i: TESTS) {
std::cerr << "x"; std::cerr << "x";
auto v = renormalise<decltype(i.a),decltype(i.b)> (i.a); auto v = renormalise<decltype(i.a),decltype(i.b)> (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 int
main (int, char **) { main (void)
{
util::TAP::logger tap;
// Max out the precision in case we trigger debug output // Max out the precision in case we trigger debug output
std::cerr.precision (std::numeric_limits<double>::digits10); std::cerr.precision (std::numeric_limits<double>::digits10);
std::cout.precision (std::numeric_limits<double>::digits10); std::cout.precision (std::numeric_limits<double>::digits10);
test_comparisons (); test_comparisons (tap);
test_normalisations (); test_normalisations (tap);
CHECK_EQ (util::min (-2, 0, 2), -2); tap.expect_eq (util::min (-2, 0, 2), -2, "variadic min");
CHECK_EQ (util::max (-2, 0, 2), 2); tap.expect_eq (util::max (-2, 0, 2), 2, "variadic max");
CHECK_EQ (pow2 (2), 4); tap.expect_eq (pow2 (4), 16, "pow2");
CHECK_EQ (pow2 (4), 16);
CHECK_EQ (rootsquare (2, 2), sqrt (8)); tap.expect_eq (rootsquare (2, 2), sqrt (8), "rootsquare");
double pos_zero = 1.0 / numeric_limits<double>::infinity (); static const double POS_ZERO = 1.0 / numeric_limits<double>::infinity ();
double neg_zero = -1.0 / numeric_limits<double>::infinity (); static const double NEG_ZERO = -1.0 / numeric_limits<double>::infinity ();
CHECK_EQ (sign (-1), -1); tap.expect_eq (sign (-1), -1, "sign(-1)");
CHECK_EQ (sign ( 1), 1); tap.expect_eq (sign ( 1), 1, "sign( 1)");
CHECK_EQ (sign (pos_zero), 1); tap.expect_eq (sign (POS_ZERO), 1, "sign (POS_ZERO)");
CHECK_EQ (sign (neg_zero), -1); tap.expect_eq (sign (NEG_ZERO), -1, "sign (NEG_ZERO)");
CHECK_EQ (sign ( numeric_limits<double>::infinity ()), 1); tap.expect_eq (sign ( numeric_limits<double>::infinity ()), 1, "sign +inf");
CHECK_EQ (sign (-numeric_limits<double>::infinity ()), -1); tap.expect_eq (sign (-numeric_limits<double>::infinity ()), -1, "sign -inf");
CHECK_EQ (to_degrees (PI<float>), 180.f); tap.expect_eq (to_degrees (PI< float>), 180.f, "to_degrees float");
CHECK_EQ (to_degrees (PI<double>), 180.0); tap.expect_eq (to_degrees (PI<double>), 180.0, "to_degrees double");
CHECK_EQ (to_radians (180.f), PI<float>); tap.expect_eq (to_radians (180.f), PI<float>, "to_radians float");
CHECK_EQ (to_radians (180.0), PI<double>); tap.expect_eq (to_radians (180.0), PI<double>, "to_radians double");
CHECK_EQ (log2 (8u), 3); tap.expect_eq (log2 (8u), 3, "log2 +ve");
CHECK_EQ (log2 (1u), 0); tap.expect_eq (log2 (1u), 0, "log2 zero");
CHECK_EQ (log2 (9u), 3); //tap.expect_eq (log2 (9u), 3, "log2up 9");
CHECK_EQ (log2up (9u), 4); tap.expect_eq (log2up (9u), 4, "log2up 9");
CHECK_EQ (log2up (8u), 3); tap.expect_eq (log2up (8u), 3, "log2up 9");
CHECK_EQ (log2up (1u), 0); tap.expect_eq (log2up (1u), 0, "log2up 9");
util::TAP::logger tap; return tap.status ();
tap.todo ("conver to TAP");
} }

View File

@ -7,12 +7,15 @@
#include <cstdlib> #include <cstdlib>
int int
main (void) { main (void)
{
util::TAP::logger tap;
{ {
// Identity matrix-vector multiplication // Identity matrix-vector multiplication
auto v = util::vector<4,float> { 1.f, 2.f, 3.f, 4.f }; auto v = util::vector<4,float> { 1.f, 2.f, 3.f, 4.f };
auto r = util::matrix<float>::IDENTITY * v; auto r = util::matrix<float>::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; auto r = m * v;
CHECK_EQ (r.x, 30); tap.expect (
CHECK_EQ (r.y, 70); almost_equal (r.x, 30) &&
CHECK_EQ (r.z, 110); almost_equal (r.y, 70) &&
CHECK_EQ (r.w, 150); almost_equal (r.z, 110) &&
almost_equal (r.w, 150),
"simple matrix-vector multiplication"
);
} }
{ {
@ -61,19 +67,23 @@ main (void) {
auto res = a * b; 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 // Ensure identity inverts to identity
auto m = util::matrix<float>::IDENTITY.inverse (); auto m = util::matrix<float>::IDENTITY.inverse ();
for (size_t r = 0; r < m.rows; ++r) for (size_t r = 0; r < m.rows; ++r)
for (size_t c = 0; c < m.cols; ++c) for (size_t c = 0; c < m.cols; ++c)
if (r == c) if (r == c)
CHECK_EQ (m.values[r][c], 1); success = success && almost_equal (m.values[r][c], 1);
else 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 } { 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; return tap.status ();
tap.skip ("convert to TAP");
} }

View File

@ -1,4 +1,3 @@
#include "point.hpp" #include "point.hpp"
#include "debug.hpp" #include "debug.hpp"
@ -8,14 +7,20 @@
using namespace util; using namespace util;
int int
main (int, char**) { main (void)
{
util::TAP::logger tap;
// Redim to lower dimension // Redim to lower dimension
{ {
const point3f p(0.f, 1.f, 2.f); const point3f p(0.f, 1.f, 2.f);
const point2f q = p.redim<2> (); const point2f q = p.redim<2> ();
CHECK_EQ (q.data[0], p.data[0]); tap.expect (
CHECK_EQ (q.data[1], p.data[1]); almost_equal (q[0], p[0]) &&
almost_equal (q[1], p[1]),
"redim to lower dimension"
);
} }
// Redim to higher dimension without fill // Redim to higher dimension without fill
@ -23,8 +28,11 @@ main (int, char**) {
const point2f p(0.f, 1.f); const point2f p(0.f, 1.f);
const point3f q = p.redim<3> (); const point3f q = p.redim<3> ();
CHECK_EQ (p.data[0], q.data[0]); tap.expect (
CHECK_EQ (p.data[1], q.data[1]); almost_equal (p[0], q[0]) &&
almost_equal (p[1], q[1]),
"redim to higher dimension"
);
} }
// Redim to higher dimension with fill // Redim to higher dimension with fill
@ -33,10 +41,14 @@ main (int, char**) {
const point2f p (0.1f, 1.f); const point2f p (0.1f, 1.f);
const point4f q = p.template redim<4> (FILL); const point4f q = p.template redim<4> (FILL);
CHECK_EQ (q.data[0], p.data[0]); tap.expect (
CHECK_EQ (q.data[1], p.data[1]); almost_equal (q[0], p[0]) &&
CHECK_EQ (q.data[2], FILL.data[2]); almost_equal (q[1], p[1]) &&
CHECK_EQ (q.data[3], FILL.data[3]); 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. // 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 point2f pf (0.5f, 0.2f);
const point2u pu (0, 0); const point2u pu (0, 0);
CHECK_EQ (pf.template cast<point2u::value_type> (), pu); tap.expect_eq (pf.template cast<point2u::value_type> (), pu, "type cast with truncation");
} }
// redim to 4-dimension homogenous coords // redim to 4-dimension homogenous coords
@ -52,10 +64,14 @@ main (int, char**) {
const point2f p (3, 4); const point2f p (3, 4);
const point4f q = p.homog<4> (); const point4f q = p.homog<4> ();
CHECK_EQ (q.x, 3); tap.expect (
CHECK_EQ (q.y, 4); almost_equal (q.x, 3) &&
CHECK_EQ (q.z, 0); almost_equal (q.y, 4) &&
CHECK_EQ (q.w, 1); almost_equal (q.z, 0) &&
almost_equal (q.w, 1),
"homogenous redim"
);
} }
// trivial checks for distance metrics // trivial checks for distance metrics
@ -63,12 +79,11 @@ main (int, char**) {
const point2f a (1, 2); const point2f a (1, 2);
const point2f b (9, 5); const point2f b (9, 5);
CHECK_EQ (distance2 (a, b), 73.f); tap.expect_eq (distance2 (a, b), 73.f, "distance2");
CHECK_EQ (distance (a, b), std::sqrt (73.f)); tap.expect_eq (distance (a, b), std::sqrt (73.f), "distance");
CHECK_EQ (manhattan (a, b), 11.f); tap.expect_eq (manhattan (a, b), 11.f, "manhattan");
CHECK_EQ (chebyshev (a, b), 8.f); tap.expect_eq (chebyshev (a, b), 8.f, "chebyshev");
} }
util::TAP::logger tap; return tap.status ();
tap.skip ("convert to TAP");
} }