]> sourceware.org Git - glibc.git/blame - time/tst-getdate.c
tst-getdate: Improve testcase flexibility and add test.
[glibc.git] / time / tst-getdate.c
CommitLineData
fc304e02 1/* Test for getdate.
6d7e8eda 2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
fc304e02 3 This file is part of the GNU C Library.
fc304e02
UD
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
fc304e02
UD
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
41bdb6e2 13 Lesser General Public License for more details.
fc304e02 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
fc304e02 18
ef8239f1
AZ
19#include <array_length.h>
20#include <stdbool.h>
fc304e02
UD
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
ef8239f1
AZ
24#include <support/check.h>
25#include <support/temp_file.h>
26#include <support/xunistd.h>
fc304e02
UD
27#include <time.h>
28
29static const struct
30{
31 const char *str;
9d07cf73 32 const char *tz;
fc304e02 33 struct tm tm;
ef8239f1 34 bool time64;
31df9fa8
JST
35 int err_val;
36 bool check_tm;
fc304e02
UD
37} tests [] =
38{
ef8239f1 39 {"21:01:10 1999-1-31", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
31df9fa8 40 false , 0, true},
ef8239f1 41 {"21:01:10 1999-1-31", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
31df9fa8 42 false , 0, true},
ef8239f1 43 {" 21:01:10 1999-1-31", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
31df9fa8 44 false , 0, true},
ef8239f1 45 {"21:01:10 1999-1-31 ", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
31df9fa8 46 false , 0, true},
ef8239f1 47 {" 21:01:10 1999-1-31 ", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
31df9fa8 48 false , 0, true},
ef8239f1 49 {"21:01:10 1999-2-28", "Universal", {10, 1, 21, 28, 1, 99, 0, 0, 0},
31df9fa8 50 false , 0, true},
ef8239f1 51 {"16:30:46 2000-2-29", "Universal", {46, 30,16, 29, 1, 100, 0, 0, 0},
31df9fa8 52 false , 0, true},
ef8239f1 53 {"01-08-2000 05:06:07", "Europe/Berlin", {7, 6, 5, 1, 7, 100, 0, 0, 0},
31df9fa8
JST
54 false , 0, true},
55 {"01-08-2000 05:06:07", "Europe/Berlin", {7, 6, 5, 1, 7, 100, 0, 0, 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},
ef8239f1
AZ
61
62 /* 64 bit time_t tests. */
63 {"21:01:10 2038-1-31", "Universal", {10, 1, 21, 31, 0, 138, 0, 0, 0},
31df9fa8 64 true , 0, true},
ef8239f1 65 {"22:01:10 2048-5-20", "Universal", {10, 1, 22, 20, 4, 148, 0, 0, 0},
31df9fa8 66 true , 0, true},
ef8239f1 67 {"01-08-2038 05:06:07", "Europe/Berlin", {7, 6, 5, 1, 7, 138, 0, 0, 0},
31df9fa8 68 true , 0, true},
ef8239f1 69 {"20-03-2050 21:30:08", "Europe/Berlin", {8, 30, 21, 20, 2, 150, 0, 0, 0},
31df9fa8 70 true , 0, true}
fc304e02
UD
71};
72
ef8239f1
AZ
73static const char *
74report_date_error (void)
fc304e02 75{
ef8239f1 76 switch (getdate_err)
fc304e02
UD
77 {
78 case 1:
ef8239f1 79 return "The environment variable DATEMSK is not defined or null.";
fc304e02 80 case 2:
ef8239f1
AZ
81 return "The template file denoted by the DATEMSK environment variable "
82 "cannot be opened.";
fc304e02 83 case 3:
ef8239f1 84 return "Information about the template file cannot retrieved.";
fc304e02 85 case 4:
ef8239f1 86 return "The template file is not a regular file.\n";
fc304e02 87 case 5:
ef8239f1 88 return "An I/O error occurred while reading the template file.";
fc304e02 89 case 6:
ef8239f1 90 return "Not enough memory available to execute the function.";
fc304e02 91 case 7:
ef8239f1 92 return "The template file contains no matching template.";
fc304e02 93 case 8:
ef8239f1
AZ
94 return "The input date is invalid, but would match a template "
95 "otherwise.";
fc304e02 96 default:
ef8239f1 97 return "Unknown error code.";
fc304e02
UD
98 }
99}
100
ef8239f1
AZ
101static char *datemsk;
102static const char datemskstr[] =
103 "%H:%M:%S %F\n"
31df9fa8
JST
104 "%d-%m-%Y %T\n"
105 "%I %p\n";
ef8239f1
AZ
106
107static void
108do_prepare (int argc, char **argv)
109{
5e8d1b03 110 int fd = create_temp_file ("tst-getdate.", &datemsk);
ef8239f1
AZ
111 xwrite (fd, datemskstr, sizeof (datemskstr) - 1);
112
113 setenv ("DATEMSK", datemsk, 1);
114}
115#define PREPARE do_prepare
fc304e02 116
29955b5d
AS
117static int
118do_test (void)
fc304e02 119{
fc304e02 120 struct tm *tm;
768b0732 121
ef8239f1 122 for (int i = 0; i < array_length (tests); ++i)
fc304e02 123 {
9d07cf73 124 setenv ("TZ", tests[i].tz, 1);
57b36a0a 125
ef8239f1 126 tm = getdate (tests[i].str);
31df9fa8
JST
127
128 /* Only check getdate_err when tm is NULL as getdate doesn't set
129 getdate_err on success. */
130 if (tm == NULL)
131 {
132 TEST_COMPARE (getdate_err, tests[i].err_val);
133 if (getdate_err != tests[i].err_val)
134 printf ("%s\n", report_date_error ());
135 }
136 if (tests[i].err_val != 0) /* Expected failure */
fc304e02 137 {
31df9fa8
JST
138 TEST_VERIFY (tm == NULL);
139 continue;
fc304e02 140 }
31df9fa8
JST
141
142 if (tests[i].check_tm)
ef8239f1
AZ
143 {
144 TEST_COMPARE (tests[i].tm.tm_mon, tm->tm_mon);
145 TEST_COMPARE (tests[i].tm.tm_year, tm->tm_year);
146 TEST_COMPARE (tests[i].tm.tm_mday, tm->tm_mday);
147 TEST_COMPARE (tests[i].tm.tm_hour, tm->tm_hour);
148 TEST_COMPARE (tests[i].tm.tm_min, tm->tm_min);
149 TEST_COMPARE (tests[i].tm.tm_sec, tm->tm_sec);
150 }
151
152 struct tm tms;
31df9fa8
JST
153 int retval = getdate_r (tests[i].str, &tms);
154 TEST_COMPARE (retval, tests[i].err_val);
155 if (retval == tests[i].err_val && tests[i].check_tm)
fc304e02 156 {
ef8239f1
AZ
157 TEST_COMPARE (tests[i].tm.tm_mon, tms.tm_mon);
158 TEST_COMPARE (tests[i].tm.tm_year, tms.tm_year);
159 TEST_COMPARE (tests[i].tm.tm_mday, tms.tm_mday);
160 TEST_COMPARE (tests[i].tm.tm_hour, tms.tm_hour);
161 TEST_COMPARE (tests[i].tm.tm_min, tms.tm_min);
162 TEST_COMPARE (tests[i].tm.tm_sec, tms.tm_sec);
fc304e02 163 }
fc304e02 164 }
768b0732 165
ef8239f1 166 return 0;
fc304e02 167}
29955b5d 168
ef8239f1 169#include <support/test-driver.c>
This page took 0.65044 seconds and 5 git commands to generate.