next release will be 1.3.3
[ppastats.git] / tests / test_ptime.c
1 /*
2   Copyright (C) 2010-2011 jeanfi@gmail.com
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 2 of the License, or
7   (at your option) 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
16   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17   02110-1301 USA
18 */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <ptime.h>
25
26 int test_time_to_ISO8601_time(time_t t, const char *ref)
27 {
28         char *result;
29         int failure;
30
31         result = time_to_ISO8601_time(&t);
32
33         failure = !!strcmp(result, ref);
34
35         if (failure)
36                 fprintf(stderr,
37                         "test_time_to_ISO8601_time(%ld)=%s instead of %s.\n",
38                         t,
39                         result,
40                         ref);
41
42         free(result);
43
44         return failure;
45 }
46
47 int test_time_to_ISO8601_date(time_t t, const char *ref)
48 {
49         char *result;
50         int failure;
51
52         result = time_to_ISO8601_date(&t);
53
54         failure = !!strcmp(result, ref);
55
56         if (failure)
57                 fprintf(stderr,
58                         "test_date_to_ISO8601_time(%ld)=%s instead of %s.\n",
59                         t,
60                         result,
61                         ref);
62
63         free(result);
64
65         return failure;
66 }
67
68 static int tests_time_to_ISO8601_time()
69 {
70         int failures;
71
72         failures = 0;
73
74         failures += test_time_to_ISO8601_time(0, "1970-01-01T00:00:00");
75         failures += test_time_to_ISO8601_time(83, "1970-01-01T00:01:23");
76         failures += test_time_to_ISO8601_time(1392542321,
77                                               "2014-02-16T09:18:41");
78
79         return failures;
80 }
81
82 static int tests_time_to_ISO8601_date()
83 {
84         int failures;
85
86         failures = 0;
87
88         failures += test_time_to_ISO8601_date(0, "1970-01-01");
89         failures += test_time_to_ISO8601_date(83, "1970-01-01");
90         failures += test_time_to_ISO8601_date(1392542321, "2014-02-16");
91
92         return failures;
93 }
94
95 int main(int argc, char **argv)
96 {
97         int failures;
98
99         failures = 0;
100
101         failures += tests_time_to_ISO8601_time();
102         failures += tests_time_to_ISO8601_date();
103
104         if (failures)
105                 exit(EXIT_FAILURE);
106         else
107                 exit(EXIT_SUCCESS);
108 }