genericopenlibs/cstdlib/TSTLIB/TMisc3.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Simple STDLIB tests.
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32test.h>
       
    19 #include <e32svr.h>
       
    20 #include <ctype.h>
       
    21 #include <math.h>
       
    22 #include <errno.h>
       
    23 #include "ESTLIB.H"
       
    24 
       
    25 //CPP file is used for C tests, because by default any console opened from a C file
       
    26 //expects a key to be pressed when it is about to be closed. That makes impossible
       
    27 //the creation of automated C tests.
       
    28 
       
    29 //
       
    30 // Globals
       
    31 
       
    32 static RTest TheTest(_L("TMisc3"));
       
    33 const double KPi = 3.14159265358979323846;
       
    34 
       
    35 //
       
    36 //
       
    37 //Test macroses and functions
       
    38 
       
    39 static void Check(TInt aValue, TInt aLine)
       
    40 	{
       
    41 	if(!aValue)
       
    42 		{
       
    43 		TheTest(EFalse, aLine);
       
    44 		}
       
    45 	}
       
    46 static  void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    47 	{
       
    48 	if(aValue != aExpected)
       
    49 		{
       
    50 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
    51 		TheTest(EFalse, aLine);
       
    52 		}
       
    53 	}
       
    54 #define TEST(arg) ::Check((arg), __LINE__)
       
    55 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
       
    56 
       
    57 #define TEST_NAN (0.0/0.0)
       
    58 #define TEST_POS_INFINITY (1.0/0.0)
       
    59 #define TEST_NEG_INFINITY (-1.0/0.0)
       
    60 
       
    61 //
       
    62 // Tests
       
    63 
       
    64 static void TestCType()
       
    65 	{
       
    66 	char c;
       
    67 	for(c='a';c<='z';++c)
       
    68 		{
       
    69 		TEST(isalnum(c));
       
    70 		TEST(isalpha(c));
       
    71 		TEST(!iscntrl(c));
       
    72 		TEST(!isdigit(c));
       
    73 		TEST(isgraph(c));
       
    74 		TEST(islower(c));
       
    75 		TEST(isprint(c));
       
    76 		TEST(!ispunct(c));
       
    77 		TEST(!isspace(c));
       
    78 		TEST(!isupper(c));
       
    79 		TEST(c >= 'a' && c <= 'f' ? isxdigit(c) : !isxdigit(c));
       
    80 		}
       
    81 	for(c='A';c<='Z';++c)
       
    82 		{
       
    83 		TEST(isalnum(c));
       
    84 		TEST(isalpha(c));
       
    85 		TEST(!iscntrl(c));
       
    86 		TEST(!isdigit(c));
       
    87 		TEST(isgraph(c));
       
    88 		TEST(!islower(c));
       
    89 		TEST(isprint(c));
       
    90 		TEST(!ispunct(c));
       
    91 		TEST(!isspace(c));
       
    92 		TEST(isupper(c));
       
    93 		TEST(c >= 'A' && c <= 'F' ? isxdigit(c) : !isxdigit(c));
       
    94 		}
       
    95 	for(c='0';c<='9';++c)
       
    96 		{
       
    97 		TEST(isalnum(c));
       
    98 		TEST(!isalpha(c));
       
    99 		TEST(!iscntrl(c));
       
   100 		TEST(isdigit(c));
       
   101 		TEST(isgraph(c));
       
   102 		TEST(!islower(c));
       
   103 		TEST(isprint(c));
       
   104 		TEST(!ispunct(c));
       
   105 		TEST(!isspace(c));
       
   106 		TEST(!isupper(c));
       
   107 		TEST(isxdigit(c));
       
   108 		}
       
   109 	for(c=0;c<' ';++c)
       
   110 		{
       
   111 		TEST(!isalnum(c));
       
   112 		TEST(!isalpha(c));
       
   113 		TEST(iscntrl(c));
       
   114 		TEST(!isdigit(c));
       
   115 		TEST(!isgraph(c));
       
   116 		TEST(!islower(c));
       
   117 		TEST(!isprint(c));
       
   118 		TEST(!ispunct(c));
       
   119 		if(c != '\t' && c != '\n' && c != '\r' && c != '\v' && c != '\f')
       
   120 			{
       
   121 			TEST(!isspace(c));
       
   122 			}
       
   123 		else
       
   124 			{
       
   125 			TEST(isspace(c));
       
   126 			}
       
   127 		TEST(!isupper(c));
       
   128 		}
       
   129 	}
       
   130 
       
   131 static void TestMath()
       
   132 	{
       
   133 	double eps = 0.000001;
       
   134 
       
   135 	double res = fabs(21.091);
       
   136 	TEST((21.091 - res) < eps);
       
   137 	res = fabs(-1.91);
       
   138 	TEST((res - 1.91) < eps);
       
   139 
       
   140 	res = atan(0.0);
       
   141 	TEST(fabs(res) < eps);
       
   142 	// 90 degrees
       
   143 	res = tan(KPi/2);
       
   144 	TEST(res > 1000000000.0);
       
   145 	res = atan(res);
       
   146 	TEST(fabs(res - KPi/2) < eps);
       
   147 	// 45 degrees
       
   148 	res = tan(KPi/4);
       
   149 	TEST(fabs(res - 1.0) < eps);
       
   150 	res = atan(res);
       
   151 	TEST(fabs(res - KPi/4) < eps);
       
   152 	// 135 degrees
       
   153 	res = tan((3 * KPi) / 4);
       
   154 	TEST(fabs(res + 1.0) < eps);
       
   155 	res = atan(res);
       
   156 	TEST(fabs((KPi + res) - (3 * KPi) / 4) < eps);
       
   157 
       
   158 	// 0 degrees
       
   159 	res = cos(0.0);
       
   160 	TEST(fabs(res - 1) < eps);
       
   161 	// 90 degrees
       
   162 	res = cos(KPi/2);
       
   163 	TEST(res < eps);
       
   164 	// 180 degrees
       
   165 	res = cos(KPi);
       
   166 	TEST(fabs(res + 1.0) < eps);
       
   167 
       
   168 	// 0 degrees
       
   169 	res = sin(0.0);
       
   170 	TEST(res < eps);
       
   171 	// 90 degrees
       
   172 	res = sin(KPi/2);
       
   173 	TEST(fabs(res - 1) < eps);
       
   174 	// 180 degrees
       
   175 	res = sin(KPi);
       
   176 	TEST(res < eps);
       
   177 
       
   178 	res = tanh(1.0);
       
   179 	TEST(fabs(res - 0.761594) < eps);
       
   180 
       
   181 	int exponent;
       
   182 	res = frexp(0.51E+2, &exponent);
       
   183 	TEST((0.51E+2 - res * pow(2.0, exponent)) < eps);
       
   184 
       
   185 	double integer;
       
   186 	res = modf(34.567, &integer);
       
   187 	TEST(fabs(res - 0.567) < eps);
       
   188 	TEST(fabs(integer - 34.0) < eps);
       
   189 	res = modf(-35.567, &integer);
       
   190 	TEST(fabs(res + 0.567) < eps);
       
   191 	TEST(fabs(integer + 35.0) < eps);
       
   192 
       
   193 	res = ceil(245.8903);
       
   194 	TEST(fabs(res - 246.0) < eps);
       
   195 	res = ceil(-11.91);
       
   196 	TEST(fabs(res + 11.0) < eps);
       
   197 
       
   198 	res = floor(245.8903);
       
   199 	TEST(fabs(res - 245.0) < eps);
       
   200 	res = floor(-11.91);
       
   201 	TEST(fabs(res + 12.0) < eps);
       
   202 
       
   203 	res = copysign(4.789, -9.001);
       
   204 	TEST((res + 4.789) < eps);
       
   205 	res = copysign(-4.789, 9.001);
       
   206 	TEST((res - 4.789) < eps);
       
   207 
       
   208 	}
       
   209 
       
   210 static void TestDef113884()
       
   211 	{
       
   212 	int exponent;
       
   213 
       
   214 	errno = 0;
       
   215 	frexp(0.51E+2, &exponent);
       
   216 	pow(2.0, exponent);
       
   217 	TEST2(errno, 0);
       
   218 	errno = 0;
       
   219 	pow(-1.0, 0.5);
       
   220 	TEST2(errno, EDOM);
       
   221 	errno = 0;
       
   222 	// Added 'LL' as GCC needs to know these are explicitly 'long long'
       
   223 	pow(34523543434234LL , 23423432234LL);
       
   224 	TEST2(errno, ERANGE);
       
   225 	errno = 0;
       
   226 	pow(-34523543434234LL , -23423432234LL);
       
   227 	TEST2(errno, ERANGE);
       
   228 
       
   229 	errno = 0;
       
   230 	atan(0.0);
       
   231 	TEST2(errno, 0);
       
   232 
       
   233 	errno = 0;
       
   234 	cos(0.0);
       
   235 	TEST2(errno, 0);
       
   236 
       
   237 	errno = 0;
       
   238 	sin(0.5);
       
   239 	TEST2(errno, 0);
       
   240 
       
   241 	errno = 0;
       
   242 	tan(KPi/2);
       
   243 	TEST2(errno, 0);
       
   244 
       
   245 	errno = 0;
       
   246 	acos(0.5);
       
   247 	TEST2(errno, 0);
       
   248 	errno = 0;
       
   249 	acos(HUGE_VAL);
       
   250 	TEST2(errno, EDOM);
       
   251 
       
   252 	errno = 0;
       
   253 	asin(0.5);
       
   254 	TEST2(errno, 0);
       
   255 
       
   256 	errno = 0;
       
   257 	exp(2);
       
   258 	TEST2(errno, 0);
       
   259 	errno = 0;
       
   260 	exp(HUGE_VAL);
       
   261 	TEST2(errno, ERANGE);
       
   262 	errno = 0;
       
   263 	exp(-1 * HUGE_VAL);
       
   264 	TEST2(errno, ERANGE);
       
   265 
       
   266 	errno = 0;
       
   267 	log(2);
       
   268 	TEST2(errno, 0);
       
   269 	errno = 0;
       
   270 	log(-1);
       
   271 	TEST2(errno, EDOM);
       
   272 	errno = 0;
       
   273 	log(0);
       
   274 	TEST2(errno, ERANGE);
       
   275 
       
   276 	errno = 0;
       
   277 	log10(2);
       
   278 	TEST2(errno, 0);
       
   279 	errno = 0;
       
   280 	log10(-1);
       
   281 	TEST2(errno, EDOM);
       
   282 	errno = 0;
       
   283 	log10(0);
       
   284 	TEST2(errno, ERANGE);
       
   285 
       
   286 	errno = 0;
       
   287 	sqrt(2);
       
   288 	TEST2(errno, 0);
       
   289 	errno = 0;
       
   290 	sqrt(-1);
       
   291 	TEST2(errno, EDOM);
       
   292 #ifdef __ARMCC__
       
   293 #pragma diag_suppress 222  //armv5 Warning #222-D is an echo of the following test.  We want to ensure that the test reports the error as well.
       
   294 #else
       
   295 #pragma warning (disable: 222)  //winscw Warning #222-D is an echo of the following test.  We want to ensure that the test reports the error as well.
       
   296 #endif
       
   297 	errno = 0;
       
   298 	sqrt(2.0 * HUGE_VAL);
       
   299 	TEST2(errno, ERANGE);
       
   300 #ifdef __ARMCC__
       
   301 #pragma diag_default 222  //armv5 Warning #222-D turned back on.
       
   302 #else
       
   303 #pragma warning (default: 222)  //winscw Warning #222-D turned back on.
       
   304 #endif
       
   305 
       
   306 	errno = 0;
       
   307 	atan2(2.0, 2.0);
       
   308 	TEST2(errno, 0);
       
   309 #pragma warning (disable: 222)  //Warning #222-D is an echo of the following test.
       
   310 	errno = 0;
       
   311 	atan2(0.0, 0.0);
       
   312 	TEST2(errno, EDOM);
       
   313 #pragma warning (default: 222)  //Warning #222-D is an echo of the following test.
       
   314 
       
   315 	errno = 0;
       
   316 	fmod(2.0, 2.0);
       
   317 	TEST2(errno, 0);
       
   318 	errno = 0;
       
   319 	fmod(1.0, 0.0);
       
   320 	TEST2(errno, EDOM);
       
   321 
       
   322 	errno = 0;
       
   323 	rint(2.129999);
       
   324 	TEST2(errno, 0);
       
   325 
       
   326 	}
       
   327 
       
   328 
       
   329 //
       
   330 //
       
   331 
       
   332 static void MainL()
       
   333     {
       
   334 	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-STDLIB-LEGACY-TMISC3-0001 CTYPE tests "));
       
   335 	::TestCType();
       
   336 
       
   337 	TheTest.Next(_L("MATH tests"));
       
   338 	::TestMath();
       
   339 
       
   340 	TheTest.Next(_L("MATH DEF113884 tests"));
       
   341 	::TestDef113884();
       
   342 
       
   343 	CloseSTDLIB();
       
   344 	}
       
   345 
       
   346 TInt E32Main()
       
   347 	{
       
   348 	__UHEAP_MARK;
       
   349 
       
   350 	CTrapCleanup* tc = CTrapCleanup::New();
       
   351     TEST(tc != NULL);
       
   352 
       
   353 	TheTest.Title();
       
   354     TRAPD(err, ::MainL());
       
   355 	TEST2(err, KErrNone);
       
   356 
       
   357 	TheTest.End();
       
   358 	TheTest.Close();
       
   359 
       
   360 	delete tc;
       
   361 
       
   362 	__UHEAP_MARKEND;
       
   363 
       
   364 	User::Heap().Check();
       
   365 	return KErrNone;
       
   366 	}