-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 $<TARGET_FILE:${name}>) | ||
doctest_discover_tests(${name}) | ||
endfunction() | ||
|
||
|
||
smw_create_test(test_vec2 common/math/test_vec2.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |