Update contrib.
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Year 2000 compliance tests for STDLIB
22 #include <stdlib.h> /* definition of exit() */
29 #include <sys/time.h> /* timeval, gettimeofday */
32 void failed(char* reason)
34 printf("\nFAILURE >>>%s\n", reason);
43 int year; /* 1970-2050 */
46 char* testmonths[14] = {
48 "Jan", "Feb", "March", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec",
52 char* tmdaynames[8] = {
53 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
57 /* Format a testdate to match the Y2K document
59 void format_testdate(testdate* aDate, char* aBuffer)
62 if (aDate->day==1 || aDate->day==21 || aDate->day==31)
64 if (aDate->day==2 || aDate->day==22)
67 sprintf(aBuffer, "%d%s %s %d", aDate->day, th, testmonths[aDate->month], aDate->year);
71 testdate input; /* for conversion to seconds */
72 testdate result; /* correct value */
76 testdate input; /* for conversion to seconds */
77 int result; /* correct value */
80 /* MAJOR TEST FUNCTIONALITY - READ THIS CAREFULLY
82 * STDLIB date handling is done in terms of seconds since the "epoch", which was
83 * 00:00 on 1st January, 1970. The Y2K significant functionality consists solely of the
84 * routines which manipulate the struct tm data structure.
86 * time_t mktime (struct tm *brokentime)
88 * struct tm * gmtime (const time_t *time)
89 * struct tm * localtime (const time_t *time)
91 * The mktime conversion is always using a GMT date, so we'll ignore the localtime function for Y2K
92 * purposes. We also know that gmtime and localtime use the same underlying conversion function,
93 * one applying the relevant GMT offset on the way in.
95 * Y2K is only concerned with dates, so we will always use 12:00:00 ("High Noon") as the time
99 void tm_from_testdate(struct tm* aTm, testdate* aDate)
109 setup.tm_mday = aDate->day;
110 setup.tm_mon = aDate->month - 1; /* struct tm uses 0-11 */
111 setup.tm_year = aDate->year - 1900; /* struct tm uses years since 1900 */
113 seconds = mktime(&setup);
114 if (seconds != (time_t)(-1))
116 res = gmtime(&seconds);
121 /* unable to convert date */
122 failed("tm_from_testdate: mktime failed");
125 void normalize_tm(struct tm* aTm)
127 time_t seconds = mktime(aTm);
128 if (seconds == (time_t)(-1))
130 /* unable to convert date */
131 failed("normalize_tm: mktime failed");
136 int compare_testdate_tm(testdate* aDate, struct tm* aTm)
138 if (aDate->year-1900 != aTm->tm_year)
139 return (aDate->year-1900-aTm->tm_year);
140 if (aDate->month-1 != aTm->tm_mon)
141 return (aDate->month-1-aTm->tm_mon);
142 return (aDate->day - aTm->tm_mday);
148 * "No valid value for current date will cause any interruption in operation"
152 testpair data_4_1[14] = {
153 {{ 31,12,1998 } , { 1, 1, 1999 } },
154 {{ 27, 2,1999 } , { 28, 2, 1999 } },
155 {{ 28, 2,1999 } , { 1, 3, 1999 } },
156 {{ 31, 8,1999 } , { 1, 9, 1999 } },
157 {{ 8, 9,1999 } , { 9, 9, 1999 } },
158 {{ 9, 9,1999 } , { 10, 9, 1999 } },
159 {{ 31,12,1999 } , { 1, 1, 2000 } },
160 {{ 27, 2,2000 } , { 28, 2, 2000 } },
161 {{ 28, 2,2000 } , { 29, 2, 2000 } },
162 {{ 29, 2,2000 } , { 1, 3, 2000 } },
163 {{ 31,12,2000 } , { 1, 1, 2001 } },
164 {{ 28, 2,2001 } , { 1, 3, 2001 } },
165 {{ 28, 2,2004 } , { 29, 2, 2004 } },
166 {{ 29, 2,2004 } , { 1, 3, 2004 } }
170 @SYMTestCaseID SYSLIB-STDLIB-CT-1043
171 @SYMTestCaseDesc Tests for standard date boundaries
172 @SYMTestPriority High
173 @SYMTestActions Tests for standard date format
174 @SYMTestExpectedResults Test must not fail
186 printf("\n4.1 Rule 1 Tests\n\n");
187 printf("4.1.1.1 Standard Date Boundaries\n\n");
190 from_dp=&data_4_1[i].input;
191 to_dp=&data_4_1[i].result;
193 format_testdate(from_dp, frombuf);
194 format_testdate(to_dp, tobuf);
195 printf("%-2d From %-14s to %-14s ", i+1, frombuf, tobuf);
197 tm_from_testdate(&workdate, from_dp);
198 if (compare_testdate_tm(from_dp, &workdate) != 0)
199 failed("test_4_1: invalid from date");
201 workdate.tm_mday += 1; /* move the time on by one day */
203 normalize_tm(&workdate);
204 if (compare_testdate_tm(to_dp, &workdate) != 0)
206 printf("*** FAILED\n");
216 * 4.2 Rule 2 Tests (incorporating Rule 4 Tests)
218 * "All manipulations of date or time related data will produce the required results for
219 * all valid date values prior to, during and after Year 2000"
221 * "Year 2000 must be recognised as a leap year"
225 testdate data_4_2_1[19] = {
254 printf("\n4.2 Rule 2 Tests (incorporating Rule 4 Tests)\n\n");
255 printf("4.2.1 Valid Dates\n\n");
258 from_dp=&data_4_2_1[i];
260 format_testdate(from_dp, frombuf);
261 printf("%-2d Valid date %-14s ", i+1, frombuf);
263 tm_from_testdate(&workdate, from_dp);
264 if (compare_testdate_tm(from_dp, &workdate) != 0)
266 printf("*** FAILED\n");
274 testdate data_4_2_2[5] = {
289 printf("\n4.2.2 Invalid Dates\n\n");
292 from_dp=&data_4_2_2[i];
294 format_testdate(from_dp, frombuf);
295 printf("%-2d Invalid date %-14s ", i+1, frombuf);
297 tm_from_testdate(&workdate, from_dp);
298 if (compare_testdate_tm(from_dp, &workdate) == 0)
300 printf("*** FAILED\n");
308 testint data_4_2_4[3] = {
309 { { 31, 12, 2000 }, 366 },
310 { { 31, 12, 1999 }, 365 },
311 { { 31, 12, 2004 }, 366 }
321 printf("\n4.2.4 Year Lengths\n\n");
324 from_dp=&data_4_2_4[i].input;
326 format_testdate(from_dp, frombuf);
327 printf("%-2d Yearday of %-14s == %d ", i+1, frombuf, data_4_2_4[i].result);
329 tm_from_testdate(&workdate, from_dp);
330 if (workdate.tm_yday+1 != data_4_2_4[i].result)
332 printf("*** FAILED\n");
340 testint data_4_2_7[14] = {
341 { { 1, 1, 1900 }, 1 }, // not supported
342 { { 28, 2, 1900 }, 3 }, // not supported
343 { { 1, 3, 1900 }, 4 }, // not supported
344 { { 28, 2, 1999 }, 0 },
345 { { 1, 3, 1999 }, 1 },
346 { { 31, 12, 1999 }, 5 },
347 { { 1, 1, 2000 }, 6 },
348 { { 28, 2, 2000 }, 1 },
349 { { 29, 2, 2000 }, 2 },
350 { { 1, 3, 2000 }, 3 },
351 { { 1, 1, 2001 }, 1 },
352 { { 28, 2, 2004 }, 6 },
353 { { 29, 2, 2004 }, 0 },
354 { { 1, 3, 2004 }, 1 }
364 printf("\n4.2.7 Calculation of days of the week from dates\n\n");
367 from_dp=&data_4_2_7[i].input;
369 format_testdate(from_dp, frombuf);
370 printf("%-2d %-14s == %-10s ", i+1, frombuf, tmdaynames[data_4_2_7[i].result]);
372 tm_from_testdate(&workdate, from_dp);
373 if (compare_testdate_tm(from_dp, &workdate) != 0)
374 failed("test_4_2_7: invalid from date");
376 if (workdate.tm_wday != data_4_2_7[i].result)
378 printf("*** FAILED (%d %s)\n", workdate.tm_wday, tmdaynames[workdate.tm_wday]);
388 time_t now = time(0);
398 printf("\n\n NOT YEAR 2000 COMPLIANT\n\n");
400 printf("\n\n ALL TESTS PASSED - YEAR 2000 COMPLIANT\n\n");
402 printf("Test run dated: %s", ctime(&now));
411 /* Now repeat the tests into a log file */
413 fd=open("c:/stdlib_y2k.txt", O_WRONLY+O_CREAT+O_TRUNC, 0);
416 printf("unable to create test result file\n");
420 setbuf(stderr, NULL);
421 dup2(fd, fileno(stderr)); /* redirect stderr */
422 setbuf(stdout, NULL);
423 dup2(fd, fileno(stdout)); /* redirect stdout */