SFML/test/System/Vector3.cpp

240 lines
6.1 KiB
C++
Raw Normal View History

2018-08-19 22:14:54 +08:00
#include <SFML/System/Vector3.hpp>
2018-08-20 15:51:42 +08:00
#include "SystemUtil.hpp"
#include <type_traits>
2018-08-19 22:14:54 +08:00
#include <doctest.h>
using doctest::Approx;
// 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
2021-12-24 21:31:27 +08:00
TEST_CASE("sf::Vector3 class template - [system]")
2018-08-19 22:14:54 +08:00
{
2021-12-24 21:31:27 +08:00
SUBCASE("Construction")
2018-08-19 22:14:54 +08:00
{
2021-12-24 21:31:27 +08:00
SUBCASE("Default constructor")
2018-08-19 22:14:54 +08:00
{
sf::Vector3i vector;
CHECK(vector.x == 0);
CHECK(vector.y == 0);
CHECK(vector.z == 0);
}
2021-12-24 21:31:27 +08:00
SUBCASE("(x, y, z) coordinate constructor")
2018-08-19 22:14:54 +08:00
{
sf::Vector3i vector(1, 2, 3);
CHECK(vector.x == 1);
CHECK(vector.y == 2);
CHECK(vector.z == 3);
}
2021-12-24 21:31:27 +08:00
SUBCASE("Conversion constructor")
2018-08-19 22:14:54 +08:00
{
sf::Vector3f sourceVector(1.0f, 2.0f, 3.0f);
sf::Vector3i vector(sourceVector);
CHECK(vector.x == static_cast<int>(sourceVector.x));
CHECK(vector.y == static_cast<int>(sourceVector.y));
CHECK(vector.z == static_cast<int>(sourceVector.z));
}
}
2021-12-24 21:31:27 +08:00
SUBCASE("Unary operations")
2018-08-19 22:14:54 +08:00
{
2021-12-24 21:31:27 +08:00
SUBCASE("-vector")
2018-08-19 22:14:54 +08:00
{
sf::Vector3i vector(1, 2, 3);
sf::Vector3i negatedVector = -vector;
CHECK(negatedVector.x == -1);
CHECK(negatedVector.y == -2);
CHECK(negatedVector.z == -3);
}
}
2021-12-24 21:31:27 +08:00
SUBCASE("Arithmetic operations between two vectors")
2018-08-19 22:14:54 +08:00
{
sf::Vector3i firstVector(2, 5, 6);
sf::Vector3i secondVector(8, 3, 7);
2021-12-24 21:31:27 +08:00
SUBCASE("vector += vector")
2018-08-19 22:14:54 +08:00
{
firstVector += secondVector;
CHECK(firstVector.x == 10);
CHECK(firstVector.y == 8);
CHECK(firstVector.z == 13);
}
2021-12-24 21:31:27 +08:00
SUBCASE("vector -= vector")
2018-08-19 22:14:54 +08:00
{
firstVector -= secondVector;
CHECK(firstVector.x == -6);
CHECK(firstVector.y == 2);
CHECK(firstVector.z == -1);
}
2021-12-24 21:31:27 +08:00
SUBCASE("vector + vector")
2018-08-19 22:14:54 +08:00
{
sf::Vector3i result = firstVector + secondVector;
CHECK(result.x == 10);
CHECK(result.y == 8);
CHECK(result.z == 13);
}
2021-12-24 21:31:27 +08:00
SUBCASE("vector - vector")
2018-08-19 22:14:54 +08:00
{
sf::Vector3i result = firstVector - secondVector;
CHECK(result.x == -6);
CHECK(result.y == 2);
CHECK(result.z == -1);
}
}
2021-12-24 21:31:27 +08:00
SUBCASE("Arithmetic operations between vector and scalar value")
2018-08-19 22:14:54 +08:00
{
sf::Vector3i vector(26, 12, 6);
int scalar = 2;
2021-12-24 21:31:27 +08:00
SUBCASE("vector * scalar")
2018-08-19 22:14:54 +08:00
{
sf::Vector3i result = vector * scalar;
CHECK(result.x == 52);
CHECK(result.y == 24);
CHECK(result.z == 12);
}
2021-12-24 21:31:27 +08:00
SUBCASE("scalar * vector")
2018-08-19 22:14:54 +08:00
{
sf::Vector3i result = scalar * vector;
CHECK(result.x == 52);
CHECK(result.y == 24);
CHECK(result.z == 12);
}
2021-12-24 21:31:27 +08:00
SUBCASE("vector *= scalar")
2018-08-19 22:14:54 +08:00
{
vector *= scalar;
CHECK(vector.x == 52);
CHECK(vector.y == 24);
CHECK(vector.z == 12);
}
2021-12-24 21:31:27 +08:00
SUBCASE("vector / scalar")
2018-08-19 22:14:54 +08:00
{
sf::Vector3i result = vector / scalar;
CHECK(result.x == 13);
CHECK(result.y == 6);
CHECK(result.z == 3);
}
2021-12-24 21:31:27 +08:00
SUBCASE("vector /= scalar")
2018-08-19 22:14:54 +08:00
{
vector /= scalar;
CHECK(vector.x == 13);
CHECK(vector.y == 6);
CHECK(vector.z == 3);
}
}
2021-12-24 21:31:27 +08:00
SUBCASE("Comparison operations (two equal and one different vector)")
2018-08-19 22:14:54 +08:00
{
sf::Vector3i firstEqualVector(1, 5, 6);
sf::Vector3i secondEqualVector(1, 5, 6);
sf::Vector3i differentVector(6, 9, 7);
2021-12-24 21:31:27 +08:00
SUBCASE("vector == vector")
2018-08-19 22:14:54 +08:00
{
CHECK(firstEqualVector == secondEqualVector);
CHECK_FALSE(firstEqualVector == differentVector);
}
2021-12-24 21:31:27 +08:00
SUBCASE("vector != vector")
2018-08-19 22:14:54 +08:00
{
CHECK(firstEqualVector != differentVector);
CHECK_FALSE(firstEqualVector != secondEqualVector);
}
}
2021-12-24 21:31:27 +08:00
SUBCASE("Structured bindings")
{
sf::Vector3i vector(1, 2, 3);
2021-12-24 21:31:27 +08:00
SUBCASE("destructure by value")
{
auto [x, y, z] = vector;
CHECK(x == 1);
CHECK(y == 2);
CHECK(z == 3);
static_assert(std::is_same_v<decltype(x), decltype(vector.x)>);
x = 3;
CHECK(x == 3);
CHECK(vector.x == 1);
}
2021-12-24 21:31:27 +08:00
SUBCASE("destructure by ref")
{
auto& [x, y, z] = vector;
CHECK(x == 1);
CHECK(y == 2);
CHECK(z == 3);
static_assert(std::is_same_v<decltype(x), decltype(vector.x)>);
x = 3;
CHECK(x == 3);
CHECK(vector.x == 3);
}
}
SUBCASE("Length and normalization")
{
const sf::Vector3f v(2.4f, 3.0f, 5.2f);
CHECK(v.length() == Approx(6.46529));
CHECK(v.lengthSq() == Approx(41.79997));
CHECK(v.normalized() == ApproxVec3(0.37121f, 0.46401f, 0.80429f));
}
SUBCASE("Products and quotients")
{
const sf::Vector3f v(2.4f, 3.0f, 5.2f);
const sf::Vector3f w(-0.7f, -2.2f, -4.8f);
CHECK(v.dot(w) == Approx(-33.24));
CHECK(w.dot(v) == Approx(-33.24));
CHECK(v.cross(w) == ApproxVec3(-2.96f, 7.88f, -3.18f));
CHECK(w.cross(v) == ApproxVec3(2.96f, -7.88f, 3.18f));
CHECK(v.cwiseMul(w) == ApproxVec3(-1.68f, -6.6f, -24.96f));
CHECK(w.cwiseMul(v) == ApproxVec3(-1.68f, -6.6f, -24.96f));
CHECK(v.cwiseDiv(w) == ApproxVec3(-3.428571f, -1.363636f, -1.0833333f));
CHECK(w.cwiseDiv(v) == ApproxVec3(-0.291666f, -0.733333f, -0.9230769f));
}
2021-12-24 21:31:27 +08:00
SUBCASE("Constexpr support")
{
constexpr sf::Vector3i vector(1, 2, 3);
static_assert(vector.x == 1);
static_assert(vector.y == 2);
static_assert(vector.z == 3);
static_assert(vector + sf::Vector3i(3, 2, 1) == sf::Vector3i(4, 4, 4));
}
2018-08-19 22:14:54 +08:00
}