| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- //
- // 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 <testing/xcode_gtest_helper.h>
- #include "test_printers.h"
- using namespace math::dim2;
- using namespace testing;
- struct FromOriginTest : TestWithParam<line> {};
- 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<line> 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<point> {};
- 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<point> 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<std::pair<point, float>> {};
- 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<std::pair<point, float>> 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<std::tuple<float, float>> {};
- 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)));
|