os/ossrv/genericopenlibs/cstdlib/TSTLIB/STDLIB2K.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/STDLIB2K.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,431 @@
     1.4 +/*
     1.5 +* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:
    1.18 +* Year 2000 compliance tests for STDLIB
    1.19 +* 
    1.20 +*
    1.21 +*/
    1.22 +
    1.23 +
    1.24 +
    1.25 +#include <stdlib.h>	/* definition of exit() */
    1.26 +#include <stdio.h>
    1.27 +#include <errno.h>
    1.28 +#include <string.h>
    1.29 +#include <time.h>
    1.30 +#include <fcntl.h>
    1.31 +#include <unistd.h>
    1.32 +#include <sys/time.h>	/* timeval, gettimeofday */
    1.33 +
    1.34 +int failures=0;
    1.35 +void failed(char* reason)
    1.36 +	{
    1.37 +	printf("\nFAILURE >>>%s\n", reason);
    1.38 +	failures++;
    1.39 +	}
    1.40 +
    1.41 +
    1.42 +
    1.43 +typedef struct {
    1.44 +	int day;	/* 1-31 */
    1.45 +	int month;	/* 1-12 */
    1.46 +	int year;	/* 1970-2050 */
    1.47 +	} testdate;
    1.48 +
    1.49 +char* testmonths[14] = {
    1.50 +	"invalid_0",
    1.51 +	"Jan", "Feb", "March", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec",
    1.52 +	"invalid_13"
    1.53 +	};
    1.54 +
    1.55 +char* tmdaynames[8] = {
    1.56 +	"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
    1.57 +	"invalid_7"
    1.58 +	};
    1.59 +
    1.60 +/* Format a testdate to match the Y2K document
    1.61 + */
    1.62 +void format_testdate(testdate* aDate, char* aBuffer)
    1.63 +	{
    1.64 +	char* th="th";
    1.65 +	if (aDate->day==1 || aDate->day==21 || aDate->day==31)
    1.66 +		th="st";
    1.67 +	if (aDate->day==2 || aDate->day==22)
    1.68 +		th="nd";
    1.69 +
    1.70 +	sprintf(aBuffer, "%d%s %s %d", aDate->day, th, testmonths[aDate->month], aDate->year);
    1.71 +	}
    1.72 +
    1.73 +typedef struct {
    1.74 +	testdate	input;		/* for conversion to seconds */
    1.75 +	testdate	result;		/* correct value */
    1.76 +	} testpair;
    1.77 +
    1.78 +typedef struct {
    1.79 +	testdate	input;		/* for conversion to seconds */
    1.80 +	int		result;		/* correct value */
    1.81 +	} testint;
    1.82 +
    1.83 +/* MAJOR TEST FUNCTIONALITY - READ THIS CAREFULLY
    1.84 + *
    1.85 + * STDLIB date handling is done in terms of seconds since the "epoch", which was
    1.86 + * 00:00 on 1st January, 1970. The Y2K significant functionality consists solely of the
    1.87 + * routines which manipulate the struct tm data structure.
    1.88 + *
    1.89 + *   time_t mktime (struct tm *brokentime)
    1.90 + *
    1.91 + *   struct tm * gmtime (const time_t *time)
    1.92 + *   struct tm * localtime (const time_t *time)
    1.93 + *
    1.94 + * The mktime conversion is always using a GMT date, so we'll ignore the localtime function for Y2K
    1.95 + * purposes. We also know that gmtime and localtime use the same underlying conversion function,
    1.96 + * one applying the relevant GMT offset on the way in.
    1.97 + *
    1.98 + * Y2K is only concerned with dates, so we will always use 12:00:00 ("High Noon") as the time
    1.99 + * of day.
   1.100 + */
   1.101 +
   1.102 +void tm_from_testdate(struct tm* aTm, testdate* aDate)
   1.103 +	{
   1.104 +	time_t seconds;
   1.105 +	struct tm setup;
   1.106 +	struct tm* res;
   1.107 +
   1.108 +	setup.tm_hour = 12;
   1.109 +	setup.tm_min  = 0;
   1.110 +	setup.tm_sec  = 0;
   1.111 +
   1.112 +	setup.tm_mday = aDate->day;
   1.113 +	setup.tm_mon  = aDate->month - 1;	/* struct tm uses 0-11 */
   1.114 +	setup.tm_year = aDate->year - 1900;	/* struct tm uses years since 1900 */
   1.115 +
   1.116 +	seconds = mktime(&setup);
   1.117 +	if (seconds != (time_t)(-1))
   1.118 +		{
   1.119 +		res = gmtime(&seconds);
   1.120 +		*aTm = *res;
   1.121 +		return;
   1.122 +		}
   1.123 +
   1.124 +	/* unable to convert date */
   1.125 +	failed("tm_from_testdate: mktime failed");
   1.126 +	}
   1.127 +
   1.128 +void normalize_tm(struct tm* aTm)
   1.129 +	{
   1.130 +	time_t seconds = mktime(aTm);
   1.131 +	if (seconds == (time_t)(-1))
   1.132 +		{
   1.133 +		/* unable to convert date */
   1.134 +		failed("normalize_tm: mktime failed");
   1.135 +		}
   1.136 +	return;
   1.137 +	}
   1.138 +
   1.139 +int compare_testdate_tm(testdate* aDate, struct tm* aTm)
   1.140 +	{
   1.141 +	if (aDate->year-1900 != aTm->tm_year)
   1.142 +		return (aDate->year-1900-aTm->tm_year);
   1.143 +	if (aDate->month-1 != aTm->tm_mon)
   1.144 +		return (aDate->month-1-aTm->tm_mon);
   1.145 +	return (aDate->day - aTm->tm_mday);
   1.146 +	}
   1.147 +
   1.148 +/*
   1.149 + *   4.1 Rule 1 Tests
   1.150 + *
   1.151 + * "No valid value for current date will cause any interruption in operation"
   1.152 + *
   1.153 + */
   1.154 +
   1.155 +testpair data_4_1[14] = {
   1.156 +	{{ 31,12,1998 } , {  1, 1, 1999 } },
   1.157 +	{{ 27, 2,1999 } , { 28, 2, 1999 } },
   1.158 +	{{ 28, 2,1999 } , {  1, 3, 1999 } },
   1.159 +	{{ 31, 8,1999 } , {  1, 9, 1999 } },
   1.160 +	{{  8, 9,1999 } , {  9, 9, 1999 } },
   1.161 +	{{  9, 9,1999 } , { 10, 9, 1999 } },
   1.162 +	{{ 31,12,1999 } , {  1, 1, 2000 } },
   1.163 +	{{ 27, 2,2000 } , { 28, 2, 2000 } },
   1.164 +	{{ 28, 2,2000 } , { 29, 2, 2000 } },
   1.165 +	{{ 29, 2,2000 } , {  1, 3, 2000 } },
   1.166 +	{{ 31,12,2000 } , {  1, 1, 2001 } },
   1.167 +	{{ 28, 2,2001 } , {  1, 3, 2001 } },
   1.168 +	{{ 28, 2,2004 } , { 29, 2, 2004 } },
   1.169 +	{{ 29, 2,2004 } , {  1, 3, 2004 } }
   1.170 +	};
   1.171 +
   1.172 +/**
   1.173 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1043
   1.174 +@SYMTestCaseDesc	    Tests for standard date boundaries
   1.175 +@SYMTestPriority 	    High
   1.176 +@SYMTestActions  	    Tests for standard date format
   1.177 +@SYMTestExpectedResults Test must not fail
   1.178 +@SYMREQ                 REQ0000
   1.179 +*/		
   1.180 +void tests_4_1()
   1.181 +	{
   1.182 +	int i;
   1.183 +	testdate* from_dp;
   1.184 +	testdate* to_dp;
   1.185 +	char frombuf[80];
   1.186 +	char tobuf[80];
   1.187 +	struct tm workdate;
   1.188 +
   1.189 +	printf("\n4.1 Rule 1 Tests\n\n");
   1.190 +	printf("4.1.1.1 Standard Date Boundaries\n\n");
   1.191 +	for (i=0; i<14; i++)
   1.192 +		{
   1.193 +		from_dp=&data_4_1[i].input;
   1.194 +		to_dp=&data_4_1[i].result;
   1.195 +
   1.196 +		format_testdate(from_dp, frombuf); 
   1.197 +		format_testdate(to_dp, tobuf); 
   1.198 +		printf("%-2d  From %-14s to %-14s ", i+1, frombuf, tobuf);
   1.199 +
   1.200 +		tm_from_testdate(&workdate, from_dp);
   1.201 +		if (compare_testdate_tm(from_dp, &workdate) != 0)
   1.202 +			failed("test_4_1: invalid from date");
   1.203 +
   1.204 +		workdate.tm_mday += 1;	/* move the time on by one day */
   1.205 +
   1.206 +		normalize_tm(&workdate);
   1.207 +		if (compare_testdate_tm(to_dp, &workdate) != 0)
   1.208 +			{
   1.209 +			printf("*** FAILED\n");
   1.210 +			failures++;
   1.211 +			}
   1.212 +		else
   1.213 +			printf("PASSED\n");
   1.214 +		}
   1.215 +
   1.216 +	}
   1.217 +
   1.218 +/*
   1.219 + *   4.2 Rule 2 Tests (incorporating Rule 4 Tests)
   1.220 + *
   1.221 + * "All manipulations of date or time related data will produce the required results for
   1.222 + *  all valid date values prior to, during and after Year 2000"
   1.223 + *
   1.224 + * "Year 2000 must be recognised as a leap year"
   1.225 + *
   1.226 + */
   1.227 +
   1.228 +testdate data_4_2_1[19] = {
   1.229 +	{ 31, 12, 1998 },
   1.230 +	{  1,  3, 1999 },
   1.231 +	{ 27,  2, 2000 },
   1.232 +	{ 31, 12, 2000 },
   1.233 +	{ 28,  2, 2004 },
   1.234 +	{  1,  1, 1999 },
   1.235 +	{  9,  9, 1999 },
   1.236 +	{ 28,  2, 2000 },
   1.237 +	{  1,  1, 2001 },
   1.238 +	{ 29,  2, 2004 },
   1.239 +	{ 27,  2, 1999 },
   1.240 +	{ 31, 12, 1999 },
   1.241 +	{ 29,  2, 2000 },
   1.242 +	{ 28,  2, 2000 },
   1.243 +	{  1,  3, 2004 },
   1.244 +	{ 28,  2, 1999 },
   1.245 +	{  1,  1, 2000 },
   1.246 +	{  1,  3, 2000 },
   1.247 +	{  1,  3, 2001 }
   1.248 +	};
   1.249 +
   1.250 +void tests_4_2_1()
   1.251 +	{
   1.252 +	int i;
   1.253 +	testdate* from_dp;
   1.254 +	char frombuf[80];
   1.255 +	struct tm workdate;
   1.256 +
   1.257 +	printf("\n4.2 Rule 2 Tests (incorporating Rule 4 Tests)\n\n");
   1.258 +	printf("4.2.1 Valid Dates\n\n");
   1.259 +	for (i=0; i<19; i++)
   1.260 +		{
   1.261 +		from_dp=&data_4_2_1[i];
   1.262 +
   1.263 +		format_testdate(from_dp, frombuf); 
   1.264 +		printf("%-2d  Valid date %-14s  ", i+1, frombuf);
   1.265 +
   1.266 +		tm_from_testdate(&workdate, from_dp);
   1.267 +		if (compare_testdate_tm(from_dp, &workdate) != 0)
   1.268 +			{
   1.269 +			printf("*** FAILED\n");
   1.270 +			failures++;
   1.271 +			}
   1.272 +		else
   1.273 +			printf("PASSED\n");
   1.274 +		}
   1.275 +	}
   1.276 +
   1.277 +testdate data_4_2_2[5] = {
   1.278 +	{ 31,  4, 1998 },
   1.279 +	{ 30,  2, 2000 },
   1.280 +	{ 29,  2, 2001 },
   1.281 +	{ 29,  2, 1999 },
   1.282 +	{ 30,  2, 2004 }
   1.283 +	};
   1.284 +
   1.285 +void tests_4_2_2()
   1.286 +	{
   1.287 +	int i;
   1.288 +	testdate* from_dp;
   1.289 +	char frombuf[80];
   1.290 +	struct tm workdate;
   1.291 +
   1.292 +	printf("\n4.2.2 Invalid Dates\n\n");
   1.293 +	for (i=0; i<5; i++)
   1.294 +		{
   1.295 +		from_dp=&data_4_2_2[i];
   1.296 +
   1.297 +		format_testdate(from_dp, frombuf); 
   1.298 +		printf("%-2d  Invalid date %-14s  ", i+1, frombuf);
   1.299 +
   1.300 +		tm_from_testdate(&workdate, from_dp);
   1.301 +		if (compare_testdate_tm(from_dp, &workdate) == 0)
   1.302 +			{
   1.303 +			printf("*** FAILED\n");
   1.304 +			failures++;
   1.305 +			}
   1.306 +		else
   1.307 +			printf("PASSED\n");
   1.308 +		}
   1.309 +	}
   1.310 +
   1.311 +testint data_4_2_4[3] = {
   1.312 +	{ { 31, 12, 2000 }, 366 },
   1.313 +	{ { 31, 12, 1999 }, 365 },
   1.314 +	{ { 31, 12, 2004 }, 366 }
   1.315 +	};
   1.316 +
   1.317 +void tests_4_2_4()
   1.318 +	{
   1.319 +	int i;
   1.320 +	testdate* from_dp;
   1.321 +	char frombuf[80];
   1.322 +	struct tm workdate;
   1.323 +
   1.324 +	printf("\n4.2.4 Year Lengths\n\n");
   1.325 +	for (i=0; i<3; i++)
   1.326 +		{
   1.327 +		from_dp=&data_4_2_4[i].input;
   1.328 +
   1.329 +		format_testdate(from_dp, frombuf); 
   1.330 +		printf("%-2d  Yearday of %-14s == %d ", i+1, frombuf, data_4_2_4[i].result);
   1.331 +
   1.332 +		tm_from_testdate(&workdate, from_dp);
   1.333 +		if (workdate.tm_yday+1 != data_4_2_4[i].result)
   1.334 +			{
   1.335 +			printf("*** FAILED\n");
   1.336 +			failures++;
   1.337 +			}
   1.338 +		else
   1.339 +			printf("PASSED\n");
   1.340 +		}
   1.341 +	}
   1.342 +
   1.343 +testint data_4_2_7[14] = {
   1.344 +	{ {  1,  1, 1900 }, 1 },	// not supported
   1.345 +	{ { 28,  2, 1900 }, 3 },	// not supported
   1.346 +	{ {  1,  3, 1900 }, 4 },	// not supported
   1.347 +	{ { 28,  2, 1999 }, 0 },
   1.348 +	{ {  1,  3, 1999 }, 1 },
   1.349 +	{ { 31, 12, 1999 }, 5 },
   1.350 +	{ {  1,  1, 2000 }, 6 },
   1.351 +	{ { 28,  2, 2000 }, 1 },
   1.352 +	{ { 29,  2, 2000 }, 2 },
   1.353 +	{ {  1,  3, 2000 }, 3 },
   1.354 +	{ {  1,  1, 2001 }, 1 },
   1.355 +	{ { 28,  2, 2004 }, 6 },
   1.356 +	{ { 29,  2, 2004 }, 0 },
   1.357 +	{ {  1,  3, 2004 }, 1 }
   1.358 +	};
   1.359 +
   1.360 +void tests_4_2_7()
   1.361 +	{
   1.362 +	int i;
   1.363 +	testdate* from_dp;
   1.364 +	char frombuf[80];
   1.365 +	struct tm workdate;
   1.366 +
   1.367 +	printf("\n4.2.7 Calculation of days of the week from dates\n\n");
   1.368 +	for (i=3; i<14; i++)
   1.369 +		{
   1.370 +		from_dp=&data_4_2_7[i].input;
   1.371 +
   1.372 +		format_testdate(from_dp, frombuf); 
   1.373 +		printf("%-2d  %-14s == %-10s ", i+1, frombuf, tmdaynames[data_4_2_7[i].result]);
   1.374 +
   1.375 +		tm_from_testdate(&workdate, from_dp);
   1.376 +		if (compare_testdate_tm(from_dp, &workdate) != 0)
   1.377 +			failed("test_4_2_7: invalid from date");
   1.378 +
   1.379 +		if (workdate.tm_wday != data_4_2_7[i].result)
   1.380 +			{
   1.381 +			printf("*** FAILED  (%d %s)\n", workdate.tm_wday, tmdaynames[workdate.tm_wday]);
   1.382 +			failures++;
   1.383 +			}
   1.384 +		else
   1.385 +			printf("PASSED\n");
   1.386 +		}
   1.387 +	}
   1.388 +
   1.389 +void do_all_tests()
   1.390 +	{
   1.391 +	time_t now = time(0);
   1.392 +	failures=0;
   1.393 +
   1.394 +	tests_4_1();
   1.395 +	tests_4_2_1();
   1.396 +	tests_4_2_2();
   1.397 +	tests_4_2_4();
   1.398 +	tests_4_2_7();
   1.399 +
   1.400 +	if (failures != 0)
   1.401 +		printf("\n\n NOT YEAR 2000 COMPLIANT\n\n");
   1.402 +	else
   1.403 +		printf("\n\n ALL TESTS PASSED - YEAR 2000 COMPLIANT\n\n");
   1.404 +
   1.405 +	printf("Test run dated: %s", ctime(&now));
   1.406 +	}
   1.407 +
   1.408 +int main()
   1.409 +	{
   1.410 +	int fd;
   1.411 +
   1.412 +	do_all_tests();	
   1.413 +
   1.414 +	/* Now repeat the tests into a log file */
   1.415 +
   1.416 +	fd=open("c:/stdlib_y2k.txt", O_WRONLY+O_CREAT+O_TRUNC, 0);
   1.417 +	if (fd<0)
   1.418 +		{
   1.419 +		printf("unable to create test result file\n");
   1.420 +		return -1;
   1.421 +		}
   1.422 +
   1.423 +	setbuf(stderr, NULL);
   1.424 +	dup2(fd, fileno(stderr));	/* redirect stderr */
   1.425 +	setbuf(stdout, NULL);
   1.426 +	dup2(fd, fileno(stdout));	/* redirect stdout */
   1.427 +	close(fd);
   1.428 +	fclose(stdin);
   1.429 +
   1.430 +	do_all_tests();
   1.431 +
   1.432 +	fflush(stdout);
   1.433 +	return 0;
   1.434 +	}