summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-04-09 12:35:16 -0400
committerJonathan Bradley <jcb@pikum.xyz>2025-04-09 12:35:16 -0400
commit3aa4bf34cf1f8a710764bfd35849c2474589bf2e (patch)
treea134799ed41152806f868754a83595b10888b346 /tests
parent63ce7559ce34505eec576fcf43c7cb62a814f31a (diff)
pke-test: compare floats using epsilon
Diffstat (limited to 'tests')
-rw-r--r--tests/pke-test-static-ui.cpp19
-rw-r--r--tests/pke-test-types.h6
2 files changed, 20 insertions, 5 deletions
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 <limits>
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<pke_ui_box_instance_buffer_item> 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<float>(
+ c_ui_box->internal.px_size.x,
+ (ui_box->internal.px_size.x - (built_in_offset * 2)) * 0.8,
+ std::numeric_limits<float>::epsilon());
+ PKE_TEST_ASSERT(b, err_index);
+ b = flt_equal<float>(
+ c_ui_box->internal.px_size.y,
+ (ui_box->internal.px_size.y - (built_in_offset * 2)) * 0.8,
+ std::numeric_limits<float>::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 <setjmp.h>
#include <stdint.h>
+#include <cmath>
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<typename T>
+inline bool flt_equal(T a, T b, T epsilon) {
+ return std::abs(a - b) < epsilon;
+}
+
#endif /* PKE_PKE_TEST_TYPES_H */