First public contribution.
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 #include <kernel/kern_priv.h>
20 TInt DOperatorNewTest::Request(TInt aReqNo, TAny* /*a1*/, TAny* /*a2*/)
25 case RNewLddTest::ENew:
30 case RNewLddTest::EPlacementVectorNew:
32 return TestPlacementVectorNew();
35 case RNewLddTest::EVectorNew:
37 return TestVectorNew();
39 case RNewLddTest::EPlacementNew:
41 return TestPlacementNew();
49 TInt DOperatorNewTest::TestNew()
51 Kern::Printf("::TestNew()");
52 NKern::ThreadEnterCS();
54 #define TEST_KERN_NEW_OOM(CLASS)\
56 Kern::Printf("new " #CLASS);\
57 CLASS* p##CLASS=NULL;\
58 XTRAPD(r,XT_DEFAULT, p##CLASS = new CLASS);\
61 NKern::ThreadLeaveCS();\
67 NKern::ThreadLeaveCS();\
72 //OOM tests: should(must) not throw
73 Kern::Printf("OOM Tests;");
75 TEST_KERN_NEW_OOM(XVeryLargeClassCtorAndDtor);
76 TEST_KERN_NEW_OOM(XVeryLargeClassCtorOnly);
77 TEST_KERN_NEW_OOM(XVeryLargeClassDtorOnly);
78 TEST_KERN_NEW_OOM(XVeryLargeClassNoTors);
80 Kern::Printf("non-OOM Tests;");
83 #define TEST_KERN_NEW(CLASS, TEST_CTOR)\
85 Kern::Printf("new " #CLASS);\
86 CLASS* p##CLASS=NULL;\
87 XTRAPD(r,XT_DEFAULT, p##CLASS = new CLASS);\
90 NKern::ThreadLeaveCS();\
95 NKern::ThreadLeaveCS();\
98 volatile TBool testCtor=(TEST_CTOR);\
99 if(testCtor && (p##CLASS->iState!=EConstructed) )\
109 TEST_KERN_NEW(XCtorAndDtor, ETrue);
110 TEST_KERN_NEW(XCtorOnly, ETrue);
111 TEST_KERN_NEW(XDtorOnly, EFalse);
112 TEST_KERN_NEW(XNoTors, EFalse);
114 NKern::ThreadLeaveCS();
118 TInt DOperatorNewTest::TestPlacementNew()
120 Kern::Printf("::TestPlacementNew");
122 #define TEST_KERN_PLACEMENT_NEW(CLASS, POST_CTOR, POST_DTOR) \
124 Kern::Printf("new(someram) " #CLASS);\
125 NKern::ThreadEnterCS();\
126 void* someram = Kern::AllocZ(sizeof(CLASS));\
127 NKern::ThreadLeaveCS();\
129 return KErrNoMemory;\
131 CLASS* p##CLASS = new (someram) CLASS;\
133 if(p##CLASS->iState != POST_CTOR)\
140 if(p##CLASS->iState != POST_DTOR)\
145 NKern::ThreadEnterCS();\
146 Kern::Free(someram);\
147 NKern::ThreadLeaveCS();\
152 TEST_KERN_PLACEMENT_NEW(XCtorAndDtor, EConstructed, EDeconstructed);
153 TEST_KERN_PLACEMENT_NEW(XCtorOnly, EConstructed, EConstructed);
154 TEST_KERN_PLACEMENT_NEW(XDtorOnly, ENull, EDeconstructed);
155 TEST_KERN_PLACEMENT_NEW(XNoTors, ENull, ENull);
160 TInt DOperatorNewTest::TestPlacementVectorNew()
163 Kern::Printf("::TestPlacementVectorNew");
165 //for vector placement new, emulator compilers
166 //allocate a cookie at start of buffer.
167 //this seems wrong since the cookie is an internal compiller
168 //detail which the user should not need to know about
169 #if defined(__WINSCW__) || defined(__VC32__)
170 Kern::Printf("Not running on emulator. WINSCW and Visual studio\n insert a cookie for placement vector new");
174 #define TEST_KERN_PLACEMENT_VECTOR_NEW(CLASS, ARRAY_LENGTH, POST_CTOR, POST_DTOR) \
176 NKern::ThreadEnterCS();\
177 void* someram = Kern::AllocZ(sizeof(CLASS) * (ARRAY_LENGTH));\
178 NKern::ThreadLeaveCS();\
180 return KErrNoMemory;\
183 Kern::Printf("new (someram) " #CLASS "[%d]", ARRAY_LENGTH);\
185 CLASS* p##CLASS = new (someram) CLASS[ARRAY_LENGTH];\
186 for(TInt i=0; i<(ARRAY_LENGTH); ++i)\
188 if(p##CLASS[i].iState != POST_CTOR)\
193 p##CLASS[i].~CLASS();\
194 if(p##CLASS[i].iState != POST_DTOR)\
200 NKern::ThreadEnterCS();\
201 Kern::Free(someram);\
202 NKern::ThreadLeaveCS();\
207 TEST_KERN_PLACEMENT_VECTOR_NEW(XCtorAndDtor, KTestArrayLength, EConstructed, EDeconstructed);
208 TEST_KERN_PLACEMENT_VECTOR_NEW(XCtorOnly, KTestArrayLength, EConstructed, EConstructed);
209 TEST_KERN_PLACEMENT_VECTOR_NEW(XDtorOnly, KTestArrayLength, ENull, EDeconstructed);
210 TEST_KERN_PLACEMENT_VECTOR_NEW(XNoTors, KTestArrayLength, ENull, ENull);
216 TInt DOperatorNewTest::TestVectorNew()
219 Kern::Printf("::TestVectorNew()");
220 Kern::Printf("OOM test");
224 #define TEST_KERN_VECTOR_NEW_OOM(CLASS, ARRAY_LENGTH)\
226 Kern::Printf("new " #CLASS "[%d]", ARRAY_LENGTH);\
227 CLASS* p##CLASS = NULL;\
228 NKern::ThreadEnterCS();\
229 XTRAP(r,XT_DEFAULT,p##CLASS = new CLASS[ARRAY_LENGTH]; );\
235 NKern::ThreadLeaveCS();\
242 TEST_KERN_VECTOR_NEW_OOM(XCtorAndDtor, KOOMArraySize);
243 TEST_KERN_VECTOR_NEW_OOM(XCtorOnly, KOOMArraySize);
244 TEST_KERN_VECTOR_NEW_OOM(XDtorOnly, KOOMArraySize);
245 TEST_KERN_VECTOR_NEW_OOM(XNoTors, KOOMArraySize);
249 Kern::Printf("non-OOM test");
251 #define TEST_KERN_VECTOR_NEW(CLASS, ARRAY_LENGTH, TEST_CTOR)\
253 Kern::Printf("new " #CLASS "[%d]", ARRAY_LENGTH);\
254 CLASS* p##CLASS = NULL;\
255 NKern::ThreadEnterCS();\
256 XTRAP(r,XT_DEFAULT,p##CLASS = new CLASS[ARRAY_LENGTH]; );\
257 NKern::ThreadLeaveCS();\
258 if(p##CLASS == NULL)\
260 return KErrNoMemory;\
263 TBool testCtor=(TEST_CTOR);\
266 for(TInt i=0; i<(ARRAY_LENGTH); ++i)\
268 if(p##CLASS[i].iState!= EConstructed)\
276 NKern::ThreadEnterCS();\
278 NKern::ThreadLeaveCS();\
286 TEST_KERN_VECTOR_NEW(XCtorAndDtor, KTestArrayLength, ETrue);
287 TEST_KERN_VECTOR_NEW(XCtorOnly, KTestArrayLength, ETrue);
288 TEST_KERN_VECTOR_NEW(XDtorOnly, KTestArrayLength, EFalse);
289 TEST_KERN_VECTOR_NEW(XNoTors, KTestArrayLength, EFalse);
294 TInt DOperatorNewTestFactory::Create(DLogicalChannelBase*& aChannel)
296 // Create a new DTest on this logical device
299 aChannel=new DOperatorNewTest();
300 return aChannel?KErrNone:KErrNoMemory;
303 TInt DOperatorNewTestFactory::Install()
305 // Install the LDD - overriding pure virtual
308 iVersion = TVersion(0,1,1);
309 return SetName(&KLddName);
312 void DOperatorNewTestFactory::GetCaps(TDes8& /*aDes*/) const
314 // Get capabilities - overriding pure virtual
322 DECLARE_STANDARD_LDD()
324 //create factory here.
325 return new DOperatorNewTestFactory;