[v3,0/7] Fix some libm static issues
[platform/upstream/glibc.git] / time / tst-getdate.c
1 /* Test for getdate.
2    Copyright (C) 2000-2024 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
9
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.
14
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/>.  */
18
19 #include <array_length.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <support/check.h>
25 #include <support/temp_file.h>
26 #include <support/xunistd.h>
27 #include <time.h>
28
29 static const struct
30 {
31   const char *str;
32   const char *tz;
33   struct tm tm;
34   bool time64;
35   int err_val;
36   bool check_tm;
37 } tests [] =
38 {
39   {"21:01:10 1999-1-31", "Universal", {10, 1, 21, 31, 0, 99, 0, 30, 0},
40    false , 0, true},
41   {"21:01:10    1999-1-31", "Universal", {10, 1, 21, 31, 0, 99, 0, 30, 0},
42    false , 0, true},
43   {"   21:01:10 1999-1-31", "Universal", {10, 1, 21, 31, 0, 99, 0, 30, 0},
44    false , 0, true},
45   {"21:01:10 1999-1-31   ", "Universal", {10, 1, 21, 31, 0, 99, 0, 30, 0},
46    false , 0, true},
47   {"    21:01:10 1999-1-31   ", "Universal", {10, 1, 21, 31, 0, 99, 0, 30, 0},
48    false , 0, true},
49   {"21:01:10 1999-2-28", "Universal", {10, 1, 21, 28, 1, 99, 0, 58, 0},
50    false , 0, true},
51   {"16:30:46 2000-2-29", "Universal", {46, 30,16, 29, 1, 100, 2, 59, 0},
52    false , 0, true},
53   {"01-08-2000 05:06:07", "Europe/Berlin", {7, 6, 5, 1, 7, 100, 2, 213, 0},
54    false , 0, true},
55   {"01-08-2000     05:06:07", "Europe/Berlin", {7, 6, 5, 1, 7, 100, 2, 213, 0},
56    false , 0, true},
57   {"01-08-2000 a 05:06:07", "Europe/Berlin", {0, 0, 0, 0, 0, 0, 0, 0, 0},
58    false , 7, false},
59   {"       12          AM     ", "Europe/Berlin", {0, 0, 0, 0, 0, 0, 0, 0, 0},
60    false , 0, false},
61   {"01-01-1900 2 PM", "Universal", {0, 0, 14, 1, 0, 0, 1, 0, 0},
62    false, 0, true},
63   {"12-12-1850 16h", "Universal", {0, 0, 16, 12, 11, -50, 4, 345, 0},
64    false, 0, true},
65   {"12/31/93 21:35", "Universal", {0, 35, 21, 31, 11, 93, 5, 364, 0},
66    false, 0, true},
67
68   /* 64 bit time_t tests.  */
69   {"21:01:10 2038-1-31", "Universal", {10, 1, 21, 31, 0, 138, 0, 30, 0},
70    true , 0, true},
71   {"22:01:10 2048-5-20", "Universal", {10, 1, 22, 20, 4, 148, 3, 140, 0},
72    true , 0, true},
73   {"01-08-2038 05:06:07", "Europe/Berlin", {7, 6, 5, 1, 7, 138, 0, 212, 0},
74    true , 0, true},
75   {"20-03-2050 21:30:08", "Europe/Berlin", {8, 30, 21, 20, 2, 150, 0, 78, 0},
76    true , 0, true}
77 };
78
79 static const char *
80 report_date_error (void)
81 {
82   switch (getdate_err)
83     {
84     case 1:
85       return "The environment variable DATEMSK is not defined or null.";
86     case 2:
87       return "The template file denoted by the DATEMSK environment variable "
88              "cannot be opened.";
89     case 3:
90       return "Information about the template file cannot retrieved.";
91     case 4:
92       return "The template file is not a regular file.\n";
93     case 5:
94       return "An I/O error occurred while reading the template file.";
95     case 6:
96       return "Not enough memory available to execute the function.";
97     case 7:
98       return "The template file contains no matching template.";
99     case 8:
100       return "The input date is invalid, but would match a template "
101               "otherwise.";
102     default:
103       return "Unknown error code.";
104     }
105 }
106
107 static char *datemsk;
108 static const char datemskstr[] =
109   "%H:%M:%S %F\n"
110   "%d-%m-%Y %T\n"
111   "%I %p\n"
112   "%d-%m-%Y %I %p\n"
113   "%d-%m-%Y %H%nh\n"
114   "%D %R\n";
115
116 static void
117 do_prepare (int argc, char **argv)
118 {
119   int fd = create_temp_file ("tst-getdate.", &datemsk);
120   xwrite (fd, datemskstr, sizeof (datemskstr) - 1);
121
122   setenv ("DATEMSK", datemsk, 1);
123 }
124 #define PREPARE do_prepare
125
126 static int
127 do_test (void)
128 {
129   struct tm *tm;
130
131   for (int i = 0; i < array_length (tests); ++i)
132     {
133       setenv ("TZ", tests[i].tz, 1);
134
135       tm = getdate (tests[i].str);
136
137       /* Only check getdate_err when tm is NULL as getdate doesn't set
138          getdate_err on success. */
139       if (tm == NULL)
140         {
141           TEST_COMPARE (getdate_err, tests[i].err_val);
142           if (getdate_err != tests[i].err_val)
143             printf ("%s\n", report_date_error ());
144         }
145       if (tests[i].err_val != 0)  /* Expected failure */
146         {
147           TEST_VERIFY (tm == NULL);
148           continue;
149         }
150
151       if (tests[i].check_tm)
152         {
153           TEST_COMPARE (tests[i].tm.tm_mon, tm->tm_mon);
154           TEST_COMPARE (tests[i].tm.tm_year, tm->tm_year);
155           TEST_COMPARE (tests[i].tm.tm_mday, tm->tm_mday);
156           TEST_COMPARE (tests[i].tm.tm_hour, tm->tm_hour);
157           TEST_COMPARE (tests[i].tm.tm_min, tm->tm_min);
158           TEST_COMPARE (tests[i].tm.tm_sec, tm->tm_sec);
159           TEST_COMPARE (tests[i].tm.tm_wday, tm->tm_wday);
160           TEST_COMPARE (tests[i].tm.tm_yday, tm->tm_yday);
161         }
162
163       struct tm tms;
164       int retval = getdate_r (tests[i].str, &tms);
165       TEST_COMPARE (retval, tests[i].err_val);
166       if (retval == tests[i].err_val && tests[i].check_tm)
167         {
168           TEST_COMPARE (tests[i].tm.tm_mon, tms.tm_mon);
169           TEST_COMPARE (tests[i].tm.tm_year, tms.tm_year);
170           TEST_COMPARE (tests[i].tm.tm_mday, tms.tm_mday);
171           TEST_COMPARE (tests[i].tm.tm_hour, tms.tm_hour);
172           TEST_COMPARE (tests[i].tm.tm_min, tms.tm_min);
173           TEST_COMPARE (tests[i].tm.tm_sec, tms.tm_sec);
174           TEST_COMPARE (tests[i].tm.tm_wday, tms.tm_wday);
175           TEST_COMPARE (tests[i].tm.tm_yday, tms.tm_yday);
176         }
177     }
178
179   return 0;
180 }
181
182 #include <support/test-driver.c>