tests/test_string.c

changeset 1063
e453e717876e
parent 1061
c7d23892eab5
--- a/tests/test_string.c	Sat Dec 28 17:31:28 2024 +0100
+++ b/tests/test_string.c	Sat Dec 28 17:32:36 2024 +0100
@@ -30,6 +30,7 @@
 #include "util_allocator.h"
 
 #include "cx/string.h"
+#include "cx/compare.h"
 
 #include <limits.h>
 #include <errno.h>
@@ -1167,16 +1168,47 @@
     float f;
     CX_TEST_DO {
         CX_TEST_ASSERT(0 == cx_strtof(cx_str("11.3"), &f));
-        CX_TEST_ASSERT(11.3f == f);
+        CX_TEST_ASSERT(0 == cx_vcmp_float(11.3f, f));
+
+        CX_TEST_ASSERT(0 == cx_strtof(cx_str("-4.711e+1"), &f));
+        CX_TEST_ASSERT(0 == cx_vcmp_float(-47.11f, f));
 
         CX_TEST_ASSERT(0 == cx_strtof(cx_str("1.67262192595e-27"), &f));
-        CX_TEST_ASSERT(1.67262192595e-27f == f);
+        CX_TEST_ASSERT(0 == cx_vcmp_float(1.67262192595e-27f, f));
 
         CX_TEST_ASSERT(0 == cx_strtof_lc(cx_str("138,339.4"), &f, '.', ","));
-        CX_TEST_ASSERT(138339.4f == f);
+        CX_TEST_ASSERT(0 == cx_vcmp_float(138339.4f, f));
 
         CX_TEST_ASSERT(0 == cx_strtof_lc(cx_str("138,339.4"), &f, ',', "."));
-        CX_TEST_ASSERT(138.3394f == f);
+        CX_TEST_ASSERT(0 == cx_vcmp_float(138.3394f, f));
+
+        errno = 0;
+        CX_TEST_ASSERT(0 != cx_strtof(cx_str("15e"), &f));
+        CX_TEST_ASSERT(errno == EINVAL);
+        errno = 0;
+        CX_TEST_ASSERT(0 != cx_strtof(cx_str("15e+"), &f));
+        CX_TEST_ASSERT(errno == EINVAL);
+        errno = 0;
+        CX_TEST_ASSERT(0 != cx_strtof(cx_str("15e-"), &f));
+        CX_TEST_ASSERT(errno == EINVAL);
+        CX_TEST_ASSERT(0 == cx_strtof(cx_str("15e-0"), &f));
+        CX_TEST_ASSERT(0 == cx_vcmp_float(15.f, f));
+
+        CX_TEST_ASSERT(0 == cx_strtof(cx_str("3e38"), &f));
+        CX_TEST_ASSERT(0 == cx_vcmp_float(3e38f, f));
+        errno = 0;
+        CX_TEST_ASSERT(0 != cx_strtof(cx_str("3e39"), &f));
+        CX_TEST_ASSERT(errno == ERANGE);
+        CX_TEST_ASSERT(0 == cx_strtof(cx_str("-3e38"), &f));
+        CX_TEST_ASSERT(0 == cx_vcmp_float(-3e38f, f));
+        errno = 0;
+        CX_TEST_ASSERT(0 != cx_strtof(cx_str("-3e39"), &f));
+        CX_TEST_ASSERT(errno == ERANGE);
+        CX_TEST_ASSERT(0 == cx_strtof(cx_str("1.18e-38"), &f));
+        CX_TEST_ASSERT(0 == cx_vcmp_float(1.18e-38f, f));
+        errno = 0;
+        CX_TEST_ASSERT(0 != cx_strtof(cx_str("1.17e-38"), &f));
+        CX_TEST_ASSERT(errno == ERANGE);
     }
 }
 
@@ -1184,24 +1216,24 @@
     double d;
     CX_TEST_DO {
         CX_TEST_ASSERT(0 == cx_strtod(cx_str("11.3"), &d));
-        CX_TEST_ASSERT(11.3 == d);
+        CX_TEST_ASSERT(0 == cx_vcmp_double(11.3, d));
+
+        CX_TEST_ASSERT(0 == cx_strtod(cx_str("-13.37"), &d));
+        CX_TEST_ASSERT(0 == cx_vcmp_double(-13.37, d));
+
+        CX_TEST_ASSERT(0 == cx_strtod(cx_str("-4.711e+1"), &d));
+        CX_TEST_ASSERT(0 == cx_vcmp_double(-47.11, d));
 
         CX_TEST_ASSERT(0 == cx_strtod(cx_str("1.67262192595e-27"), &d));
-        CX_TEST_ASSERT(1.67262192595e-27 == d);
+        CX_TEST_ASSERT(0 == cx_vcmp_double(1.67262192595e-27, d));
 
         CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, '.', ","));
-        CX_TEST_ASSERT(138339.4 == d);
+        CX_TEST_ASSERT(0 == cx_vcmp_double(138339.4, d));
 
         CX_TEST_ASSERT(0 == cx_strtod_lc(cx_str("138,339.4"), &d, ',', "."));
-        CX_TEST_ASSERT(138.3394 == d);
-    }
-}
+        CX_TEST_ASSERT(0 == cx_vcmp_double(138.3394, d));
 
-CX_TEST(test_string_to_float_german) {
-    float f;
-    CX_TEST_DO {
-        // TODO: implement
-        (void)f;
+        // TODO: test and improve support for big numbers, precision, and out-of-range detection
     }
 }
 
@@ -1248,7 +1280,6 @@
     cx_test_register(suite, test_string_to_unsigned_integer);
     cx_test_register(suite, test_string_to_float);
     cx_test_register(suite, test_string_to_double);
-    cx_test_register(suite, test_string_to_float_german);
 
     return suite;
 }

mercurial