os/kernelhwsrv/kerneltest/e32test/device/t_newldd.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/device/t_newldd.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,352 @@
     1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#define __E32TEST_EXTENSION__
    1.20 +#include <e32test.h>
    1.21 +#include <e32def.h>
    1.22 +#include <e32def_private.h>
    1.23 +#include "t_newldd.h"
    1.24 +#include "t_new_classes.h"
    1.25 +
    1.26 +RTest test(_L("Testing Operator New"));
    1.27 +
    1.28 +TInt RNewLddTest::DoControl(TInt aFunction)
    1.29 +	{
    1.30 +	return RBusLogicalChannel::DoControl(aFunction);
    1.31 +	}
    1.32 +
    1.33 +TInt RNewLddTest::Open()
    1.34 +	{
    1.35 +	return DoCreate(KLddName,TVersion(0,1,1),KNullUnit,NULL,NULL);
    1.36 +	}
    1.37 +
    1.38 +RNewLddTest lddconn;
    1.39 +
    1.40 +TInt TestNew()
    1.41 +	{
    1.42 +	return lddconn.DoControl(RNewLddTest::ENew);
    1.43 +	}
    1.44 +TInt TestPlacementVectorNew()
    1.45 +	{
    1.46 +	return lddconn.DoControl(RNewLddTest::EPlacementVectorNew);
    1.47 +	}
    1.48 +TInt TestVectorNew()
    1.49 +	{
    1.50 +	return lddconn.DoControl(RNewLddTest::EVectorNew);
    1.51 +	}
    1.52 +TInt TestPlacementNew()
    1.53 +	{
    1.54 +	return lddconn.DoControl(RNewLddTest::EPlacementNew);
    1.55 +	}
    1.56 +
    1.57 +void UserSideTestNewOOM()
    1.58 +	{
    1.59 +	RDebug::Printf("User-Side: operator new OOM");
    1.60 +	//OOM tests: should not throw
    1.61 +	
    1.62 +	#define TEST_NEW_OOM(CLASS) \
    1.63 +	RDebug::Printf("new " #CLASS);\
    1.64 +		{\
    1.65 +		CLASS* p##CLASS = new CLASS;\
    1.66 +		test_Equal(NULL, p##CLASS);\
    1.67 +		}
    1.68 +
    1.69 +	TEST_NEW_OOM(XVeryLargeClassCtorAndDtor);
    1.70 +	TEST_NEW_OOM(XVeryLargeClassCtorOnly);
    1.71 +	TEST_NEW_OOM(XVeryLargeClassDtorOnly);
    1.72 +	TEST_NEW_OOM(XVeryLargeClassNoTors);
    1.73 +	}
    1.74 +
    1.75 +void UserSideTestNewConstruction()
    1.76 +	{
    1.77 +	RDebug::Printf("User-Side: operator new non-OOM");
    1.78 +	//Non-OOM:
    1.79 +	
    1.80 +
    1.81 +	#define TEST_NEW_CONSTRUCTION(CLASS) \
    1.82 +	RDebug::Printf("new " #CLASS);\
    1.83 +		{\
    1.84 +		CLASS* p##CLASS = new CLASS;\
    1.85 +		test_NotNull(p##CLASS);\
    1.86 +		test_Equal(EConstructed, (p##CLASS)->iState);\
    1.87 +		delete p##CLASS;\
    1.88 +		}
    1.89 +
    1.90 +	TEST_NEW_CONSTRUCTION(XCtorAndDtor)
    1.91 +	TEST_NEW_CONSTRUCTION(XCtorOnly)
    1.92 +
    1.93 +
    1.94 +	#define TEST_NEW(CLASS) \
    1.95 +	RDebug::Printf("new " #CLASS);\
    1.96 +		{\
    1.97 +		CLASS* p##CLASS = new CLASS;\
    1.98 +		test_NotNull(p##CLASS);\
    1.99 +		delete p##CLASS;\
   1.100 +		}
   1.101 +	TEST_NEW(XDtorOnly)
   1.102 +	TEST_NEW(XNoTors)
   1.103 +	}
   1.104 +	
   1.105 +void UserSideTestNew()
   1.106 +	{
   1.107 +	UserSideTestNewOOM();
   1.108 +	UserSideTestNewConstruction();
   1.109 +	}
   1.110 +
   1.111 +void TrappedUserSideTestNew()
   1.112 +	{
   1.113 +	RDebug::Printf("TRAPPED User-Side: operator new");
   1.114 +	//OOM tests: should not throw
   1.115 +
   1.116 +	#define TEST_NEW_ELEAVE_OOM(CLASS) \
   1.117 +	RDebug::Printf("new(ELeave) " #CLASS);\
   1.118 +		{\
   1.119 +		TRAPD(r, new(ELeave) (CLASS));\
   1.120 +		test_Equal(KErrNoMemory, r);\
   1.121 +		}
   1.122 +
   1.123 +	TEST_NEW_ELEAVE_OOM(XVeryLargeClassCtorAndDtor);
   1.124 +	TEST_NEW_ELEAVE_OOM(XVeryLargeClassCtorOnly);
   1.125 +	TEST_NEW_ELEAVE_OOM(XVeryLargeClassDtorOnly);
   1.126 +	TEST_NEW_ELEAVE_OOM(XVeryLargeClassNoTors);
   1.127 +
   1.128 +
   1.129 +
   1.130 +	RDebug::Printf("User-Side: operator new non-OOM");
   1.131 +	//Non-OOM:
   1.132 +
   1.133 +	#define TEST_NEW_ELEAVE(CLASS, TEST_CTOR ) \
   1.134 +	RDebug::Printf("new(ELeave) " #CLASS);\
   1.135 +		{\
   1.136 +		CLASS* p##CLASS=NULL;\
   1.137 +		TRAPD(r, p##CLASS = new(ELeave) (CLASS));\
   1.138 +		test_KErrNone(r);\
   1.139 +		volatile TBool testCtor=(TEST_CTOR);\
   1.140 +		if(testCtor)\
   1.141 +			{\
   1.142 +			test_Equal(EConstructed, (p##CLASS)->iState);\
   1.143 +			}\
   1.144 +		delete p##CLASS;\
   1.145 +		}
   1.146 +
   1.147 +	TEST_NEW_ELEAVE(XCtorAndDtor, ETrue);
   1.148 +	TEST_NEW_ELEAVE(XCtorOnly, ETrue);
   1.149 +	TEST_NEW_ELEAVE(XDtorOnly, EFalse);
   1.150 +	TEST_NEW_ELEAVE(XNoTors, EFalse);
   1.151 +	}
   1.152 +
   1.153 +
   1.154 +#define TEST_ARRAY_CONSTRUCTION(ARRAY, LENGTH)\
   1.155 +	{\
   1.156 +	for(TInt i=0; i<(LENGTH); ++i)\
   1.157 +		{\
   1.158 +		test_Equal(EConstructed, ARRAY[i].iState);\
   1.159 +		}\
   1.160 +	}
   1.161 +
   1.162 +void UserSideTestVectorNew()
   1.163 +	{
   1.164 +	RDebug::Printf("User-Side:vector operator new");
   1.165 +	RDebug::Printf("OOM tests");
   1.166 +
   1.167 +	#define TEST_VEC_NEW_OOM(CLASS) \
   1.168 +	RDebug::Printf("new " #CLASS "[%d]", KOOMArraySize );\
   1.169 +		{\
   1.170 +		CLASS* p##CLASS = new CLASS[KOOMArraySize];\
   1.171 +		test_Equal(NULL, p##CLASS);\
   1.172 +		}
   1.173 +
   1.174 +	TEST_VEC_NEW_OOM(XCtorAndDtor);
   1.175 +	TEST_VEC_NEW_OOM(XCtorOnly);
   1.176 +	TEST_VEC_NEW_OOM(XDtorOnly);
   1.177 +	TEST_VEC_NEW_OOM(XNoTors);
   1.178 +	
   1.179 +	RDebug::Printf("non-OOM tests");
   1.180 +
   1.181 +	#define TEST_VEC_NEW(CLASS, ARRAY_LENGTH, TEST_CTOR) \
   1.182 +	RDebug::Printf("new " #CLASS "[%d]", ARRAY_LENGTH);\
   1.183 +		{\
   1.184 +		CLASS* p##CLASS = new CLASS[(ARRAY_LENGTH)];\
   1.185 +		test_NotNull(p##CLASS);\
   1.186 +		volatile TBool testCtor=(TEST_CTOR);\
   1.187 +		if(testCtor)\
   1.188 +			{\
   1.189 +			TEST_ARRAY_CONSTRUCTION(p##CLASS, ARRAY_LENGTH);\
   1.190 +			}\
   1.191 +		delete[] p##CLASS;\
   1.192 +		}
   1.193 +
   1.194 +	TEST_VEC_NEW(XCtorAndDtor, KTestArrayLength, ETrue);
   1.195 +	TEST_VEC_NEW(XCtorOnly, KTestArrayLength, ETrue);
   1.196 +	TEST_VEC_NEW(XDtorOnly, KTestArrayLength, EFalse);
   1.197 +	TEST_VEC_NEW(XNoTors, KTestArrayLength, EFalse);
   1.198 +	}
   1.199 +
   1.200 +void TrappedUserSideTestVectorNew()
   1.201 +	{
   1.202 +	RDebug::Printf("User-Side:vector operator new");
   1.203 +	RDebug::Printf("OOM tests");
   1.204 +
   1.205 +	#define TEST_VEC_NEW_ELEAVE_OOM(CLASS) \
   1.206 +	RDebug::Printf("new(ELeave) " #CLASS "[%d]", KOOMArraySize );\
   1.207 +		{\
   1.208 +	   	TRAPD(r, new(ELeave) CLASS[KOOMArraySize];)\
   1.209 +		test_Equal(KErrNoMemory, r);\
   1.210 +		}
   1.211 +
   1.212 +	TEST_VEC_NEW_ELEAVE_OOM(XCtorAndDtor);
   1.213 +	TEST_VEC_NEW_ELEAVE_OOM(XCtorOnly);
   1.214 +	TEST_VEC_NEW_ELEAVE_OOM(XDtorOnly);
   1.215 +	TEST_VEC_NEW_ELEAVE_OOM(XNoTors);
   1.216 +
   1.217 +
   1.218 +
   1.219 +	RDebug::Printf("non-OOM tests");
   1.220 +	#define TEST_VEC_NEW_ELEAVE(CLASS, ARRAY_LENGTH, TEST_CTOR) \
   1.221 +	RDebug::Printf("new(ELeave) " #CLASS "[%d]", ARRAY_LENGTH);\
   1.222 +		{\
   1.223 +		CLASS* p##CLASS = NULL;\
   1.224 +		TRAPD(r, p##CLASS = new(ELeave) CLASS[(ARRAY_LENGTH)]);\
   1.225 +		test_KErrNone(r);\
   1.226 +		TBool testCtor=(TEST_CTOR);\
   1.227 +		if(testCtor)\
   1.228 +			{\
   1.229 +			TEST_ARRAY_CONSTRUCTION(p##CLASS, ARRAY_LENGTH);\
   1.230 +			}\
   1.231 +		delete[] p##CLASS;\
   1.232 +		}
   1.233 +
   1.234 +	TEST_VEC_NEW_ELEAVE(XCtorAndDtor, KTestArrayLength, ETrue);
   1.235 +	TEST_VEC_NEW_ELEAVE(XCtorOnly, KTestArrayLength, ETrue);
   1.236 +	TEST_VEC_NEW_ELEAVE(XDtorOnly, KTestArrayLength, EFalse);
   1.237 +	TEST_VEC_NEW_ELEAVE(XNoTors, KTestArrayLength, EFalse);
   1.238 +	}
   1.239 +
   1.240 +void UserSideTestPlacementNew()
   1.241 +	{
   1.242 +	RDebug::Printf("::UserSideTestPlacementNew");
   1.243 +	
   1.244 +	#define TEST_PLACMENT_NEW(CLASS, POST_CTOR_STATE, POST_DTOR_STATE)\
   1.245 +		{\
   1.246 +		void* someram = User::AllocZ(sizeof(CLASS));\
   1.247 +		test_NotNull(someram);\
   1.248 +		RDebug::Printf("new (someram) " #CLASS);\
   1.249 +		CLASS* p##CLASS = new (someram) CLASS;\
   1.250 +		test_Equal(someram, p##CLASS);\
   1.251 +		test_Equal(POST_CTOR_STATE, p##CLASS->iState);\
   1.252 +		p##CLASS->~CLASS();\
   1.253 +		test_Equal(POST_DTOR_STATE, p##CLASS->iState);\
   1.254 +		User::Free(someram);\
   1.255 +		p##CLASS=NULL;\
   1.256 +		}\
   1.257 +
   1.258 +	TEST_PLACMENT_NEW(XCtorAndDtor, EConstructed, EDeconstructed);
   1.259 +	TEST_PLACMENT_NEW(XCtorOnly, EConstructed, EConstructed);
   1.260 +	TEST_PLACMENT_NEW(XDtorOnly, ENull, EDeconstructed);
   1.261 +	TEST_PLACMENT_NEW(XNoTors, ENull, ENull);
   1.262 +	}
   1.263 +
   1.264 +
   1.265 +void UserSideTestPlacementVectorNew()
   1.266 +	{
   1.267 +	__UHEAP_MARK;
   1.268 +
   1.269 +	RDebug::Printf("::UserSideTestPlacementVectorNew");
   1.270 +
   1.271 +	#define TEST_VEC_PLACEMENT_NEW(CLASS, ARRAY_LENGTH, POST_CTOR_STATE, POST_DTOR_STATE)\
   1.272 +	RDebug::Printf("new(someram) " #CLASS "[%d]", ARRAY_LENGTH);\
   1.273 +		{\
   1.274 +		void* someram = User::AllocZ(sizeof(CLASS) * ARRAY_LENGTH);\
   1.275 +		test_NotNull(someram);\
   1.276 +		CLASS* p##CLASS = new (someram) CLASS[(ARRAY_LENGTH)];\
   1.277 +		for(TInt i=0; i<(ARRAY_LENGTH); ++i)\
   1.278 +			{\
   1.279 +			test_Equal(POST_CTOR_STATE, p##CLASS[i].iState);\
   1.280 +			p##CLASS[i].~CLASS();\
   1.281 +			test_Equal(POST_DTOR_STATE, p##CLASS[i].iState);\
   1.282 +			}\
   1.283 +		User::Free(someram);\
   1.284 +		}
   1.285 +
   1.286 +	TEST_VEC_PLACEMENT_NEW(XCtorAndDtor, KTestArrayLength, EConstructed, EDeconstructed);
   1.287 +	TEST_VEC_PLACEMENT_NEW(XCtorOnly, KTestArrayLength, EConstructed, EConstructed);
   1.288 +	TEST_VEC_PLACEMENT_NEW(XDtorOnly, KTestArrayLength, ENull, EDeconstructed);
   1.289 +	TEST_VEC_PLACEMENT_NEW(XNoTors, KTestArrayLength, ENull, ENull);
   1.290 +
   1.291 +	__UHEAP_MARKEND;
   1.292 +	}
   1.293 +
   1.294 +
   1.295 +TInt E32Main()
   1.296 +	{
   1.297 +	__UHEAP_MARK;
   1.298 +
   1.299 +	test.Start(_L("Testing operator new"));
   1.300 +	
   1.301 +	test.Next(_L("Installing LDD"));
   1.302 +	TInt r=User::LoadLogicalDevice(KKInstallLddName);
   1.303 +	test(r==KErrNone || r==KErrAlreadyExists);
   1.304 +	
   1.305 +	__KHEAP_MARK;
   1.306 +
   1.307 +	#define TEST_THIS(X) test.Next(_L(#X)); (X)
   1.308 +	
   1.309 +	TEST_THIS(UserSideTestNew());
   1.310 +	TEST_THIS(UserSideTestPlacementNew());
   1.311 +
   1.312 +// Workaround for bug in MSVC6/CW compilers. The following test case fails on
   1.313 +// WINS, WINSCW and X86 targets (not X86GCC), where the placement vector new
   1.314 +// operator does not behave as expected, ultimately resulting in heap corruption
   1.315 +// when the parameter to placement array new is passed on the stack.
   1.316 +#if !(defined(__WINS__) || (defined(__VC32__) && (_MSC_VER < 1300)))
   1.317 +	TEST_THIS(UserSideTestPlacementVectorNew());
   1.318 +#else
   1.319 +	test.Next(_L("Emulator and/or VC32 - Skipped: UserSideTestPlacementVectorNew"));
   1.320 +#endif
   1.321 +
   1.322 +	TEST_THIS(UserSideTestVectorNew());
   1.323 +	TEST_THIS(TrappedUserSideTestNew());
   1.324 +	TEST_THIS(TrappedUserSideTestVectorNew());
   1.325 +	
   1.326 +	r=lddconn.Open();
   1.327 +	test_KErrNone(r);
   1.328 +
   1.329 +	test.Next(_L("Kernel-side:Normal operator new"));
   1.330 +	r = TestNew();
   1.331 +	test_KErrNone(r);
   1.332 +		
   1.333 +	test.Next(_L("Kernel-side:Placement operator new"));
   1.334 +	r = TestPlacementNew();
   1.335 +	test_KErrNone(r);
   1.336 +
   1.337 +	test.Next(_L("Kernel-side:Placement Vector operator new"));
   1.338 +	r = TestPlacementVectorNew();
   1.339 +	test_KErrNone(r);
   1.340 +
   1.341 +	test.Next(_L("Kernel-side:Vector operator new"));
   1.342 +	r = TestVectorNew();
   1.343 +	test_KErrNone(r);
   1.344 +
   1.345 +	r = RTest::CloseHandleAndWaitForDestruction(lddconn);
   1.346 +	test_KErrNone(r);
   1.347 +
   1.348 +	test.End();
   1.349 +	test.Close();
   1.350 +
   1.351 +	__KHEAP_MARKEND;
   1.352 +	__UHEAP_MARKEND;
   1.353 +	return KErrNone;
   1.354 +	}
   1.355 +