From 3aa4bf34cf1f8a710764bfd35849c2474589bf2e Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Wed, 9 Apr 2025 12:35:16 -0400 Subject: pke-test: compare floats using epsilon --- tests/pke-test-static-ui.cpp | 19 ++++++++++++++----- tests/pke-test-types.h | 6 ++++++ 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/pke-test-static-ui.cpp b/tests/pke-test-static-ui.cpp index 9ca4cdd..410f290 100644 --- a/tests/pke-test-static-ui.cpp +++ b/tests/pke-test-static-ui.cpp @@ -1,9 +1,12 @@ #include "./pke-test-static-ui.h" +#include "./pke-test-types.h" + #include "window.hpp" #include "dynamic-array.hpp" #include "vendor-glm-include.hpp" +#include struct pke_ui_box_instance_buffer_item { glm::mat4 pos_scale; glm::vec2 px_scale; @@ -74,6 +77,7 @@ int pke_test_static_ui_000() { // test dynamic int pke_test_static_ui_100() { DynArray arr{}; + bool b; float unit; float calculated_offset_x, calculated_offset_y; uint8_t err_index = 0; @@ -116,11 +120,16 @@ int pke_test_static_ui_100() { PKE_TEST_ASSERT(ui_box->internal.px_size.x == (Extent.width * 0.1) * 8, err_index); PKE_TEST_ASSERT(ui_box->internal.px_size.y == (Extent.height * 0.1) * 8, err_index); - // TODO rounding? - // using a debugger, the actual and calculated values are ~0.01 off - // this is either a real issue or a rounding issue, either here or within pke - PKE_TEST_ASSERT(c_ui_box->internal.px_size.x == (ui_box->internal.px_size.x - (built_in_offset * 2)) * 0.8, err_index); - PKE_TEST_ASSERT(c_ui_box->internal.px_size.y == (ui_box->internal.px_size.y - (built_in_offset * 2)) * 0.8, err_index); + b = flt_equal( + c_ui_box->internal.px_size.x, + (ui_box->internal.px_size.x - (built_in_offset * 2)) * 0.8, + std::numeric_limits::epsilon()); + PKE_TEST_ASSERT(b, err_index); + b = flt_equal( + c_ui_box->internal.px_size.y, + (ui_box->internal.px_size.y - (built_in_offset * 2)) * 0.8, + std::numeric_limits::epsilon()); + PKE_TEST_ASSERT(b, err_index); return 0; } diff --git a/tests/pke-test-types.h b/tests/pke-test-types.h index 5a9ee24..097d42f 100644 --- a/tests/pke-test-types.h +++ b/tests/pke-test-types.h @@ -3,6 +3,7 @@ #include #include +#include struct pke_test_long_jump { uint8_t expected_exit; @@ -38,4 +39,9 @@ struct pke_test_group { #define PKE_TEST_ASSERT(condition, index) if (!(condition)) { return ++index; } else { ++index; } +template +inline bool flt_equal(T a, T b, T epsilon) { + return std::abs(a - b) < epsilon; +} + #endif /* PKE_PKE_TEST_TYPES_H */ -- cgit v1.2.3