os/ossrv/genericopenlibs/cstdlib/TSTLIB/T_SPRINTF.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
 
sl@0
    16
#include <e32test.h>
sl@0
    17
#include <e32svr.h>
sl@0
    18
sl@0
    19
#include <stdlib.h>
sl@0
    20
#include <stdio.h>
sl@0
    21
#include <string.h>
sl@0
    22
sl@0
    23
//
sl@0
    24
// Globals
sl@0
    25
sl@0
    26
static RTest TheTest(_L("T_SPRINTF"));
sl@0
    27
sl@0
    28
//
sl@0
    29
//
sl@0
    30
//Test macro and function
sl@0
    31
sl@0
    32
static void Check(TInt aValue, TInt aLine)
sl@0
    33
	{
sl@0
    34
	if(!aValue)
sl@0
    35
		{
sl@0
    36
		TheTest(EFalse, aLine);
sl@0
    37
		}
sl@0
    38
	}
sl@0
    39
sl@0
    40
#define TEST(arg) ::Check((arg), __LINE__)
sl@0
    41
sl@0
    42
//
sl@0
    43
// Tests
sl@0
    44
sl@0
    45
/**
sl@0
    46
@SYMTestCaseID 			SYSLIB-STDLIB-UT-1670
sl@0
    47
@SYMTestCaseDesc	    Making sure that error codes are returned from sprintf when when out of memory. 
sl@0
    48
@SYMTestPriority 	    Critical
sl@0
    49
@SYMTestActions  	    Sets heap failures to occur at different stages and calls sprintf. The sprintf function should return with error codes
sl@0
    50
						when errors occur.
sl@0
    51
@SYMTestExpectedResults The test should not fail.
sl@0
    52
@SYMDEF 				DEF083988
sl@0
    53
*/	
sl@0
    54
void DEF083988()
sl@0
    55
	{
sl@0
    56
    TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-UT-1670 DEF083988 - Prop: sprintf panics with ESTLIB-INIT 0 when out of memory "));
sl@0
    57
	
sl@0
    58
	int ret;
sl@0
    59
	char buf[30];
sl@0
    60
	char* hw = "How are you?";	
sl@0
    61
	
sl@0
    62
	#ifdef _DEBUG
sl@0
    63
	const int err = -1;
sl@0
    64
	
sl@0
    65
	__UHEAP_FAILNEXT(1);
sl@0
    66
    ret = sprintf(buf, "Hello");
sl@0
    67
	TEST(ret == err);  // Error returned as memory needed to be allocated, but none was spare.	
sl@0
    68
	__UHEAP_RESET;
sl@0
    69
	
sl@0
    70
	__UHEAP_FAILNEXT(2);
sl@0
    71
    ret = sprintf(buf, "Hello");
sl@0
    72
	TEST(ret == 5);  // No Error, as memory allocation failure did not affected sprintf.
sl@0
    73
	TEST(strcmp(buf, "Hello")==0);
sl@0
    74
	__UHEAP_RESET;
sl@0
    75
	// Releases all the STDLIB resources so memory for the globals will need to be allocated once more.	
sl@0
    76
	CloseSTDLIB(); 
sl@0
    77
	#endif
sl@0
    78
sl@0
    79
	
sl@0
    80
	#ifdef _DEBUG
sl@0
    81
	__UHEAP_FAILNEXT(3);
sl@0
    82
    ret = sprintf(buf, "Hello");
sl@0
    83
	TEST(ret == err);  // Error returned as memory needed to be allocated, but none was spare.
sl@0
    84
	__UHEAP_RESET;	
sl@0
    85
	CloseSTDLIB();
sl@0
    86
	#endif
sl@0
    87
		
sl@0
    88
				
sl@0
    89
    ret = sprintf(buf, "Hello");
sl@0
    90
	TEST(strcmp(buf, "Hello") == 0);
sl@0
    91
	TEST(ret == 5);  // Returns the number of characters printed.
sl@0
    92
sl@0
    93
	ret = sprintf(buf, hw);
sl@0
    94
	TEST(strcmp(buf, "How are you?") == 0);
sl@0
    95
	TEST(ret == 12); // Returns the number of characters printed, as the memory is already allocated.
sl@0
    96
sl@0
    97
	#ifdef _DEBUG
sl@0
    98
	__UHEAP_FAILNEXT(1);	
sl@0
    99
	ret = sprintf(buf, hw);
sl@0
   100
	TEST(strcmp(buf, "How are you?") == 0);
sl@0
   101
	TEST(ret == 12); // Returns the number of characters printed, as the memory is already allocated.
sl@0
   102
	__UHEAP_RESET;
sl@0
   103
	#endif
sl@0
   104
		
sl@0
   105
	CloseSTDLIB();
sl@0
   106
		
sl@0
   107
	ret = sprintf(buf, "How are you?");
sl@0
   108
	TEST(strcmp(buf, "How are you?") == 0);
sl@0
   109
	TEST(ret == 12); // Returns the number of characters printed, as the memory is able to be allocated.
sl@0
   110
	
sl@0
   111
	CloseSTDLIB();
sl@0
   112
		
sl@0
   113
	}
sl@0
   114
sl@0
   115
sl@0
   116
sl@0
   117
static void Main()
sl@0
   118
	{
sl@0
   119
	TheTest.Start(_L("Defect..."));
sl@0
   120
	DEF083988(); //DEF083988 - Prop: sprintf panics with ESTLIB-INIT 0 when out of memory
sl@0
   121
	}
sl@0
   122
sl@0
   123
sl@0
   124
TInt E32Main()
sl@0
   125
	{
sl@0
   126
	__UHEAP_MARK;
sl@0
   127
sl@0
   128
	CTrapCleanup* tc = CTrapCleanup::New();
sl@0
   129
    TEST(tc != NULL);
sl@0
   130
    
sl@0
   131
	CloseSTDLIB();
sl@0
   132
	TheTest.Title();
sl@0
   133
	
sl@0
   134
    ::Main();
sl@0
   135
	
sl@0
   136
	TheTest.End();
sl@0
   137
	TheTest.Close();
sl@0
   138
sl@0
   139
	delete tc;
sl@0
   140
	CloseSTDLIB();	
sl@0
   141
sl@0
   142
	__UHEAP_MARKEND;
sl@0
   143
sl@0
   144
	User::Heap().Check();
sl@0
   145
	return KErrNone;
sl@0
   146
	}