Add self-test for getdate module.
[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) 0
47 #endif
48
49 int
50 main (int argc, char **argv)
51 {
52   bool ret;
53   struct timespec result;
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   return 0;
89 }