1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2025 Oxide Computer Company
* Copyright 2025 Bill Sommerfeld
*/
/*
* Tests for locale interaction with strto{f,d,ld}(3C) and
* strto{f,d,ld}_l(3C). The libc tests depend on locale/ar, locale/de,
* locale/en, and locale/ja. We limit ourselves to these locales, plus
* C.UTF-8.
*/
#include <err.h>
#include <stdlib.h>
#include <xlocale.h>
#include <locale.h>
#include <sys/sysmacros.h>
#include <sys/debug.h>
#include <stdbool.h>
#include <string.h>
/*
* We use a value with a fractional part that can be represented exactly
* as a binary floating point number
*/
static struct test_locale {
char *name;
char *number;
} locales[] = {
{ "C.UTF-8", "1.5"},
{"ja_JP.UTF-8", "1.5"},
{"de_DE.UTF-8", "1,5"},
{"en_US.UTF-8", "1.5"},
{"en_GB.UTF-8", "1.5" },
};
/*
* Test that the correct decimal point is recognized.
*/
bool
test_locale(char *name, char *number)
{
bool result_ok = true;
const float expected_f = 1.5;
const double expected_d = 1.5;
const long double expected_ld = 1.5;
char *expected_end = number + strlen(number);
char *actual_end;
float actual_f;
double actual_d;
long double actual_ld;
locale_t loc = newlocale(LC_ALL_MASK, name, NULL);
if (loc == NULL) {
err(EXIT_FAILURE, "INTERNAL TEST FAILURE: failed to "
"construct locale %s", name);
}
#define CHECK_END(fn) \
if (actual_end != expected_end) { \
warn("Locale %s, function %s: consumed %td characters" \
" (%td expected)", name, #fn, \
actual_end - number, expected_end - number); \
result_ok = false; \
}
actual_f = strtof_l(number, &actual_end, loc);
if (actual_f != expected_f) {
result_ok = false;
warn("Locale %s: strtof_l: mismatched value %f vs %f",
name, actual_f, expected_f);
}
CHECK_END(strtof_l);
actual_d = strtod_l(number, &actual_end, loc);
if (actual_d != expected_d) {
result_ok = false;
warn("Locale %s: strtod_l: mismatched value %f vs %f",
name, actual_d, expected_d);
}
CHECK_END(strtod_l);
actual_ld = strtold_l(number, &actual_end, loc);
if (actual_ld != expected_ld) {
result_ok = false;
warn("Locale %s: strtold_l: mismatched value %Lf vs %Lf",
name, actual_ld, expected_ld);
}
CHECK_END(strtold_l);
if (uselocale(loc) == NULL) {
err(EXIT_FAILURE, "INTERNAL TEST FAILURE: failed to "
"set locale %s", name);
}
actual_f = strtod(number, &actual_end);
if (actual_f != expected_f) {
result_ok = false;
warn("Locale %s: strtof: mismatched value %f vs %f",
name, actual_f, expected_f);
}
CHECK_END(strtof);
actual_d = strtod(number, &actual_end);
if (actual_d != expected_d) {
result_ok = false;
warn("Locale %s: strtod: mismatched value %f vs %f",
name, actual_d, expected_d);
}
CHECK_END(strtod);
actual_ld = strtold(number, &actual_end);
if (actual_ld != expected_ld) {
result_ok = false;
warn("Locale %s: strtold: mismatched value %Lf vs %Lf",
name, actual_ld, expected_ld);
}
CHECK_END(strtold);
return (result_ok);
}
int
main(void)
{
int ret = EXIT_SUCCESS;
for (size_t i = 0; i < ARRAY_SIZE(locales); i++) {
if (!test_locale(locales[i].name, locales[i].number))
ret = EXIT_FAILURE;
}
if (ret == EXIT_SUCCESS) {
(void) printf("All tests completed successfully\n");
}
return (ret);
}
|