Skip to content

Commit

Permalink
Added some vector utility code
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed May 27, 2024
1 parent 09796b5 commit a355e16
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/build_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/build_macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/build_mingw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ if(NOT NO_NETWORK)
add_subdirectory(src/server)
endif()

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
include(CTest)
include(dependencies/doctest/doctest.cmake)
add_subdirectory(dependencies/doctest)
add_subdirectory(tests)
endif()

#-----------------------------------------------------------------------------
#
# Packaging
Expand Down
1 change: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions src/common/math/Vec2.h
Original file line number Diff line number Diff line change
@@ -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);
}
8 changes: 8 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function(smw_create_test name path)
add_executable(${name} ${path})
target_link_libraries(${name} PRIVATE doctest CommonFiles)
doctest_discover_tests(${name})
endfunction()


smw_create_test(test_vec2 common/math/test_vec2.cpp)
99 changes: 99 additions & 0 deletions tests/common/math/test_vec2.cpp
Original file line number Diff line number Diff line change
@@ -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));
}

0 comments on commit a355e16

Please sign in to comment.