2015-09-21 15:27:36 +10:00
|
|
|
#include "tap.hpp"
|
|
|
|
#include "point.hpp"
|
2016-08-11 14:58:46 +10:00
|
|
|
#include "vector.hpp"
|
2015-09-21 15:27:36 +10:00
|
|
|
|
2016-03-14 22:32:52 +11:00
|
|
|
#include "coord/iostream.hpp"
|
2019-03-08 09:37:02 +11:00
|
|
|
#include "coord/comparator.hpp"
|
2016-03-14 22:32:52 +11:00
|
|
|
|
2018-03-22 13:20:00 +11:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
|
2015-09-21 15:27:36 +10:00
|
|
|
int
|
|
|
|
main (void)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
static_assert (std::is_trivially_copyable_v<cruft::point2i>);
|
2018-03-22 13:20:00 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
static_assert (cruft::has_result_v<cruft::point3f, cruft::vector3f>);
|
2018-01-10 17:19:39 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
static_assert (sizeof (cruft::point1u) == 1 * sizeof (cruft::point1u::value_type), "point1u is not packed");
|
|
|
|
static_assert (sizeof (cruft::point2u) == 2 * sizeof (cruft::point2u::value_type), "point2u is not packed");
|
|
|
|
static_assert (sizeof (cruft::point3u) == 3 * sizeof (cruft::point3u::value_type), "point3u is not packed");
|
|
|
|
static_assert (sizeof (cruft::point4u) == 4 * sizeof (cruft::point4u::value_type), "point4u is not packed");
|
2015-09-21 15:27:36 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
static_assert (sizeof (cruft::point1f) == 1 * sizeof (cruft::point1f::value_type), "point1f is not packed");
|
|
|
|
static_assert (sizeof (cruft::point2f) == 2 * sizeof (cruft::point2f::value_type), "point2f is not packed");
|
|
|
|
static_assert (sizeof (cruft::point3f) == 3 * sizeof (cruft::point3f::value_type), "point3f is not packed");
|
|
|
|
static_assert (sizeof (cruft::point4f) == 4 * sizeof (cruft::point4f::value_type), "point4f is not packed");
|
2015-09-21 15:27:36 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2016-03-14 22:32:52 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
constexpr cruft::point2i p { -1, 2 };
|
2016-03-14 22:32:52 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (-p, cruft::point2i { 1, -2 }, "unary point negation");
|
2017-05-22 13:55:21 +10:00
|
|
|
tap.expect_eq ( p, p, "unary point addition");
|
2015-09-21 15:27:36 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
auto vec = cruft::vector4f (0.5f);
|
|
|
|
tap.expect (cruft::almost_equal (vec, cruft::normalised (vec)), "normalisation of normalised vector");
|
2016-08-11 14:58:46 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (sum (cruft::vector4f::ones ()), 4.f, "elementwise summation");
|
2016-08-15 18:46:04 +10:00
|
|
|
|
2017-06-15 16:24:44 +10:00
|
|
|
// check that structured bindings work
|
|
|
|
{
|
|
|
|
const auto &[x,y] = p;
|
|
|
|
tap.expect (x == p.x && y == p.y, "structured bindings extract correct data");
|
|
|
|
}
|
|
|
|
|
2018-06-12 10:54:08 +10:00
|
|
|
// check that we can assign to arrays from coord types
|
|
|
|
{
|
|
|
|
std::array<int,2> q = p;
|
|
|
|
tap.expect (
|
|
|
|
std::equal (std::begin (q), std::end (q), std::begin (p)),
|
|
|
|
"assignment to array preserves values"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-24 12:50:17 +10:00
|
|
|
// ensure the distance function behaves correctly with non-normal numbers.
|
2017-06-16 17:38:55 +10:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::point3f a { 103, 0, 14 };
|
|
|
|
cruft::point3f b { 104, INFINITY, 15 };
|
2017-06-16 17:38:55 +10:00
|
|
|
|
|
|
|
tap.expect_eq (
|
|
|
|
std::numeric_limits<float>::infinity (),
|
2018-08-05 14:42:02 +10:00
|
|
|
::cruft::distance (a, b),
|
2017-06-16 17:38:55 +10:00
|
|
|
"distance with an infinity is infinite"
|
|
|
|
);
|
|
|
|
}
|
2017-06-15 16:24:44 +10:00
|
|
|
|
2017-08-27 12:28:56 +10:00
|
|
|
// test expected outputs for various vector-logical operations
|
2017-08-24 14:32:58 +10:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
constexpr cruft::point3i a { 0, -1, 2 };
|
|
|
|
constexpr cruft::point3i b { 0, 1, -2 };
|
|
|
|
constexpr cruft::point3i c { -9, -9, -9 };
|
2017-08-24 14:32:58 +10:00
|
|
|
|
|
|
|
tap.expect (!all (a <= b), "all, expected failure");
|
|
|
|
tap.expect ( all (a <= a), "all, expected success");
|
|
|
|
tap.expect (!any (a <= c), "any, expected failure");
|
|
|
|
tap.expect ( any (a <= b), "any, expected success");
|
|
|
|
};
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
// ensure the cruft::select function behaves as expected
|
2017-07-31 15:41:33 +10:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
const cruft::point3f a { -1, 2, 0 };
|
|
|
|
const cruft::point3f b { 1, 0, 2 };
|
2017-07-31 15:41:33 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
const cruft::point3f lo { -1, 0, 0 };
|
|
|
|
const cruft::point3f hi { 1, 2, 2 };
|
2017-07-31 15:41:33 +10:00
|
|
|
|
|
|
|
tap.expect_eq (select (a < b, a, b), lo, "select with points and min");
|
|
|
|
tap.expect_eq (select (a > b, a, b), hi, "select with points and max");
|
|
|
|
};
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
// ensure that cruft::clamp resolves to the coord overload. the exact
|
2017-08-24 12:50:17 +10:00
|
|
|
// values are less useful than exercising the compiler/linker.
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
const cruft::vector3f val { 0, -1, 2 };
|
|
|
|
const cruft::vector3f lo { -1, 1, -2 };
|
|
|
|
const cruft::vector3f hi { 1, 2, 0 };
|
|
|
|
|
|
|
|
tap.expect_eq (clamp (val, lo, hi), cruft::vector3f { 0, 1, 0 }, "clamp with vec/vec/vec");
|
|
|
|
tap.expect_eq (clamp (val, 0.f, hi), cruft::vector3f { 0, 0, 0 }, "clamp with vec/num/vec");
|
|
|
|
tap.expect_eq (clamp (val, lo, 2.f), cruft::vector3f { 0, 1, 2 }, "clamp with vec/vec/num");
|
|
|
|
tap.expect_eq (clamp (val, 0.f, 2.f), cruft::vector3f { 0, 0, 2 }, "clamp with vec/num/num");
|
2017-08-24 12:50:17 +10:00
|
|
|
}
|
|
|
|
|
2017-08-24 14:39:06 +10:00
|
|
|
// ensure that klass::indices appears to link correctly
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
const cruft::vector3i seq { 0, 1, 2 };
|
|
|
|
const cruft::vector4i res { 2, 0, 0, 1 };
|
2017-08-24 14:39:06 +10:00
|
|
|
|
|
|
|
tap.expect_eq (seq.indices<2,0,0,1> (), res, "coord::indices expansion");
|
|
|
|
};
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
// ensure that cruft::shift operations appear to operate correctly
|
2017-08-25 13:03:21 +10:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
const cruft::vector3i seq { 0, 1, 2 };
|
|
|
|
tap.expect_eq (rshift (seq, 1, 0), cruft::make_vector (0, 0, 1), "rshift, scalar fill");
|
2017-08-25 13:03:21 +10:00
|
|
|
|
|
|
|
tap.expect_eq (
|
2018-08-05 14:42:02 +10:00
|
|
|
rshift (seq, 2, cruft::make_vector (3, 4, 5 )),
|
|
|
|
cruft::make_vector (3, 4, 0),
|
2017-08-25 13:03:21 +10:00
|
|
|
"rshift, coord fill"
|
|
|
|
);
|
2019-03-08 09:37:02 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
cruft::point2i a;
|
|
|
|
cruft::point2i b;
|
|
|
|
bool expected;
|
|
|
|
char const *message;
|
|
|
|
} const TESTS[] {
|
|
|
|
{ { 0, 0 }, { 0, 0 }, false, "equal zeroes" },
|
|
|
|
{ { 1, 0 }, { 0, 0 }, false, "a, leading high" },
|
|
|
|
{ { 0, 1 }, { 0, 0 }, false, "a, trailing high" },
|
|
|
|
{ { 0, 0 }, { 1, 0 }, true, "b, leading high" },
|
|
|
|
{ { 0, 0 }, { 0, 1 }, true, "b, trailing high" },
|
|
|
|
};
|
|
|
|
|
|
|
|
cruft::coord::ordering<cruft::point2i> const comp;
|
|
|
|
for (auto const &t: TESTS)
|
|
|
|
tap.expect (comp (t.a, t.b) == t.expected, "ordering; %!", t.message);
|
|
|
|
}
|
2017-08-25 13:03:21 +10:00
|
|
|
|
2017-05-22 13:55:21 +10:00
|
|
|
return tap.status ();
|
2015-09-21 15:27:36 +10:00
|
|
|
}
|