From 6e219a09c2353ddbf52a66536db6c2b5593a903b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 7 Sep 2025 03:30:21 +0900 Subject: [PATCH] test-parse-util: use newlocale() and freelocale() to set temporarl locale This also drops unnecessary call of strtod(), as it is a test for safe_atod(). --- src/test/test-parse-util.c | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/src/test/test-parse-util.c b/src/test/test-parse-util.c index 2a54dae2a46..37d0280cadd 100644 --- a/src/test/test-parse-util.c +++ b/src/test/test-parse-util.c @@ -6,6 +6,7 @@ #include #include +#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); } -- 2.47.3