From c7e1d89538683b849e193e39a9d430377fd18277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ty=C3=A1s=20Mustoha?= Date: Sat, 25 May 2024 19:05:49 +0200 Subject: [PATCH] Added some vector utility code --- .github/workflows/build_linux.yml | 3 + .github/workflows/build_macos.yml | 3 + .github/workflows/build_mingw.yml | 5 +- CMakeLists.txt | 6 ++ src/common/CMakeLists.txt | 1 + src/common/math/Vec2.h | 57 ++++++++++++++++++ tests/CMakeLists.txt | 9 +++ tests/common/math/test_vec2.cpp | 99 +++++++++++++++++++++++++++++++ 8 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 src/common/math/Vec2.h create mode 100644 tests/CMakeLists.txt create mode 100644 tests/common/math/test_vec2.cpp diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 4e6d5066..7f44d2ce 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -43,6 +43,9 @@ jobs: - name: Build run: cmake --build build --verbose + - name: Test + run: ctest --test-dir build --output-on-failure + - name: Package if: matrix.sdl == 2 run: | diff --git a/.github/workflows/build_macos.yml b/.github/workflows/build_macos.yml index 4001f465..092fbab0 100644 --- a/.github/workflows/build_macos.yml +++ b/.github/workflows/build_macos.yml @@ -19,6 +19,9 @@ jobs: - name: Build run: cmake --build build --verbose + - name: Test + run: ctest --test-dir build --output-on-failure + - name: Package run: | cmake --install build --prefix supermariowar --strip diff --git a/.github/workflows/build_mingw.yml b/.github/workflows/build_mingw.yml index bea29f75..622ddd7f 100644 --- a/.github/workflows/build_mingw.yml +++ b/.github/workflows/build_mingw.yml @@ -20,7 +20,8 @@ jobs: - name: Configure run: cmake - -S . -B build -G Ninja + -B build + -G Ninja -DCMAKE_TOOLCHAIN_FILE=../.github/workflows/mingw_toolchain.cmake -DUSE_PNG_SAVE=ON -DUSE_SDL2_LIBS=ON @@ -28,6 +29,8 @@ jobs: -DCMAKE_CXX_FLAGS=-fdiagnostics-color=always - name: Build run: cmake --build build --verbose + - name: Test + run: ctest --test-dir build --output-on-failure - name: Package run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index a927cac1..ebeda556 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -281,6 +281,12 @@ if(NOT NO_NETWORK) add_subdirectory(src/server) endif() +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + include(CTest) + add_subdirectory(dependencies/doctest) + add_subdirectory(tests) +endif() + #----------------------------------------------------------------------------- # # Packaging diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 6f294085..908bdd1e 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -42,6 +42,7 @@ add_library(CommonFiles STATIC map/MapReader16xx.cpp map/MapReader17xx.cpp map/MapReader18xx.cpp + math/Vec2.h MapList.cpp MapList.h MatchTypes.h diff --git a/src/common/math/Vec2.h b/src/common/math/Vec2.h new file mode 100644 index 00000000..443efa0d --- /dev/null +++ b/src/common/math/Vec2.h @@ -0,0 +1,57 @@ +#pragma once + + +struct Vec2f { + float x = 0.f; + float y = 0.f; + + constexpr explicit Vec2f() = default; + constexpr explicit Vec2f(float inX, float inY) : x(inX), y(inY) {} + + static constexpr Vec2f zero() { + return Vec2f(); + } + + constexpr Vec2f& operator+=(const Vec2f& rhs) { + x += rhs.x; + y += rhs.y; + return *this; + } + constexpr Vec2f& operator-=(const Vec2f& rhs) { + x -= rhs.x; + y -= rhs.y; + return *this; + } + + constexpr Vec2f& operator*=(float val) { + x *= val; + y *= val; + return *this; + } + constexpr Vec2f& operator/=(float val) { + x /= val; + y /= val; + return *this; + } +}; + + +constexpr Vec2f operator+(const Vec2f& lhs, const Vec2f& rhs) { + return Vec2f(lhs.x + rhs.x, lhs.y + rhs.y); +} +constexpr Vec2f operator-(const Vec2f& lhs, const Vec2f& rhs) { + return Vec2f(lhs.x - rhs.x, lhs.y - rhs.y); +} +constexpr Vec2f operator*(const Vec2f& lhs, const Vec2f& rhs) { + return Vec2f(lhs.x * rhs.x, lhs.y * rhs.y); +} +constexpr Vec2f operator/(const Vec2f& lhs, const Vec2f& rhs) { + return Vec2f(lhs.x / rhs.x, lhs.y / rhs.y); +} + +constexpr Vec2f operator*(const Vec2f& lhs, float scalar) { + return Vec2f(lhs.x * scalar, lhs.y * scalar); +} +constexpr Vec2f operator/(const Vec2f& lhs, float scalar) { + return Vec2f(lhs.x / scalar, lhs.y / scalar); +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..4f6bd600 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,9 @@ +function(smw_create_test name path) + add_executable(${name} ${path}) + target_link_libraries(${name} PRIVATE doctest CommonFiles) + # add_test(NAME ${name} COMMAND $) + doctest_discover_tests(${name}) +endfunction() + + +smw_create_test(test_vec2 common/math/test_vec2.cpp) diff --git a/tests/common/math/test_vec2.cpp b/tests/common/math/test_vec2.cpp new file mode 100644 index 00000000..5acb5c29 --- /dev/null +++ b/tests/common/math/test_vec2.cpp @@ -0,0 +1,99 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" + +#include "math/Vec2.h" + + +TEST_CASE("Testing Vec2f default constructor") { + Vec2f v; + CHECK(v.x == doctest::Approx(0.0f)); + CHECK(v.y == doctest::Approx(0.0f)); +} + +TEST_CASE("Testing Vec2f parameterized constructor") { + Vec2f v(1.0f, 2.0f); + CHECK(v.x == doctest::Approx(1.0f)); + CHECK(v.y == doctest::Approx(2.0f)); +} + +TEST_CASE("Testing Vec2f::zero method") { + Vec2f v = Vec2f::zero(); + CHECK(v.x == doctest::Approx(0.0f)); + CHECK(v.y == doctest::Approx(0.0f)); +} + +TEST_CASE("Testing Vec2f operator+=") { + Vec2f v1(1.0f, 2.0f); + Vec2f v2(3.0f, 4.0f); + v1 += v2; + CHECK(v1.x == doctest::Approx(4.0f)); + CHECK(v1.y == doctest::Approx(6.0f)); +} + +TEST_CASE("Testing Vec2f operator-=") { + Vec2f v1(5.0f, 7.0f); + Vec2f v2(2.0f, 3.0f); + v1 -= v2; + CHECK(v1.x == doctest::Approx(3.0f)); + CHECK(v1.y == doctest::Approx(4.0f)); +} + +TEST_CASE("Testing Vec2f operator*=") { + Vec2f v(2.0f, 3.0f); + v *= 2.0f; + CHECK(v.x == doctest::Approx(4.0f)); + CHECK(v.y == doctest::Approx(6.0f)); +} + +TEST_CASE("Testing Vec2f operator/=") { + Vec2f v(6.0f, 8.0f); + v /= 2.0f; + CHECK(v.x == doctest::Approx(3.0f)); + CHECK(v.y == doctest::Approx(4.0f)); +} + +TEST_CASE("Testing Vec2f operator+") { + Vec2f v1(1.0f, 2.0f); + Vec2f v2(3.0f, 4.0f); + Vec2f result = v1 + v2; + CHECK(result.x == doctest::Approx(4.0f)); + CHECK(result.y == doctest::Approx(6.0f)); +} + +TEST_CASE("Testing Vec2f operator-") { + Vec2f v1(5.0f, 7.0f); + Vec2f v2(2.0f, 3.0f); + Vec2f result = v1 - v2; + CHECK(result.x == doctest::Approx(3.0f)); + CHECK(result.y == doctest::Approx(4.0f)); +} + +TEST_CASE("Testing Vec2f operator* with Vec2f") { + Vec2f v1(2.0f, 3.0f); + Vec2f v2(4.0f, 5.0f); + Vec2f result = v1 * v2; + CHECK(result.x == doctest::Approx(8.0f)); + CHECK(result.y == doctest::Approx(15.0f)); +} + +TEST_CASE("Testing Vec2f operator/ with Vec2f") { + Vec2f v1(6.0f, 8.0f); + Vec2f v2(2.0f, 4.0f); + Vec2f result = v1 / v2; + CHECK(result.x == doctest::Approx(3.0f)); + CHECK(result.y == doctest::Approx(2.0f)); +} + +TEST_CASE("Testing Vec2f operator* with scalar") { + Vec2f v(2.0f, 3.0f); + Vec2f result = v * 2.0f; + CHECK(result.x == doctest::Approx(4.0f)); + CHECK(result.y == doctest::Approx(6.0f)); +} + +TEST_CASE("Testing Vec2f operator/ with scalar") { + Vec2f v(6.0f, 8.0f); + Vec2f result = v / 2.0f; + CHECK(result.x == doctest::Approx(3.0f)); + CHECK(result.y == doctest::Approx(4.0f)); +}