os/ossrv/genericopenlibs/cstdlib/TSTLIB/T_StdlibDefect.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/T_StdlibDefect.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,392 @@
     1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <string.h>
    1.20 +#include <e32test.h> //this includes e32cmn.h
    1.21 +#include <e32debug.h>
    1.22 +
    1.23 +#include <sys/types.h>
    1.24 +#include <sys/socket.h>
    1.25 +#include <sys/ioctl.h>
    1.26 +#include <libc/netinet/in.h>
    1.27 +#include <libc/arpa/inet.h>
    1.28 +#include <limits.h>
    1.29 +
    1.30 +#include <sys/errno.h>
    1.31 +#include <stdlib.h>
    1.32 +#include <math.h>
    1.33 +
    1.34 +LOCAL_D RTest				TheTest (_L("T_StdlibDefect"));
    1.35 +//
    1.36 +//
    1.37 +//Test macroses and functions
    1.38 +
    1.39 +static void Check(TInt aValue, TInt aLine)
    1.40 +	{
    1.41 +	if(!aValue)
    1.42 +		{
    1.43 +		TheTest(EFalse, aLine);
    1.44 +		}
    1.45 +	}
    1.46 +static  void Check(TInt aValue, TInt aExpected, TInt aLine)
    1.47 +	{
    1.48 +	if(aValue != aExpected)
    1.49 +		{
    1.50 +		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
    1.51 +		TheTest(EFalse, aLine);
    1.52 +		}
    1.53 +	}
    1.54 +#define TEST(arg) ::Check((arg), __LINE__)
    1.55 +#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__) 
    1.56 +
    1.57 +
    1.58 +
    1.59 +const TInt KTestDataLen = 10000;
    1.60 +const TInt KTestIterations = 1000;
    1.61 +
    1.62 +#ifdef __ARMCC__
    1.63 +const TInt KMinCharARMCC = 0;
    1.64 +const TInt KMaxCharARMCC = 255;
    1.65 +#else
    1.66 +const TInt KMinCharNoARMCC = -128;
    1.67 +const TInt KMaxCharNoARMCC = 127;
    1.68 +#endif
    1.69 +
    1.70 +/**
    1.71 +DEF062679  : memcpy in stdlib is slow
    1.72 +*/ 
    1.73 +void Defect_DEF062679_memcpy()
    1.74 +	{
    1.75 +
    1.76 +	TUint8* src1 = new TUint8[KTestDataLen];
    1.77 +	TEST(src1 != NULL);
    1.78 +	Mem::Fill(src1, KTestDataLen, 'A');
    1.79 +	
    1.80 +	TUint8* dst1 = new TUint8[KTestDataLen];
    1.81 +	TEST(dst1 != NULL);
    1.82 +
    1.83 +	TTime startTime, stopTime;
    1.84 +	TInt i;
    1.85 +	//Loop to check time spent using Mem::Copy
    1.86 +	startTime.UniversalTime();
    1.87 +	for(i=0; i<KTestIterations ; i++)
    1.88 +		Mem::Copy(dst1, src1, KTestDataLen );
    1.89 +	stopTime.UniversalTime();
    1.90 +	TTimeIntervalMicroSeconds timeTaken = stopTime.MicroSecondsFrom(startTime);
    1.91 +	TheTest.Printf(_L("Time taken using Mem::Copy :%d microseconds\n"), timeTaken.Int64() );
    1.92 +	
    1.93 +	//Loop to check the time spent using memcpy after the change
    1.94 +	startTime.UniversalTime();
    1.95 +	for(i=0; i<KTestIterations ; i++)
    1.96 +		memcpy(dst1, src1, KTestDataLen );
    1.97 +	stopTime.UniversalTime();
    1.98 +	TTimeIntervalMicroSeconds timeTaken2 = stopTime.MicroSecondsFrom(startTime);
    1.99 +	TheTest.Printf(_L("Time taken using  memcpy: %d microseconds\n"), timeTaken2.Int64() );
   1.100 +	TheTest.Printf(_L("Time taken using  memcpy before the change about 613125 microseconds\n"));
   1.101 +
   1.102 +	//Test memcpy works fine
   1.103 +	for(i=0; i<KTestIterations ; i++)
   1.104 +		TEST(dst1[i] == src1[i]);
   1.105 +
   1.106 +	delete [] src1;
   1.107 +	delete [] dst1;
   1.108 +	
   1.109 +	}
   1.110 +	
   1.111 +	
   1.112 +	
   1.113 +	
   1.114 +/**
   1.115 +DEF062679  : memcpy in stdlib is slow
   1.116 +*/ 
   1.117 +void Defect_DEF062679_memcmp()
   1.118 +	{
   1.119 +
   1.120 +	TUint8* str1 = new TUint8[KTestDataLen];
   1.121 +	TEST(str1 != NULL);
   1.122 +	Mem::Fill(str1, KTestDataLen, 'A');
   1.123 +	
   1.124 +	TUint8* str2 = new TUint8[KTestDataLen];
   1.125 +	TEST(str2 != NULL);
   1.126 +	Mem::Fill(str2, KTestDataLen, 'A');
   1.127 +	
   1.128 +	TTime startTime, stopTime;
   1.129 +	
   1.130 +	//Loop to check the time using Mem::Copy
   1.131 +	startTime.UniversalTime();
   1.132 +	TInt i,ret;
   1.133 +	for(i=0; i<KTestIterations ; i++)
   1.134 +		ret = Mem::Compare(str2,KTestDataLen, str1, KTestDataLen );
   1.135 +	stopTime.UniversalTime();
   1.136 +	TTimeIntervalMicroSeconds timeTaken = stopTime.MicroSecondsFrom(startTime);
   1.137 +	TheTest.Printf(_L("Time taken using Mem::Compare :%ld microseconds\n"), timeTaken.Int64() );
   1.138 +	
   1.139 +	//Loop to check the time spent using memcpy after the change
   1.140 +	startTime.UniversalTime();
   1.141 +	
   1.142 +	for(i=0; i<KTestIterations ; i++)
   1.143 +		ret = memcmp(str2,str1, KTestDataLen );
   1.144 +	TEST(ret==0); //check that memcmp works fine	
   1.145 +	stopTime.UniversalTime();
   1.146 +	TTimeIntervalMicroSeconds timeTaken2 = stopTime.MicroSecondsFrom(startTime);
   1.147 +	TheTest.Printf(_L("Time taken using  memcmp: %ld microseconds\n"), timeTaken2.Int64() );
   1.148 +	
   1.149 +	TheTest.Printf(_L("Time taken using  memcmp before changes 1007000 microseconds\n"));
   1.150 +	
   1.151 +
   1.152 +	//Test memcmp works fine
   1.153 +	
   1.154 +	TUint8* str3 = new TUint8[KTestDataLen];
   1.155 +	TEST(str3 != NULL);
   1.156 +	Mem::Fill(str3, KTestDataLen, 'B');
   1.157 +	ret = memcmp(str3, str1, KTestDataLen);
   1.158 +	TEST(ret>0);
   1.159 +	ret = memcmp(str1, str3, KTestDataLen);
   1.160 +	TEST(ret<0);
   1.161 +	
   1.162 +	delete str1;
   1.163 +	delete str2;
   1.164 +	delete str3;
   1.165 +	}	
   1.166 +
   1.167 +/**
   1.168 +INC073740: inet_addr and inet_aton returns wrong results with invalid input on STDLIB
   1.169 +*/ 
   1.170 +void Defect_INC073740()
   1.171 +	{
   1.172 +	TheTest.Next(_L("INC073740: inet_addr and inet_aton returns wrong results with invalid input on STDLIB"));
   1.173 +	
   1.174 +	int err;
   1.175 +	struct in_addr iaddr;	
   1.176 +	char* good_addr="16.33.50.67";
   1.177 +	char* bad_addr="256.33.50.67";
   1.178 +	char* worse_addr="16.1456.50.67";
   1.179 +	char* worst_addr="16.33.333333.67";
   1.180 +	
   1.181 +	err=inet_aton(good_addr, &iaddr);
   1.182 +	TEST2(err, 1);
   1.183 +
   1.184 +	err=inet_aton(bad_addr, &iaddr);
   1.185 +	TEST2(err, 0);
   1.186 +
   1.187 +	err=inet_aton(worse_addr, &iaddr);
   1.188 +	TEST2(err, 0);
   1.189 +
   1.190 +	err=inet_aton(worst_addr, &iaddr);
   1.191 +	TEST2(err, 0);
   1.192 +	}
   1.193 +
   1.194 +/**
   1.195 +@SYMTestCaseID         	SYSLIB-STDLIB-CT-1863
   1.196 +@SYMTestCaseDesc        Tests for minimum and maximum values type "char" with ARMCC macro
   1.197 +@SYMTestPriority        High
   1.198 +@SYMTestActions         Tests for checking minimum and maximum values for a variable of type "char"
   1.199 +@SYMTestExpectedResults Tests must not fail
   1.200 +@SYMDEF                 PDEF091928
   1.201 +*/ 
   1.202 +void Defect_PDEF091928()
   1.203 +	{
   1.204 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-1863 PDEF091928: limits.h not correct for ARM RVCT compiler "));
   1.205 +	
   1.206 +	char charmn=CHAR_MIN;
   1.207 +	char charmx=CHAR_MAX;
   1.208 +	
   1.209 +#ifdef __ARMCC__
   1.210 +	
   1.211 +	TEST2(charmn, KMinCharARMCC);
   1.212 +	TEST2(charmx, KMaxCharARMCC);
   1.213 +	
   1.214 +#else
   1.215 +
   1.216 +	TEST2(charmn, KMinCharNoARMCC);
   1.217 +	TEST2(charmx, KMaxCharNoARMCC);
   1.218 +
   1.219 +#endif
   1.220 +	}
   1.221 +
   1.222 +/**
   1.223 +@SYMTestCaseID         	SYSLIB-STDLIB-UT-3612
   1.224 +@SYMTestCaseDesc        Tests for __errno().
   1.225 +@SYMTestPriority        Normal
   1.226 +@SYMTestActions         Tests for __errno(). Test whether it is correctly exported and functioning 
   1.227 +						as expected after being declared with IMPORT_C. 
   1.228 +@SYMTestExpectedResults Tests must not fail
   1.229 +@SYMDEF                 DEF110593
   1.230 +*/ 
   1.231 +void Defect_DEF110593()
   1.232 +	{
   1.233 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-UT-3612 DEF110593: IMPORT_C/EXPORT_C: ERRNO.H "));
   1.234 +
   1.235 +	// randomly selected 24 macros from errno.h
   1.236 +	const int errMacros[24] =
   1.237 +		{
   1.238 +		EPERM,		ENOENT,		ESRCH,		EINTR,		EIO,
   1.239 +		EBADF,		ENOMEM,		EFAULT,		EBUSY,		ENOTDIR,
   1.240 +		ENFILE,		ESPIPE,		ERANGE,		ENOMSG,		EDEADLK,
   1.241 +		ENOLCK,		ENOTSOCK,	ENOLINK,	EBADMSG,	ENOTUNIQ,
   1.242 +		EBADFD,		ENOSYS,		EILSEQ, 	__ELASTERROR
   1.243 +		};
   1.244 +	
   1.245 +	// Step 1: setting errno using assign "=", test value returned by errno
   1.246 +	errno = 0;
   1.247 +	TEST(errno == 0);
   1.248 +	
   1.249 +	int i;
   1.250 +	for (i = 0; i < 24; i++)
   1.251 +		{
   1.252 +		errno = errMacros[i];
   1.253 +		TEST(errno == errMacros[i]);
   1.254 +		}
   1.255 +	
   1.256 +	// Step 2: setting errno using the library globals struct, test value returned by errno
   1.257 +	struct _reent *r = _REENT2;
   1.258 +	r->_errno = 0;
   1.259 +	TEST(errno == 0);
   1.260 +	
   1.261 +	for (i = 0; i < 24; i++)
   1.262 +		{
   1.263 +		r->_errno = errMacros[i];
   1.264 +		TEST(errno == errMacros[i]);
   1.265 +		}
   1.266 +	
   1.267 +	r->_errno = 0;
   1.268 +	TEST(errno == 0);
   1.269 +	
   1.270 +	// Step3: Test errno by using other C function in STDLIB
   1.271 +	// Test using ldexp(double value, int exp);
   1.272 +	// Giving val a huge number to make res overflow. errno should return ERANGE
   1.273 +	double val = 1.5E+308;
   1.274 +	int exp = 10;
   1.275 +	ldexp(val, exp);
   1.276 +	TEST(errno == ERANGE);
   1.277 +		
   1.278 +	//finish
   1.279 +	CloseSTDLIB();	
   1.280 +	}
   1.281 +
   1.282 +/**
   1.283 +@SYMTestCaseID          SYSLIB-STDLIB-CT-4001
   1.284 +@SYMTestCaseDesc        Test strtoul() with a string whose first character is '-' or '+'. 
   1.285 +@SYMTestPriority        3. Medium
   1.286 +@SYMTestActions         Test strtoul() with a string whose first character is '-' or '+'. 
   1.287 +@SYMTestExpectedResults Tests must not fail
   1.288 +@SYMDEF                 PDEF114447 
   1.289 +*/ 
   1.290 +void Defect_PDEF114447 ()
   1.291 +    {
   1.292 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-4001 PDEF114447 : add the processing for '-' and '+' in a string passed to strtoul() "));
   1.293 +    
   1.294 +    unsigned long result;
   1.295 +    
   1.296 +    result = strtoul("+80", NULL, 10);    
   1.297 +    TEST2(result, 80);
   1.298 +
   1.299 +    result = strtoul("-80", NULL, 10);    
   1.300 +    TEST2(result, -80);    
   1.301 +    }
   1.302 +
   1.303 +/**
   1.304 +Calls ImpurePtr2() to allocate memory for the library globals struct then calls CloseSTDLIB() 
   1.305 +to release it.
   1.306 +Leaves if system-wide error occurs.
   1.307 +*/
   1.308 +LOCAL_C void TestImpurePtrL()
   1.309 +{
   1.310 +	struct _reent * p = ImpurePtr2();
   1.311 +	User::LeaveIfNull(p);
   1.312 +	CloseSTDLIB();
   1.313 +}
   1.314 +
   1.315 +/**
   1.316 +@SYMTestCaseID         	SYSLIB-STDLIB-UT-4002
   1.317 +@SYMTestCaseDesc        Test checks the constructor of CLocalSystemInterface does not panics in OOM test
   1.318 +						or when error KErrNoMemory occurs.
   1.319 +@SYMTestPriority        Normal
   1.320 +@SYMTestActions         In an OOM test, repeats calling ImpurePtr2(), which creates CLocalSystemInterface instance. 
   1.321 +@SYMTestExpectedResults The test program should not panic or fail.
   1.322 +@SYMDEF                 DEF114383
   1.323 +*/ 
   1.324 +LOCAL_C void Defect_DEF114383()
   1.325 +	{
   1.326 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-UT-4002 DEF114383: STDLIB, CLocalSystemInterface::CLocalSystemInterface() panics in OOM "));		
   1.327 +	
   1.328 +	TInt err=KErrNone;
   1.329 +	TInt tryCount = 0;
   1.330 +	do
   1.331 +		{
   1.332 +		__UHEAP_MARK;
   1.333 +
   1.334 +		__UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
   1.335 +		TRAP(err, TestImpurePtrL());
   1.336 +		__UHEAP_SETFAIL(RHeap::ENone, 0);
   1.337 +		
   1.338 +		if (err!=KErrNoMemory)
   1.339 +			TEST(err == KErrNone);
   1.340 +		
   1.341 +		__UHEAP_MARKEND;
   1.342 +		} while(err == KErrNoMemory);
   1.343 +		
   1.344 +	TEST(err == KErrNone);
   1.345 +		
   1.346 +	TheTest.Printf(_L("- ImpurePtr2() succeeded at heap failure rate of %i\n"), tryCount);
   1.347 +	} 
   1.348 +
   1.349 +/**
   1.350 +Invoke the tests
   1.351 +*/
   1.352 +LOCAL_C void RunTestsL ()
   1.353 +    {
   1.354 +	Defect_DEF062679_memcpy();
   1.355 +	Defect_DEF062679_memcmp();
   1.356 +
   1.357 +	Defect_INC073740();
   1.358 +	
   1.359 +	Defect_PDEF091928();
   1.360 +	
   1.361 +	Defect_DEF110593(); 
   1.362 +	
   1.363 +	Defect_PDEF114447();
   1.364 +
   1.365 +	Defect_DEF114383();
   1.366 +	}
   1.367 +
   1.368 +
   1.369 +
   1.370 +GLDEF_C TInt E32Main()
   1.371 + {		
   1.372 + 		
   1.373 +	CTrapCleanup* tc = CTrapCleanup::New();
   1.374 +	TEST(tc != NULL);
   1.375 +	
   1.376 +	__UHEAP_MARK;
   1.377 +
   1.378 +	TheTest.Title();
   1.379 +	TheTest.Start (_L("Defect Tests"));
   1.380 +	TInt err;
   1.381 +	TRAP(err, ::RunTestsL())
   1.382 +	TEST2(err, KErrNone);
   1.383 +
   1.384 +	TheTest.End();
   1.385 +	TheTest.Close();
   1.386 +
   1.387 +	delete tc;
   1.388 +	__UHEAP_MARKEND;
   1.389 +	
   1.390 +	
   1.391 +	
   1.392 +	return(KErrNone);
   1.393 +	
   1.394 +	}
   1.395 +