// // shape_test.cxx // math // // Created by Sam Jaffe on 5/4/19. // Copyright © 2019 Sam Jaffe. All rights reserved. // #include "game/math/shape.hpp" #include #include "test_printers.h" using namespace math::dim2; using namespace testing; struct FromOriginTest : TestWithParam {}; TEST_P(FromOriginTest, IntersectsAtOrigin) { line l1 = {GetParam().first, {{0, 0}}}; line l2 = {{{0, 0}}, GetParam().second}; EXPECT_THAT(math::lines::intersection(l1, l2), Eq(point{{0, 0}})); } std::vector const point_pairs{ {{{1, 1}}, {{0, 0}}}, // 0 length {{{1, 1}}, {{1, 0}}}, // -45deg {{{1, 1}}, {{0, 1}}}, // +45deg {{{1, 1}}, {{1, 1}}}, // +0deg {{{1, 1}}, {{2, 1}}}, // -18deg (approx) {{{1, 1}}, {{1, 2}}}, // +18deg (approx) {{{2, 3}}, {{1, 2}}}, // {{{1, 1}}, {{-1, 0}}}, // +135deg {{{1, 1}}, {{0, -1}}}, // -135deg {{{1, 1}}, {{-1, -1}}}, // +180deg {{{1, 1}}, {{1, -1}}}, // +90deg {{{1, 1}}, {{-1, 1}}}, // -90deg }; INSTANTIATE_TEST_SUITE_P(LineIntersection, FromOriginTest, ValuesIn(point_pairs)); struct UnitLineTest : TestWithParam {}; TEST(LineTest, UnitLineHasLengthOne) { line const unit{{{0, 0}}, {{1, 0}}}; EXPECT_THAT(unit.length(), Eq(1)); } TEST(LineTest, ParallelLinesHaveSameSlope) { line const lhs{{{0, 0}}, {{1, 0}}}; line const rhs{{{-1, 0}}, {{-2, 0}}}; EXPECT_TRUE(math::lines::parallel(lhs, rhs)); } TEST_P(UnitLineTest, OrthoOnIntersection) { line const ln{{{0, 0}}, {{1, 0}}}; point const pt = GetParam(); line const expected{pt, {{pt[0], 0}}}; EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected)); } TEST_P(UnitLineTest, OrthoOnIntersectionY) { line const ln{{{0, 0}}, {{0, 1}}}; point const pt = GetParam(); line const expected{pt, {{pt[0], 0}}}; EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected)); } std::vector x_orthos{ {{0, 1}}, {{1, 1}}, {{1, 0}}, {{-1, 0}}, {{0, -1}}, {{-1, -1}}, {{0, 2}}, {{2, 2}}, {{2, 0}}, {{-2, 0}}, {{0, -2}}, {{-2, -2}}, {{2, 1}}, {{1, 2}}, {{-2, 1}}, {{-1, 2}}, {{1, -2}}, {{2, -1}}}; INSTANTIATE_TEST_SUITE_P(LineOrthogonal, UnitLineTest, ValuesIn(x_orthos)); struct DiagonalTest : TestWithParam> {}; TEST_P(DiagonalTest, OrthoOnIntersection) { line const ln{{{0, 0}}, {{1, 1}}}; point const pt = GetParam().first; line const expected{pt, {{GetParam().second, GetParam().second}}}; EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected)); } std::vector> diag_orthos{ {{{0, 1}}, 0.5f}, {{{1, 1}}, 1.f}, {{{1, 0}}, 0.5f}, {{{-1, 0}}, -0.5f}, {{{0, -1}}, -0.5f}, {{{-1, -1}}, -1.f}, {{{0, 2}}, 1.f}, {{{2, 2}}, 2.f}, {{{2, 0}}, 1.f}, {{{-2, 0}}, -1.f}, {{{0, -2}}, -1.f}, {{{-2, -2}}, -2.f}, {{{2, 1}}, 1.5f}, {{{1, 2}}, 1.5f}, {{{-2, 1}}, -0.5f}, {{{-1, 2}}, 0.5f}, {{{1, -2}}, -0.5f}, {{{2, -1}}, 0.5f}}; INSTANTIATE_TEST_SUITE_P(LineOrthogonal, DiagonalTest, ValuesIn(diag_orthos)); struct QuadTest : TestWithParam> {}; TEST_P(QuadTest, SquareProducesQuadWithCornersAtX) { float x = std::get<0>(GetParam()); square const square{{{0, 0}}, x}; quad const expected{{{0, 0}}, {{x, 0}}, {{x, x}}, {{0, x}}}; EXPECT_THAT(quad(square), Eq(expected)); } TEST_P(QuadTest, SquareProducesRectangleWithXX) { float side = std::get<0>(GetParam()); square const square{{{0, 0}}, side}; EXPECT_THAT(rectangle(square).origin, Eq(square.origin)); EXPECT_THAT(rectangle(square).size[0], Eq(side)); EXPECT_THAT(rectangle(square).size[0], Eq(side)); } TEST_P(QuadTest, OffsetSquareProducesRectangleWithXX) { float side = std::get<0>(GetParam()); square const square{{{1, 1}}, side}; EXPECT_THAT(rectangle(square).origin, Eq(square.origin)); EXPECT_THAT(rectangle(square).size[0], Eq(side)); EXPECT_THAT(rectangle(square).size[0], Eq(side)); } TEST_P(QuadTest, RectProducesQuadWithCornersAtXY) { float x = std::get<0>(GetParam()); float y = std::get<1>(GetParam()); rectangle const square{{{0, 0}}, {{x, y}}}; quad const expected{{{0, 0}}, {{x, 0}}, {{x, y}}, {{0, y}}}; EXPECT_THAT(quad(square), Eq(expected)); } INSTANTIATE_TEST_SUITE_P(Upcast, QuadTest, Combine(Values(0.5, 1.0, 1.5, 2.0), Values(0.5, 1.0, 1.5, 2.0)));