]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-parse-util: use newlocale() and freelocale() to set temporarl locale
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 6 Sep 2025 18:30:21 +0000 (03:30 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 17 Sep 2025 13:20:42 +0000 (22:20 +0900)
This also drops unnecessary call of strtod(), as it is a test for
safe_atod().

src/test/test-parse-util.c

index 2a54dae2a4663014f1ddda46a08c4a410b886474..37d0280cadd639aa1b706cb82c4b29a76c6e7d91 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include <sys/socket.h>
 
+#include "locale-util.h"
 #include "parse-util.h"
 #include "tests.h"
 
@@ -655,7 +656,6 @@ TEST(safe_atoux64) {
 
 TEST(safe_atod) {
         double d;
-        char *e;
 
         ASSERT_ERROR(safe_atod("junk", &d), EINVAL);
 
@@ -663,39 +663,18 @@ TEST(safe_atod) {
         assert_se(fabs(d - 0.2244) < 0.000001);
 
         ASSERT_ERROR(safe_atod("0,5", &d), EINVAL);
-
-        errno = 0;
-        strtod("0,5", &e);
-        assert_se(*e == ',');
-
         ASSERT_ERROR(safe_atod("", &d), EINVAL);
 
         /* Check if this really is locale independent */
-        if (setlocale(LC_NUMERIC, "de_DE.utf8")) {
-
-                ASSERT_OK_ZERO(safe_atod("0.2244", &d));
-                assert_se(fabs(d - 0.2244) < 0.000001);
-
-                ASSERT_ERROR(safe_atod("0,5", &d), EINVAL);
-
-                errno = 0;
-                assert_se(fabs(strtod("0,5", &e) - 0.5) < 0.00001);
-
-                ASSERT_ERROR(safe_atod("", &d), EINVAL);
-        }
-
-        /* And check again, reset */
-        ASSERT_NOT_NULL(setlocale(LC_NUMERIC, "C"));
+        _cleanup_(freelocalep) locale_t loc =
+                newlocale(LC_NUMERIC_MASK, "de_DE.utf8", (locale_t) 0);
+        if (!loc)
+                return (void) log_tests_skipped_errno(errno, "locale de_DE.utf8 not found");
 
         ASSERT_OK_ZERO(safe_atod("0.2244", &d));
         assert_se(fabs(d - 0.2244) < 0.000001);
 
         ASSERT_ERROR(safe_atod("0,5", &d), EINVAL);
-
-        errno = 0;
-        strtod("0,5", &e);
-        assert_se(*e == ',');
-
         ASSERT_ERROR(safe_atod("", &d), EINVAL);
 }