summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2013-04-25 00:04:02 -0300
committerLennart Poettering <lennart@poettering.net>2013-04-25 00:05:14 -0300
commitd6dd604b551987b411ec8930c23bd5c9c93ef864 (patch)
tree6599838f561b0621fda1bfb660bef74981607367 /src/test
parentdb5c0122853a9ecf1cc92e6593461932df2fa866 (diff)
util: rework safe_atod() to be locale-independent
This adds some syntactic sugar with a macro RUN_WITH_LOCALE() that reset the thread-specific locale temporarily.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test-util.c38
1 files changed, 37 insertions, 1 deletions
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 83959c095..66a10ead4 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -23,6 +23,7 @@
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
+#include <locale.h>
#include "util.h"
@@ -145,13 +146,48 @@ static void test_safe_atolli(void) {
static void test_safe_atod(void) {
int r;
double d;
+ char *e;
+
+ r = safe_atod("junk", &d);
+ assert_se(r == -EINVAL);
r = safe_atod("0.2244", &d);
assert_se(r == 0);
assert_se(abs(d - 0.2244) < 0.000001);
- r = safe_atod("junk", &d);
+ r = safe_atod("0,5", &d);
assert_se(r == -EINVAL);
+
+ errno = 0;
+ strtod("0,5", &e);
+ assert_se(*e == ',');
+
+ /* Check if this really is locale independent */
+ setlocale(LC_NUMERIC, "de_DE.utf8");
+
+ r = safe_atod("0.2244", &d);
+ assert_se(r == 0);
+ assert_se(abs(d - 0.2244) < 0.000001);
+
+ r = safe_atod("0,5", &d);
+ assert_se(r == -EINVAL);
+
+ errno = 0;
+ assert_se(abs(strtod("0,5", &e) - 0.5) < 0.00001);
+
+ /* And check again, reset */
+ setlocale(LC_NUMERIC, "C");
+
+ r = safe_atod("0.2244", &d);
+ assert_se(r == 0);
+ assert_se(abs(d - 0.2244) < 0.000001);
+
+ r = safe_atod("0,5", &d);
+ assert_se(r == -EINVAL);
+
+ errno = 0;
+ strtod("0,5", &e);
+ assert_se(*e == ',');
}
static void test_strappend(void) {