Files
Library/tests/standalone/Math/VecMat.cpp
T
faumarayandClaude Sonnet 4.6 a09e000f0f Add Hash, Random, Uuid, Encoding, Bits, Math modules + make Testing deps optional
New header-only modules:
- Void::Hash — FNV-1a 32/64, CRC-32, DJB2, MurmurHash3-32, UDL literals
- Void::Random — Xoshiro256** PRNG, uniform_int/real, coin_flip, shuffle, random_string
- Void::Uuid — v4 (random), v5_lite (name-based), parse, RFC 4122 namespaces
- Void::Encoding — Base64 (standard + URL-safe), Hex encode/decode
- Void::Bits — popcount, bit extract/deposit, rotation, align, byteswap, masks
- Void::Math — Vec2/Vec3/Vec4, Mat4 (identity, translation, scale, rotation)

Testing module: move Stacktrace from INTERNAL_DEPS to OPTIONAL_DEPS. Added
Detail/Color.hpp shim that delegates to Stacktrace::Color when present or
provides inline ANSI fallback otherwise. All Stacktrace calls in SignalHandler,
ConsoleReporter, and Runner are gated on VOID_HAS_STACKTRACE.

Fix Repr.hpp reflect_struct to handle template specializations that lack
std::meta::identifier_of (now guarded by has_identifier check).

33/33 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 21:52:22 +03:00

155 lines
4.9 KiB
C++

#include <Void/Math/Mat.hpp>
#include <Void/Math/Vec.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cmath>
static constexpr float EPS = 1e-5f;
// ── Vec2 ──────────────────────────────────────────────────────────────────────
Void::Testing::Test vec2_add()
{
constexpr auto v = Void::Math::Vec2f{1, 2} + Void::Math::Vec2f{3, 4};
static_assert(v.x == 4.f && v.y == 6.f);
Void::Testing::expect_eq(v.x, 4.f);
}
Void::Testing::Test vec2_subtract()
{
constexpr auto v = Void::Math::Vec2f{3, 4} - Void::Math::Vec2f{1, 1};
Void::Testing::expect_eq(v.x, 2.f);
Void::Testing::expect_eq(v.y, 3.f);
}
Void::Testing::Test vec2_scale()
{
constexpr auto v = Void::Math::Vec2f{2, 3} * 2.f;
Void::Testing::expect_eq(v.x, 4.f);
Void::Testing::expect_eq(v.y, 6.f);
}
Void::Testing::Test vec2_dot()
{
constexpr auto d = Void::Math::Vec2f{1, 0}.dot({0, 1});
static_assert(d == 0.f);
Void::Testing::expect_eq(d, 0.f);
}
Void::Testing::Test vec2_length()
{
const Void::Math::Vec2f v{3, 4};
Void::Testing::expect(std::abs(v.length() - 5.f) < EPS);
}
Void::Testing::Test vec2_normalized()
{
const Void::Math::Vec2f v = Void::Math::Vec2f{3, 4}.normalized();
Void::Testing::expect(std::abs(v.length() - 1.f) < EPS);
}
Void::Testing::Test vec2_lerp()
{
const auto v = Void::Math::lerp(Void::Math::Vec2f{0, 0},
Void::Math::Vec2f{2, 4}, 0.5f);
Void::Testing::expect(std::abs(v.x - 1.f) < EPS);
Void::Testing::expect(std::abs(v.y - 2.f) < EPS);
}
// ── Vec3 ──────────────────────────────────────────────────────────────────────
Void::Testing::Test vec3_cross_unit_vectors()
{
constexpr Void::Math::Vec3f x{1, 0, 0};
constexpr Void::Math::Vec3f y{0, 1, 0};
constexpr Void::Math::Vec3f z = x.cross(y);
static_assert(z.x == 0.f && z.y == 0.f && z.z == 1.f);
Void::Testing::expect_eq(z.z, 1.f);
}
Void::Testing::Test vec3_length()
{
const Void::Math::Vec3f v{1, 2, 2};
Void::Testing::expect(std::abs(v.length() - 3.f) < EPS);
}
Void::Testing::Test vec3_normalized_unit_length()
{
const auto v = Void::Math::Vec3f{2, 3, 6}.normalized();
Void::Testing::expect(std::abs(v.length() - 1.f) < EPS);
}
// ── Vec4 ──────────────────────────────────────────────────────────────────────
Void::Testing::Test vec4_from_vec3()
{
constexpr Void::Math::Vec4f v{Void::Math::Vec3f{1, 2, 3}, 1.f};
static_assert(v.w == 1.f);
Void::Testing::expect_eq(v.z, 3.f);
}
Void::Testing::Test vec4_xyz()
{
constexpr Void::Math::Vec4f v{1, 2, 3, 4};
constexpr auto xyz = v.xyz();
static_assert(xyz.x == 1.f && xyz.y == 2.f && xyz.z == 3.f);
}
// ── Mat4 ──────────────────────────────────────────────────────────────────────
Void::Testing::Test mat4_identity_times_vec()
{
constexpr auto I = Void::Math::Mat4f::identity();
constexpr Void::Math::Vec4f v{1, 2, 3, 1};
constexpr auto r = I * v;
static_assert(r.x == 1.f && r.y == 2.f && r.z == 3.f && r.w == 1.f);
}
Void::Testing::Test mat4_translation()
{
const auto T = Void::Math::Mat4f::translation(1, 2, 3);
const Void::Math::Vec4f v{0, 0, 0, 1};
const auto r = T * v;
Void::Testing::expect(std::abs(r.x - 1.f) < EPS);
Void::Testing::expect(std::abs(r.y - 2.f) < EPS);
Void::Testing::expect(std::abs(r.z - 3.f) < EPS);
}
Void::Testing::Test mat4_scale()
{
const auto S = Void::Math::Mat4f::scale(2, 3, 4);
const Void::Math::Vec4f v{1, 1, 1, 1};
const auto r = S * v;
Void::Testing::expect(std::abs(r.x - 2.f) < EPS);
Void::Testing::expect(std::abs(r.y - 3.f) < EPS);
Void::Testing::expect(std::abs(r.z - 4.f) < EPS);
}
Void::Testing::Test mat4_rotation_z_90_degrees()
{
const auto R = Void::Math::Mat4f::rotation_z(
static_cast<float>(M_PI) / 2.f);
const Void::Math::Vec4f v{1, 0, 0, 0};
const auto r = R * v;
Void::Testing::expect(std::abs(r.x - 0.f) < EPS);
Void::Testing::expect(std::abs(r.y - 1.f) < EPS);
}
Void::Testing::Test mat4_mul_identity_nop()
{
const auto A = Void::Math::Mat4f::translation(1, 2, 3);
const auto I = Void::Math::Mat4f::identity();
const auto R = A * I;
Void::Testing::expect_eq(R, A);
}
Void::Testing::Test mat4_transpose()
{
auto M = Void::Math::Mat4f::identity();
M.at(0, 1) = 5.f;
const auto T = M.transposed();
Void::Testing::expect_eq(T.at(1, 0), 5.f);
Void::Testing::expect_eq(T.at(0, 1), 0.f);
}