sl@0: // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: // sl@0: // Globals sl@0: sl@0: static RTest TheTest(_L("T_SPRINTF")); sl@0: sl@0: // sl@0: // sl@0: //Test macro and function sl@0: sl@0: static void Check(TInt aValue, TInt aLine) sl@0: { sl@0: if(!aValue) sl@0: { sl@0: TheTest(EFalse, aLine); sl@0: } sl@0: } sl@0: sl@0: #define TEST(arg) ::Check((arg), __LINE__) sl@0: sl@0: // sl@0: // Tests sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-STDLIB-UT-1670 sl@0: @SYMTestCaseDesc Making sure that error codes are returned from sprintf when when out of memory. sl@0: @SYMTestPriority Critical sl@0: @SYMTestActions Sets heap failures to occur at different stages and calls sprintf. The sprintf function should return with error codes sl@0: when errors occur. sl@0: @SYMTestExpectedResults The test should not fail. sl@0: @SYMDEF DEF083988 sl@0: */ sl@0: void DEF083988() sl@0: { sl@0: TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-UT-1670 DEF083988 - Prop: sprintf panics with ESTLIB-INIT 0 when out of memory ")); sl@0: sl@0: int ret; sl@0: char buf[30]; sl@0: char* hw = "How are you?"; sl@0: sl@0: #ifdef _DEBUG sl@0: const int err = -1; sl@0: sl@0: __UHEAP_FAILNEXT(1); sl@0: ret = sprintf(buf, "Hello"); sl@0: TEST(ret == err); // Error returned as memory needed to be allocated, but none was spare. sl@0: __UHEAP_RESET; sl@0: sl@0: __UHEAP_FAILNEXT(2); sl@0: ret = sprintf(buf, "Hello"); sl@0: TEST(ret == 5); // No Error, as memory allocation failure did not affected sprintf. sl@0: TEST(strcmp(buf, "Hello")==0); sl@0: __UHEAP_RESET; sl@0: // Releases all the STDLIB resources so memory for the globals will need to be allocated once more. sl@0: CloseSTDLIB(); sl@0: #endif sl@0: sl@0: sl@0: #ifdef _DEBUG sl@0: __UHEAP_FAILNEXT(3); sl@0: ret = sprintf(buf, "Hello"); sl@0: TEST(ret == err); // Error returned as memory needed to be allocated, but none was spare. sl@0: __UHEAP_RESET; sl@0: CloseSTDLIB(); sl@0: #endif sl@0: sl@0: sl@0: ret = sprintf(buf, "Hello"); sl@0: TEST(strcmp(buf, "Hello") == 0); sl@0: TEST(ret == 5); // Returns the number of characters printed. sl@0: sl@0: ret = sprintf(buf, hw); sl@0: TEST(strcmp(buf, "How are you?") == 0); sl@0: TEST(ret == 12); // Returns the number of characters printed, as the memory is already allocated. sl@0: sl@0: #ifdef _DEBUG sl@0: __UHEAP_FAILNEXT(1); sl@0: ret = sprintf(buf, hw); sl@0: TEST(strcmp(buf, "How are you?") == 0); sl@0: TEST(ret == 12); // Returns the number of characters printed, as the memory is already allocated. sl@0: __UHEAP_RESET; sl@0: #endif sl@0: sl@0: CloseSTDLIB(); sl@0: sl@0: ret = sprintf(buf, "How are you?"); sl@0: TEST(strcmp(buf, "How are you?") == 0); sl@0: TEST(ret == 12); // Returns the number of characters printed, as the memory is able to be allocated. sl@0: sl@0: CloseSTDLIB(); sl@0: sl@0: } sl@0: sl@0: sl@0: sl@0: static void Main() sl@0: { sl@0: TheTest.Start(_L("Defect...")); sl@0: DEF083988(); //DEF083988 - Prop: sprintf panics with ESTLIB-INIT 0 when out of memory sl@0: } sl@0: sl@0: sl@0: TInt E32Main() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: CTrapCleanup* tc = CTrapCleanup::New(); sl@0: TEST(tc != NULL); sl@0: sl@0: CloseSTDLIB(); sl@0: TheTest.Title(); sl@0: sl@0: ::Main(); sl@0: sl@0: TheTest.End(); sl@0: TheTest.Close(); sl@0: sl@0: delete tc; sl@0: CloseSTDLIB(); sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: User::Heap().Check(); sl@0: return KErrNone; sl@0: }