tst-getdate: Improve testcase flexibility and add test.
[glibc.git] / stdio-common / tst-grouping_iterator.c
blob241cd15be62a39251fb65de794cd7841232ea9e1
1 /* Test for struct grouping_iterator.
2 Copyright (C) 2022-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* Rebuild the fail to access internal-only functions. */
20 #include <grouping_iterator.c>
22 #include <stdio.h>
23 #include <support/check.h>
24 #include <support/support.h>
25 #include <support/test-driver.h>
27 static void
28 check (int lineno, const char *groupings,
29 const char *input, const char *expected)
31 if (test_verbose)
33 printf ("info: %s:%d: \"%s\" via \"", __FILE__, lineno, input);
34 for (const char *p = groupings; *p != 0; ++p)
35 printf ("\\%o", *p & 0xff);
36 printf ("\" to \"%s\"\n", expected);
39 size_t initial_group = strchrnul (expected, '\'') - expected;
40 size_t separators = 0;
41 for (const char *p = expected; *p != '\0'; ++p)
42 separators += *p == '\'';
44 size_t digits = strlen (input);
45 char *out = xmalloc (2 * digits + 1);
47 struct grouping_iterator it;
48 TEST_COMPARE (grouping_iterator_setup (&it, digits, groupings),
49 strchr (expected, '\'') != NULL);
50 TEST_COMPARE (it.remaining, digits);
51 TEST_COMPARE (it.remaining_in_current_group, initial_group);
52 TEST_COMPARE (it.separators, separators);
54 char *p = out;
55 while (*input != '\0')
57 if (__grouping_iterator_next (&it))
58 *p++ = '\'';
59 TEST_COMPARE (it.separators, separators);
60 *p++ = *input++;
62 *p++ = '\0';
64 TEST_COMPARE (it.remaining, 0);
65 TEST_COMPARE (it.remaining_in_current_group, 0);
67 TEST_COMPARE_STRING (out, expected);
69 free (out);
72 static int
73 do_test (void)
75 check (__LINE__, "", "1", "1");
76 check (__LINE__, "", "12", "12");
77 check (__LINE__, "", "123", "123");
78 check (__LINE__, "", "1234", "1234");
80 check (__LINE__, "\3", "1", "1");
81 check (__LINE__, "\3", "12", "12");
82 check (__LINE__, "\3", "123", "123");
83 check (__LINE__, "\3", "1234", "1'234");
84 check (__LINE__, "\3", "12345", "12'345");
85 check (__LINE__, "\3", "123456", "123'456");
86 check (__LINE__, "\3", "1234567", "1'234'567");
87 check (__LINE__, "\3", "12345678", "12'345'678");
88 check (__LINE__, "\3", "123456789", "123'456'789");
89 check (__LINE__, "\3", "1234567890", "1'234'567'890");
91 check (__LINE__, "\2\3", "1", "1");
92 check (__LINE__, "\2\3", "12", "12");
93 check (__LINE__, "\2\3", "123", "1'23");
94 check (__LINE__, "\2\3", "1234", "12'34");
95 check (__LINE__, "\2\3", "12345", "123'45");
96 check (__LINE__, "\2\3", "123456", "1'234'56");
97 check (__LINE__, "\2\3", "1234567", "12'345'67");
98 check (__LINE__, "\2\3", "12345678", "123'456'78");
99 check (__LINE__, "\2\3", "123456789", "1'234'567'89");
100 check (__LINE__, "\2\3", "1234567890", "12'345'678'90");
102 check (__LINE__, "\3\2", "1", "1");
103 check (__LINE__, "\3\2", "12", "12");
104 check (__LINE__, "\3\2", "123", "123");
105 check (__LINE__, "\3\2", "1234", "1'234");
106 check (__LINE__, "\3\2", "12345", "12'345");
107 check (__LINE__, "\3\2", "123456", "1'23'456");
108 check (__LINE__, "\3\2", "1234567", "12'34'567");
109 check (__LINE__, "\3\2", "12345678", "1'23'45'678");
110 check (__LINE__, "\3\2", "123456789", "12'34'56'789");
111 check (__LINE__, "\3\2", "1234567890", "1'23'45'67'890");
113 check (__LINE__, "\3\2\1", "1", "1");
114 check (__LINE__, "\3\2\1", "12", "12");
115 check (__LINE__, "\3\2\1", "123", "123");
116 check (__LINE__, "\3\2\1", "1234", "1'234");
117 check (__LINE__, "\3\2\1", "12345", "12'345");
118 check (__LINE__, "\3\2\1", "123456", "1'23'456");
119 check (__LINE__, "\3\2\1", "1234567", "1'2'34'567");
120 check (__LINE__, "\3\2\1", "12345678", "1'2'3'45'678");
121 check (__LINE__, "\3\2\1", "123456789", "1'2'3'4'56'789");
122 check (__LINE__, "\3\2\1", "1234567890", "1'2'3'4'5'67'890");
124 check (__LINE__, "\2\3\1", "1", "1");
125 check (__LINE__, "\2\3\1", "12", "12");
126 check (__LINE__, "\2\3\1", "123", "1'23");
127 check (__LINE__, "\2\3\1", "1234", "12'34");
128 check (__LINE__, "\2\3\1", "12345", "123'45");
129 check (__LINE__, "\2\3\1", "123456", "1'234'56");
130 check (__LINE__, "\2\3\1", "1234567", "1'2'345'67");
131 check (__LINE__, "\2\3\1", "12345678", "1'2'3'456'78");
132 check (__LINE__, "\2\3\1", "123456789", "1'2'3'4'567'89");
133 check (__LINE__, "\2\3\1", "1234567890", "1'2'3'4'5'678'90");
135 /* No repeats. */
136 check (__LINE__, "\3\377", "1", "1");
137 check (__LINE__, "\3\377", "12", "12");
138 check (__LINE__, "\3\377", "123", "123");
139 check (__LINE__, "\3\377", "1234", "1'234");
140 check (__LINE__, "\3\377", "12345", "12'345");
141 check (__LINE__, "\3\377", "123456", "123'456");
142 check (__LINE__, "\3\377", "1234567", "1234'567");
143 check (__LINE__, "\3\377", "12345678", "12345'678");
145 check (__LINE__, "\2\3\377", "1", "1");
146 check (__LINE__, "\2\3\377", "12", "12");
147 check (__LINE__, "\2\3\377", "123", "1'23");
148 check (__LINE__, "\2\3\377", "1234", "12'34");
149 check (__LINE__, "\2\3\377", "12345", "123'45");
150 check (__LINE__, "\2\3\377", "123456", "1'234'56");
151 check (__LINE__, "\2\3\377", "1234567", "12'345'67");
152 check (__LINE__, "\2\3\377", "12345678", "123'456'78");
153 check (__LINE__, "\2\3\377", "123456789", "1234'567'89");
154 check (__LINE__, "\2\3\377", "1234567890", "12345'678'90");
156 check (__LINE__, "\3\2\377", "1", "1");
157 check (__LINE__, "\3\2\377", "12", "12");
158 check (__LINE__, "\3\2\377", "123", "123");
159 check (__LINE__, "\3\2\377", "1234", "1'234");
160 check (__LINE__, "\3\2\377", "12345", "12'345");
161 check (__LINE__, "\3\2\377", "123456", "1'23'456");
162 check (__LINE__, "\3\2\377", "1234567", "12'34'567");
163 check (__LINE__, "\3\2\377", "12345678", "123'45'678");
164 check (__LINE__, "\3\2\377", "123456789", "1234'56'789");
165 check (__LINE__, "\3\2\377", "1234567890", "12345'67'890");
167 /* Locale-based tests. */
169 locale_t loc;
170 struct lc_ctype_data *ctype;
171 struct grouping_iterator it;
173 loc = newlocale (LC_ALL_MASK, "de_DE.UTF-8", 0);
174 TEST_VERIFY_EXIT (loc != 0);
175 ctype = loc->__locales[LC_CTYPE]->private;
176 TEST_VERIFY (!ctype->outdigit_translation_needed);
177 for (int i = 0; i <= 9; ++i)
178 TEST_COMPARE (ctype->outdigit_bytes[i], 1);
179 TEST_COMPARE (ctype->outdigit_bytes_all_equal, 1);
180 TEST_COMPARE (__grouping_iterator_init (&it, LC_NUMERIC, loc, 8), true);
181 TEST_COMPARE (it.remaining_in_current_group, 2);
182 TEST_COMPARE (it.remaining, 8);
183 TEST_COMPARE (*it.groupings, 3);
184 TEST_COMPARE (it.non_repeating_groups, 3); /* Locale duplicates 3. */
185 TEST_COMPARE (it.separators, 2);
186 TEST_COMPARE (__grouping_iterator_init (&it, LC_MONETARY, loc, 8), true);
187 TEST_COMPARE (it.remaining_in_current_group, 2);
188 TEST_COMPARE (it.remaining, 8);
189 TEST_COMPARE (*it.groupings, 3);
190 TEST_COMPARE (it.non_repeating_groups, 3); /* Locale duplicates 3. */
191 TEST_COMPARE (it.separators, 2);
192 freelocale (loc);
194 loc = newlocale (LC_ALL_MASK, "tg_TJ.UTF-8", 0);
195 TEST_VERIFY_EXIT (loc != 0);
196 ctype = loc->__locales[LC_CTYPE]->private;
197 TEST_VERIFY (!ctype->outdigit_translation_needed);
198 for (int i = 0; i <= 9; ++i)
199 TEST_COMPARE (ctype->outdigit_bytes[i], 1);
200 TEST_COMPARE (ctype->outdigit_bytes_all_equal, 1);
201 TEST_COMPARE (__grouping_iterator_init (&it, LC_NUMERIC, loc, 8), true);
202 TEST_COMPARE (it.remaining_in_current_group, 2);
203 TEST_COMPARE (it.remaining, 8);
204 TEST_COMPARE (*it.groupings, 3);
205 TEST_COMPARE (it.non_repeating_groups, 3); /* Locale duplicates 3. */
206 TEST_COMPARE (it.separators, 2);
207 TEST_COMPARE (__grouping_iterator_init (&it, LC_MONETARY, loc, 8), true);
208 TEST_COMPARE (it.remaining_in_current_group, 2);
209 TEST_COMPARE (it.remaining, 8);
210 TEST_COMPARE (*it.groupings, 3);
211 TEST_COMPARE (it.non_repeating_groups, 3); /* Locale duplicates 3. */
212 TEST_COMPARE (it.separators, 2);
213 freelocale (loc);
215 loc = newlocale (LC_ALL_MASK, "hi_IN.UTF-8", 0);
216 TEST_VERIFY_EXIT (loc != 0);
217 ctype = loc->__locales[LC_CTYPE]->private;
218 TEST_VERIFY (ctype->outdigit_translation_needed);
219 for (int i = 0; i <= 9; ++i)
220 /* Locale uses Devanagari digits. */
221 TEST_COMPARE (ctype->outdigit_bytes[i], 3);
222 TEST_COMPARE (ctype->outdigit_bytes_all_equal, 3);
223 TEST_COMPARE (__grouping_iterator_init (&it, LC_NUMERIC, loc, 8), true);
224 TEST_COMPARE (it.remaining_in_current_group, 2);
225 TEST_COMPARE (it.remaining, 8);
226 TEST_COMPARE (*it.groupings, 3);
227 TEST_COMPARE (it.non_repeating_groups, 0);
228 TEST_COMPARE (it.separators, 2);
229 TEST_COMPARE (__grouping_iterator_init (&it, LC_MONETARY, loc, 8), true);
230 TEST_COMPARE (it.remaining_in_current_group, 1);
231 TEST_COMPARE (it.remaining, 8);
232 TEST_COMPARE (*it.groupings, 2);
233 TEST_COMPARE (it.non_repeating_groups, 3);
234 TEST_COMPARE (it.separators, 3);
235 freelocale (loc);
237 loc = newlocale (LC_ALL_MASK, "ps_AF.UTF-8", 0);
238 TEST_VERIFY_EXIT (loc != 0);
239 ctype = loc->__locales[LC_CTYPE]->private;
240 TEST_VERIFY (ctype->outdigit_translation_needed);
241 for (int i = 0; i <= 9; ++i)
242 /* Locale uses non-ASCII digits. */
243 TEST_COMPARE (ctype->outdigit_bytes[i], 2);
244 TEST_COMPARE (ctype->outdigit_bytes_all_equal, 2);
245 TEST_COMPARE (__grouping_iterator_init (&it, LC_NUMERIC, loc, 8), true);
246 TEST_COMPARE (it.remaining_in_current_group, 2);
247 TEST_COMPARE (it.remaining, 8);
248 TEST_COMPARE (*it.groupings, 3);
249 TEST_COMPARE (it.non_repeating_groups, 0);
250 TEST_COMPARE (it.separators, 2);
251 TEST_COMPARE (__grouping_iterator_init (&it, LC_MONETARY, loc, 8), true);
252 TEST_COMPARE (it.remaining_in_current_group, 2);
253 TEST_COMPARE (it.remaining, 8);
254 TEST_COMPARE (*it.groupings, 3);
255 TEST_COMPARE (it.non_repeating_groups, 0);
256 TEST_COMPARE (it.separators, 2);
257 freelocale (loc);
259 return 0;
262 #include <support/test-driver.c>