tap: add rudimentary TAP helpers

This commit is contained in:
Danny Robson 2015-01-29 15:45:30 +11:00
parent c841aadda0
commit 54cb3c2153
2 changed files with 82 additions and 13 deletions

52
tap.hpp Normal file
View File

@ -0,0 +1,52 @@
/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_TAP_HPP
#define __UTIL_TAP_HPP
#include <iostream>
#define TAP_PLAN(N) do { \
std::cout << "1.." << (N) << '\n'; \
} while (0)
#define TAP(OK, MSG) do { \
std::cout << ((OK) ? "ok - " : "not ok - ") << (MSG) << '\n'; \
} while (0)
#define TAP_EQ(A,B,MSG) do { \
const auto &&__tap_eq_a = (A); \
const auto &&__tap_eq_b = (B); \
\
TAP (almost_equal (__tap_eq_a, __tap_eq_b), (MSG)); \
} while (0)
#define TAP_SKIP(MSG) do { \
std::cout << "ok # skip " << (MSG) << '\n'; \
} while (0)
#define TAP_BAIL(MSG) do { \
std::cout << "Bail out! " << (MSG) << '\n'; \
} while (0)
#endif

View File

@ -2,6 +2,8 @@
#include "float.hpp"
#include "debug.hpp"
#include "tap.hpp"
#include "types.hpp"
#include <algorithm>
#include <cmath>
@ -13,30 +15,45 @@ main (int, char**)
auto nan = std::numeric_limits<float>::quiet_NaN ();
struct {
const char *name;
std::array<float,4> coeffs;
std::array<float,3> solutions;
} CUBICS [] = {
{ { 0, 0, -8, 7 }, { 7.f / 8, nan, nan } },
{ { 0, 4, -8, 2 }, { 0.292893f, 1.70710f, nan } },
{ { 1, 0, 0, 0 }, { 0, nan, nan } },
{ { 1, 0, 2, 0 }, { 0, nan, nan } },
{ { -2, 2, -2, 2 }, { 1, nan, nan } },
{ { 1, 0, 0, 2 }, { -std::cbrt(2.f), nan, nan } },
{ { 2, -3, 8, -2 }, { 0.2728376017309678f, nan, nan } },
{ { 1, 4, -8, 7 }, { -5.6389f, nan, nan } },
{ { 1, 3, -6, -8 }, { -4, -1, 2, } },
{ "linear", { 0, 0, -8, 7 }, { 7.f / 8, nan, nan } },
{ "quadratic", { 0, 4, -8, 2 }, { 0.2928932188134524f, 1.7071067811865475f, nan } },
{ "unit cubic", { 1, 0, 0, 0 }, { 0, nan, nan } },
{ "simple cubic", { 1, 0, 2, 0 }, { 0, nan, nan } },
{ "twos cubic", { -2, 2, -2, 2 }, { 1, nan, nan } },
{ "a & d cubic", { 1, 0, 0, 2 }, { -std::cbrt(2.f), nan, nan } },
{ "cubic 1 real 1", { 2, -3, 8, -2 }, { 0.2728376017309678f, nan, nan } },
{ "cubic 1 real 2", { 1, 4, -8, 7 }, { -5.638871145892407f, nan, nan } },
{ "cubic all real", { 1, 3, -6, -8 }, { -4, -1, 2, } },
{ "cubic 1 uniq", { 1, -3, 3, -1 }, { 1, nan, nan } },
};
TAP_PLAN(elems (CUBICS));
for (auto &i: CUBICS) {
std::array<float,3> s = util::polynomial::solve<3> (i.coeffs);
std::sort (s.begin (), s.end ());
for (size_t j = 0; j < 3; ++j) {
bool equal = ieee_single::almost_equal (i.solutions[j], s[j]);
bool invalid = std::isnan (i.solutions[j]) && std::isnan (s[j]);
bool ok = true;
CHECK (equal || invalid);
for (size_t j = 0; j < 3; ++j) {
if (std::isnan (i.solutions[j])) {
if (!std::isnan (s[j]))
ok = false;
continue;
}
if (!almost_equal (i.solutions[j], s[j]))
ok = false;
}
TAP (ok, i.name);
}
return 0;
}