upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr-util / test / testdate.c
1 /* This program tests the date_parse_http routine in ../main/util_date.c.
2  *
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 '
10  * 
11  * Roy Fielding, 1996
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <time.h>
17 #include "apr_date.h"
18
19 #ifndef srand48
20 #define srand48 srandom
21 #endif
22
23 #ifndef mrand48
24 #define mrand48 random
25 #endif
26
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);
30
31 static const apr_time_t year2secs[] = {
32              APR_INT64_C(0),    /* 1970 */
33       APR_INT64_C(31536000),    /* 1971 */
34       APR_INT64_C(63072000),    /* 1972 */
35       APR_INT64_C(94694400),    /* 1973 */
36      APR_INT64_C(126230400),    /* 1974 */
37      APR_INT64_C(157766400),    /* 1975 */
38      APR_INT64_C(189302400),    /* 1976 */
39      APR_INT64_C(220924800),    /* 1977 */
40      APR_INT64_C(252460800),    /* 1978 */
41      APR_INT64_C(283996800),    /* 1979 */
42      APR_INT64_C(315532800),    /* 1980 */
43      APR_INT64_C(347155200),    /* 1981 */
44      APR_INT64_C(378691200),    /* 1982 */
45      APR_INT64_C(410227200),    /* 1983 */
46      APR_INT64_C(441763200),    /* 1984 */
47      APR_INT64_C(473385600),    /* 1985 */
48      APR_INT64_C(504921600),    /* 1986 */
49      APR_INT64_C(536457600),    /* 1987 */
50      APR_INT64_C(567993600),    /* 1988 */
51      APR_INT64_C(599616000),    /* 1989 */
52      APR_INT64_C(631152000),    /* 1990 */
53      APR_INT64_C(662688000),    /* 1991 */
54      APR_INT64_C(694224000),    /* 1992 */
55      APR_INT64_C(725846400),    /* 1993 */
56      APR_INT64_C(757382400),    /* 1994 */
57      APR_INT64_C(788918400),    /* 1995 */
58      APR_INT64_C(820454400),    /* 1996 */
59      APR_INT64_C(852076800),    /* 1997 */
60      APR_INT64_C(883612800),    /* 1998 */
61      APR_INT64_C(915148800),    /* 1999 */
62      APR_INT64_C(946684800),    /* 2000 */
63      APR_INT64_C(978307200),    /* 2001 */
64     APR_INT64_C(1009843200),    /* 2002 */
65     APR_INT64_C(1041379200),    /* 2003 */
66     APR_INT64_C(1072915200),    /* 2004 */
67     APR_INT64_C(1104537600),    /* 2005 */
68     APR_INT64_C(1136073600),    /* 2006 */
69     APR_INT64_C(1167609600),    /* 2007 */
70     APR_INT64_C(1199145600),    /* 2008 */
71     APR_INT64_C(1230768000),    /* 2009 */
72     APR_INT64_C(1262304000),    /* 2010 */
73     APR_INT64_C(1293840000),    /* 2011 */
74     APR_INT64_C(1325376000),    /* 2012 */
75     APR_INT64_C(1356998400),    /* 2013 */
76     APR_INT64_C(1388534400),    /* 2014 */
77     APR_INT64_C(1420070400),    /* 2015 */
78     APR_INT64_C(1451606400),    /* 2016 */
79     APR_INT64_C(1483228800),    /* 2017 */
80     APR_INT64_C(1514764800),    /* 2018 */
81     APR_INT64_C(1546300800),    /* 2019 */
82     APR_INT64_C(1577836800),    /* 2020 */
83     APR_INT64_C(1609459200),    /* 2021 */
84     APR_INT64_C(1640995200),    /* 2022 */
85     APR_INT64_C(1672531200),    /* 2023 */
86     APR_INT64_C(1704067200),    /* 2024 */
87     APR_INT64_C(1735689600),    /* 2025 */
88     APR_INT64_C(1767225600),    /* 2026 */
89     APR_INT64_C(1798761600),    /* 2027 */
90     APR_INT64_C(1830297600),    /* 2028 */
91     APR_INT64_C(1861920000),    /* 2029 */
92     APR_INT64_C(1893456000),    /* 2030 */
93     APR_INT64_C(1924992000),    /* 2031 */
94     APR_INT64_C(1956528000),    /* 2032 */
95     APR_INT64_C(1988150400),    /* 2033 */
96     APR_INT64_C(2019686400),    /* 2034 */
97     APR_INT64_C(2051222400),    /* 2035 */
98     APR_INT64_C(2082758400),    /* 2036 */
99     APR_INT64_C(2114380800),    /* 2037 */
100     APR_INT64_C(2145916800)     /* 2038 */
101 };
102
103 const char month_snames[12][4] = {
104     "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
105 };
106
107 void gm_timestr_822(char *ts, apr_time_t sec)
108 {
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;
113
114     tms = gmtime(&ls);
115  
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);
119 }
120
121 void gm_timestr_850(char *ts, apr_time_t sec)
122 {
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;
129  
130     tms = gmtime(&ls);
131
132     year = tms->tm_year;
133     if (year >= 100) year -= 100;
134  
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);
138 }
139
140 void gm_timestr_ccc(char *ts, apr_time_t sec)
141 {
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;
146  
147     tms = gmtime(&ls);
148  
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);
152 }
153
154 int main (void)
155 {
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];
163
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);
177     }
178     
179     srand48(978245L);
180
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);
196     }
197     exit(0);
198 }