os/kernelhwsrv/kerneltest/e32test/device/t_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
#define __E32TEST_EXTENSION__
sl@0
    17
#include <e32test.h>
sl@0
    18
#include <e32def.h>
sl@0
    19
#include <e32def_private.h>
sl@0
    20
#include "t_newldd.h"
sl@0
    21
#include "t_new_classes.h"
sl@0
    22
sl@0
    23
RTest test(_L("Testing Operator New"));
sl@0
    24
sl@0
    25
TInt RNewLddTest::DoControl(TInt aFunction)
sl@0
    26
	{
sl@0
    27
	return RBusLogicalChannel::DoControl(aFunction);
sl@0
    28
	}
sl@0
    29
sl@0
    30
TInt RNewLddTest::Open()
sl@0
    31
	{
sl@0
    32
	return DoCreate(KLddName,TVersion(0,1,1),KNullUnit,NULL,NULL);
sl@0
    33
	}
sl@0
    34
sl@0
    35
RNewLddTest lddconn;
sl@0
    36
sl@0
    37
TInt TestNew()
sl@0
    38
	{
sl@0
    39
	return lddconn.DoControl(RNewLddTest::ENew);
sl@0
    40
	}
sl@0
    41
TInt TestPlacementVectorNew()
sl@0
    42
	{
sl@0
    43
	return lddconn.DoControl(RNewLddTest::EPlacementVectorNew);
sl@0
    44
	}
sl@0
    45
TInt TestVectorNew()
sl@0
    46
	{
sl@0
    47
	return lddconn.DoControl(RNewLddTest::EVectorNew);
sl@0
    48
	}
sl@0
    49
TInt TestPlacementNew()
sl@0
    50
	{
sl@0
    51
	return lddconn.DoControl(RNewLddTest::EPlacementNew);
sl@0
    52
	}
sl@0
    53
sl@0
    54
void UserSideTestNewOOM()
sl@0
    55
	{
sl@0
    56
	RDebug::Printf("User-Side: operator new OOM");
sl@0
    57
	//OOM tests: should not throw
sl@0
    58
	
sl@0
    59
	#define TEST_NEW_OOM(CLASS) \
sl@0
    60
	RDebug::Printf("new " #CLASS);\
sl@0
    61
		{\
sl@0
    62
		CLASS* p##CLASS = new CLASS;\
sl@0
    63
		test_Equal(NULL, p##CLASS);\
sl@0
    64
		}
sl@0
    65
sl@0
    66
	TEST_NEW_OOM(XVeryLargeClassCtorAndDtor);
sl@0
    67
	TEST_NEW_OOM(XVeryLargeClassCtorOnly);
sl@0
    68
	TEST_NEW_OOM(XVeryLargeClassDtorOnly);
sl@0
    69
	TEST_NEW_OOM(XVeryLargeClassNoTors);
sl@0
    70
	}
sl@0
    71
sl@0
    72
void UserSideTestNewConstruction()
sl@0
    73
	{
sl@0
    74
	RDebug::Printf("User-Side: operator new non-OOM");
sl@0
    75
	//Non-OOM:
sl@0
    76
	
sl@0
    77
sl@0
    78
	#define TEST_NEW_CONSTRUCTION(CLASS) \
sl@0
    79
	RDebug::Printf("new " #CLASS);\
sl@0
    80
		{\
sl@0
    81
		CLASS* p##CLASS = new CLASS;\
sl@0
    82
		test_NotNull(p##CLASS);\
sl@0
    83
		test_Equal(EConstructed, (p##CLASS)->iState);\
sl@0
    84
		delete p##CLASS;\
sl@0
    85
		}
sl@0
    86
sl@0
    87
	TEST_NEW_CONSTRUCTION(XCtorAndDtor)
sl@0
    88
	TEST_NEW_CONSTRUCTION(XCtorOnly)
sl@0
    89
sl@0
    90
sl@0
    91
	#define TEST_NEW(CLASS) \
sl@0
    92
	RDebug::Printf("new " #CLASS);\
sl@0
    93
		{\
sl@0
    94
		CLASS* p##CLASS = new CLASS;\
sl@0
    95
		test_NotNull(p##CLASS);\
sl@0
    96
		delete p##CLASS;\
sl@0
    97
		}
sl@0
    98
	TEST_NEW(XDtorOnly)
sl@0
    99
	TEST_NEW(XNoTors)
sl@0
   100
	}
sl@0
   101
	
sl@0
   102
void UserSideTestNew()
sl@0
   103
	{
sl@0
   104
	UserSideTestNewOOM();
sl@0
   105
	UserSideTestNewConstruction();
sl@0
   106
	}
sl@0
   107
sl@0
   108
void TrappedUserSideTestNew()
sl@0
   109
	{
sl@0
   110
	RDebug::Printf("TRAPPED User-Side: operator new");
sl@0
   111
	//OOM tests: should not throw
sl@0
   112
sl@0
   113
	#define TEST_NEW_ELEAVE_OOM(CLASS) \
sl@0
   114
	RDebug::Printf("new(ELeave) " #CLASS);\
sl@0
   115
		{\
sl@0
   116
		TRAPD(r, new(ELeave) (CLASS));\
sl@0
   117
		test_Equal(KErrNoMemory, r);\
sl@0
   118
		}
sl@0
   119
sl@0
   120
	TEST_NEW_ELEAVE_OOM(XVeryLargeClassCtorAndDtor);
sl@0
   121
	TEST_NEW_ELEAVE_OOM(XVeryLargeClassCtorOnly);
sl@0
   122
	TEST_NEW_ELEAVE_OOM(XVeryLargeClassDtorOnly);
sl@0
   123
	TEST_NEW_ELEAVE_OOM(XVeryLargeClassNoTors);
sl@0
   124
sl@0
   125
sl@0
   126
sl@0
   127
	RDebug::Printf("User-Side: operator new non-OOM");
sl@0
   128
	//Non-OOM:
sl@0
   129
sl@0
   130
	#define TEST_NEW_ELEAVE(CLASS, TEST_CTOR ) \
sl@0
   131
	RDebug::Printf("new(ELeave) " #CLASS);\
sl@0
   132
		{\
sl@0
   133
		CLASS* p##CLASS=NULL;\
sl@0
   134
		TRAPD(r, p##CLASS = new(ELeave) (CLASS));\
sl@0
   135
		test_KErrNone(r);\
sl@0
   136
		volatile TBool testCtor=(TEST_CTOR);\
sl@0
   137
		if(testCtor)\
sl@0
   138
			{\
sl@0
   139
			test_Equal(EConstructed, (p##CLASS)->iState);\
sl@0
   140
			}\
sl@0
   141
		delete p##CLASS;\
sl@0
   142
		}
sl@0
   143
sl@0
   144
	TEST_NEW_ELEAVE(XCtorAndDtor, ETrue);
sl@0
   145
	TEST_NEW_ELEAVE(XCtorOnly, ETrue);
sl@0
   146
	TEST_NEW_ELEAVE(XDtorOnly, EFalse);
sl@0
   147
	TEST_NEW_ELEAVE(XNoTors, EFalse);
sl@0
   148
	}
sl@0
   149
sl@0
   150
sl@0
   151
#define TEST_ARRAY_CONSTRUCTION(ARRAY, LENGTH)\
sl@0
   152
	{\
sl@0
   153
	for(TInt i=0; i<(LENGTH); ++i)\
sl@0
   154
		{\
sl@0
   155
		test_Equal(EConstructed, ARRAY[i].iState);\
sl@0
   156
		}\
sl@0
   157
	}
sl@0
   158
sl@0
   159
void UserSideTestVectorNew()
sl@0
   160
	{
sl@0
   161
	RDebug::Printf("User-Side:vector operator new");
sl@0
   162
	RDebug::Printf("OOM tests");
sl@0
   163
sl@0
   164
	#define TEST_VEC_NEW_OOM(CLASS) \
sl@0
   165
	RDebug::Printf("new " #CLASS "[%d]", KOOMArraySize );\
sl@0
   166
		{\
sl@0
   167
		CLASS* p##CLASS = new CLASS[KOOMArraySize];\
sl@0
   168
		test_Equal(NULL, p##CLASS);\
sl@0
   169
		}
sl@0
   170
sl@0
   171
	TEST_VEC_NEW_OOM(XCtorAndDtor);
sl@0
   172
	TEST_VEC_NEW_OOM(XCtorOnly);
sl@0
   173
	TEST_VEC_NEW_OOM(XDtorOnly);
sl@0
   174
	TEST_VEC_NEW_OOM(XNoTors);
sl@0
   175
	
sl@0
   176
	RDebug::Printf("non-OOM tests");
sl@0
   177
sl@0
   178
	#define TEST_VEC_NEW(CLASS, ARRAY_LENGTH, TEST_CTOR) \
sl@0
   179
	RDebug::Printf("new " #CLASS "[%d]", ARRAY_LENGTH);\
sl@0
   180
		{\
sl@0
   181
		CLASS* p##CLASS = new CLASS[(ARRAY_LENGTH)];\
sl@0
   182
		test_NotNull(p##CLASS);\
sl@0
   183
		volatile TBool testCtor=(TEST_CTOR);\
sl@0
   184
		if(testCtor)\
sl@0
   185
			{\
sl@0
   186
			TEST_ARRAY_CONSTRUCTION(p##CLASS, ARRAY_LENGTH);\
sl@0
   187
			}\
sl@0
   188
		delete[] p##CLASS;\
sl@0
   189
		}
sl@0
   190
sl@0
   191
	TEST_VEC_NEW(XCtorAndDtor, KTestArrayLength, ETrue);
sl@0
   192
	TEST_VEC_NEW(XCtorOnly, KTestArrayLength, ETrue);
sl@0
   193
	TEST_VEC_NEW(XDtorOnly, KTestArrayLength, EFalse);
sl@0
   194
	TEST_VEC_NEW(XNoTors, KTestArrayLength, EFalse);
sl@0
   195
	}
sl@0
   196
sl@0
   197
void TrappedUserSideTestVectorNew()
sl@0
   198
	{
sl@0
   199
	RDebug::Printf("User-Side:vector operator new");
sl@0
   200
	RDebug::Printf("OOM tests");
sl@0
   201
sl@0
   202
	#define TEST_VEC_NEW_ELEAVE_OOM(CLASS) \
sl@0
   203
	RDebug::Printf("new(ELeave) " #CLASS "[%d]", KOOMArraySize );\
sl@0
   204
		{\
sl@0
   205
	   	TRAPD(r, new(ELeave) CLASS[KOOMArraySize];)\
sl@0
   206
		test_Equal(KErrNoMemory, r);\
sl@0
   207
		}
sl@0
   208
sl@0
   209
	TEST_VEC_NEW_ELEAVE_OOM(XCtorAndDtor);
sl@0
   210
	TEST_VEC_NEW_ELEAVE_OOM(XCtorOnly);
sl@0
   211
	TEST_VEC_NEW_ELEAVE_OOM(XDtorOnly);
sl@0
   212
	TEST_VEC_NEW_ELEAVE_OOM(XNoTors);
sl@0
   213
sl@0
   214
sl@0
   215
sl@0
   216
	RDebug::Printf("non-OOM tests");
sl@0
   217
	#define TEST_VEC_NEW_ELEAVE(CLASS, ARRAY_LENGTH, TEST_CTOR) \
sl@0
   218
	RDebug::Printf("new(ELeave) " #CLASS "[%d]", ARRAY_LENGTH);\
sl@0
   219
		{\
sl@0
   220
		CLASS* p##CLASS = NULL;\
sl@0
   221
		TRAPD(r, p##CLASS = new(ELeave) CLASS[(ARRAY_LENGTH)]);\
sl@0
   222
		test_KErrNone(r);\
sl@0
   223
		TBool testCtor=(TEST_CTOR);\
sl@0
   224
		if(testCtor)\
sl@0
   225
			{\
sl@0
   226
			TEST_ARRAY_CONSTRUCTION(p##CLASS, ARRAY_LENGTH);\
sl@0
   227
			}\
sl@0
   228
		delete[] p##CLASS;\
sl@0
   229
		}
sl@0
   230
sl@0
   231
	TEST_VEC_NEW_ELEAVE(XCtorAndDtor, KTestArrayLength, ETrue);
sl@0
   232
	TEST_VEC_NEW_ELEAVE(XCtorOnly, KTestArrayLength, ETrue);
sl@0
   233
	TEST_VEC_NEW_ELEAVE(XDtorOnly, KTestArrayLength, EFalse);
sl@0
   234
	TEST_VEC_NEW_ELEAVE(XNoTors, KTestArrayLength, EFalse);
sl@0
   235
	}
sl@0
   236
sl@0
   237
void UserSideTestPlacementNew()
sl@0
   238
	{
sl@0
   239
	RDebug::Printf("::UserSideTestPlacementNew");
sl@0
   240
	
sl@0
   241
	#define TEST_PLACMENT_NEW(CLASS, POST_CTOR_STATE, POST_DTOR_STATE)\
sl@0
   242
		{\
sl@0
   243
		void* someram = User::AllocZ(sizeof(CLASS));\
sl@0
   244
		test_NotNull(someram);\
sl@0
   245
		RDebug::Printf("new (someram) " #CLASS);\
sl@0
   246
		CLASS* p##CLASS = new (someram) CLASS;\
sl@0
   247
		test_Equal(someram, p##CLASS);\
sl@0
   248
		test_Equal(POST_CTOR_STATE, p##CLASS->iState);\
sl@0
   249
		p##CLASS->~CLASS();\
sl@0
   250
		test_Equal(POST_DTOR_STATE, p##CLASS->iState);\
sl@0
   251
		User::Free(someram);\
sl@0
   252
		p##CLASS=NULL;\
sl@0
   253
		}\
sl@0
   254
sl@0
   255
	TEST_PLACMENT_NEW(XCtorAndDtor, EConstructed, EDeconstructed);
sl@0
   256
	TEST_PLACMENT_NEW(XCtorOnly, EConstructed, EConstructed);
sl@0
   257
	TEST_PLACMENT_NEW(XDtorOnly, ENull, EDeconstructed);
sl@0
   258
	TEST_PLACMENT_NEW(XNoTors, ENull, ENull);
sl@0
   259
	}
sl@0
   260
sl@0
   261
sl@0
   262
void UserSideTestPlacementVectorNew()
sl@0
   263
	{
sl@0
   264
	__UHEAP_MARK;
sl@0
   265
sl@0
   266
	RDebug::Printf("::UserSideTestPlacementVectorNew");
sl@0
   267
sl@0
   268
	#define TEST_VEC_PLACEMENT_NEW(CLASS, ARRAY_LENGTH, POST_CTOR_STATE, POST_DTOR_STATE)\
sl@0
   269
	RDebug::Printf("new(someram) " #CLASS "[%d]", ARRAY_LENGTH);\
sl@0
   270
		{\
sl@0
   271
		void* someram = User::AllocZ(sizeof(CLASS) * ARRAY_LENGTH);\
sl@0
   272
		test_NotNull(someram);\
sl@0
   273
		CLASS* p##CLASS = new (someram) CLASS[(ARRAY_LENGTH)];\
sl@0
   274
		for(TInt i=0; i<(ARRAY_LENGTH); ++i)\
sl@0
   275
			{\
sl@0
   276
			test_Equal(POST_CTOR_STATE, p##CLASS[i].iState);\
sl@0
   277
			p##CLASS[i].~CLASS();\
sl@0
   278
			test_Equal(POST_DTOR_STATE, p##CLASS[i].iState);\
sl@0
   279
			}\
sl@0
   280
		User::Free(someram);\
sl@0
   281
		}
sl@0
   282
sl@0
   283
	TEST_VEC_PLACEMENT_NEW(XCtorAndDtor, KTestArrayLength, EConstructed, EDeconstructed);
sl@0
   284
	TEST_VEC_PLACEMENT_NEW(XCtorOnly, KTestArrayLength, EConstructed, EConstructed);
sl@0
   285
	TEST_VEC_PLACEMENT_NEW(XDtorOnly, KTestArrayLength, ENull, EDeconstructed);
sl@0
   286
	TEST_VEC_PLACEMENT_NEW(XNoTors, KTestArrayLength, ENull, ENull);
sl@0
   287
sl@0
   288
	__UHEAP_MARKEND;
sl@0
   289
	}
sl@0
   290
sl@0
   291
sl@0
   292
TInt E32Main()
sl@0
   293
	{
sl@0
   294
	__UHEAP_MARK;
sl@0
   295
sl@0
   296
	test.Start(_L("Testing operator new"));
sl@0
   297
	
sl@0
   298
	test.Next(_L("Installing LDD"));
sl@0
   299
	TInt r=User::LoadLogicalDevice(KKInstallLddName);
sl@0
   300
	test(r==KErrNone || r==KErrAlreadyExists);
sl@0
   301
	
sl@0
   302
	__KHEAP_MARK;
sl@0
   303
sl@0
   304
	#define TEST_THIS(X) test.Next(_L(#X)); (X)
sl@0
   305
	
sl@0
   306
	TEST_THIS(UserSideTestNew());
sl@0
   307
	TEST_THIS(UserSideTestPlacementNew());
sl@0
   308
sl@0
   309
// Workaround for bug in MSVC6/CW compilers. The following test case fails on
sl@0
   310
// WINS, WINSCW and X86 targets (not X86GCC), where the placement vector new
sl@0
   311
// operator does not behave as expected, ultimately resulting in heap corruption
sl@0
   312
// when the parameter to placement array new is passed on the stack.
sl@0
   313
#if !(defined(__WINS__) || (defined(__VC32__) && (_MSC_VER < 1300)))
sl@0
   314
	TEST_THIS(UserSideTestPlacementVectorNew());
sl@0
   315
#else
sl@0
   316
	test.Next(_L("Emulator and/or VC32 - Skipped: UserSideTestPlacementVectorNew"));
sl@0
   317
#endif
sl@0
   318
sl@0
   319
	TEST_THIS(UserSideTestVectorNew());
sl@0
   320
	TEST_THIS(TrappedUserSideTestNew());
sl@0
   321
	TEST_THIS(TrappedUserSideTestVectorNew());
sl@0
   322
	
sl@0
   323
	r=lddconn.Open();
sl@0
   324
	test_KErrNone(r);
sl@0
   325
sl@0
   326
	test.Next(_L("Kernel-side:Normal operator new"));
sl@0
   327
	r = TestNew();
sl@0
   328
	test_KErrNone(r);
sl@0
   329
		
sl@0
   330
	test.Next(_L("Kernel-side:Placement operator new"));
sl@0
   331
	r = TestPlacementNew();
sl@0
   332
	test_KErrNone(r);
sl@0
   333
sl@0
   334
	test.Next(_L("Kernel-side:Placement Vector operator new"));
sl@0
   335
	r = TestPlacementVectorNew();
sl@0
   336
	test_KErrNone(r);
sl@0
   337
sl@0
   338
	test.Next(_L("Kernel-side:Vector operator new"));
sl@0
   339
	r = TestVectorNew();
sl@0
   340
	test_KErrNone(r);
sl@0
   341
sl@0
   342
	r = RTest::CloseHandleAndWaitForDestruction(lddconn);
sl@0
   343
	test_KErrNone(r);
sl@0
   344
sl@0
   345
	test.End();
sl@0
   346
	test.Close();
sl@0
   347
sl@0
   348
	__KHEAP_MARKEND;
sl@0
   349
	__UHEAP_MARKEND;
sl@0
   350
	return KErrNone;
sl@0
   351
	}
sl@0
   352