diff --git a/CMakeLists.txt b/CMakeLists.txt index c6d40ce..16724b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,6 +82,11 @@ option(BUILD_TESTS "Build test suite" ON) if(BUILD_TESTS) enable_testing() + + add_executable(test_framebuffer tests/test_framebuffer.cpp) + target_link_libraries(test_framebuffer PRIVATE soft_render) + add_test(NAME test_framebuffer COMMAND test_framebuffer) + add_executable(test_vec3 tests/test_vec3.cpp) target_link_libraries(test_vec3 PRIVATE soft_render) add_test(NAME test_vec3 COMMAND test_vec3) diff --git a/tests/test_framebuffer.cpp b/tests/test_framebuffer.cpp new file mode 100644 index 0000000..1f96481 --- /dev/null +++ b/tests/test_framebuffer.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include "soft_render/core/framebuffer.hpp" +#include "soft_render/math/vec3.hpp" + +using namespace sr::core; +using namespace sr::math; + +void test_framebuffer_clear() { + std::cout << "Running test_framebuffer_clear..." << std::endl; + + int width = 10; + int height = 10; + Framebuffer fb(width, height); + + // Test clear with default color (black) + fb.clear(); + const Pixel* pixels = fb.pixels(); + const float* depth = fb.depthBuffer(); + + for (int i = 0; i < width * height; ++i) { + assert(pixels[i].r == 0); + assert(pixels[i].g == 0); + assert(pixels[i].b == 0); + assert(pixels[i].a == 255); + assert(depth[i] == std::numeric_limits::infinity()); + } + std::cout << " Default clear passed" << std::endl; + + // Test clear with custom color + Color customColor(1.0f, 0.5f, 0.25f); + fb.clear(customColor); + + for (int i = 0; i < width * height; ++i) { + assert(pixels[i].r == 255); + assert(pixels[i].g == 127); + assert(pixels[i].b == 63); + assert(pixels[i].a == 255); + assert(depth[i] == std::numeric_limits::infinity()); + } + std::cout << " Custom color clear passed" << std::endl; + + // Test clear with out-of-bounds color values + Color outOfBoundsColor(1.5f, -0.5f, 2.0f); + fb.clear(outOfBoundsColor); + + for (int i = 0; i < width * height; ++i) { + assert(pixels[i].r == 255); // clamped to 1.0 + assert(pixels[i].g == 0); // clamped to 0.0 + assert(pixels[i].b == 255); // clamped to 1.0 + assert(pixels[i].a == 255); + assert(depth[i] == std::numeric_limits::infinity()); + } + std::cout << " Out-of-bounds color clear passed" << std::endl; + + // Test depth test modifying depth, then clear + fb.depthTest(0, 0, 0.5f); + assert(fb.getDepth(0, 0) == 0.5f); + fb.clear(); + assert(fb.getDepth(0, 0) == std::numeric_limits::infinity()); + std::cout << " Depth buffer reset passed" << std::endl; + + std::cout << "test_framebuffer_clear passed!" << std::endl; +} + +int main() { + test_framebuffer_clear(); + std::cout << "All tests passed!" << std::endl; + return 0; +}