SFML/test/System/Vector3.test.cpp

238 lines
6.5 KiB
C++
Raw Permalink Normal View History

2018-08-19 22:14:54 +08:00
#include <SFML/System/Vector3.hpp>
2023-01-18 12:51:08 +08:00
#include <catch2/catch_test_macros.hpp>
2022-07-05 00:20:58 +08:00
#include <SystemUtil.hpp>
#include <type_traits>
// Use sf::Vector3i for tests (except for float vector algebra).
// Test coverage is given, as there are no template specializations.
2018-08-19 22:14:54 +08:00
TEST_CASE("[System] sf::Vector3")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("Type traits")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(std::is_copy_constructible_v<sf::Vector3i>);
STATIC_CHECK(std::is_copy_assignable_v<sf::Vector3i>);
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::Vector3i>);
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Vector3i>);
}
SECTION("Construction")
{
SECTION("Default constructor")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
constexpr sf::Vector3i vector;
STATIC_CHECK(vector.x == 0);
STATIC_CHECK(vector.y == 0);
STATIC_CHECK(vector.z == 0);
2018-08-19 22:14:54 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("(x, y, z) coordinate constructor")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
constexpr sf::Vector3i vector(1, 2, 3);
STATIC_CHECK(vector.x == 1);
STATIC_CHECK(vector.y == 2);
STATIC_CHECK(vector.z == 3);
2018-08-19 22:14:54 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Conversion constructor")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
constexpr sf::Vector3f sourceVector(1.0f, 2.0f, 3.0f);
constexpr sf::Vector3i vector(sourceVector);
2018-08-19 22:14:54 +08:00
2023-01-18 12:51:08 +08:00
STATIC_CHECK(vector.x == static_cast<int>(sourceVector.x));
STATIC_CHECK(vector.y == static_cast<int>(sourceVector.y));
STATIC_CHECK(vector.z == static_cast<int>(sourceVector.z));
2018-08-19 22:14:54 +08:00
}
}
2023-01-18 12:51:08 +08:00
SECTION("Unary operations")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("-vector")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
constexpr sf::Vector3i vector(1, 2, 3);
constexpr sf::Vector3i negatedVector = -vector;
2018-08-19 22:14:54 +08:00
2023-01-18 12:51:08 +08:00
STATIC_CHECK(negatedVector.x == -1);
STATIC_CHECK(negatedVector.y == -2);
STATIC_CHECK(negatedVector.z == -3);
2018-08-19 22:14:54 +08:00
}
}
2023-01-18 12:51:08 +08:00
SECTION("Arithmetic operations between two vectors")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
sf::Vector3i firstVector(2, 5, 6);
constexpr sf::Vector3i secondVector(8, 3, 7);
2018-08-19 22:14:54 +08:00
2023-01-18 12:51:08 +08:00
SECTION("vector += vector")
2018-08-19 22:14:54 +08:00
{
firstVector += secondVector;
CHECK(firstVector.x == 10);
CHECK(firstVector.y == 8);
CHECK(firstVector.z == 13);
}
2023-01-18 12:51:08 +08:00
SECTION("vector -= vector")
2018-08-19 22:14:54 +08:00
{
firstVector -= secondVector;
CHECK(firstVector.x == -6);
CHECK(firstVector.y == 2);
CHECK(firstVector.z == -1);
}
2023-01-18 12:51:08 +08:00
SECTION("vector + vector")
2018-08-19 22:14:54 +08:00
{
2022-08-19 12:59:28 +08:00
const sf::Vector3i result = firstVector + secondVector;
2018-08-19 22:14:54 +08:00
CHECK(result.x == 10);
CHECK(result.y == 8);
CHECK(result.z == 13);
}
2023-01-18 12:51:08 +08:00
SECTION("vector - vector")
2018-08-19 22:14:54 +08:00
{
2022-08-19 12:59:28 +08:00
const sf::Vector3i result = firstVector - secondVector;
2018-08-19 22:14:54 +08:00
CHECK(result.x == -6);
CHECK(result.y == 2);
CHECK(result.z == -1);
}
}
2023-01-18 12:51:08 +08:00
SECTION("Arithmetic operations between vector and scalar value")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
sf::Vector3i vector(26, 12, 6);
constexpr int scalar = 2;
2018-08-19 22:14:54 +08:00
2023-01-18 12:51:08 +08:00
SECTION("vector * scalar")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
const sf::Vector3i result = vector * scalar;
2018-08-19 22:14:54 +08:00
CHECK(result.x == 52);
CHECK(result.y == 24);
CHECK(result.z == 12);
}
2023-01-18 12:51:08 +08:00
SECTION("scalar * vector")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
const sf::Vector3i result = scalar * vector;
2018-08-19 22:14:54 +08:00
CHECK(result.x == 52);
CHECK(result.y == 24);
CHECK(result.z == 12);
}
2023-01-18 12:51:08 +08:00
SECTION("vector *= scalar")
2018-08-19 22:14:54 +08:00
{
vector *= scalar;
CHECK(vector.x == 52);
CHECK(vector.y == 24);
CHECK(vector.z == 12);
}
2023-01-18 12:51:08 +08:00
SECTION("vector / scalar")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
const sf::Vector3i result = vector / scalar;
2018-08-19 22:14:54 +08:00
CHECK(result.x == 13);
CHECK(result.y == 6);
CHECK(result.z == 3);
}
2023-01-18 12:51:08 +08:00
SECTION("vector /= scalar")
2018-08-19 22:14:54 +08:00
{
vector /= scalar;
CHECK(vector.x == 13);
CHECK(vector.y == 6);
CHECK(vector.z == 3);
}
}
2023-01-18 12:51:08 +08:00
SECTION("Comparison operations (two equal and one different vector)")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
constexpr sf::Vector3i firstEqualVector(1, 5, 6);
constexpr sf::Vector3i secondEqualVector(1, 5, 6);
constexpr sf::Vector3i differentVector(6, 9, 7);
2018-08-19 22:14:54 +08:00
2023-01-18 12:51:08 +08:00
SECTION("vector == vector")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(firstEqualVector == secondEqualVector);
STATIC_CHECK_FALSE(firstEqualVector == differentVector);
2018-08-19 22:14:54 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("vector != vector")
2018-08-19 22:14:54 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(firstEqualVector != differentVector);
STATIC_CHECK_FALSE(firstEqualVector != secondEqualVector);
2018-08-19 22:14:54 +08:00
}
}
2023-01-18 12:51:08 +08:00
SECTION("Structured bindings")
{
2023-01-18 12:51:08 +08:00
sf::Vector3i vector(1, 2, 3); // NOLINT(misc-const-correctness)
2023-01-18 12:51:08 +08:00
SECTION("destructure by value")
{
auto [x, y, z] = vector;
CHECK(x == 1);
CHECK(y == 2);
CHECK(z == 3);
2023-01-18 12:51:08 +08:00
STATIC_CHECK(std::is_same_v<decltype(x), decltype(vector.x)>);
x = 3;
CHECK(x == 3);
CHECK(vector.x == 1);
}
2023-01-18 12:51:08 +08:00
SECTION("destructure by ref")
{
auto& [x, y, z] = vector;
CHECK(x == 1);
CHECK(y == 2);
CHECK(z == 3);
2023-01-18 12:51:08 +08:00
STATIC_CHECK(std::is_same_v<decltype(x), decltype(vector.x)>);
x = 3;
CHECK(x == 3);
CHECK(vector.x == 3);
}
}
2023-01-18 12:51:08 +08:00
SECTION("Length and normalization")
{
2023-01-18 12:51:08 +08:00
constexpr sf::Vector3f v(2.4f, 3.0f, 5.2f);
CHECK(v.length() == Approx(6.46529f));
CHECK(v.lengthSq() == Approx(41.79997f));
CHECK(v.normalized() == Approx(sf::Vector3f(0.37121f, 0.46401f, 0.80429f)));
}
2023-01-18 12:51:08 +08:00
SECTION("Products and quotients")
{
2023-01-18 12:51:08 +08:00
constexpr sf::Vector3f v(2.4f, 3.0f, 5.2f);
constexpr sf::Vector3f w(-0.7f, -2.2f, -4.8f);
CHECK(v.dot(w) == Approx(-33.24f));
CHECK(w.dot(v) == Approx(-33.24f));
CHECK(v.cross(w) == Approx(sf::Vector3f(-2.96f, 7.88f, -3.18f)));
CHECK(w.cross(v) == Approx(sf::Vector3f(2.96f, -7.88f, 3.18f)));
CHECK(v.cwiseMul(w) == Approx(sf::Vector3f(-1.68f, -6.6f, -24.96f)));
CHECK(w.cwiseMul(v) == Approx(sf::Vector3f(-1.68f, -6.6f, -24.96f)));
CHECK(v.cwiseDiv(w) == Approx(sf::Vector3f(-3.428571f, -1.363636f, -1.0833333f)));
CHECK(w.cwiseDiv(v) == Approx(sf::Vector3f(-0.291666f, -0.733333f, -0.9230769f)));
}
2018-08-19 22:14:54 +08:00
}