Update contrib.
1 // Copyright (c) 2008-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 the License "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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #define __E32TEST_EXTENSION__
19 #include <e32def_private.h>
21 #include "t_new_classes.h"
23 RTest test(_L("Testing Operator New"));
25 TInt RNewLddTest::DoControl(TInt aFunction)
27 return RBusLogicalChannel::DoControl(aFunction);
30 TInt RNewLddTest::Open()
32 return DoCreate(KLddName,TVersion(0,1,1),KNullUnit,NULL,NULL);
39 return lddconn.DoControl(RNewLddTest::ENew);
41 TInt TestPlacementVectorNew()
43 return lddconn.DoControl(RNewLddTest::EPlacementVectorNew);
47 return lddconn.DoControl(RNewLddTest::EVectorNew);
49 TInt TestPlacementNew()
51 return lddconn.DoControl(RNewLddTest::EPlacementNew);
54 void UserSideTestNewOOM()
56 RDebug::Printf("User-Side: operator new OOM");
57 //OOM tests: should not throw
59 #define TEST_NEW_OOM(CLASS) \
60 RDebug::Printf("new " #CLASS);\
62 CLASS* p##CLASS = new CLASS;\
63 test_Equal(NULL, p##CLASS);\
66 TEST_NEW_OOM(XVeryLargeClassCtorAndDtor);
67 TEST_NEW_OOM(XVeryLargeClassCtorOnly);
68 TEST_NEW_OOM(XVeryLargeClassDtorOnly);
69 TEST_NEW_OOM(XVeryLargeClassNoTors);
72 void UserSideTestNewConstruction()
74 RDebug::Printf("User-Side: operator new non-OOM");
78 #define TEST_NEW_CONSTRUCTION(CLASS) \
79 RDebug::Printf("new " #CLASS);\
81 CLASS* p##CLASS = new CLASS;\
82 test_NotNull(p##CLASS);\
83 test_Equal(EConstructed, (p##CLASS)->iState);\
87 TEST_NEW_CONSTRUCTION(XCtorAndDtor)
88 TEST_NEW_CONSTRUCTION(XCtorOnly)
91 #define TEST_NEW(CLASS) \
92 RDebug::Printf("new " #CLASS);\
94 CLASS* p##CLASS = new CLASS;\
95 test_NotNull(p##CLASS);\
102 void UserSideTestNew()
104 UserSideTestNewOOM();
105 UserSideTestNewConstruction();
108 void TrappedUserSideTestNew()
110 RDebug::Printf("TRAPPED User-Side: operator new");
111 //OOM tests: should not throw
113 #define TEST_NEW_ELEAVE_OOM(CLASS) \
114 RDebug::Printf("new(ELeave) " #CLASS);\
116 TRAPD(r, new(ELeave) (CLASS));\
117 test_Equal(KErrNoMemory, r);\
120 TEST_NEW_ELEAVE_OOM(XVeryLargeClassCtorAndDtor);
121 TEST_NEW_ELEAVE_OOM(XVeryLargeClassCtorOnly);
122 TEST_NEW_ELEAVE_OOM(XVeryLargeClassDtorOnly);
123 TEST_NEW_ELEAVE_OOM(XVeryLargeClassNoTors);
127 RDebug::Printf("User-Side: operator new non-OOM");
130 #define TEST_NEW_ELEAVE(CLASS, TEST_CTOR ) \
131 RDebug::Printf("new(ELeave) " #CLASS);\
133 CLASS* p##CLASS=NULL;\
134 TRAPD(r, p##CLASS = new(ELeave) (CLASS));\
136 volatile TBool testCtor=(TEST_CTOR);\
139 test_Equal(EConstructed, (p##CLASS)->iState);\
144 TEST_NEW_ELEAVE(XCtorAndDtor, ETrue);
145 TEST_NEW_ELEAVE(XCtorOnly, ETrue);
146 TEST_NEW_ELEAVE(XDtorOnly, EFalse);
147 TEST_NEW_ELEAVE(XNoTors, EFalse);
151 #define TEST_ARRAY_CONSTRUCTION(ARRAY, LENGTH)\
153 for(TInt i=0; i<(LENGTH); ++i)\
155 test_Equal(EConstructed, ARRAY[i].iState);\
159 void UserSideTestVectorNew()
161 RDebug::Printf("User-Side:vector operator new");
162 RDebug::Printf("OOM tests");
164 #define TEST_VEC_NEW_OOM(CLASS) \
165 RDebug::Printf("new " #CLASS "[%d]", KOOMArraySize );\
167 CLASS* p##CLASS = new CLASS[KOOMArraySize];\
168 test_Equal(NULL, p##CLASS);\
171 TEST_VEC_NEW_OOM(XCtorAndDtor);
172 TEST_VEC_NEW_OOM(XCtorOnly);
173 TEST_VEC_NEW_OOM(XDtorOnly);
174 TEST_VEC_NEW_OOM(XNoTors);
176 RDebug::Printf("non-OOM tests");
178 #define TEST_VEC_NEW(CLASS, ARRAY_LENGTH, TEST_CTOR) \
179 RDebug::Printf("new " #CLASS "[%d]", ARRAY_LENGTH);\
181 CLASS* p##CLASS = new CLASS[(ARRAY_LENGTH)];\
182 test_NotNull(p##CLASS);\
183 volatile TBool testCtor=(TEST_CTOR);\
186 TEST_ARRAY_CONSTRUCTION(p##CLASS, ARRAY_LENGTH);\
191 TEST_VEC_NEW(XCtorAndDtor, KTestArrayLength, ETrue);
192 TEST_VEC_NEW(XCtorOnly, KTestArrayLength, ETrue);
193 TEST_VEC_NEW(XDtorOnly, KTestArrayLength, EFalse);
194 TEST_VEC_NEW(XNoTors, KTestArrayLength, EFalse);
197 void TrappedUserSideTestVectorNew()
199 RDebug::Printf("User-Side:vector operator new");
200 RDebug::Printf("OOM tests");
202 #define TEST_VEC_NEW_ELEAVE_OOM(CLASS) \
203 RDebug::Printf("new(ELeave) " #CLASS "[%d]", KOOMArraySize );\
205 TRAPD(r, new(ELeave) CLASS[KOOMArraySize];)\
206 test_Equal(KErrNoMemory, r);\
209 TEST_VEC_NEW_ELEAVE_OOM(XCtorAndDtor);
210 TEST_VEC_NEW_ELEAVE_OOM(XCtorOnly);
211 TEST_VEC_NEW_ELEAVE_OOM(XDtorOnly);
212 TEST_VEC_NEW_ELEAVE_OOM(XNoTors);
216 RDebug::Printf("non-OOM tests");
217 #define TEST_VEC_NEW_ELEAVE(CLASS, ARRAY_LENGTH, TEST_CTOR) \
218 RDebug::Printf("new(ELeave) " #CLASS "[%d]", ARRAY_LENGTH);\
220 CLASS* p##CLASS = NULL;\
221 TRAPD(r, p##CLASS = new(ELeave) CLASS[(ARRAY_LENGTH)]);\
223 TBool testCtor=(TEST_CTOR);\
226 TEST_ARRAY_CONSTRUCTION(p##CLASS, ARRAY_LENGTH);\
231 TEST_VEC_NEW_ELEAVE(XCtorAndDtor, KTestArrayLength, ETrue);
232 TEST_VEC_NEW_ELEAVE(XCtorOnly, KTestArrayLength, ETrue);
233 TEST_VEC_NEW_ELEAVE(XDtorOnly, KTestArrayLength, EFalse);
234 TEST_VEC_NEW_ELEAVE(XNoTors, KTestArrayLength, EFalse);
237 void UserSideTestPlacementNew()
239 RDebug::Printf("::UserSideTestPlacementNew");
241 #define TEST_PLACMENT_NEW(CLASS, POST_CTOR_STATE, POST_DTOR_STATE)\
243 void* someram = User::AllocZ(sizeof(CLASS));\
244 test_NotNull(someram);\
245 RDebug::Printf("new (someram) " #CLASS);\
246 CLASS* p##CLASS = new (someram) CLASS;\
247 test_Equal(someram, p##CLASS);\
248 test_Equal(POST_CTOR_STATE, p##CLASS->iState);\
250 test_Equal(POST_DTOR_STATE, p##CLASS->iState);\
251 User::Free(someram);\
255 TEST_PLACMENT_NEW(XCtorAndDtor, EConstructed, EDeconstructed);
256 TEST_PLACMENT_NEW(XCtorOnly, EConstructed, EConstructed);
257 TEST_PLACMENT_NEW(XDtorOnly, ENull, EDeconstructed);
258 TEST_PLACMENT_NEW(XNoTors, ENull, ENull);
262 void UserSideTestPlacementVectorNew()
266 RDebug::Printf("::UserSideTestPlacementVectorNew");
268 #define TEST_VEC_PLACEMENT_NEW(CLASS, ARRAY_LENGTH, POST_CTOR_STATE, POST_DTOR_STATE)\
269 RDebug::Printf("new(someram) " #CLASS "[%d]", ARRAY_LENGTH);\
271 void* someram = User::AllocZ(sizeof(CLASS) * ARRAY_LENGTH);\
272 test_NotNull(someram);\
273 CLASS* p##CLASS = new (someram) CLASS[(ARRAY_LENGTH)];\
274 for(TInt i=0; i<(ARRAY_LENGTH); ++i)\
276 test_Equal(POST_CTOR_STATE, p##CLASS[i].iState);\
277 p##CLASS[i].~CLASS();\
278 test_Equal(POST_DTOR_STATE, p##CLASS[i].iState);\
280 User::Free(someram);\
283 TEST_VEC_PLACEMENT_NEW(XCtorAndDtor, KTestArrayLength, EConstructed, EDeconstructed);
284 TEST_VEC_PLACEMENT_NEW(XCtorOnly, KTestArrayLength, EConstructed, EConstructed);
285 TEST_VEC_PLACEMENT_NEW(XDtorOnly, KTestArrayLength, ENull, EDeconstructed);
286 TEST_VEC_PLACEMENT_NEW(XNoTors, KTestArrayLength, ENull, ENull);
296 test.Start(_L("Testing operator new"));
298 test.Next(_L("Installing LDD"));
299 TInt r=User::LoadLogicalDevice(KKInstallLddName);
300 test(r==KErrNone || r==KErrAlreadyExists);
304 #define TEST_THIS(X) test.Next(_L(#X)); (X)
306 TEST_THIS(UserSideTestNew());
307 TEST_THIS(UserSideTestPlacementNew());
309 // Workaround for bug in MSVC6/CW compilers. The following test case fails on
310 // WINS, WINSCW and X86 targets (not X86GCC), where the placement vector new
311 // operator does not behave as expected, ultimately resulting in heap corruption
312 // when the parameter to placement array new is passed on the stack.
313 #if !(defined(__WINS__) || (defined(__VC32__) && (_MSC_VER < 1300)))
314 TEST_THIS(UserSideTestPlacementVectorNew());
316 test.Next(_L("Emulator and/or VC32 - Skipped: UserSideTestPlacementVectorNew"));
319 TEST_THIS(UserSideTestVectorNew());
320 TEST_THIS(TrappedUserSideTestNew());
321 TEST_THIS(TrappedUserSideTestVectorNew());
326 test.Next(_L("Kernel-side:Normal operator new"));
330 test.Next(_L("Kernel-side:Placement operator new"));
331 r = TestPlacementNew();
334 test.Next(_L("Kernel-side:Placement Vector operator new"));
335 r = TestPlacementVectorNew();
338 test.Next(_L("Kernel-side:Vector operator new"));
342 r = RTest::CloseHandleAndWaitForDestruction(lddconn);