1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/lowlevellibsandfws/genericusabilitylib/test/src/t_emanagedLcleanup.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,427 @@
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 "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 +// Overview:
1.18 +// Test methods of the LManagedX and LCleanedupX classes.
1.19 +//
1.20 +//
1.21 +//
1.22 +
1.23 +#include "emanaged.h"
1.24 +#include <e32base.h>
1.25 +#include <e32test.h>
1.26 +#include <e32def.h>
1.27 +#include <e32cmn.h>
1.28 +
1.29 +
1.30 +extern RTest test;
1.31 +
1.32 +template<TInt Value>
1.33 +class CTracker : public CBase
1.34 + {
1.35 +public:
1.36 + enum TConstructionMode { ENonleavingConstructor, EConstructorLeaves};
1.37 +
1.38 + static CTracker* NewL()
1.39 + {
1.40 + CTracker* ptr = new(ELeave) CTracker;
1.41 + ptr->ConstructL();
1.42 + return ptr;
1.43 + }
1.44 + CTracker():iData(NULL)
1.45 + {
1.46 + test.Printf(_L(" CTracker - %x\n"), this);
1.47 + }
1.48 +
1.49 + void ConstructL()
1.50 + {
1.51 + test.Printf(_L(" CTracker::ConstructL - %x\n"), this);
1.52 + iData = new(ELeave) TInt(Value);
1.53 + }
1.54 +public:
1.55 + virtual ~CTracker()
1.56 + {
1.57 + test.Printf(_L(" ~CTracker - %x\n"), this);
1.58 + if(iData)
1.59 + {
1.60 + delete iData;
1.61 + }
1.62 + }
1.63 +
1.64 + virtual void MemFunc()
1.65 + {
1.66 + test.Printf(_L(" CTracker::MemFunc - %x\n"), this);
1.67 + }
1.68 +
1.69 + static void StaticMemberRef(const CTracker& aTracker)
1.70 + {
1.71 + test.Printf(_L(" CTracker::StaticMemberRef - %x\n"), &aTracker);
1.72 + }
1.73 +
1.74 + static void StaticMemberPtr(CTracker* aTracker)
1.75 + {
1.76 + test.Printf(_L(" CTracker::StaticMemberPtr - %x\n"), aTracker);
1.77 + }
1.78 +
1.79 + private:
1.80 + TInt* iData;
1.81 + };
1.82 +
1.83 +namespace Log
1.84 +{
1.85 +
1.86 +class RLoggerNew
1.87 + {
1.88 + public:
1.89 + RLoggerNew()
1.90 + : iData(NULL)
1.91 + {
1.92 + test.Printf(_L(" RLoggerNew - %x\n"), this);
1.93 + }
1.94 +
1.95 + RLoggerNew(TInt* aData)
1.96 + : iData(aData)
1.97 + {
1.98 + test.Printf(_L(" RLoggerNew - %x ptr %x -> val %d\n"), this, aData, *iData);
1.99 + }
1.100 +
1.101 + RLoggerNew(TInt aValue)
1.102 + : iData(new(ELeave) TInt(aValue))
1.103 + {
1.104 + test.Printf(_L(" RLoggerNew - %x value %d\n"), this, *iData);
1.105 + }
1.106 +
1.107 + void OpenL(TInt aValue)
1.108 + {
1.109 + iData = new(ELeave) TInt(aValue);
1.110 + test.Printf(_L(" RLoggerNew::OpenL(TInt aValue) - %x value %d\n"), this, *iData);
1.111 + }
1.112 +
1.113 + RLoggerNew(const RLoggerNew& aLogger)
1.114 + : iData(aLogger.iData)
1.115 + {
1.116 + test.Printf(_L(" RLoggerNew::RLoggerNew(const RLoggerNew&) - %x (copy)\n"), this);
1.117 + }
1.118 +
1.119 +
1.120 + RLoggerNew& operator=(const RLoggerNew& aLogger)
1.121 + {
1.122 + iData = aLogger.iData;
1.123 +
1.124 + test.Printf(_L(" RLoggerNew::operator=(const RLoggerNew&) - %x copy from %x val %d\n"), this, &aLogger, *aLogger.iData);
1.125 + return *this;
1.126 + }
1.127 +
1.128 +
1.129 + ~RLoggerNew()
1.130 + {
1.131 + test.Printf(_L(" ~RLoggerNew - %x\n"), this);
1.132 + }
1.133 +
1.134 +
1.135 + void Close()
1.136 + {
1.137 + test.Printf(_L(" RLoggerNew::Close - %x\n"), this);
1.138 +
1.139 + // Open or non-NULL initializing constructor not called or
1.140 + // cleanup function already called
1.141 +
1.142 + __ASSERT_ALWAYS(iData != NULL, test.Panic(_L("NULL pointer")));
1.143 + delete iData;
1.144 + iData = NULL;
1.145 + }
1.146 +
1.147 +
1.148 + void Release()
1.149 + {
1.150 + test.Printf(_L(" RLoggerNew::Release - %x\n"), this);
1.151 +
1.152 + // Open or non-NULL initializing constructor not called or
1.153 + // cleanup function already called
1.154 +
1.155 + __ASSERT_ALWAYS(iData != NULL, test.Panic(_L("NULL pointer")));
1.156 + delete iData;
1.157 + iData = NULL;
1.158 + }
1.159 +
1.160 +
1.161 + void Destroy()
1.162 + {
1.163 + test.Printf(_L(" RLoggerNew::Destroy - %x\n"), this);
1.164 +
1.165 + // Open or non-NULL initializing constructor not called or
1.166 + // cleanup function already called
1.167 +
1.168 + __ASSERT_ALWAYS(iData != NULL, test.Panic(_L("NULL pointer")));
1.169 + delete iData;
1.170 + iData = NULL;
1.171 + }
1.172 +
1.173 +
1.174 + void Free()
1.175 + {
1.176 + test.Printf(_L(" RLoggerNew::Free - %x\n"), this);
1.177 +
1.178 + // Open or non-NULL initializing constructor not called or
1.179 + // cleanup function already called
1.180 +
1.181 + __ASSERT_ALWAYS(iData != NULL, test.Panic(_L("NULL pointer")));
1.182 + delete iData;
1.183 + iData = NULL;
1.184 + }
1.185 +
1.186 + void MemFunc()
1.187 + {
1.188 + test.Printf(_L(" RLoggerNew::MemFunc - %x %x\n"), this, iData);
1.189 + }
1.190 +
1.191 + static void StaticMemberRef(const RLoggerNew& aTracker)
1.192 + {
1.193 + test.Printf(_L(" RLoggerNew::StaticMemberRef - %x\n"), &aTracker);
1.194 + }
1.195 +
1.196 + static void StaticMemberPtr(RLoggerNew* aTracker)
1.197 + {
1.198 + test.Printf(_L(" RLoggerNew::StaticMemberPtr - %x\n"), aTracker);
1.199 + }
1.200 +
1.201 + static void Cleanup(TAny* aRLoggerNew)
1.202 + {
1.203 + static_cast<RLoggerNew*>(aRLoggerNew)->Close();
1.204 + }
1.205 +
1.206 + TInt* GetData() const
1.207 + {
1.208 + return iData;
1.209 + }
1.210 +
1.211 + protected:
1.212 + TInt* iData;
1.213 + };
1.214 +
1.215 +
1.216 +DEFINE_CLEANUP_FUNCTION(RLoggerNew, Release);
1.217 +}//namespace Log
1.218 +
1.219 +
1.220 +
1.221 +using Log::RLoggerNew;
1.222 +
1.223 +//------------------------------------------
1.224 +//LCleanedupPtr tests here
1.225 +//------------------
1.226 +template<TInt Value>
1.227 +void TestLCleanedupPtrGenerateL()
1.228 + {
1.229 + __UHEAP_MARK;
1.230 +
1.231 + {
1.232 + LCleanedupPtr<CTracker<Value> > tracker(CTracker<Value>::NewL());
1.233 +
1.234 + tracker->MemFunc();
1.235 +
1.236 + }
1.237 +
1.238 + __UHEAP_MARKEND;
1.239 + test.Printf(_L("__UHEAP_MARKEND - OK\n"));
1.240 + }
1.241 +
1.242 +template<TInt Value>
1.243 +void GenerateLCleanedupPtrTestL()
1.244 + {
1.245 + TestLCleanedupPtrGenerateL<Value>();
1.246 + GenerateLCleanedupPtrTestL<Value - 1>();
1.247 + }
1.248 +
1.249 +template<>
1.250 +void GenerateLCleanedupPtrTestL<0>()
1.251 + {
1.252 + TestLCleanedupPtrGenerateL<0>();
1.253 + }
1.254 +
1.255 +
1.256 +//-------------------------------------------
1.257 +//LCleanedupArray tests here
1.258 +//-----------------
1.259 +template<TInt Value>
1.260 +void TestLCleanedupArrayGenerateL()
1.261 + {
1.262 + __UHEAP_MARK;
1.263 +
1.264 + {
1.265 + LCleanedupArray<CTracker<Value> > tracker(new(ELeave) CTracker<Value>[Value]);
1.266 + }
1.267 +
1.268 + __UHEAP_MARKEND;
1.269 + test.Printf(_L("__UHEAP_MARKEND - OK\n"));
1.270 + }
1.271 +
1.272 +template<TInt Value>
1.273 +void GenerateLCleanedupArrayTestL()
1.274 + {
1.275 + TestLCleanedupArrayGenerateL<Value>();
1.276 + GenerateLCleanedupArrayTestL<Value - 1>();
1.277 + }
1.278 +
1.279 +template<>
1.280 +void GenerateLCleanedupArrayTestL<0>()
1.281 + {
1.282 + TestLCleanedupArrayGenerateL<0>();
1.283 + }
1.284 +
1.285 +//-----------------------------------
1.286 +//LCleanedupGuard Tests Here
1.287 +//---------------------------
1.288 +template<TInt Value>
1.289 +void TestLCleanedupGuardTestGenerateL()
1.290 + {
1.291 + __UHEAP_MARK;
1.292 +
1.293 + {
1.294 + RLoggerNew logger(Value);
1.295 + LCleanedupGuard cleanGuard(RLoggerNew::Cleanup, &logger);
1.296 + }
1.297 +
1.298 + __UHEAP_MARKEND;
1.299 + test.Printf(_L("__UHEAP_MARKEND - OK\n"));
1.300 + }
1.301 +
1.302 +template<TInt Value>
1.303 +void GenerateLCleanedupGuardTestL()
1.304 + {
1.305 + TestLCleanedupGuardTestGenerateL<Value>();
1.306 + GenerateLCleanedupGuardTestL<Value - 1>();
1.307 + }
1.308 +
1.309 +template<>
1.310 +void GenerateLCleanedupGuardTestL<0>()
1.311 + {
1.312 + TestLCleanedupGuardTestGenerateL<0>();
1.313 + }
1.314 +
1.315 +//----------------------------------------
1.316 +//LCleanedupHandle Tests Here
1.317 +//-----------------------
1.318 +
1.319 +template<TInt Value>
1.320 +void TestLCleanedupHandleGenerateL()
1.321 + {
1.322 + __UHEAP_MARK;
1.323 +
1.324 + {
1.325 + LCleanedupHandle<RLoggerNew> logger(Value);
1.326 + }
1.327 +
1.328 + __UHEAP_MARKEND;
1.329 + test.Printf(_L("__UHEAP_MARKEND - OK\n"));
1.330 + }
1.331 +
1.332 +template<TInt Value>
1.333 +void GenerateLCleanedupHandleTestL()
1.334 + {
1.335 + TestLCleanedupHandleGenerateL<Value>();
1.336 + GenerateLCleanedupHandleTestL<Value - 1>();
1.337 + }
1.338 +
1.339 +template<>
1.340 +void GenerateLCleanedupHandleTestL<0>()
1.341 + {
1.342 + TestLCleanedupHandleGenerateL<0>();
1.343 + }
1.344 +//------------------------------------------
1.345 +//LCleanedupRef Tests Here
1.346 +//-------------------------
1.347 +
1.348 +template<TInt Value>
1.349 +void TestLCleanedupRefGenerateL()
1.350 + {
1.351 + __UHEAP_MARK;
1.352 +
1.353 + {
1.354 + RLoggerNew logger;
1.355 + logger.OpenL(Value);
1.356 + LCleanedupRef<RLoggerNew> rlog(logger);
1.357 + rlog.ReleaseResource();
1.358 + }
1.359 +
1.360 + __UHEAP_MARKEND;
1.361 + test.Printf(_L("__UHEAP_MARKEND - OK\n"));
1.362 + }
1.363 +
1.364 +template<TInt Value>
1.365 +void GenerateLCleanedupRefTestL()
1.366 + {
1.367 + TestLCleanedupRefGenerateL<Value>();
1.368 + GenerateLCleanedupRefTestL<Value - 1>();
1.369 + }
1.370 +
1.371 +template<>
1.372 +void GenerateLCleanedupRefTestL<0>()
1.373 + {
1.374 + TestLCleanedupRefGenerateL<0>();
1.375 + }
1.376 +
1.377 +/**
1.378 +@SYMTestCaseID BASESRVCS-EUSERHL-UT-4071
1.379 +@SYMTestCaseDesc Tests multiple defined symbols of ManagedPopCleanupStackItem method
1.380 + and also does stress testing of the template classes.
1.381 + Tests All the different Templated Class such as
1.382 + LCleanedupRef,LCleanedupGuard,LCleanedupHandle,LCleanedupArray and LCleanedupPtr
1.383 + for performance as well by explicit instantiation of the templated class using recurrsion.
1.384 +@SYMTestPriority High
1.385 +@SYMTestActions Creates multiple instances of LCleanedupArray,LCleanedupPtr and LCleanedupRef etc
1.386 + Verifies that the objects are automatically cleaned up when they go
1.387 + out of scope
1.388 +@SYMTestExpectedResults All memory allocated for the LXXX objects
1.389 + is automatically freed when they go out of scope and there are no
1.390 + errors on multiple definition of ManagedPopCleanupStackItem is produced.
1.391 +@SYMREQ DEF137572
1.392 +*/
1.393 +
1.394 +TInt TExtendedTestL()
1.395 + {
1.396 + //------------
1.397 + TRAPD(status, GenerateLCleanedupPtrTestL<64>());
1.398 + if (status != KErrNone)
1.399 + {
1.400 + test.Printf(_L("LCleanedupPtr; leave code: %d\n"), status);
1.401 + }
1.402 +
1.403 + TRAPD(status1,GenerateLCleanedupArrayTestL<64>());
1.404 + if(status1 !=KErrNone)
1.405 + {
1.406 + test.Printf(_L("LCleanedupArray completed;leave code:%d\n"),status1);
1.407 + }
1.408 +
1.409 + TRAPD(status2,GenerateLCleanedupGuardTestL<64>());
1.410 + if(status2 !=KErrNone)
1.411 + {
1.412 + test.Printf(_L("LCleanedupGuard completed;leave code:%d\n"),status2);
1.413 + }
1.414 +
1.415 + TRAPD(status3,GenerateLCleanedupHandleTestL<64>());
1.416 + if(status3 !=KErrNone)
1.417 + {
1.418 + test.Printf(_L("LCleanedupHandle completed;leave code:%d\n"),status3);
1.419 + }
1.420 +
1.421 + TRAPD(status4,GenerateLCleanedupRefTestL<64>());
1.422 + if(status4 !=KErrNone)
1.423 + {
1.424 + test.Printf(_L("LCleanedupRef completed;leave code:%d\n"),status4);
1.425 + }
1.426 + return status;
1.427 + }
1.428 +
1.429 +
1.430 +