signal cleanup fix
[gnupg.git] / common / t-gettime.c
bloba4401d8ae3001e54263449f9c94ae444835bbee4
1 /* t-gettime.c - Module test for gettime.c
2 * Copyright (C) 2007 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
24 #include "util.h"
26 #define pass() do { ; } while(0)
27 #define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
28 __FILE__,__LINE__, (a)); \
29 errcount++; \
30 } while(0)
32 static int verbose;
33 static int errcount;
34 #define INVALID ((time_t)(-1))
37 static void
38 test_isotime2epoch (void)
40 struct { const char *string; time_t expected; } array [] = {
41 { "19700101T000001", 1 },
42 { "19700101T235959", 86399 },
43 { "19980815T143712", 903191832 },
44 { "19700101T000000", 0 },
45 { "19691231T235959", INVALID },
46 { "19000101T000000", INVALID },
47 { "", INVALID },
48 { "19000101T00000", INVALID },
49 { "20010101t123456", INVALID },
50 { "20010101T123456", 978352496 },
51 { "20070629T160000", 1183132800 },
52 { "20070629T160000:", 1183132800 },
53 { "20070629T160000,", 1183132800 },
54 { "20070629T160000 ", 1183132800 },
55 { "20070629T160000\n", 1183132800 },
56 { "20070629T160000.", INVALID },
57 { NULL, 0 }
59 int idx;
60 time_t val;
61 gnupg_isotime_t tbuf;
63 for (idx=0; array[idx].string; idx++)
65 val = isotime2epoch (array[idx].string);
66 if (val != array[idx].expected )
68 fail (idx);
69 if (verbose)
70 fprintf (stderr, "string `%s' exp: %ld got: %ld\n",
71 array[idx].string, (long)array[idx].expected,
72 (long)val);
74 if (array[idx].expected != INVALID)
76 epoch2isotime (tbuf, val);
77 if (strlen (tbuf) != 15)
79 if (verbose)
80 fprintf (stderr, "string `%s', time-t %ld, revert: `%s'\n",
81 array[idx].string, (long)val, tbuf);
82 fail (idx);
84 if (strncmp (array[idx].string, tbuf, 15))
85 fail (idx);
93 int
94 main (int argc, char **argv)
96 if (argc > 1 && !strcmp (argv[1], "--verbose"))
97 verbose = 1;
99 test_isotime2epoch ();
101 return !!errcount;