Fix memory leak when maintaining resources. When extra resources are
[apr-util.git] / test / testdate.c
blobb13bc4d1f46df2e929cbd763ed0ae1d8521fc86a
1 /* This program tests the date_parse_http routine in ../main/util_date.c.
3 * It is only semiautomated in that I would run it, modify the code to
4 * use a different algorithm or seed, recompile and run again, etc.
5 * Obviously it should use an argument for that, but I never got around
6 * to changing the implementation.
7 *
8 * gcc -g -O2 -I../main -o test_date ../main/util_date.o test_date.c
9 * test_date | egrep '^No '
11 * Roy Fielding, 1996
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <time.h>
17 #include "apr_date.h"
19 #ifndef srand48
20 #define srand48 srandom
21 #endif
23 #ifndef mrand48
24 #define mrand48 random
25 #endif
27 void gm_timestr_822(char *ts, apr_time_t sec);
28 void gm_timestr_850(char *ts, apr_time_t sec);
29 void gm_timestr_ccc(char *ts, apr_time_t sec);
31 static const apr_time_t year2secs[] = {
32 0LL, /* 1970 */
33 31536000LL, /* 1971 */
34 63072000LL, /* 1972 */
35 94694400LL, /* 1973 */
36 126230400LL, /* 1974 */
37 157766400LL, /* 1975 */
38 189302400LL, /* 1976 */
39 220924800LL, /* 1977 */
40 252460800LL, /* 1978 */
41 283996800LL, /* 1979 */
42 315532800LL, /* 1980 */
43 347155200LL, /* 1981 */
44 378691200LL, /* 1982 */
45 410227200LL, /* 1983 */
46 441763200LL, /* 1984 */
47 473385600LL, /* 1985 */
48 504921600LL, /* 1986 */
49 536457600LL, /* 1987 */
50 567993600LL, /* 1988 */
51 599616000LL, /* 1989 */
52 631152000LL, /* 1990 */
53 662688000LL, /* 1991 */
54 694224000LL, /* 1992 */
55 725846400LL, /* 1993 */
56 757382400LL, /* 1994 */
57 788918400LL, /* 1995 */
58 820454400LL, /* 1996 */
59 852076800LL, /* 1997 */
60 883612800LL, /* 1998 */
61 915148800LL, /* 1999 */
62 946684800LL, /* 2000 */
63 978307200LL, /* 2001 */
64 1009843200LL, /* 2002 */
65 1041379200LL, /* 2003 */
66 1072915200LL, /* 2004 */
67 1104537600LL, /* 2005 */
68 1136073600LL, /* 2006 */
69 1167609600LL, /* 2007 */
70 1199145600LL, /* 2008 */
71 1230768000LL, /* 2009 */
72 1262304000LL, /* 2010 */
73 1293840000LL, /* 2011 */
74 1325376000LL, /* 2012 */
75 1356998400LL, /* 2013 */
76 1388534400LL, /* 2014 */
77 1420070400LL, /* 2015 */
78 1451606400LL, /* 2016 */
79 1483228800LL, /* 2017 */
80 1514764800LL, /* 2018 */
81 1546300800LL, /* 2019 */
82 1577836800LL, /* 2020 */
83 1609459200LL, /* 2021 */
84 1640995200LL, /* 2022 */
85 1672531200LL, /* 2023 */
86 1704067200LL, /* 2024 */
87 1735689600LL, /* 2025 */
88 1767225600LL, /* 2026 */
89 1798761600LL, /* 2027 */
90 1830297600LL, /* 2028 */
91 1861920000LL, /* 2029 */
92 1893456000LL, /* 2030 */
93 1924992000LL, /* 2031 */
94 1956528000LL, /* 2032 */
95 1988150400LL, /* 2033 */
96 2019686400LL, /* 2034 */
97 2051222400LL, /* 2035 */
98 2082758400LL, /* 2036 */
99 2114380800LL, /* 2037 */
100 2145916800LL /* 2038 */
103 const char month_snames[12][4] = {
104 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
107 void gm_timestr_822(char *ts, apr_time_t sec)
109 static const char *const days[7]=
110 {"Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
111 struct tm *tms;
112 time_t ls = (time_t)sec;
114 tms = gmtime(&ls);
116 sprintf(ts, "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", days[tms->tm_wday],
117 tms->tm_mday, month_snames[tms->tm_mon], tms->tm_year + 1900,
118 tms->tm_hour, tms->tm_min, tms->tm_sec);
121 void gm_timestr_850(char *ts, apr_time_t sec)
123 static const char *const days[7]=
124 {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
125 "Saturday"};
126 struct tm *tms;
127 int year;
128 time_t ls = (time_t)sec;
130 tms = gmtime(&ls);
132 year = tms->tm_year;
133 if (year >= 100) year -= 100;
135 sprintf(ts, "%s, %.2d-%s-%.2d %.2d:%.2d:%.2d GMT", days[tms->tm_wday],
136 tms->tm_mday, month_snames[tms->tm_mon], year,
137 tms->tm_hour, tms->tm_min, tms->tm_sec);
140 void gm_timestr_ccc(char *ts, apr_time_t sec)
142 static const char *const days[7]=
143 {"Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
144 struct tm *tms;
145 time_t ls = (time_t)sec;
147 tms = gmtime(&ls);
149 sprintf(ts, "%s %s %2d %.2d:%.2d:%.2d %d", days[tms->tm_wday],
150 month_snames[tms->tm_mon], tms->tm_mday,
151 tms->tm_hour, tms->tm_min, tms->tm_sec, tms->tm_year + 1900);
154 int main (void)
156 int year, i;
157 apr_time_t guess;
158 apr_time_t offset = 0;
159 /* apr_time_t offset = 0; */
160 /* apr_time_t offset = ((31 + 28) * 24 * 3600) - 1; */
161 apr_time_t secstodate, newsecs;
162 char datestr[50];
164 for (year = 1970; year < 2038; ++year) {
165 secstodate = year2secs[year - 1970] + offset;
166 gm_timestr_822(datestr, secstodate);
167 secstodate *= APR_USEC_PER_SEC;
168 newsecs = apr_date_parse_http(datestr);
169 if (secstodate == newsecs)
170 printf("Yes %4d %19" APR_TIME_T_FMT " %s\n", year, secstodate, datestr);
171 else if (newsecs == APR_DATE_BAD)
172 printf("No %4d %19" APR_TIME_T_FMT " %19" APR_TIME_T_FMT " %s\n",
173 year, secstodate, newsecs, datestr);
174 else
175 printf("No* %4d %19" APR_TIME_T_FMT " %19" APR_TIME_T_FMT " %s\n",
176 year, secstodate, newsecs, datestr);
179 srand48(978245L);
181 for (i = 0; i < 10000; ++i) {
182 guess = (time_t)mrand48();
183 if (guess < 0) guess *= -1;
184 secstodate = guess + offset;
185 gm_timestr_822(datestr, secstodate);
186 secstodate *= APR_USEC_PER_SEC;
187 newsecs = apr_date_parse_http(datestr);
188 if (secstodate == newsecs)
189 printf("Yes %" APR_TIME_T_FMT " %s\n", secstodate, datestr);
190 else if (newsecs == APR_DATE_BAD)
191 printf("No %" APR_TIME_T_FMT " %" APR_TIME_T_FMT " %s\n",
192 secstodate, newsecs, datestr);
193 else
194 printf("No* %" APR_TIME_T_FMT " %" APR_TIME_T_FMT " %s\n",
195 secstodate, newsecs, datestr);
197 exit(0);