1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/system/t_dobject.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,492 @@
1.4 +// Copyright (c) 2004-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 +// e32test\system\t_dobject.cpp
1.18 +// Overview:
1.19 +// Test RObjectIx strategy of memory reallocation and
1.20 +// free list maintenance.
1.21 +// Test DObjectCon findhandle methods
1.22 +// API Information:
1.23 +// DObject, RObjectIx
1.24 +// Details:
1.25 +// - Add a number of DObjects to RObjectIx, then remove them
1.26 +// in the same order. Verify results are as expected. Time
1.27 +// how long the process takes to complete.
1.28 +// - Add a number of DObjects to RObjectIx, then remove them
1.29 +// in the reverse order. Verify results are as expected. Time
1.30 +// how long the process takes to complete.
1.31 +// - Add and remove a random number of DObjects to/from RObjectIx.
1.32 +// Time how long the process takes to complete.
1.33 +// Platforms/Drives/Compatibility:
1.34 +// All.
1.35 +// Assumptions/Requirement/Pre-requisites:
1.36 +// Failures and causes:
1.37 +// Base Port information:
1.38 +//
1.39 +//
1.40 +
1.41 +#define __E32TEST_EXTENSION__
1.42 +#include <e32test.h>
1.43 +#include <e32math.h>
1.44 +#include "d_dobject.h"
1.45 +
1.46 +LOCAL_D RTest test(_L("T_DOBJECT"));
1.47 +
1.48 +TInt TestRObjectIxAccess(TAny* aRObjectIxPtr)
1.49 + {
1.50 + const TInt KConcurrentDObjectTestRepeats = 1;
1.51 +
1.52 + RTestDObject ldd;
1.53 + TInt ret;
1.54 +
1.55 + ret = ldd.Open();
1.56 + if (ret == KErrNone)
1.57 + {
1.58 + for (TInt repeat = 0; repeat < KConcurrentDObjectTestRepeats; repeat++)
1.59 + {
1.60 + ret = ldd.RObjectIxThreadTestExerciseIx(aRObjectIxPtr);
1.61 + if (ret != KErrNone)
1.62 + {
1.63 + break;
1.64 + }
1.65 + }
1.66 + ldd.Close();
1.67 + }
1.68 +
1.69 + return ret;
1.70 + } // TestRObjectIxAccess
1.71 +
1.72 +void TestConcurrentRObjectIxAccess(RTestDObject& aLdd)
1.73 + {
1.74 + const TInt KConcurrentDObjectThreads = 4;
1.75 + _LIT(KConcurrentDObjectThreadName, "T_DObject_Thread");
1.76 +
1.77 + TInt ret;
1.78 +
1.79 + //
1.80 + // Create a RObjectIx in the driver (pointer not valid user side!)...
1.81 + //
1.82 + void* objectIxPtr = NULL;
1.83 +
1.84 + ret = aLdd.RObjectIxThreadTestCreateIx(objectIxPtr);
1.85 + test_KErrNone(ret);
1.86 +
1.87 + //
1.88 + // Run KConcurrentDObjectThreads number of threads which random add/remove
1.89 + // DObjects to the same RObjectIx...
1.90 + //
1.91 + RThread threadHandle[KConcurrentDObjectThreads];
1.92 + TRequestStatus status[KConcurrentDObjectThreads];
1.93 + TBuf<32> threadName;
1.94 + TInt thread;
1.95 +
1.96 + for (thread = 0; thread < KConcurrentDObjectThreads; thread++)
1.97 + {
1.98 + threadName.Copy(KConcurrentDObjectThreadName);
1.99 + threadName.AppendNum(thread);
1.100 +
1.101 + ret = threadHandle[thread].Create(threadName, TestRObjectIxAccess, KDefaultStackSize, NULL, objectIxPtr);
1.102 + test_KErrNone(ret);
1.103 +
1.104 + threadHandle[thread].Logon(status[thread]);
1.105 + }
1.106 +
1.107 + //
1.108 + // The test thread must be higher priority to ensure all the threads start.
1.109 + // All the threads are then resumed and allowed to run to completion...
1.110 + //
1.111 + RThread().SetPriority(EPriorityMore);
1.112 +
1.113 + for (thread = 0; thread < KConcurrentDObjectThreads; thread++)
1.114 + {
1.115 + threadHandle[thread].Resume();
1.116 + }
1.117 +
1.118 + for (thread = 0; thread < KConcurrentDObjectThreads; thread++)
1.119 + {
1.120 + User::WaitForRequest(status[thread]);
1.121 + test_KErrNone(status[thread].Int());
1.122 + CLOSE_AND_WAIT(threadHandle[thread]);
1.123 + }
1.124 +
1.125 + RThread().SetPriority(EPriorityNormal);
1.126 +
1.127 + //
1.128 + // Free the RObjectIx in the driver...
1.129 + //
1.130 + ret = aLdd.RObjectIxThreadTestFreeIx(objectIxPtr);
1.131 + test_KErrNone(ret);
1.132 + } // TestConcurrentRObjectIxAccess
1.133 +
1.134 +
1.135 +void ListAllMutexes()
1.136 + {
1.137 + test.Printf(_L("Mutexes:\n"));
1.138 + TFullName name;
1.139 + TFindMutex find;
1.140 + while (find.Next(name) == KErrNone)
1.141 + {
1.142 + test.Printf(_L(" %S (find handle == %08x)\n"), &name, find.Handle());
1.143 + }
1.144 + }
1.145 +
1.146 +const TInt KObjectCount = 20;
1.147 +_LIT(KDoubleMatch, "*double*");
1.148 +_LIT(KTrippleMatch, "*tripple*");
1.149 +
1.150 +RMutex Mutexes[KObjectCount];
1.151 +TBuf<32> ObjectName;
1.152 +
1.153 +const TDesC& MutexName(TInt i)
1.154 + {
1.155 + ObjectName.Zero();
1.156 + ObjectName.AppendFormat(_L("Mutex_%02d"), i);
1.157 + if (i % 2 == 0)
1.158 + ObjectName.Append(_L("_double"));
1.159 + if (i % 3 == 0)
1.160 + ObjectName.Append(_L("_tripple"));
1.161 + return ObjectName;
1.162 + }
1.163 +
1.164 +void CreateMutexes()
1.165 + {
1.166 + for (TInt i = 0 ; i < KObjectCount ; ++i)
1.167 + {
1.168 + test(Mutexes[i].CreateGlobal(MutexName(i)) == KErrNone);
1.169 + }
1.170 + }
1.171 +
1.172 +void DeleteMutexes()
1.173 + {
1.174 + for (TInt i = 0 ; i < KObjectCount ; ++i)
1.175 + {
1.176 + Mutexes[i].Close();
1.177 + }
1.178 + }
1.179 +
1.180 +void TestMutexesCreated()
1.181 + {
1.182 + test.Next(_L("Test mutexes have been created"));
1.183 +
1.184 + TFullName name;
1.185 +
1.186 + for (TInt i = 0 ; i < KObjectCount ; ++i)
1.187 + {
1.188 + TFindMutex find(MutexName(i));
1.189 + test(find.Next(name) == KErrNone);
1.190 + test.Printf(_L(" %02d: found handle %08x\n"), i, find.Handle());
1.191 + }
1.192 + }
1.193 +
1.194 +void TestMutexesDeleted()
1.195 + {
1.196 + test.Next(_L("Test mutexes deleted"));
1.197 +
1.198 + TFullName name;
1.199 +
1.200 + for (TInt i = 0 ; i < KObjectCount ; ++i)
1.201 + {
1.202 + TFindMutex find(MutexName(i));
1.203 + test(find.Next(name) == KErrNotFound);
1.204 + }
1.205 + }
1.206 +
1.207 +
1.208 +void TestFindSpecificMutex()
1.209 + {
1.210 + test.Next(_L("Test finding specific mutexes"));
1.211 +
1.212 + for (TInt i = 0 ; i < KObjectCount ; ++i)
1.213 + {
1.214 + TFullName name;
1.215 + TFindMutex find(MutexName(i));
1.216 + test(find.Next(name) == KErrNone);
1.217 + test.Printf(_L(" %02d: found handle %08x\n"), i, find.Handle());
1.218 + test(name == MutexName(i));
1.219 + RMutex mutex;
1.220 + test(mutex.Open(find) == KErrNone);
1.221 + test(mutex.Name() == MutexName(i));
1.222 + mutex.Close();
1.223 + test(find.Next(name) == KErrNotFound);
1.224 + }
1.225 + }
1.226 +
1.227 +void TestFindMutexGroups()
1.228 + {
1.229 + test.Next(_L("Test finding groups of mutexes using wildcard name matching"));
1.230 +
1.231 + TFullName name;
1.232 + TInt i;
1.233 +
1.234 + TFindMutex find2(KDoubleMatch);
1.235 + for (i = 0 ; i < KObjectCount ; i += 2)
1.236 + {
1.237 + test(find2.Next(name) == KErrNone);
1.238 + test.Printf(_L(" %02d: found handle %08x\n"), i, find2.Handle());
1.239 + test(name == MutexName(i));
1.240 + }
1.241 + test(find2.Next(name) == KErrNotFound);
1.242 +
1.243 + TFindMutex find3(KTrippleMatch);
1.244 + for (i = 0 ; i < KObjectCount ; i += 3)
1.245 + {
1.246 + test(find3.Next(name) == KErrNone);
1.247 + test.Printf(_L(" %02d: found handle %08x\n"), i, find3.Handle());
1.248 + test(name == MutexName(i));
1.249 + }
1.250 + test(find3.Next(name) == KErrNotFound);
1.251 + }
1.252 +
1.253 +void TestMatchChange()
1.254 + {
1.255 + test.Next(_L("Test changing match half way through find"));
1.256 +
1.257 +
1.258 + TFullName name;
1.259 + TInt i;
1.260 +
1.261 + TFindMutex find(KDoubleMatch);
1.262 + for (i = 0 ; i < KObjectCount/2 ; i += 2)
1.263 + {
1.264 + test(find.Next(name) == KErrNone);
1.265 + test.Printf(_L(" %02d: found handle %08x\n"), i, find.Handle());
1.266 + test(name == MutexName(i));
1.267 + }
1.268 +
1.269 + find.Find(KTrippleMatch);
1.270 + for (i = 0 ; i < KObjectCount ; i += 3)
1.271 + {
1.272 + test(find.Next(name) == KErrNone);
1.273 + test.Printf(_L(" %02d: found handle %08x\n"), i, find.Handle());
1.274 + test(name == MutexName(i));
1.275 + }
1.276 + test(find.Next(name) == KErrNotFound);
1.277 + }
1.278 +
1.279 +void TestFindAndDeleteMutex1()
1.280 + {
1.281 + test.Next(_L("Test finding mutexes when the last found object has been deleted"));
1.282 +
1.283 + // Find and delete even mutexes
1.284 + TFullName name;
1.285 + TInt i;
1.286 + for (i = 0 ; i < KObjectCount ; i += 2)
1.287 + {
1.288 + TFindMutex find2(MutexName(i));
1.289 + test(find2.Next(name) == KErrNone);
1.290 + test.Printf(_L(" %02d: found handle %08x\n"), i, find2.Handle());
1.291 + Mutexes[i].Close();
1.292 + RMutex mutex;
1.293 + test(mutex.Open(find2) == KErrNotFound);
1.294 + }
1.295 +
1.296 + // Check odd mutexes remaining
1.297 + for (i = 1 ; i < KObjectCount ; i += 2)
1.298 + {
1.299 + TFindMutex find(MutexName(i));
1.300 + test(find.Next(name) == KErrNone);
1.301 + }
1.302 + }
1.303 +
1.304 +void TestFindAndDeleteMutex2()
1.305 + {
1.306 + test.Next(_L("Test finding mutexes when the last found object has moved in the container"));
1.307 +
1.308 + // Find even mutexes and delete odd
1.309 + TFullName name;
1.310 + TInt i;
1.311 + for (i = 0 ; i < KObjectCount ; i += 2)
1.312 + {
1.313 + TFindMutex find2(MutexName(i));
1.314 + test(find2.Next(name) == KErrNone);
1.315 + test.Printf(_L(" %02d: found handle %08x\n"), i, find2.Handle());
1.316 + Mutexes[(i+KObjectCount-1)%KObjectCount].Close(); // -1%n = -1 or n-1, unspecified
1.317 + RMutex mutex;
1.318 + test(mutex.Open(find2) == KErrNone);
1.319 + test(mutex.Name() == MutexName(i));
1.320 + mutex.Close();
1.321 + }
1.322 +
1.323 + // Check even mutexes remaining
1.324 + for (i = 0 ; i < KObjectCount ; i += 2)
1.325 + {
1.326 + TFindMutex find(MutexName(i));
1.327 + test(find.Next(name) == KErrNone);
1.328 + }
1.329 +
1.330 + }
1.331 +
1.332 +void TestFindWithCreation()
1.333 + {
1.334 + test.Next(_L("Test finding mutexes interleaved with creation"));
1.335 +
1.336 + TFullName name;
1.337 +
1.338 + for (TInt i = 0 ; i < KObjectCount ; ++i)
1.339 + {
1.340 + test(Mutexes[i].CreateGlobal(MutexName(i)) == KErrNone);
1.341 + TFindMutex find(MutexName(i));
1.342 + test(find.Next(name) == KErrNone);
1.343 + test.Printf(_L(" %02d: found handle %08x\n"), i, find.Handle());
1.344 + RMutex mutex;
1.345 + test(mutex.Open(find) == KErrNone);
1.346 + test(mutex.Name() == MutexName(i));
1.347 + mutex.Close();
1.348 + }
1.349 + }
1.350 +
1.351 +void TestFindWithCreation2()
1.352 + {
1.353 + test.Next(_L("Test finding mutexes interleaved with creation and deletion"));
1.354 +
1.355 + TFullName name;
1.356 +
1.357 + for (TInt i = 0 ; i < KObjectCount ; ++i)
1.358 + {
1.359 + RMutex mutex;
1.360 + test(mutex.CreateGlobal(MutexName(0)) == KErrNone);
1.361 + TFindMutex find(MutexName(0));
1.362 + test(find.Next(name) == KErrNone);
1.363 + test(name == MutexName(0));
1.364 + test.Printf(_L(" %02d: found handle %08x\n"), i, find.Handle());
1.365 + mutex.Close();
1.366 +
1.367 + TFindMutex find2(MutexName(0));
1.368 + test(find2.Next(name) == KErrNotFound);
1.369 + }
1.370 + }
1.371 +
1.372 +void TestFindHandleOutOfRange()
1.373 + {
1.374 + test.Next(_L("Test finding mutexes when find handle index is off the end of container's array"));
1.375 +
1.376 + TFullName name;
1.377 +
1.378 + for (TInt i = 0 ; i < KObjectCount ; ++i)
1.379 + {
1.380 + TFindMutex find(MutexName(i));
1.381 + test(find.Next(name) == KErrNone);
1.382 + test.Printf(_L(" %02d: found handle %08x\n"), i, find.Handle());
1.383 + RMutex mutex;
1.384 + test(mutex.Open(find) == KErrNone);
1.385 + test(mutex.Name() == MutexName(i));
1.386 + mutex.Close();
1.387 +
1.388 + // towards the end, suddenly delete half the mutexes
1.389 + if (i == (3 * KObjectCount) / 4)
1.390 + {
1.391 + for (TInt j = 0 ; j < KObjectCount / 2 ; ++j)
1.392 + Mutexes[j].Close();
1.393 + }
1.394 + }
1.395 + }
1.396 +
1.397 +void TestFindHandles()
1.398 + {
1.399 + test.Start(_L("Test FindHandle APIs using mutex classes"));
1.400 +
1.401 + CreateMutexes();
1.402 + ListAllMutexes();
1.403 + TestMutexesCreated();
1.404 + TestFindSpecificMutex();
1.405 + TestFindMutexGroups();
1.406 + TestMatchChange();
1.407 + DeleteMutexes();
1.408 + TestMutexesDeleted();
1.409 +
1.410 + CreateMutexes();
1.411 + TestFindAndDeleteMutex1();
1.412 + DeleteMutexes();
1.413 +
1.414 + CreateMutexes();
1.415 + TestFindHandleOutOfRange();
1.416 + DeleteMutexes();
1.417 +
1.418 + CreateMutexes();
1.419 + TestFindAndDeleteMutex2();
1.420 + DeleteMutexes();
1.421 +
1.422 + TestFindWithCreation();
1.423 + DeleteMutexes();
1.424 +
1.425 + TestFindWithCreation2();
1.426 + TestMutexesDeleted();
1.427 +
1.428 + test.End();
1.429 + }
1.430 +
1.431 +GLDEF_C TInt E32Main()
1.432 + {
1.433 + SParam param;
1.434 + TInt duration, r;
1.435 + RTestDObject ldd;
1.436 +
1.437 + test.Title();
1.438 +
1.439 + test.Start(_L("Loading test driver..."));
1.440 +
1.441 + r=User::LoadLogicalDevice(KDObjectTestLddName);
1.442 + test(r==KErrNone || r==KErrAlreadyExists);
1.443 + r=ldd.Open();
1.444 + test(r==KErrNone);
1.445 +
1.446 + test.Next(_L("RObjectIxTest1 test ..."));
1.447 + r=ldd.RObjectIxTest1(duration);
1.448 + test(KErrNone==r);
1.449 + test.Printf(_L("... completed in %d kernel ticks\n") , duration);
1.450 +
1.451 + test.Next(_L("RObjectIxTest2 test ..."));
1.452 + r=ldd.RObjectIxTest2(duration);
1.453 + test(KErrNone==r);
1.454 + test.Printf(_L("... completed in %d kernel ticks\n") , duration);
1.455 +
1.456 + test.Next(_L("RObjectIxTest3 test (performance) ..."));
1.457 + param.iSeed[0] = 0;
1.458 + param.iSeed[1] = 1;
1.459 + param.iPerformanceTest = ETrue;
1.460 + r=ldd.RObjectIxTest3(param);
1.461 + test(KErrNone==r);
1.462 + test.Printf(_L("... completed in %d kernel ticks\n") , param.duration);
1.463 +
1.464 + test.Next(_L("RObjectIxTest3 test (random)..."));
1.465 + param.iSeed[0]=User::TickCount();
1.466 + param.iSeed[1]=User::TickCount();
1.467 + param.iPerformanceTest = EFalse;
1.468 + test.Printf(_L("... seeds=%xh and %xh ..."),param.iSeed[0],param.iSeed[1]);
1.469 + r=ldd.RObjectIxTest3(param);
1.470 + test(KErrNone==r);
1.471 + test.Printf(_L("... completed in %d kernel ticks\n") , param.duration);
1.472 +
1.473 + test.Next(_L("RObjectIxTest4 test (reserved slots)..."));
1.474 + test_KErrNone(ldd.RObjectIxTest4(duration));
1.475 + test.Printf(_L("... completed in %d kernel ticks\n") , duration);
1.476 +
1.477 + test.Next(_L("Test Concurrent access to RObjectIx"));
1.478 + TestConcurrentRObjectIxAccess(ldd);
1.479 +
1.480 + test.Next(_L("Test Invalid handle look up"));
1.481 + test_KErrNone(ldd.InvalidHandleLookupTest());
1.482 +
1.483 + test.Next(_L("Test Kern::ValidateName and Kern::ValidateFullName"));
1.484 + test_KErrNone(ldd.DObjectNameTest());
1.485 +
1.486 + test.Next(_L("Closing test driver"));
1.487 + ldd.Close();
1.488 +
1.489 + test.Next(_L("FindHandles test"));
1.490 + TestFindHandles();
1.491 +
1.492 + test.End();
1.493 +
1.494 + return(0);
1.495 + }