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