kerneltest/e32test/system/t_dobject.cpp
changeset 0 a41df078684a
child 36 538db54a451d
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2004-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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\system\t_dobject.cpp
       
    15 // Overview:
       
    16 // Test RObjectIx strategy of memory reallocation and 
       
    17 // free list maintenance.
       
    18 // Test DObjectCon findhandle methods
       
    19 // API Information:
       
    20 // DObject, RObjectIx
       
    21 // Details:
       
    22 // - Add a number of DObjects to RObjectIx, then remove them 
       
    23 // in the same order. Verify results are as expected. Time
       
    24 // how long the process takes to complete.
       
    25 // - Add a number of DObjects to RObjectIx, then remove them
       
    26 // in the reverse order. Verify results are as expected. Time
       
    27 // how long the process takes to complete.
       
    28 // - Add and remove a random number of DObjects to/from RObjectIx.
       
    29 // Time how long the process takes to complete.
       
    30 // Platforms/Drives/Compatibility:
       
    31 // All.
       
    32 // Assumptions/Requirement/Pre-requisites:
       
    33 // Failures and causes:
       
    34 // Base Port information:
       
    35 // 
       
    36 //
       
    37 
       
    38 #define __E32TEST_EXTENSION__
       
    39 #include <e32test.h>
       
    40 #include <e32math.h>
       
    41 #include "d_dobject.h"
       
    42 
       
    43 LOCAL_D RTest test(_L("T_DOBJECT"));
       
    44 
       
    45 TInt TestRObjectIxAccess(TAny* aRObjectIxPtr)
       
    46 	{
       
    47 	const TInt  KConcurrentDObjectTestRepeats = 1;
       
    48 	
       
    49 	RTestDObject  ldd;
       
    50 	TInt  ret;
       
    51 	
       
    52 	ret = ldd.Open();
       
    53 	if (ret == KErrNone)
       
    54 		{
       
    55 		for (TInt  repeat = 0;  repeat < KConcurrentDObjectTestRepeats;  repeat++)
       
    56 			{
       
    57 			ret = ldd.RObjectIxThreadTestExerciseIx(aRObjectIxPtr);
       
    58 			if (ret != KErrNone)
       
    59 				{
       
    60 				break;
       
    61 				}
       
    62 			}
       
    63 		ldd.Close();
       
    64 		}
       
    65 	
       
    66 	return ret;
       
    67 	} // TestRObjectIxAccess
       
    68 
       
    69 void TestConcurrentRObjectIxAccess(RTestDObject& aLdd)
       
    70 	{
       
    71 	const TInt  KConcurrentDObjectThreads = 4;
       
    72 	_LIT(KConcurrentDObjectThreadName, "T_DObject_Thread");
       
    73 	
       
    74 	TInt ret;
       
    75 	
       
    76 	//
       
    77 	// Create a RObjectIx in the driver (pointer not valid user side!)...
       
    78 	//
       
    79 	void*  objectIxPtr = NULL;
       
    80 	
       
    81 	ret = aLdd.RObjectIxThreadTestCreateIx(objectIxPtr);
       
    82 	test_KErrNone(ret);
       
    83 	
       
    84 	//
       
    85 	// Run KConcurrentDObjectThreads number of threads which random add/remove
       
    86 	// DObjects to the same RObjectIx...
       
    87 	//
       
    88 	RThread  threadHandle[KConcurrentDObjectThreads];
       
    89 	TRequestStatus  status[KConcurrentDObjectThreads];
       
    90 	TBuf<32>  threadName;
       
    91 	TInt  thread;
       
    92 
       
    93 	for (thread = 0;  thread < KConcurrentDObjectThreads;  thread++)
       
    94 		{
       
    95 		threadName.Copy(KConcurrentDObjectThreadName);
       
    96 		threadName.AppendNum(thread);
       
    97 		
       
    98 		ret = threadHandle[thread].Create(threadName, TestRObjectIxAccess, KDefaultStackSize, NULL, objectIxPtr);
       
    99 		test_KErrNone(ret);
       
   100 
       
   101 		threadHandle[thread].Logon(status[thread]);
       
   102 		}
       
   103 
       
   104 	//
       
   105 	// The test thread must be higher priority to ensure all the threads start.
       
   106 	// All the threads are then resumed and allowed to run to completion...
       
   107 	//
       
   108 	RThread().SetPriority(EPriorityMore);
       
   109 
       
   110 	for (thread = 0;  thread < KConcurrentDObjectThreads;  thread++)
       
   111 		{
       
   112 		threadHandle[thread].Resume();
       
   113 		}
       
   114 
       
   115 	for (thread = 0;  thread < KConcurrentDObjectThreads;  thread++)
       
   116 		{
       
   117 		User::WaitForRequest(status[thread]);
       
   118 		test_KErrNone(status[thread].Int());
       
   119 		CLOSE_AND_WAIT(threadHandle[thread]);
       
   120 		}
       
   121 	
       
   122 	RThread().SetPriority(EPriorityNormal);
       
   123 
       
   124 	//
       
   125 	// Free the RObjectIx in the driver...
       
   126 	//
       
   127 	ret = aLdd.RObjectIxThreadTestFreeIx(objectIxPtr);
       
   128 	test_KErrNone(ret);
       
   129 	} // TestConcurrentRObjectIxAccess
       
   130 
       
   131 
       
   132 void ListAllMutexes()
       
   133 	{
       
   134 	test.Printf(_L("Mutexes:\n"));
       
   135 	TFullName name;
       
   136 	TFindMutex find;
       
   137 	while (find.Next(name) == KErrNone)
       
   138 		{
       
   139 		test.Printf(_L("  %S (find handle == %08x)\n"), &name, find.Handle());
       
   140 		}
       
   141 	}
       
   142 
       
   143 const TInt KObjectCount = 20;
       
   144 _LIT(KDoubleMatch, "*double*");
       
   145 _LIT(KTrippleMatch, "*tripple*");
       
   146 
       
   147 RMutex Mutexes[KObjectCount];
       
   148 TBuf<32> ObjectName;
       
   149 
       
   150 const TDesC& MutexName(TInt i)
       
   151 	{
       
   152 	ObjectName.Zero();
       
   153 	ObjectName.AppendFormat(_L("Mutex_%02d"), i);
       
   154 	if (i % 2 == 0)
       
   155 		ObjectName.Append(_L("_double"));
       
   156 	if (i % 3 == 0)
       
   157 		ObjectName.Append(_L("_tripple"));
       
   158 	return ObjectName;
       
   159 	}
       
   160 
       
   161 void CreateMutexes()
       
   162 	{
       
   163 	for (TInt i = 0 ; i < KObjectCount ; ++i)
       
   164 		{
       
   165 		test(Mutexes[i].CreateGlobal(MutexName(i)) == KErrNone);
       
   166 		}
       
   167 	}
       
   168 
       
   169 void DeleteMutexes()
       
   170 	{
       
   171 	for (TInt i = 0 ; i < KObjectCount ; ++i)
       
   172 		{
       
   173 		Mutexes[i].Close();
       
   174 		}
       
   175 	}
       
   176 
       
   177 void TestMutexesCreated()
       
   178 	{
       
   179 	test.Next(_L("Test mutexes have been created"));
       
   180 	
       
   181 	TFullName name;
       
   182 	TFindMutex find;
       
   183 	for (TInt i = 0 ; i < KObjectCount ; ++i)
       
   184 		{
       
   185 		test(find.Next(name) == KErrNone);
       
   186 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
       
   187 		test(name == MutexName(i));
       
   188 		}
       
   189 	test(find.Next(name) == KErrNotFound);
       
   190 	}
       
   191 
       
   192 void TestMutexesDeleted()
       
   193 	{
       
   194 	test.Next(_L("Test mutexes deleted"));
       
   195 
       
   196 	TFindMutex find;
       
   197 	TFullName name;
       
   198 	test(find.Next(name) == KErrNotFound);
       
   199 	}
       
   200 
       
   201 void TestFindSpecificMutex()
       
   202 	{
       
   203 	test.Next(_L("Test finding specific mutexes"));
       
   204 	
       
   205 	for (TInt i = 0 ; i < KObjectCount ; ++i)
       
   206 		{
       
   207 		TFullName name;
       
   208 		TFindMutex find(MutexName(i));
       
   209 		test(find.Next(name) == KErrNone);
       
   210 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
       
   211 		test(name == MutexName(i));
       
   212 		RMutex mutex;
       
   213 		test(mutex.Open(find) == KErrNone);
       
   214 		test(mutex.Name() == MutexName(i));
       
   215 		mutex.Close();
       
   216 		test(find.Next(name) == KErrNotFound);
       
   217 		}	
       
   218 	}
       
   219 
       
   220 void TestFindMutexGroups()
       
   221 	{
       
   222 	test.Next(_L("Test finding groups of mutexes using wildcard name matching"));
       
   223 
       
   224 	TFullName name;
       
   225 	TInt i;
       
   226 	
       
   227 	TFindMutex find2(KDoubleMatch);
       
   228 	for (i = 0 ; i < KObjectCount ; i += 2)
       
   229 		{
       
   230 		test(find2.Next(name) == KErrNone);
       
   231 		test.Printf(_L("  %02d: found handle %08x\n"), i, find2.Handle());
       
   232 		test(name == MutexName(i));
       
   233 		}
       
   234 	test(find2.Next(name) == KErrNotFound);
       
   235 
       
   236 	TFindMutex find3(KTrippleMatch);
       
   237 	for (i = 0 ; i < KObjectCount ; i += 3)
       
   238 		{
       
   239 		test(find3.Next(name) == KErrNone);
       
   240 		test.Printf(_L("  %02d: found handle %08x\n"), i, find3.Handle());
       
   241 		test(name == MutexName(i));
       
   242 		}
       
   243 	test(find3.Next(name) == KErrNotFound);
       
   244 	}
       
   245 
       
   246 void TestMatchChange()
       
   247 	{
       
   248 	test.Next(_L("Test changing match half way through find"));
       
   249 
       
   250 
       
   251 	TFullName name;
       
   252 	TInt i;
       
   253 	
       
   254 	TFindMutex find(KDoubleMatch);
       
   255 	for (i = 0 ; i < KObjectCount/2 ; i += 2)
       
   256 		{
       
   257 		test(find.Next(name) == KErrNone);
       
   258 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
       
   259 		test(name == MutexName(i));
       
   260 		}
       
   261 
       
   262 	find.Find(KTrippleMatch);
       
   263 	for (i = 0 ; i < KObjectCount ; i += 3)
       
   264 		{
       
   265 		test(find.Next(name) == KErrNone);
       
   266 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
       
   267 		test(name == MutexName(i));
       
   268 		}
       
   269 	test(find.Next(name) == KErrNotFound);
       
   270 	}
       
   271 
       
   272 void TestFindAndDeleteMutex1()
       
   273 	{
       
   274 	test.Next(_L("Test finding mutexes when the last found object has been deleted"));
       
   275 
       
   276 	// Find and delete even mutexes
       
   277 	TFullName name;
       
   278 	TFindMutex find2(KDoubleMatch);
       
   279 	TInt i;
       
   280 	for (i = 0 ; i < KObjectCount ; i += 2)
       
   281 		{
       
   282 		test(find2.Next(name) == KErrNone);
       
   283 		test.Printf(_L("  %02d: found handle %08x\n"), i, find2.Handle());
       
   284 		test(name == MutexName(i));
       
   285 		Mutexes[i].Close();
       
   286 		RMutex mutex;
       
   287 		test(mutex.Open(find2) == KErrNotFound);
       
   288 		}
       
   289 	test(find2.Next(name) == KErrNotFound);
       
   290 
       
   291 	// Check odd mutexes remaining
       
   292 	TFindMutex find;
       
   293 	for (i = 1 ; i < KObjectCount ; i += 2)
       
   294 		{
       
   295 		test(find.Next(name) == KErrNone);
       
   296 		test(name == MutexName(i));
       
   297 		}
       
   298 	test(find2.Next(name) == KErrNotFound);
       
   299 	}
       
   300 
       
   301 void TestFindAndDeleteMutex2()
       
   302 	{
       
   303 	test.Next(_L("Test finding mutexes when the last found object has moved in the container"));
       
   304 
       
   305 	// Find even mutexes and delete odd
       
   306 	TFullName name;
       
   307 	TFindMutex find2(KDoubleMatch);
       
   308 	TInt i;
       
   309 	for (i = 0 ; i < KObjectCount ; i += 2)
       
   310 		{
       
   311 		test(find2.Next(name) == KErrNone);
       
   312 		test.Printf(_L("  %02d: found handle %08x\n"), i, find2.Handle());
       
   313 		test(name == MutexName(i));
       
   314 		Mutexes[(i+KObjectCount-1)%KObjectCount].Close();	// -1%n = -1 or n-1, unspecified
       
   315 		RMutex mutex;
       
   316 		test(mutex.Open(find2) == KErrNone);
       
   317 		test(mutex.Name() == MutexName(i));
       
   318 		mutex.Close();
       
   319 		}
       
   320 	test(find2.Next(name) == KErrNotFound);
       
   321 
       
   322 	// Check even mutexes remaining
       
   323 	TFindMutex find;
       
   324 	for (i = 0 ; i < KObjectCount ; i += 2)
       
   325 		{
       
   326 		test(find.Next(name) == KErrNone);
       
   327 		test(name == MutexName(i));
       
   328 		}
       
   329 	test(find2.Next(name) == KErrNotFound);
       
   330 	}
       
   331 
       
   332 void TestFindWithCreation()
       
   333 	{
       
   334 	test.Next(_L("Test finding mutexes interleaved with creation"));
       
   335 
       
   336 	TFullName name;
       
   337 	TFindMutex find;
       
   338 	
       
   339 	for (TInt i = 0 ; i < KObjectCount ; ++i)
       
   340 		{
       
   341 		test(Mutexes[i].CreateGlobal(MutexName(i)) == KErrNone);
       
   342 		test(find.Next(name) == KErrNone);
       
   343 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
       
   344 		test(name == MutexName(i));
       
   345 		RMutex mutex;
       
   346 		test(mutex.Open(find) == KErrNone);
       
   347 		test(mutex.Name() == MutexName(i));
       
   348 		mutex.Close();
       
   349 		}
       
   350 	test(find.Next(name) == KErrNotFound);
       
   351 	}
       
   352 
       
   353 void TestFindWithCreation2()
       
   354 	{
       
   355 	test.Next(_L("Test finding mutexes interleaved with creation and deletion"));
       
   356 
       
   357 	TFullName name;
       
   358 	TFindMutex find;
       
   359 
       
   360 	for (TInt i = 0 ; i < KObjectCount ; ++i)
       
   361 		{
       
   362 		RMutex mutex;
       
   363 		test(mutex.CreateGlobal(MutexName(0)) == KErrNone);
       
   364 		test(find.Next(name) == KErrNone);
       
   365 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
       
   366 		test(name == MutexName(0));
       
   367 		mutex.Close();
       
   368 		RMutex mutex2;
       
   369 		test(mutex2.Open(find) == KErrNotFound);
       
   370 		}
       
   371 	test(find.Next(name) == KErrNotFound);
       
   372 	}
       
   373 
       
   374 void TestFindHandleOutOfRange()
       
   375 	{
       
   376 	test.Next(_L("Test finding mutexes when find handle index is off the end of container's array"));
       
   377 
       
   378 	TFullName name;
       
   379 	TFindMutex find;
       
   380 	for (TInt i = 0 ; i < KObjectCount ; ++i)
       
   381 		{
       
   382 		test(find.Next(name) == KErrNone);
       
   383 		test.Printf(_L("  %02d: found handle %08x\n"), i, find.Handle());
       
   384 		test(name == MutexName(i));
       
   385 		RMutex mutex;
       
   386 		test(mutex.Open(find) == KErrNone);
       
   387 		test(mutex.Name() == MutexName(i));
       
   388 		mutex.Close();
       
   389 
       
   390 		// towards the end, suddenly delete half the mutexes
       
   391 		if (i == (3 * KObjectCount) / 4)
       
   392 			{
       
   393 			for (TInt j = 0 ; j < KObjectCount / 2 ; ++j)
       
   394 				Mutexes[j].Close();
       
   395 			}
       
   396 		}
       
   397 	test(find.Next(name) == KErrNotFound);
       
   398 	}
       
   399 
       
   400 void TestFindHandles()
       
   401 	{
       
   402 	test.Start(_L("Test FindHandle APIs using mutex classes"));
       
   403 
       
   404 	CreateMutexes();
       
   405 	ListAllMutexes();
       
   406 	TestMutexesCreated();
       
   407 	TestFindSpecificMutex();
       
   408 	TestFindMutexGroups();
       
   409 	TestMatchChange();
       
   410 	DeleteMutexes();
       
   411 	TestMutexesDeleted();
       
   412 
       
   413 	CreateMutexes();
       
   414 	TestFindAndDeleteMutex1();
       
   415 	DeleteMutexes();
       
   416 
       
   417 	CreateMutexes();
       
   418 	TestFindHandleOutOfRange();
       
   419 	DeleteMutexes();
       
   420 	
       
   421 	CreateMutexes();
       
   422 	TestFindAndDeleteMutex2();
       
   423 	DeleteMutexes();
       
   424 
       
   425 	TestFindWithCreation();
       
   426 	DeleteMutexes();
       
   427 
       
   428 	TestFindWithCreation2();
       
   429 	TestMutexesDeleted();
       
   430 	
       
   431 	test.End();
       
   432 	}
       
   433 
       
   434 GLDEF_C TInt E32Main()
       
   435     {
       
   436 	SParam param;
       
   437 	TInt duration, r;
       
   438 	RTestDObject ldd;
       
   439 
       
   440 	test.Title();
       
   441 
       
   442 	test.Start(_L("Loading test driver..."));
       
   443 
       
   444 	r=User::LoadLogicalDevice(KDObjectTestLddName);
       
   445 	test(r==KErrNone || r==KErrAlreadyExists);
       
   446 	r=ldd.Open();
       
   447 	test(r==KErrNone);
       
   448 
       
   449 	test.Next(_L("RObjectIxTest1 test ..."));
       
   450 	r=ldd.RObjectIxTest1(duration);
       
   451 	test(KErrNone==r);
       
   452 	test.Printf(_L("... completed in %d kernel ticks\n") , duration);
       
   453 
       
   454 	test.Next(_L("RObjectIxTest2 test ..."));
       
   455 	r=ldd.RObjectIxTest2(duration);
       
   456 	test(KErrNone==r);
       
   457 	test.Printf(_L("... completed in %d kernel ticks\n") , duration);
       
   458 
       
   459 	test.Next(_L("RObjectIxTest3 test (performance) ..."));
       
   460 	param.iSeed[0] = 0;
       
   461 	param.iSeed[1] = 1;
       
   462 	param.iPerformanceTest = ETrue;
       
   463 	r=ldd.RObjectIxTest3(param);
       
   464 	test(KErrNone==r);
       
   465 	test.Printf(_L("... completed in %d kernel ticks\n") , param.duration);
       
   466 
       
   467 	test.Next(_L("RObjectIxTest3 test (random)..."));
       
   468 	param.iSeed[0]=User::TickCount();
       
   469 	param.iSeed[1]=User::TickCount();
       
   470 	param.iPerformanceTest = EFalse;
       
   471 	test.Printf(_L("... seeds=%xh and %xh ..."),param.iSeed[0],param.iSeed[1]);
       
   472 	r=ldd.RObjectIxTest3(param);
       
   473 	test(KErrNone==r);
       
   474 	test.Printf(_L("... completed in %d kernel ticks\n") , param.duration);
       
   475  
       
   476 	test.Next(_L("RObjectIxTest4 test (reserved slots)..."));
       
   477 	test_KErrNone(ldd.RObjectIxTest4(duration));
       
   478 	test.Printf(_L("... completed in %d kernel ticks\n") , duration);
       
   479 
       
   480 	test.Next(_L("Test Concurrent access to RObjectIx"));
       
   481 	TestConcurrentRObjectIxAccess(ldd);
       
   482 
       
   483 	test.Next(_L("Test Invalid handle look up"));
       
   484  	test_KErrNone(ldd.InvalidHandleLookupTest());
       
   485 
       
   486  	test.Next(_L("Test Kern::ValidateName and Kern::ValidateFullName"));
       
   487  	test_KErrNone(ldd.DObjectNameTest());
       
   488  
       
   489 	test.Next(_L("Closing test driver"));
       
   490 	ldd.Close();
       
   491 
       
   492 	test.Next(_L("FindHandles test"));
       
   493 	TestFindHandles();
       
   494 	
       
   495 	test.End();
       
   496 
       
   497 	return(0);
       
   498 	}