Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions tests/test_math.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "soft_render/math/vec4.hpp"
#include "soft_render/math/vec3.hpp"
#include <iostream>
#include <cassert>
#include <cmath>
Expand Down Expand Up @@ -51,8 +52,63 @@ void test_vec4_perspective() {
std::cout << "test_vec4_perspective passed!" << std::endl;
}


void test_vec3_dot() {
std::cout << "Running test_vec3_dot..." << std::endl;

// Case 1: Orthogonal vectors (dot product = 0)
{
Vec3 v1(1.0f, 0.0f, 0.0f);
Vec3 v2(0.0f, 1.0f, 0.0f);
assert(std::abs(v1.dot(v2)) < 1e-6f);
std::cout << " Orthogonal vectors passed" << std::endl;
}

// Case 2: Parallel vectors (dot product = length1 * length2)
{
Vec3 v1(2.0f, 0.0f, 0.0f);
Vec3 v2(3.0f, 0.0f, 0.0f);
assert(std::abs(v1.dot(v2) - 6.0f) < 1e-6f);
std::cout << " Parallel vectors passed" << std::endl;
}

// Case 3: Anti-parallel vectors
{
Vec3 v1(2.0f, 0.0f, 0.0f);
Vec3 v2(-3.0f, 0.0f, 0.0f);
assert(std::abs(v1.dot(v2) - (-6.0f)) < 1e-6f);
std::cout << " Anti-parallel vectors passed" << std::endl;
}

// Case 4: Vector with itself (dot product = length squared)
{
Vec3 v(1.0f, 2.0f, 3.0f);
assert(std::abs(v.dot(v) - 14.0f) < 1e-6f);
std::cout << " Vector with itself passed" << std::endl;
}

// Case 5: Zero vector
{
Vec3 v1(0.0f, 0.0f, 0.0f);
Vec3 v2(1.0f, 2.0f, 3.0f);
assert(std::abs(v1.dot(v2)) < 1e-6f);
std::cout << " Zero vector passed" << std::endl;
}

// Case 6: Mixed components
{
Vec3 v1(1.0f, 2.0f, 3.0f);
Vec3 v2(4.0f, -5.0f, 6.0f);
assert(std::abs(v1.dot(v2) - 12.0f) < 1e-6f); // 4 - 10 + 18 = 12
std::cout << " Mixed components passed" << std::endl;
}

std::cout << "test_vec3_dot passed!" << std::endl;
}

int main() {
test_vec4_perspective();
test_vec3_dot();
std::cout << "All tests passed!" << std::endl;
return 0;
}