os/ossrv/genericopenlibs/cstdlib/TSTLIB/T_SPRINTF.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_SPRINTF.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,146 @@
     1.4 +// Copyright (c) 2006-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 <e32test.h>
    1.20 +#include <e32svr.h>
    1.21 +
    1.22 +#include <stdlib.h>
    1.23 +#include <stdio.h>
    1.24 +#include <string.h>
    1.25 +
    1.26 +//
    1.27 +// Globals
    1.28 +
    1.29 +static RTest TheTest(_L("T_SPRINTF"));
    1.30 +
    1.31 +//
    1.32 +//
    1.33 +//Test macro and function
    1.34 +
    1.35 +static void Check(TInt aValue, TInt aLine)
    1.36 +	{
    1.37 +	if(!aValue)
    1.38 +		{
    1.39 +		TheTest(EFalse, aLine);
    1.40 +		}
    1.41 +	}
    1.42 +
    1.43 +#define TEST(arg) ::Check((arg), __LINE__)
    1.44 +
    1.45 +//
    1.46 +// Tests
    1.47 +
    1.48 +/**
    1.49 +@SYMTestCaseID 			SYSLIB-STDLIB-UT-1670
    1.50 +@SYMTestCaseDesc	    Making sure that error codes are returned from sprintf when when out of memory. 
    1.51 +@SYMTestPriority 	    Critical
    1.52 +@SYMTestActions  	    Sets heap failures to occur at different stages and calls sprintf. The sprintf function should return with error codes
    1.53 +						when errors occur.
    1.54 +@SYMTestExpectedResults The test should not fail.
    1.55 +@SYMDEF 				DEF083988
    1.56 +*/	
    1.57 +void DEF083988()
    1.58 +	{
    1.59 +    TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-UT-1670 DEF083988 - Prop: sprintf panics with ESTLIB-INIT 0 when out of memory "));
    1.60 +	
    1.61 +	int ret;
    1.62 +	char buf[30];
    1.63 +	char* hw = "How are you?";	
    1.64 +	
    1.65 +	#ifdef _DEBUG
    1.66 +	const int err = -1;
    1.67 +	
    1.68 +	__UHEAP_FAILNEXT(1);
    1.69 +    ret = sprintf(buf, "Hello");
    1.70 +	TEST(ret == err);  // Error returned as memory needed to be allocated, but none was spare.	
    1.71 +	__UHEAP_RESET;
    1.72 +	
    1.73 +	__UHEAP_FAILNEXT(2);
    1.74 +    ret = sprintf(buf, "Hello");
    1.75 +	TEST(ret == 5);  // No Error, as memory allocation failure did not affected sprintf.
    1.76 +	TEST(strcmp(buf, "Hello")==0);
    1.77 +	__UHEAP_RESET;
    1.78 +	// Releases all the STDLIB resources so memory for the globals will need to be allocated once more.	
    1.79 +	CloseSTDLIB(); 
    1.80 +	#endif
    1.81 +
    1.82 +	
    1.83 +	#ifdef _DEBUG
    1.84 +	__UHEAP_FAILNEXT(3);
    1.85 +    ret = sprintf(buf, "Hello");
    1.86 +	TEST(ret == err);  // Error returned as memory needed to be allocated, but none was spare.
    1.87 +	__UHEAP_RESET;	
    1.88 +	CloseSTDLIB();
    1.89 +	#endif
    1.90 +		
    1.91 +				
    1.92 +    ret = sprintf(buf, "Hello");
    1.93 +	TEST(strcmp(buf, "Hello") == 0);
    1.94 +	TEST(ret == 5);  // Returns the number of characters printed.
    1.95 +
    1.96 +	ret = sprintf(buf, hw);
    1.97 +	TEST(strcmp(buf, "How are you?") == 0);
    1.98 +	TEST(ret == 12); // Returns the number of characters printed, as the memory is already allocated.
    1.99 +
   1.100 +	#ifdef _DEBUG
   1.101 +	__UHEAP_FAILNEXT(1);	
   1.102 +	ret = sprintf(buf, hw);
   1.103 +	TEST(strcmp(buf, "How are you?") == 0);
   1.104 +	TEST(ret == 12); // Returns the number of characters printed, as the memory is already allocated.
   1.105 +	__UHEAP_RESET;
   1.106 +	#endif
   1.107 +		
   1.108 +	CloseSTDLIB();
   1.109 +		
   1.110 +	ret = sprintf(buf, "How are you?");
   1.111 +	TEST(strcmp(buf, "How are you?") == 0);
   1.112 +	TEST(ret == 12); // Returns the number of characters printed, as the memory is able to be allocated.
   1.113 +	
   1.114 +	CloseSTDLIB();
   1.115 +		
   1.116 +	}
   1.117 +
   1.118 +
   1.119 +
   1.120 +static void Main()
   1.121 +	{
   1.122 +	TheTest.Start(_L("Defect..."));
   1.123 +	DEF083988(); //DEF083988 - Prop: sprintf panics with ESTLIB-INIT 0 when out of memory
   1.124 +	}
   1.125 +
   1.126 +
   1.127 +TInt E32Main()
   1.128 +	{
   1.129 +	__UHEAP_MARK;
   1.130 +
   1.131 +	CTrapCleanup* tc = CTrapCleanup::New();
   1.132 +    TEST(tc != NULL);
   1.133 +    
   1.134 +	CloseSTDLIB();
   1.135 +	TheTest.Title();
   1.136 +	
   1.137 +    ::Main();
   1.138 +	
   1.139 +	TheTest.End();
   1.140 +	TheTest.Close();
   1.141 +
   1.142 +	delete tc;
   1.143 +	CloseSTDLIB();	
   1.144 +
   1.145 +	__UHEAP_MARKEND;
   1.146 +
   1.147 +	User::Heap().Check();
   1.148 +	return KErrNone;
   1.149 +	}