New module 'unicase/u8-casexfrm'.
[gnulib.git] / tests / test-getdate.c
1 /* Test of getdate() function.
2    Copyright (C) 2008, 2009 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 "progname.h"
27
28 #include "getdate.h"
29
30 #define ASSERT(expr)                                                    \
31   do                                                                    \
32     {                                                                   \
33       if (!(expr))                                                      \
34         {                                                               \
35           fprintf (stderr, "%s:%d: assertion failed\n",                 \
36                    __FILE__, __LINE__);                                 \
37           fflush (stderr);                                              \
38           abort ();                                                     \
39         }                                                               \
40     }                                                                   \
41   while (0)
42
43 #ifdef DEBUG
44 #define LOG(str, now, res)                                              \
45   printf ("string `%s' diff %d %d\n",                   \
46           str, res.tv_sec - now.tv_sec, res.tv_nsec - now.tv_nsec);
47 #else
48 #define LOG(str, now, res) (void) 0
49 #endif
50
51 int
52 main (int argc, char **argv)
53 {
54   struct timespec result;
55   struct timespec result2;
56   struct timespec now;
57   const char *p;
58
59   set_program_name (argv[0]);
60
61   now.tv_sec = 4711;
62   now.tv_nsec = 1267;
63   p = "now";
64   ASSERT (get_date (&result, p, &now));
65   LOG (p, now, result);
66   ASSERT (now.tv_sec == result.tv_sec && now.tv_nsec == result.tv_nsec);
67
68   now.tv_sec = 4711;
69   now.tv_nsec = 1267;
70   p = "tomorrow";
71   ASSERT (get_date (&result, p, &now));
72   LOG (p, now, result);
73   ASSERT (now.tv_sec + 24 * 60 * 60 == result.tv_sec
74           && now.tv_nsec == result.tv_nsec);
75
76   now.tv_sec = 4711;
77   now.tv_nsec = 1267;
78   p = "yesterday";
79   ASSERT (get_date (&result, p, &now));
80   LOG (p, now, result);
81   ASSERT (now.tv_sec - 24 * 60 * 60 == result.tv_sec
82           && now.tv_nsec == result.tv_nsec);
83
84   now.tv_sec = 4711;
85   now.tv_nsec = 1267;
86   p = "4 hours";
87   ASSERT (get_date (&result, p, &now));
88   LOG (p, now, result);
89   ASSERT (now.tv_sec + 4 * 60 * 60 == result.tv_sec
90           && now.tv_nsec == result.tv_nsec);
91
92   /* test if timezone is not being ignored for day offset */
93   now.tv_sec = 4711;
94   now.tv_nsec = 1267;
95   p = "UTC+400 +24 hours";
96   ASSERT (get_date (&result, p, &now));
97   LOG (p, now, result);
98   p = "UTC+400 +1 day";
99   ASSERT (get_date (&result2, p, &now));
100   LOG (p, now, result2);
101   ASSERT (result.tv_sec == result2.tv_sec
102           && result.tv_nsec == result2.tv_nsec);
103
104   /* test if several time zones formats are handled same way */
105   now.tv_sec = 4711;
106   now.tv_nsec = 1267;
107   p = "UTC+14:00";
108   ASSERT (get_date (&result, p, &now));
109   LOG (p, now, result);
110   p = "UTC+14";
111   ASSERT (get_date (&result2, p, &now));
112   LOG (p, now, result2);
113   ASSERT (result.tv_sec == result2.tv_sec
114           && result.tv_nsec == result2.tv_nsec);
115   p = "UTC+1400";
116   ASSERT (get_date (&result2, p, &now));
117   LOG (p, now, result2);
118   ASSERT (result.tv_sec == result2.tv_sec
119           && result.tv_nsec == result2.tv_nsec);
120
121   now.tv_sec = 4711;
122   now.tv_nsec = 1267;
123   p = "UTC-14:00";
124   ASSERT (get_date (&result, p, &now));
125   LOG (p, now, result);
126   p = "UTC-14";
127   ASSERT (get_date (&result2, p, &now));
128   LOG (p, now, result2);
129   ASSERT (result.tv_sec == result2.tv_sec
130           && result.tv_nsec == result2.tv_nsec);
131   p = "UTC-1400";
132   ASSERT (get_date (&result2, p, &now));
133   LOG (p, now, result2);
134   ASSERT (result.tv_sec == result2.tv_sec
135           && result.tv_nsec == result2.tv_nsec);
136
137   now.tv_sec = 4711;
138   now.tv_nsec = 1267;
139   p = "UTC+0:15";
140   ASSERT (get_date (&result, p, &now));
141   LOG (p, now, result);
142   p = "UTC+0015";
143   ASSERT (get_date (&result2, p, &now));
144   LOG (p, now, result2);
145   ASSERT (result.tv_sec == result2.tv_sec
146           && result.tv_nsec == result2.tv_nsec);
147
148   now.tv_sec = 4711;
149   now.tv_nsec = 1267;
150   p = "UTC-1:30";
151   ASSERT (get_date (&result, p, &now));
152   LOG (p, now, result);
153   p = "UTC-130";
154   ASSERT (get_date (&result2, p, &now));
155   LOG (p, now, result2);
156   ASSERT (result.tv_sec == result2.tv_sec
157           && result.tv_nsec == result2.tv_nsec);
158
159
160   /* TZ out of range should cause get_date failure */
161   now.tv_sec = 4711;
162   now.tv_nsec = 1267;
163   p = "UTC+25:00";
164   ASSERT (!get_date (&result, p, &now));
165
166         /* Check for several invalid countable dayshifts */
167   now.tv_sec = 4711;
168   now.tv_nsec = 1267;
169   p = "UTC+4:00 +40 yesterday";
170   ASSERT (!get_date (&result, p, &now));
171   p = "UTC+4:00 next yesterday";
172   ASSERT (!get_date (&result, p, &now));
173   p = "UTC+4:00 tomorrow ago";
174   ASSERT (!get_date (&result, p, &now));
175   p = "UTC+4:00 40 now ago";
176   ASSERT (!get_date (&result, p, &now));
177   p = "UTC+4:00 last tomorrow";
178   ASSERT (!get_date (&result, p, &now));
179   p = "UTC+4:00 -4 today";
180   ASSERT (!get_date (&result, p, &now));
181
182   /* And check correct usage of dayshifts */
183   now.tv_sec = 4711;
184   now.tv_nsec = 1267;
185   p = "UTC+400 tomorrow";
186   ASSERT (get_date (&result, p, &now));
187   LOG (p, now, result);
188   p = "UTC+400 +1 day";
189   ASSERT (get_date (&result2, p, &now));
190   LOG (p, now, result2);
191   ASSERT (result.tv_sec == result2.tv_sec
192           && result.tv_nsec == result2.tv_nsec);
193   now.tv_sec = 4711;
194   now.tv_nsec = 1267;
195   p = "UTC+400 yesterday";
196   ASSERT (get_date (&result, p, &now));
197   LOG (p, now, result);
198   p = "UTC+400 1 day ago";
199   ASSERT (get_date (&result2, p, &now));
200   LOG (p, now, result2);
201   ASSERT (result.tv_sec == result2.tv_sec
202           && result.tv_nsec == result2.tv_nsec);
203   now.tv_sec = 4711;
204   now.tv_nsec = 1267;
205   p = "UTC+400 now";
206   ASSERT (get_date (&result, p, &now));
207   LOG (p, now, result);
208   p = "UTC+400 +0 minutes"; /* silly, but simple "UTC+400" is different*/
209   ASSERT (get_date (&result2, p, &now));
210   LOG (p, now, result2);
211   ASSERT (result.tv_sec == result2.tv_sec
212           && result.tv_nsec == result2.tv_nsec);
213
214   return 0;
215 }