Commit a149151af557dd725a67dd5806f27713b281a7d7

Thomas de Grivel 2023-01-03T04:11:42

fix tests for buf_parse_f32

diff --git a/test/buf_parse_test.c b/test/buf_parse_test.c
index 7e8898c..b1bccf3 100644
--- a/test/buf_parse_test.c
+++ b/test/buf_parse_test.c
@@ -126,14 +126,14 @@
     test_context(NULL);                                                \
   } while (0)
 
-#define BUF_PARSE_TEST_F32(test, expected)                             \
+#define BUF_PARSE_TEST_F32(test, expected1, expected2)                 \
   do {                                                                 \
     s_buf buf;                                                         \
     f32 dest = -1;                                                     \
-    test_context("buf_parse_f32(" # test ") -> " # expected);          \
+    test_context("buf_parse_f32(" # test ") -> " # expected1);         \
     buf_init_1(&buf, (test));                                          \
     TEST_EQ(buf_parse_f32(&buf, &dest), strlen(test));                 \
-    TEST_FLOAT_EQ(dest, (expected));                                   \
+    TEST_FLOAT_EQ2(dest, (expected1), (expected2));                    \
     buf_clean(&buf);                                                   \
     test_context(NULL);                                                \
   } while (0)
@@ -789,9 +789,9 @@ void buf_parse_test_digit_dec ()
 
 void buf_parse_test_f32()
 {
-  BUF_PARSE_TEST_F32("123.123", 123.123);
-  BUF_PARSE_TEST_F32("3.14159", 3.14159);
-  BUF_PARSE_TEST_F32("2.1e+2", 210);
+  BUF_PARSE_TEST_F32("123.123", 123.123, 123.1229934692383);
+  BUF_PARSE_TEST_F32("3.14159", 3.14159, 3.141589641571045);
+  BUF_PARSE_TEST_F32("2.1e+2", 210, 209.9999847412109);
 }
 
 void buf_parse_test_f64()
diff --git a/test/test.h b/test/test.h
index 99a595c..0cd41d3 100644
--- a/test/test.h
+++ b/test/test.h
@@ -66,6 +66,24 @@
     }                                                                  \
   } while (0)
 
+#define TEST_FLOAT_EQ2(test, expected1, expected2)                     \
+  do {                                                                 \
+    f32 tmp = (f32) (test);                                            \
+    if (fabsf(tmp - (f32) (expected1)) <= FLT_EPSILON ||               \
+        fabsf(tmp - (f32) (expected2)) <= FLT_EPSILON)                 \
+      test_ok();                                                       \
+    else {                                                             \
+      test_ko();                                                       \
+      printf("\n%sAssertion failed in %s:%d %s\n"                      \
+             "%s == %s\n"                                              \
+             "Expected %s got %.16g.%s\n",                             \
+             TEST_COLOR_KO,                                            \
+             __FILE__, __LINE__, __func__,                             \
+             # test, # expected1, # expected1, tmp,                    \
+             TEST_COLOR_RESET);                                        \
+    }                                                                  \
+  } while (0)
+
 #define TEST_DOUBLE_EQ(test, expected)                                 \
   do {                                                                 \
     f64 tmp = (f64) (test);                                            \
@@ -75,7 +93,7 @@
       test_ko();                                                       \
       printf("\n%sAssertion failed in %s:%d %s\n"                      \
              "%s == %s\n"                                              \
-             "Expected %s got %f.%s\n",                                \
+             "Expected %s got %lf.%s\n",                                \
              TEST_COLOR_KO,                                            \
              __FILE__, __LINE__, __func__,                             \
              # test, # expected, # expected, tmp,                      \