Tests for module 'mbsnrtowcs'.
[gnulib.git] / tests / test-getdate.c
1 /* Test of getdate() function.
2    Copyright (C) 2008 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Simon Josefsson <simon@josefsson.org>, 2008.  */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "getdate.h"
27
28 #define ASSERT(expr)                                                    \
29   do                                                                    \
30     {                                                                   \
31       if (!(expr))                                                      \
32         {                                                               \
33           fprintf (stderr, "%s:%d: assertion failed\n",                 \
34                    __FILE__, __LINE__);                                 \
35           fflush (stderr);                                              \
36           abort ();                                                     \
37         }                                                               \
38     }                                                                   \
39   while (0)
40
41 #ifdef DEBUG
42 #define LOG(str, now, res)                                              \
43   printf ("string `%s' diff %d %d\n",                   \
44           str, res.tv_sec - now.tv_sec, res.tv_nsec - now.tv_nsec);
45 #else
46 #define LOG(str, now, res) (void) 0
47 #endif
48
49 int
50 main (int argc, char **argv)
51 {
52   struct timespec result;
53   struct timespec result2;
54   struct timespec now;
55   const char *p;
56
57   now.tv_sec = 4711;
58   now.tv_nsec = 1267;
59   p = "now";
60   ASSERT (get_date (&result, p, &now));
61   LOG (p, now, result);
62   ASSERT (now.tv_sec == result.tv_sec && now.tv_nsec == result.tv_nsec);
63
64   now.tv_sec = 4711;
65   now.tv_nsec = 1267;
66   p = "tomorrow";
67   ASSERT (get_date (&result, p, &now));
68   LOG (p, now, result);
69   ASSERT (now.tv_sec + 24 * 60 * 60 == result.tv_sec
70           && now.tv_nsec == result.tv_nsec);
71
72   now.tv_sec = 4711;
73   now.tv_nsec = 1267;
74   p = "yesterday";
75   ASSERT (get_date (&result, p, &now));
76   LOG (p, now, result);
77   ASSERT (now.tv_sec - 24 * 60 * 60 == result.tv_sec
78           && now.tv_nsec == result.tv_nsec);
79
80   now.tv_sec = 4711;
81   now.tv_nsec = 1267;
82   p = "4 hours";
83   ASSERT (get_date (&result, p, &now));
84   LOG (p, now, result);
85   ASSERT (now.tv_sec + 4 * 60 * 60 == result.tv_sec
86           && now.tv_nsec == result.tv_nsec);
87
88   /* test if timezone is not being ignored for day offset */
89   now.tv_sec = 4711;
90   now.tv_nsec = 1267;
91   p = "UTC+400 +24 hours";
92   ASSERT (get_date (&result, p, &now));
93   LOG (p, now, result);
94   p = "UTC+400 +1 day";
95   ASSERT (get_date (&result2, p, &now));
96   LOG (p, now, result2);
97   ASSERT (result.tv_sec == result2.tv_sec
98           && result.tv_nsec == result2.tv_nsec);
99
100   /* test if several time zones formats are handled same way */
101   now.tv_sec = 4711;
102   now.tv_nsec = 1267;
103   p = "UTC+14:00";
104   ASSERT (get_date (&result, p, &now));
105   LOG (p, now, result);
106   p = "UTC+14";
107   ASSERT (get_date (&result2, p, &now));
108   LOG (p, now, result2);
109   ASSERT (result.tv_sec == result2.tv_sec
110           && result.tv_nsec == result2.tv_nsec);
111   p = "UTC+1400";
112   ASSERT (get_date (&result2, p, &now));
113   LOG (p, now, result2);
114   ASSERT (result.tv_sec == result2.tv_sec
115           && result.tv_nsec == result2.tv_nsec);
116
117   now.tv_sec = 4711;
118   now.tv_nsec = 1267;
119   p = "UTC-14:00";
120   ASSERT (get_date (&result, p, &now));
121   LOG (p, now, result);
122   p = "UTC-14";
123   ASSERT (get_date (&result2, p, &now));
124   LOG (p, now, result2);
125   ASSERT (result.tv_sec == result2.tv_sec
126           && result.tv_nsec == result2.tv_nsec);
127   p = "UTC-1400";
128   ASSERT (get_date (&result2, p, &now));
129   LOG (p, now, result2);
130   ASSERT (result.tv_sec == result2.tv_sec
131           && result.tv_nsec == result2.tv_nsec);
132
133   now.tv_sec = 4711;
134   now.tv_nsec = 1267;
135   p = "UTC+0:15";
136   ASSERT (get_date (&result, p, &now));
137   LOG (p, now, result);
138   p = "UTC+0015";
139   ASSERT (get_date (&result2, p, &now));
140   LOG (p, now, result2);
141   ASSERT (result.tv_sec == result2.tv_sec
142           && result.tv_nsec == result2.tv_nsec);
143
144   now.tv_sec = 4711;
145   now.tv_nsec = 1267;
146   p = "UTC-1:30";
147   ASSERT (get_date (&result, p, &now));
148   LOG (p, now, result);
149   p = "UTC-130";
150   ASSERT (get_date (&result2, p, &now));
151   LOG (p, now, result2);
152   ASSERT (result.tv_sec == result2.tv_sec
153           && result.tv_nsec == result2.tv_nsec);
154
155
156   /* TZ out of range should cause get_date failure */
157   now.tv_sec = 4711;
158   now.tv_nsec = 1267;
159   p = "UTC+25:00";
160   ASSERT (!get_date (&result, p, &now));
161
162         /* Check for several invalid countable dayshifts */
163   now.tv_sec = 4711;
164   now.tv_nsec = 1267;
165   p = "UTC+4:00 +40 yesterday";
166   ASSERT (!get_date (&result, p, &now));
167   p = "UTC+4:00 next yesterday";
168   ASSERT (!get_date (&result, p, &now));
169   p = "UTC+4:00 tomorrow ago";
170   ASSERT (!get_date (&result, p, &now));
171   p = "UTC+4:00 40 now ago";
172   ASSERT (!get_date (&result, p, &now));
173   p = "UTC+4:00 last tomorrow";
174   ASSERT (!get_date (&result, p, &now));
175   p = "UTC+4:00 -4 today";
176   ASSERT (!get_date (&result, p, &now));
177
178   /* And check correct usage of dayshifts */
179   now.tv_sec = 4711;
180   now.tv_nsec = 1267;
181   p = "UTC+400 tomorrow";
182   ASSERT (get_date (&result, p, &now));
183   LOG (p, now, result);
184   p = "UTC+400 +1 day";
185   ASSERT (get_date (&result2, p, &now));
186   LOG (p, now, result2);
187   ASSERT (result.tv_sec == result2.tv_sec
188           && result.tv_nsec == result2.tv_nsec);
189   now.tv_sec = 4711;
190   now.tv_nsec = 1267;
191   p = "UTC+400 yesterday";
192   ASSERT (get_date (&result, p, &now));
193   LOG (p, now, result);
194   p = "UTC+400 1 day ago";
195   ASSERT (get_date (&result2, p, &now));
196   LOG (p, now, result2);
197   ASSERT (result.tv_sec == result2.tv_sec
198           && result.tv_nsec == result2.tv_nsec);
199   now.tv_sec = 4711;
200   now.tv_nsec = 1267;
201   p = "UTC+400 now";
202   ASSERT (get_date (&result, p, &now));
203   LOG (p, now, result);
204   p = "UTC+400 +0 minutes"; /* silly, but simple "UTC+400" is different*/
205   ASSERT (get_date (&result2, p, &now));
206   LOG (p, now, result2);
207   ASSERT (result.tv_sec == result2.tv_sec
208           && result.tv_nsec == result2.tv_nsec);
209
210   return 0;
211 }