os/kernelhwsrv/kerneltest/e32test/device/d_newldd.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <kernel/kern_priv.h>
sl@0
    17
#include "d_newldd.h"
sl@0
    18
#include "t_newldd.h"
sl@0
    19
sl@0
    20
TInt DOperatorNewTest::Request(TInt aReqNo, TAny* /*a1*/, TAny* /*a2*/)
sl@0
    21
	{
sl@0
    22
	switch(aReqNo)
sl@0
    23
		{
sl@0
    24
		//new
sl@0
    25
		case RNewLddTest::ENew:
sl@0
    26
			{
sl@0
    27
			return TestNew();
sl@0
    28
			}
sl@0
    29
		//placement new
sl@0
    30
		case RNewLddTest::EPlacementVectorNew:
sl@0
    31
			{
sl@0
    32
			return TestPlacementVectorNew();
sl@0
    33
			}
sl@0
    34
		//vector new
sl@0
    35
		case RNewLddTest::EVectorNew:
sl@0
    36
			{
sl@0
    37
			return TestVectorNew();
sl@0
    38
			}
sl@0
    39
		case RNewLddTest::EPlacementNew:
sl@0
    40
			{
sl@0
    41
			return TestPlacementNew();
sl@0
    42
			}
sl@0
    43
		default:
sl@0
    44
			break;
sl@0
    45
		}
sl@0
    46
	return KErrNone;
sl@0
    47
	}
sl@0
    48
sl@0
    49
TInt DOperatorNewTest::TestNew()
sl@0
    50
	{
sl@0
    51
	Kern::Printf("::TestNew()");
sl@0
    52
	NKern::ThreadEnterCS();
sl@0
    53
sl@0
    54
	#define TEST_KERN_NEW_OOM(CLASS)\
sl@0
    55
		{\
sl@0
    56
		Kern::Printf("new " #CLASS);\
sl@0
    57
		CLASS* p##CLASS=NULL;\
sl@0
    58
		XTRAPD(r,XT_DEFAULT, p##CLASS = new CLASS);\
sl@0
    59
		if(r!=KErrNone)\
sl@0
    60
			{\
sl@0
    61
			NKern::ThreadLeaveCS();\
sl@0
    62
			return r;\
sl@0
    63
			}\
sl@0
    64
		if(p##CLASS)\
sl@0
    65
			{\
sl@0
    66
			delete p##CLASS;\
sl@0
    67
			NKern::ThreadLeaveCS();\
sl@0
    68
			return KErrGeneral;\
sl@0
    69
			}\
sl@0
    70
		}
sl@0
    71
sl@0
    72
	//OOM tests: should(must) not throw
sl@0
    73
	Kern::Printf("OOM Tests;");
sl@0
    74
sl@0
    75
	TEST_KERN_NEW_OOM(XVeryLargeClassCtorAndDtor);
sl@0
    76
	TEST_KERN_NEW_OOM(XVeryLargeClassCtorOnly);
sl@0
    77
	TEST_KERN_NEW_OOM(XVeryLargeClassDtorOnly);
sl@0
    78
	TEST_KERN_NEW_OOM(XVeryLargeClassNoTors);
sl@0
    79
sl@0
    80
	Kern::Printf("non-OOM Tests;");
sl@0
    81
	//Non-OOM:
sl@0
    82
	
sl@0
    83
	#define TEST_KERN_NEW(CLASS, TEST_CTOR)\
sl@0
    84
		{\
sl@0
    85
		Kern::Printf("new " #CLASS);\
sl@0
    86
		CLASS* p##CLASS=NULL;\
sl@0
    87
		XTRAPD(r,XT_DEFAULT, p##CLASS = new CLASS);\
sl@0
    88
		if(r!=KErrNone)\
sl@0
    89
			{\
sl@0
    90
			NKern::ThreadLeaveCS();\
sl@0
    91
			return r;\
sl@0
    92
			}\
sl@0
    93
		if(p##CLASS==NULL)\
sl@0
    94
			{\
sl@0
    95
			NKern::ThreadLeaveCS();\
sl@0
    96
			return KErrGeneral;\
sl@0
    97
			}\
sl@0
    98
		volatile TBool testCtor=(TEST_CTOR);\
sl@0
    99
		if(testCtor && (p##CLASS->iState!=EConstructed) )\
sl@0
   100
			{\
sl@0
   101
			r=KErrGeneral;\
sl@0
   102
			}\
sl@0
   103
		delete p##CLASS;\
sl@0
   104
		p##CLASS=NULL;\
sl@0
   105
		if(r!=KErrNone)\
sl@0
   106
			return r;\
sl@0
   107
		}
sl@0
   108
sl@0
   109
	TEST_KERN_NEW(XCtorAndDtor, ETrue);
sl@0
   110
	TEST_KERN_NEW(XCtorOnly, ETrue);
sl@0
   111
	TEST_KERN_NEW(XDtorOnly, EFalse);
sl@0
   112
	TEST_KERN_NEW(XNoTors, EFalse);
sl@0
   113
	
sl@0
   114
	NKern::ThreadLeaveCS();
sl@0
   115
	return KErrNone;
sl@0
   116
	}
sl@0
   117
sl@0
   118
TInt DOperatorNewTest::TestPlacementNew()
sl@0
   119
	{
sl@0
   120
	Kern::Printf("::TestPlacementNew");
sl@0
   121
sl@0
   122
	#define TEST_KERN_PLACEMENT_NEW(CLASS, POST_CTOR, POST_DTOR) \
sl@0
   123
		{\
sl@0
   124
		Kern::Printf("new(someram) " #CLASS);\
sl@0
   125
		NKern::ThreadEnterCS();\
sl@0
   126
		void* someram = Kern::AllocZ(sizeof(CLASS));\
sl@0
   127
		NKern::ThreadLeaveCS();\
sl@0
   128
		if(!someram)\
sl@0
   129
			return KErrNoMemory;\
sl@0
   130
		\
sl@0
   131
		CLASS* p##CLASS = new (someram) CLASS;\
sl@0
   132
		TInt r=KErrNone;\
sl@0
   133
		if(p##CLASS->iState != POST_CTOR)\
sl@0
   134
			{\
sl@0
   135
			r=KErrGeneral;\
sl@0
   136
			}\
sl@0
   137
		if(r==KErrNone)\
sl@0
   138
			{\
sl@0
   139
			p##CLASS->~CLASS();\
sl@0
   140
			if(p##CLASS->iState != POST_DTOR)\
sl@0
   141
				{\
sl@0
   142
				r=KErrGeneral;\
sl@0
   143
				}\
sl@0
   144
			}\
sl@0
   145
		NKern::ThreadEnterCS();\
sl@0
   146
		Kern::Free(someram);\
sl@0
   147
		NKern::ThreadLeaveCS();\
sl@0
   148
		if(r != KErrNone)\
sl@0
   149
			return r;\
sl@0
   150
		}
sl@0
   151
sl@0
   152
	TEST_KERN_PLACEMENT_NEW(XCtorAndDtor, EConstructed, EDeconstructed);
sl@0
   153
	TEST_KERN_PLACEMENT_NEW(XCtorOnly, EConstructed, EConstructed);
sl@0
   154
	TEST_KERN_PLACEMENT_NEW(XDtorOnly, ENull, EDeconstructed);
sl@0
   155
	TEST_KERN_PLACEMENT_NEW(XNoTors, ENull, ENull);
sl@0
   156
	
sl@0
   157
	return KErrNone;
sl@0
   158
	}
sl@0
   159
sl@0
   160
TInt DOperatorNewTest::TestPlacementVectorNew()
sl@0
   161
	{
sl@0
   162
	
sl@0
   163
	Kern::Printf("::TestPlacementVectorNew");
sl@0
   164
	
sl@0
   165
	//for vector placement new, emulator compilers 
sl@0
   166
	//allocate a cookie at start of buffer.
sl@0
   167
	//this seems wrong since the cookie is an internal compiller
sl@0
   168
	//detail which the user should not need to know about
sl@0
   169
	#if defined(__WINSCW__) || defined(__VC32__)
sl@0
   170
	Kern::Printf("Not running on emulator. WINSCW and Visual studio\n insert a cookie for placement vector new"); 
sl@0
   171
sl@0
   172
	#else
sl@0
   173
sl@0
   174
	#define TEST_KERN_PLACEMENT_VECTOR_NEW(CLASS, ARRAY_LENGTH, POST_CTOR, POST_DTOR) \
sl@0
   175
		{\
sl@0
   176
		NKern::ThreadEnterCS();\
sl@0
   177
		void* someram = Kern::AllocZ(sizeof(CLASS) * (ARRAY_LENGTH));\
sl@0
   178
		NKern::ThreadLeaveCS();\
sl@0
   179
		if(someram==NULL)\
sl@0
   180
			return KErrNoMemory;\
sl@0
   181
		\
sl@0
   182
		TInt r = KErrNone;\
sl@0
   183
		Kern::Printf("new (someram) " #CLASS "[%d]", ARRAY_LENGTH);\
sl@0
   184
		\
sl@0
   185
		CLASS* p##CLASS = new (someram) CLASS[ARRAY_LENGTH];\
sl@0
   186
		for(TInt i=0; i<(ARRAY_LENGTH); ++i)\
sl@0
   187
			{\
sl@0
   188
			if(p##CLASS[i].iState != POST_CTOR)\
sl@0
   189
				{\
sl@0
   190
				r=KErrGeneral;\
sl@0
   191
				break;\
sl@0
   192
				}\
sl@0
   193
			p##CLASS[i].~CLASS();\
sl@0
   194
			if(p##CLASS[i].iState != POST_DTOR)\
sl@0
   195
				{\
sl@0
   196
				r=KErrGeneral;\
sl@0
   197
				break;\
sl@0
   198
				}\
sl@0
   199
			}\
sl@0
   200
		NKern::ThreadEnterCS();\
sl@0
   201
		Kern::Free(someram);\
sl@0
   202
		NKern::ThreadLeaveCS();\
sl@0
   203
		if(r!=KErrNone)\
sl@0
   204
			return r;\
sl@0
   205
		}\
sl@0
   206
	
sl@0
   207
	TEST_KERN_PLACEMENT_VECTOR_NEW(XCtorAndDtor, KTestArrayLength, EConstructed, EDeconstructed);
sl@0
   208
	TEST_KERN_PLACEMENT_VECTOR_NEW(XCtorOnly, KTestArrayLength, EConstructed, EConstructed);
sl@0
   209
	TEST_KERN_PLACEMENT_VECTOR_NEW(XDtorOnly, KTestArrayLength, ENull, EDeconstructed);
sl@0
   210
	TEST_KERN_PLACEMENT_VECTOR_NEW(XNoTors, KTestArrayLength, ENull, ENull);
sl@0
   211
sl@0
   212
	#endif
sl@0
   213
sl@0
   214
	return KErrNone;
sl@0
   215
	}
sl@0
   216
TInt DOperatorNewTest::TestVectorNew()
sl@0
   217
	{
sl@0
   218
	//OOM testing
sl@0
   219
	Kern::Printf("::TestVectorNew()");
sl@0
   220
	Kern::Printf("OOM test");
sl@0
   221
	
sl@0
   222
	TInt r=KErrNone;
sl@0
   223
sl@0
   224
	#define TEST_KERN_VECTOR_NEW_OOM(CLASS, ARRAY_LENGTH)\
sl@0
   225
	{\
sl@0
   226
		Kern::Printf("new " #CLASS "[%d]", ARRAY_LENGTH);\
sl@0
   227
		CLASS* p##CLASS = NULL;\
sl@0
   228
		NKern::ThreadEnterCS();\
sl@0
   229
		XTRAP(r,XT_DEFAULT,p##CLASS = new CLASS[ARRAY_LENGTH]; );\
sl@0
   230
		if(p##CLASS)\
sl@0
   231
			{\
sl@0
   232
			r=KErrGeneral;\
sl@0
   233
			delete p##CLASS;\
sl@0
   234
			}\
sl@0
   235
		NKern::ThreadLeaveCS();\
sl@0
   236
		if(r!=KErrNone)\
sl@0
   237
			{\
sl@0
   238
			return r;\
sl@0
   239
			}\
sl@0
   240
	}\
sl@0
   241
sl@0
   242
	TEST_KERN_VECTOR_NEW_OOM(XCtorAndDtor, KOOMArraySize);
sl@0
   243
	TEST_KERN_VECTOR_NEW_OOM(XCtorOnly, KOOMArraySize);
sl@0
   244
	TEST_KERN_VECTOR_NEW_OOM(XDtorOnly, KOOMArraySize);
sl@0
   245
	TEST_KERN_VECTOR_NEW_OOM(XNoTors, KOOMArraySize);
sl@0
   246
sl@0
   247
	
sl@0
   248
	//non-OOM:
sl@0
   249
	Kern::Printf("non-OOM test");
sl@0
   250
sl@0
   251
	#define TEST_KERN_VECTOR_NEW(CLASS, ARRAY_LENGTH, TEST_CTOR)\
sl@0
   252
	{\
sl@0
   253
		Kern::Printf("new " #CLASS "[%d]", ARRAY_LENGTH);\
sl@0
   254
		CLASS* p##CLASS = NULL;\
sl@0
   255
		NKern::ThreadEnterCS();\
sl@0
   256
		XTRAP(r,XT_DEFAULT,p##CLASS = new CLASS[ARRAY_LENGTH]; );\
sl@0
   257
		NKern::ThreadLeaveCS();\
sl@0
   258
		if(p##CLASS == NULL)\
sl@0
   259
			{\
sl@0
   260
			return KErrNoMemory;\
sl@0
   261
			}\
sl@0
   262
		\
sl@0
   263
		TBool testCtor=(TEST_CTOR);\
sl@0
   264
		if(testCtor)\
sl@0
   265
			{\
sl@0
   266
			for(TInt i=0; i<(ARRAY_LENGTH); ++i)\
sl@0
   267
				{\
sl@0
   268
				if(p##CLASS[i].iState!=	EConstructed)\
sl@0
   269
					{\
sl@0
   270
					r=KErrGeneral;\
sl@0
   271
					break;\
sl@0
   272
					}\
sl@0
   273
				}\
sl@0
   274
			}\
sl@0
   275
		\
sl@0
   276
		NKern::ThreadEnterCS();\
sl@0
   277
		delete[] p##CLASS;\
sl@0
   278
		NKern::ThreadLeaveCS();\
sl@0
   279
		p##CLASS=NULL;\
sl@0
   280
		if(r!=KErrNone)\
sl@0
   281
			{\
sl@0
   282
			return r;\
sl@0
   283
			}\
sl@0
   284
	}\
sl@0
   285
sl@0
   286
	TEST_KERN_VECTOR_NEW(XCtorAndDtor, KTestArrayLength, ETrue);
sl@0
   287
	TEST_KERN_VECTOR_NEW(XCtorOnly, KTestArrayLength, ETrue);
sl@0
   288
	TEST_KERN_VECTOR_NEW(XDtorOnly, KTestArrayLength, EFalse);
sl@0
   289
	TEST_KERN_VECTOR_NEW(XNoTors, KTestArrayLength, EFalse);
sl@0
   290
sl@0
   291
	return KErrNone;
sl@0
   292
	}
sl@0
   293
sl@0
   294
TInt DOperatorNewTestFactory::Create(DLogicalChannelBase*& aChannel)
sl@0
   295
//
sl@0
   296
// Create a new DTest on this logical device
sl@0
   297
//
sl@0
   298
	{
sl@0
   299
	aChannel=new DOperatorNewTest();
sl@0
   300
	return aChannel?KErrNone:KErrNoMemory;
sl@0
   301
	}
sl@0
   302
sl@0
   303
TInt DOperatorNewTestFactory::Install()
sl@0
   304
//
sl@0
   305
// Install the LDD - overriding pure virtual
sl@0
   306
//
sl@0
   307
	{
sl@0
   308
	iVersion = TVersion(0,1,1);
sl@0
   309
	return SetName(&KLddName);
sl@0
   310
	}
sl@0
   311
sl@0
   312
void DOperatorNewTestFactory::GetCaps(TDes8& /*aDes*/) const
sl@0
   313
//
sl@0
   314
// Get capabilities - overriding pure virtual
sl@0
   315
//
sl@0
   316
	{
sl@0
   317
	//not supported
sl@0
   318
	}
sl@0
   319
sl@0
   320
sl@0
   321
sl@0
   322
DECLARE_STANDARD_LDD()
sl@0
   323
	{
sl@0
   324
	//create factory here.
sl@0
   325
	return new DOperatorNewTestFactory;
sl@0
   326
	}
sl@0
   327
sl@0
   328