feat: add invariant-based test suite (50+ uncheateable tests)#19
feat: add invariant-based test suite (50+ uncheateable tests)#19EricBorges2019 merged 2 commits intomainfrom
Conversation
Add 50+ tests across 3 new test files that verify mathematical properties, rasterization correctness, and full pipeline integration using randomized inputs — designed to be robust against stub implementations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the testing infrastructure by introducing a comprehensive, invariant-based test suite. The new tests cover critical components of the rendering pipeline, including mathematical operations, rasterization logic, and full pipeline integration. By employing randomized inputs and verifying fundamental mathematical and graphical invariants, these tests provide a robust mechanism to ensure the correctness and stability of the renderer, making it resilient to future changes or stub implementations. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This is an excellent pull request that adds a comprehensive and well-designed invariant-based test suite. The 50+ new tests for math, rasterizer, and pipeline integration will significantly improve the project's robustness and make future development safer. The strategy of testing mathematical properties rather than hardcoded values is particularly effective.
My review includes a couple of suggestions for improvement:
- In
CMakeLists.txt, I recommend refactoring the test setup to reduce code duplication. - Across the new test files, I've noticed that floating-point comparisons often use inconsistent and sometimes overly loose tolerances. Tightening these and using shared constants would make the tests more robust and precise.
Overall, this is a fantastic contribution. The new tests are a major asset to the project.
CMakeLists.txt
Outdated
| add_executable(test_math_invariants tests/test_math_invariants.cpp) | ||
| target_link_libraries(test_math_invariants PRIVATE soft_render) | ||
| add_test(NAME test_math_invariants COMMAND test_math_invariants) | ||
|
|
||
| add_executable(test_rasterizer tests/test_rasterizer.cpp) | ||
| target_link_libraries(test_rasterizer PRIVATE soft_render) | ||
| add_test(NAME test_rasterizer COMMAND test_rasterizer) | ||
|
|
||
| add_executable(test_pipeline_integration tests/test_pipeline_integration.cpp) | ||
| target_link_libraries(test_pipeline_integration PRIVATE soft_render) | ||
| add_test(NAME test_pipeline_integration COMMAND test_pipeline_integration) |
There was a problem hiding this comment.
To improve maintainability and reduce code duplication, you could refactor the logic for adding tests into a CMake function. This would make it easier to add more tests in the future.
function(add_soft_render_test test_name)
add_executable(${test_name} tests/${test_name}.cpp)
target_link_libraries(${test_name} PRIVATE soft_render)
add_test(NAME ${test_name} COMMAND ${test_name})
endfunction()
add_soft_render_test(test_math_invariants)
add_soft_render_test(test_rasterizer)
add_soft_render_test(test_pipeline_integration)
tests/test_math_invariants.cpp
Outdated
| static bool approx(float a, float b, float eps = EPS) { return std::abs(a - b) < eps; } | ||
| static bool approxVec3(const Vec3& a, const Vec3& b, float eps = EPS) { | ||
| return approx(a.x, b.x, eps) && approx(a.y, b.y, eps) && approx(a.z, b.z, eps); | ||
| } |
There was a problem hiding this comment.
The approx functions are great helpers for floating-point comparisons. However, many of the assert statements throughout this file use hardcoded, and sometimes quite large, tolerance values (e.g., 0.1f, 0.05f, 0.01f) instead of the default EPS (1e-4f). This inconsistency can make tests less precise and might hide subtle bugs.
Consider using the EPS constant more consistently for these checks. If different levels of precision are needed for different tests, it would be clearer to define named constants for them (e.g., ROUGH_EPS, MEDIUM_EPS).
| auto checker = [&](const Fragment& f) -> Color { | ||
| ++fragment_count; | ||
| // Barycentric coords should sum to ~1 | ||
| float sum = f.w0 + f.w1 + f.w2; | ||
| if (std::abs(sum - 1.0f) > 0.02f) all_valid = false; | ||
| // Each should be non-negative | ||
| if (f.w0 < -0.01f || f.w1 < -0.01f || f.w2 < -0.01f) all_valid = false; | ||
| return {f.w0, f.w1, f.w2}; |
There was a problem hiding this comment.
In this test, and others in this file, the tolerances for floating-point checks are quite loose (e.g., 0.02f for barycentric coordinate sum, -0.01f for non-negativity). While floating-point arithmetic has precision limits, these values seem overly generous and might allow incorrect implementations to pass.
For example, the sum of barycentric coordinates should be very close to 1.0. A tolerance closer to the machine epsilon for floats (like 1e-6f) would make this test more robust. Consistently using stricter tolerances across the test suite would improve confidence in the implementation's correctness.
…erances - Extract shared helpers (approx, PRNG, framebuffer inspection) into test_helpers.hpp to eliminate duplication across test files - Use sr_add_test() CMake function to reduce CMakeLists.txt boilerplate - Standardize tolerances: EPS (1e-4) for single ops, EPS_LOOSE (1e-2) for chained transforms — no more ad-hoc magic numbers Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
Test files
Test plan
ctest --output-on-failurepasses all 6 suites (0.83s total)🤖 Generated with Claude Code