Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / glib / testgdate.c
blobf74e00a3ba96cf0927c9cf519188cc7b2fe9215b
2 #include "glib.h"
4 #include <stdio.h>
5 #include <string.h>
6 #include <locale.h>
7 #include <time.h>
9 gboolean failed = FALSE;
10 guint32 passed = 0;
11 guint32 notpassed = 0;
13 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
14 if (failed) \
15 { ++notpassed; \
16 if (!m) \
17 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
18 else \
19 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
20 } \
21 else \
22 ++passed; \
23 if ((passed+notpassed) % 10000 == 0) g_print ("."); fflush (stdout); \
24 } G_STMT_END
26 void g_date_debug_print(GDate* d)
28 if (!d) g_print("NULL!\n");
29 else
30 g_print("julian: %u (%s) DMY: %u %u %u (%s)\n",
31 d->julian_days,
32 d->julian ? "valid" : "invalid",
33 d->day,
34 d->month,
35 d->year,
36 d->dmy ? "valid" : "invalid");
38 fflush(stdout);
41 int main(int argc, char** argv)
43 GDate* d;
44 guint32 j;
45 GDateMonth m;
46 GDateYear y, prev_y;
47 GDateDay day;
48 gchar buf[101];
49 gchar* loc;
50 /* Try to get all the leap year cases. */
51 GDateYear check_years[] = {
52 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
53 11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
54 398, 399, 400, 401, 402, 403, 404, 405, 406,
55 1598, 1599, 1600, 1601, 1602, 1650, 1651,
56 1897, 1898, 1899, 1900, 1901, 1902, 1903,
57 1961, 1962, 1963, 1964, 1965, 1967,
58 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
59 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
60 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
61 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
62 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
63 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
65 guint n_check_years = sizeof(check_years)/sizeof(GDateYear);
66 guint i;
67 gboolean discontinuity;
69 g_print("checking GDate...");
71 TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9);
73 d = g_date_new();
75 TEST("Empty constructor produces invalid date", !g_date_valid(d));
77 g_date_free(d);
79 d = g_date_new_dmy(1,1,1);
81 TEST("January 1, Year 1 created and valid", g_date_valid(d));
83 j = g_date_julian(d);
85 TEST("January 1, Year 1 is Julian date 1", j == 1);
87 TEST("Returned month is January", g_date_month(d) == G_DATE_JANUARY);
88 TEST("Returned day is 1", g_date_day(d) == 1);
89 TEST("Returned year is 1", g_date_year(d) == 1);
91 TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH));
92 TEST("Month 13 is invalid", !g_date_valid_month(13));
93 TEST("Bad day is invalid", !g_date_valid_day(G_DATE_BAD_DAY));
94 TEST("Day 32 is invalid", !g_date_valid_day(32));
95 TEST("Bad year is invalid", !g_date_valid_year(G_DATE_BAD_YEAR));
96 TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN));
97 TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY));
98 TEST("Year 2000 is a leap year", g_date_is_leap_year(2000));
99 TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999));
100 TEST("Year 1996 is a leap year", g_date_is_leap_year(1996));
101 TEST("Year 1600 is a leap year", g_date_is_leap_year(1600));
102 TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100));
103 TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800));
105 g_date_free(d);
107 loc = setlocale(LC_ALL,"");
108 if (loc)
109 g_print("\nLocale set to %s\n", loc);
110 else
111 g_print("\nLocale unchanged\n");
113 d = g_date_new();
114 g_date_set_time(d, time(NULL));
115 TEST("Today is valid", g_date_valid(d));
117 g_date_strftime(buf,100,"Today is a %A, %x\n", d);
118 g_print("%s", buf);
120 g_date_set_time(d, 1);
121 TEST("Beginning of Unix epoch is valid", g_date_valid(d));
123 g_date_strftime(buf,100,"1 second into the Unix epoch it was a %A, in the month of %B, %x\n", d);
124 g_print("%s", buf);
126 g_date_set_julian(d, 1);
127 TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d));
129 g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n",
131 g_print("%s", buf);
133 g_date_set_dmy(d, 10, 1, 2000);
135 g_date_strftime(buf,100,"%x", d);
137 g_date_set_parse(d, buf);
138 /* Note: this test will hopefully work, but no promises. */
139 TEST("Successfully parsed a %x-formatted string",
140 g_date_valid(d) &&
141 g_date_month(d) == 1 &&
142 g_date_day(d) == 10 &&
143 g_date_year(d) == 2000);
144 if (failed)
145 g_date_debug_print(d);
147 g_date_free(d);
149 j = G_DATE_BAD_JULIAN;
151 i = 0;
152 discontinuity = TRUE;
153 y = check_years[0];
154 prev_y = G_DATE_BAD_YEAR;
155 while (i < n_check_years)
157 guint32 first_day_of_year = G_DATE_BAD_JULIAN;
158 guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365;
159 guint sunday_week_of_year = 0;
160 guint sunday_weeks_in_year = g_date_sunday_weeks_in_year(y);
161 guint monday_week_of_year = 0;
162 guint monday_weeks_in_year = g_date_monday_weeks_in_year(y);
164 if (discontinuity)
165 g_print(" (Break in sequence of requested years to check)\n");
167 g_print("Checking year %u", y);
169 TEST("Year is valid", g_date_valid_year(y));
171 TEST("Number of Sunday weeks in year is 52 or 53",
172 sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
174 TEST("Number of Monday weeks in year is 52 or 53",
175 monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
177 m = 1;
178 while (m < 13)
180 guint8 dim = g_date_days_in_month(m,y);
181 GDate days[31]; /* This is the fast way, no allocation */
183 TEST("Sensible number of days in month", (dim > 0 && dim < 32));
185 TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
187 day = 1;
189 g_date_clear(days, 31);
191 while (day <= dim)
193 guint i;
194 GDate tmp;
196 TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
198 /* Create GDate with triplet */
200 d = &days[day-1];
202 TEST("Cleared day is invalid", !g_date_valid(d));
204 g_date_set_dmy(d,day,m,y);
206 TEST("Set day is valid", g_date_valid(d));
208 if (m == G_DATE_JANUARY && day == 1)
210 first_day_of_year = g_date_julian(d);
213 g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
215 TEST("Date with DMY triplet is valid", g_date_valid(d));
216 TEST("Month accessor works", g_date_month(d) == m);
217 TEST("Year accessor works", g_date_year(d) == y);
218 TEST("Day of month accessor works", g_date_day(d) == day);
220 TEST("Day of year is consistent with Julian dates",
221 ((g_date_julian(d) + 1 - first_day_of_year) ==
222 (g_date_day_of_year(d))));
224 if (failed)
226 g_print("first day: %u this day: %u day of year: %u\n",
227 first_day_of_year,
228 g_date_julian(d),
229 g_date_day_of_year(d));
232 if (m == G_DATE_DECEMBER && day == 31)
234 TEST("Last day of year equals number of days in year",
235 g_date_day_of_year(d) == days_in_year);
236 if (failed)
238 g_print("last day: %u days in year: %u\n",
239 g_date_day_of_year(d), days_in_year);
243 TEST("Day of year is not more than number of days in the year",
244 g_date_day_of_year(d) <= days_in_year);
246 TEST("Monday week of year is not more than number of weeks in the year",
247 g_date_monday_week_of_year(d) <= monday_weeks_in_year);
248 if (failed)
250 g_print("Weeks in year: %u\n", monday_weeks_in_year);
251 g_date_debug_print(d);
253 TEST("Monday week of year is >= than last week of year",
254 g_date_monday_week_of_year(d) >= monday_week_of_year);
256 if (g_date_weekday(d) == G_DATE_MONDAY)
259 TEST("Monday week of year on Monday 1 more than previous day's week of year",
260 (g_date_monday_week_of_year(d) - monday_week_of_year) == 1);
262 else
264 TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
265 (g_date_monday_week_of_year(d) - monday_week_of_year) == 0);
269 monday_week_of_year = g_date_monday_week_of_year(d);
272 TEST("Sunday week of year is not more than number of weeks in the year",
273 g_date_sunday_week_of_year(d) <= sunday_weeks_in_year);
274 if (failed)
276 g_date_debug_print(d);
278 TEST("Sunday week of year is >= than last week of year",
279 g_date_sunday_week_of_year(d) >= sunday_week_of_year);
281 if (g_date_weekday(d) == G_DATE_SUNDAY)
283 TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
284 (g_date_sunday_week_of_year(d) - sunday_week_of_year) == 1);
286 else
288 TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
289 (g_date_sunday_week_of_year(d) - sunday_week_of_year) == 0);
292 sunday_week_of_year = g_date_sunday_week_of_year(d);
294 TEST("Date is equal to itself",
295 g_date_compare(d,d) == 0);
298 /*************** Increments ***********/
300 i = 1;
301 while (i < 402) /* Need to get 400 year increments in */
304 /***** Days ******/
305 tmp = *d;
306 g_date_add_days(d, i);
308 TEST("Adding days gives a value greater than previous",
309 g_date_compare(d, &tmp) > 0);
311 g_date_subtract_days(d, i);
312 TEST("Forward days then backward days returns us to current day",
313 g_date_day(d) == day);
315 if (failed)
317 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
318 g_date_debug_print(d);
321 TEST("Forward days then backward days returns us to current month",
322 g_date_month(d) == m);
324 if (failed)
326 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
327 g_date_debug_print(d);
330 TEST("Forward days then backward days returns us to current year",
331 g_date_year(d) == y);
333 if (failed)
335 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
336 g_date_debug_print(d);
339 /******* Months ********/
341 tmp = *d;
342 g_date_add_months(d, i);
343 TEST("Adding months gives a larger value",
344 g_date_compare(d, &tmp) > 0);
345 g_date_subtract_months(d, i);
347 TEST("Forward months then backward months returns us to current month",
348 g_date_month(d) == m);
350 if (failed)
352 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
353 g_date_debug_print(d);
356 TEST("Forward months then backward months returns us to current year",
357 g_date_year(d) == y);
359 if (failed)
361 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
362 g_date_debug_print(d);
366 if (day < 29)
368 /* Day should be unchanged */
370 TEST("Forward months then backward months returns us to current day",
371 g_date_day(d) == day);
373 if (failed)
375 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
376 g_date_debug_print(d);
379 else
381 /* reset the day for later tests */
382 g_date_set_day(d, day);
385 /******* Years ********/
387 tmp = *d;
388 g_date_add_years(d, i);
390 TEST("Adding years gives a larger value",
391 g_date_compare(d,&tmp) > 0);
393 g_date_subtract_years(d, i);
395 TEST("Forward years then backward years returns us to current month",
396 g_date_month(d) == m);
398 if (failed)
400 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
401 g_date_debug_print(d);
404 TEST("Forward years then backward years returns us to current year",
405 g_date_year(d) == y);
407 if (failed)
409 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
410 g_date_debug_print(d);
413 if (m != 2 && day != 29)
415 TEST("Forward years then backward years returns us to current day",
416 g_date_day(d) == day);
418 if (failed)
420 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
421 g_date_debug_print(d);
424 else
426 g_date_set_day(d, day); /* reset */
429 i += 10;
432 /***** increment test relative to our local Julian count */
434 if (!discontinuity) {
436 /* We can only run sequence tests between sequential years */
438 TEST("Julians are sequential with increment 1",
439 j+1 == g_date_julian(d));
440 if (failed)
442 g_print("Out of sequence, prev: %u expected: %u got: %u\n",
443 j, j+1, g_date_julian(d));
446 g_date_add_days(d,1);
447 TEST("Next day has julian 1 higher",
448 g_date_julian(d) == j + 2);
449 g_date_subtract_days(d, 1);
451 if (j != G_DATE_BAD_JULIAN)
453 g_date_subtract_days(d, 1);
455 TEST("Previous day has julian 1 lower",
456 g_date_julian(d) == j);
458 g_date_add_days(d, 1); /* back to original */
461 discontinuity = FALSE; /* goes away now */
463 fflush(stdout);
464 fflush(stderr);
466 j = g_date_julian(d); /* inc current julian */
468 ++day;
470 ++m;
472 g_print(" done\n");
473 ++i;
474 prev_y = y;
475 y = check_years[i];
476 if (prev_y == G_DATE_BAD_YEAR ||
477 (prev_y + 1) != y) discontinuity = TRUE;
481 g_print("\n%u tests passed, %u failed\n",passed, notpassed);
483 return 0;