]>
Commit | Line | Data |
---|---|---|
32f600a2 TS |
1 | /* Verify the behavior of strftime on alternative representation for |
2 | year. | |
3 | ||
581c785b | 4 | Copyright (C) 2019-2022 Free Software Foundation, Inc. |
32f600a2 TS |
5 | This file is part of the GNU C Library. |
6 | ||
7 | The GNU C Library is free software; you can redistribute it and/or | |
8 | modify it under the terms of the GNU Lesser General Public | |
9 | License as published by the Free Software Foundation; either | |
10 | version 2.1 of the License, or (at your option) any later version. | |
11 | ||
12 | The GNU C Library is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public | |
18 | License along with the GNU C Library; if not, see | |
5a82c748 | 19 | <https://www.gnu.org/licenses/>. */ |
32f600a2 TS |
20 | |
21 | #include <array_length.h> | |
2f1d6155 TS |
22 | #include <stdbool.h> |
23 | #include <support/check.h> | |
24 | #include <stdlib.h> | |
32f600a2 TS |
25 | #include <locale.h> |
26 | #include <time.h> | |
27 | #include <stdio.h> | |
28 | #include <string.h> | |
29 | ||
2f1d6155 TS |
30 | static const char *locales[] = |
31 | { | |
84aea169 TS |
32 | "ja_JP.UTF-8", "lo_LA.UTF-8", "th_TH.UTF-8", |
33 | "zh_TW.UTF-8", "cmn_TW.UTF-8", "hak_TW.UTF-8", | |
34 | "nan_TW.UTF-8", "lzh_TW.UTF-8" | |
2f1d6155 TS |
35 | }; |
36 | ||
37 | /* Must match locale index into locales array. */ | |
38 | enum | |
39 | { | |
84aea169 TS |
40 | ja_JP, lo_LA, th_TH, |
41 | zh_TW, cmn_TW, hak_TW, nan_TW, lzh_TW | |
2f1d6155 | 42 | }; |
32f600a2 TS |
43 | |
44 | static const char *formats[] = { "%EY", "%_EY", "%-EY" }; | |
45 | ||
2f1d6155 | 46 | typedef struct |
32f600a2 TS |
47 | { |
48 | const int d, m, y; | |
2f1d6155 TS |
49 | } date_t; |
50 | ||
51 | static const date_t dates[] = | |
52 | { | |
84aea169 TS |
53 | { 1, 4, 1910 }, |
54 | { 31, 12, 1911 }, | |
55 | { 1, 1, 1912 }, | |
56 | { 1, 4, 1913 }, | |
2f1d6155 TS |
57 | { 1, 4, 1988 }, |
58 | { 7, 1, 1989 }, | |
59 | { 8, 1, 1989 }, | |
60 | { 1, 4, 1990 }, | |
61 | { 1, 4, 1997 }, | |
84aea169 TS |
62 | { 1, 4, 1998 }, |
63 | { 1, 4, 2010 }, | |
466afec3 TS |
64 | { 1, 4, 2011 }, |
65 | { 30, 4, 2019 }, | |
66 | { 1, 5, 2019 } | |
2f1d6155 | 67 | }; |
32f600a2 | 68 | |
144a794e TS |
69 | static char ref[array_length (locales)][array_length (formats)] |
70 | [array_length (dates)][100]; | |
32f600a2 | 71 | |
2f1d6155 TS |
72 | static bool |
73 | is_before (const int i, const int d, const int m, const int y) | |
74 | { | |
75 | if (dates[i].y < y) | |
76 | return true; | |
77 | else if (dates[i].y > y) | |
78 | return false; | |
79 | else if (dates[i].m < m) | |
80 | return true; | |
81 | else if (dates[i].m > m) | |
82 | return false; | |
83 | else | |
84 | return dates[i].d < d; | |
85 | } | |
86 | ||
32f600a2 TS |
87 | static void |
88 | mkreftable (void) | |
89 | { | |
2f1d6155 TS |
90 | int i, j, k, yr; |
91 | const char *era, *sfx; | |
92 | /* Japanese era year to be checked. */ | |
93 | static const int yrj[] = | |
94 | { | |
84aea169 | 95 | 43, 44, 45, 2, |
466afec3 | 96 | 63, 64, 1, 2, 9, 10, 22, 23, 31, 1 |
2f1d6155 TS |
97 | }; |
98 | /* Buddhist calendar year to be checked. */ | |
99 | static const int yrb[] = | |
100 | { | |
84aea169 | 101 | 2453, 2454, 2455, 2456, |
466afec3 | 102 | 2531, 2532, 2532, 2533, 2540, 2541, 2553, 2554, 2562, 2562 |
84aea169 TS |
103 | }; |
104 | /* R.O.C. calendar year to be checked. Negative number is prior to | |
105 | Minguo counting up. */ | |
106 | static const int yrc[] = | |
107 | { | |
108 | -2, -1, 1, 2, | |
466afec3 | 109 | 77, 78, 78, 79, 86, 87, 99, 100, 108, 108 |
2f1d6155 | 110 | }; |
32f600a2 TS |
111 | |
112 | for (i = 0; i < array_length (locales); i++) | |
113 | for (j = 0; j < array_length (formats); j++) | |
114 | for (k = 0; k < array_length (dates); k++) | |
115 | { | |
2f1d6155 | 116 | if (i == ja_JP) |
32f600a2 | 117 | { |
84aea169 TS |
118 | era = (is_before (k, 30, 7, 1912)) ? "\u660e\u6cbb" |
119 | : (is_before (k, 25, 12, 1926)) ? "\u5927\u6b63" | |
120 | : (is_before (k, 8, 1, 1989)) ? "\u662d\u548c" | |
466afec3 TS |
121 | : (is_before (k, 1, 5, 2019)) ? "\u5e73\u6210" |
122 | : "\u4ee4\u548c"; | |
2f1d6155 | 123 | yr = yrj[k], sfx = "\u5e74"; |
32f600a2 | 124 | } |
2f1d6155 TS |
125 | else if (i == lo_LA) |
126 | era = "\u0e9e.\u0eaa. ", yr = yrb[k], sfx = ""; | |
127 | else if (i == th_TH) | |
128 | era = "\u0e1e.\u0e28. ", yr = yrb[k], sfx = ""; | |
84aea169 TS |
129 | else if (i == zh_TW || i == cmn_TW || i == hak_TW |
130 | || i == nan_TW || i == lzh_TW) | |
131 | { | |
132 | era = (is_before (k, 1, 1, 1912)) ? "\u6c11\u524d" | |
133 | : "\u6c11\u570b"; | |
134 | yr = yrc[k], sfx = "\u5e74"; | |
135 | } | |
32f600a2 | 136 | else |
2f1d6155 TS |
137 | FAIL_EXIT1 ("Invalid table index!"); |
138 | if (yr == 1) | |
139 | sprintf (ref[i][j][k], "%s\u5143%s", era, sfx); | |
140 | else if (j == 0) | |
141 | sprintf (ref[i][j][k], "%s%02d%s", era, abs (yr), sfx); | |
142 | else if (j == 1) | |
143 | sprintf (ref[i][j][k], "%s%2d%s", era, abs (yr), sfx); | |
144 | else if (j == 2) | |
145 | sprintf (ref[i][j][k], "%s%d%s", era, abs (yr), sfx); | |
146 | else | |
147 | FAIL_EXIT1 ("Invalid table index!"); | |
32f600a2 TS |
148 | } |
149 | } | |
150 | ||
151 | static int | |
152 | do_test (void) | |
153 | { | |
154 | int i, j, k, result = 0; | |
155 | struct tm ttm; | |
156 | char date[11], buf[100]; | |
157 | size_t r, e; | |
158 | ||
159 | mkreftable (); | |
160 | for (i = 0; i < array_length (locales); i++) | |
161 | { | |
162 | if (setlocale (LC_ALL, locales[i]) == NULL) | |
163 | { | |
164 | printf ("locale %s does not exist, skipping...\n", locales[i]); | |
165 | continue; | |
166 | } | |
167 | printf ("[%s]\n", locales[i]); | |
168 | for (j = 0; j < array_length (formats); j++) | |
169 | { | |
170 | for (k = 0; k < array_length (dates); k++) | |
171 | { | |
172 | ttm.tm_mday = dates[k].d; | |
2f1d6155 TS |
173 | ttm.tm_mon = dates[k].m - 1; |
174 | ttm.tm_year = dates[k].y - 1900; | |
32f600a2 TS |
175 | strftime (date, sizeof (date), "%F", &ttm); |
176 | r = strftime (buf, sizeof (buf), formats[j], &ttm); | |
177 | e = strlen (ref[i][j][k]); | |
178 | printf ("%s\t\"%s\"\t\"%s\"", date, formats[j], buf); | |
179 | if (strcmp (buf, ref[i][j][k]) != 0) | |
180 | { | |
181 | printf ("\tshould be \"%s\"", ref[i][j][k]); | |
182 | if (r != e) | |
183 | printf ("\tgot: %zu, expected: %zu", r, e); | |
184 | result = 1; | |
185 | } | |
186 | else | |
187 | printf ("\tOK"); | |
188 | putchar ('\n'); | |
189 | } | |
190 | putchar ('\n'); | |
191 | } | |
192 | } | |
193 | return result; | |
194 | } | |
195 | ||
196 | #include <support/test-driver.c> |