/**initilize called within __UHEAP_MARK/__UHEAP_MARKEND of doTestStepL,
so that all relevant memmory de/allocation is in scope*/
void CPerformanceFunctionalityBase::InitializeL()
	{
	CLEAR(iContactsDatabase);
	iIterate->Clear();
	if( iConcurrent )
    	{
    	iContactsDatabase = CContactDatabase::OpenL(); //open database created in earlier step
    	for(TInt k = 1; k<=iContacts; k++)
    		{
    		TRAPD( err, ReadL(k));
    		CloseL( ETrue );
    		if( KErrNone == err )
    			{
    			iIterate->AddL( k );
    			}
    		}
		if( NULL  == iTemplate )
			{
			iTemplate = static_cast< CContactTemplate* >(
			iContactsDatabase->ReadContactL( iContactsDatabase->TemplateId(), *iViewAll ) );
			}
    	}
    else
    	{
		iContactsDatabase = CContactDatabase::ReplaceL();//create  clean/fresh database for all tests
		if( NULL  == iTemplate )
			{
			iTemplate = static_cast< CContactTemplate* >(
			iContactsDatabase->ReadContactL( iContactsDatabase->TemplateId(), *iViewAll ) );
			}
		AddEmptyContactsL(iContacts, EFalse);
    	}
	}
void CContactViewTest::ResetDatabaseL(const TInt aMany)
	{
	iIterate->Reset();
	for(TInt i = 0; i < iContacts; ++i)
		{
		const TContactItemId cid = iIterate->NextL();
		iIterate->RemoveL(cid);
		}
	if( aMany > 0 )
		{
		iContacts = aMany;
		}
	CLEAR( iContactsDatabase );
	CLEAR( iContactsDatabase2 );
	iContactsDatabase = CContactDatabase::ReplaceL();

	iContactsDatabase2 = CContactDatabase::OpenL( CContactDatabase::EMultiThread );
	AddEmptyContactsL(iContacts);
	iIterate->Reset();
	}
void CTransactionsStep::ManyUpdateCommitTransactionTestL( const TBool aRevert )
	{
	iContacts = 1000;
	CPerformanceFunctionalityBase::InitializeL();
	RCntList backuplist;
	backuplist.CopyL( *iIterate );
	iContactsDatabase->DatabaseBeginLC(EFalse);
	
	TInt added = 0;
	TInt deleted = 0;
	TInt updated = 0;
	TInt k = 0;
	for(; k < iContacts; k++)
		{
		switch( k % KNumCUDOperations )
			{
			case ( EOpen ):
				{
				OpenL( iIterate->NextL() );
				//if setting first contact read first field from stringfields section
				//otherwise read next field in list of fields stored in ini file secion
				SetAllFieldsL( GetNextFieldL(KStringFields, KField, 0 == k ) );
				CommitL(ETrue);
				++updated;
				}
			break;
			case ( EUpdate ):
				{
				TContactItemId cid = iIterate->NextL();
				ReadL( cid );
				//if setting first contact read first field from stringfields section
				//otherwise read next field in list of fields stored in ini file secion
				SetAllFieldsL( GetNextFieldL(KStringFields, KField, 0 == k ) );
				iContactsDatabase->UpdateContactLC(cid, iContactItem);
				CleanupStack::PopAndDestroy();
				CloseL( ETrue );
				++updated;
				}
			break;
			case ( EAddDelete ):
				{
				DeleteContactL( iIterate->NextL(), ETrue );
				//iterator position is shifted back, so that next nextL call is correct
				iIterate->PreviousL();
				++deleted;
				//Empty contact is appended to the database, 
				//Contact id is stored at the end of iterator list not at current iterator position
				AddEmptyContactsL( 1, ETrue );
				++added;
				}
			break;
			default:
				{
				_LIT(KInvalidTest, "Invalid Case");
				User::Panic(KInvalidTest, 555);
				}
			break;
			}
		}
	
	EndTransactionL(aRevert);
	TESTPRINT( iContactsDatabase->CountL() == iContacts );
	
	CContactItemFieldSet& templateFields = iTemplate->CardFields();
	TInt fieldsCount = templateFields.Count() - 1;
	
	if(aRevert)
		{
		//if transaction is reverted / rolledback, all contacts should be
		//unchanged, i.e. they should all contain empty fields only
		backuplist.Reset();
		TInt err = KErrNone;
		for(k = 0; k < iContacts; ++k)
			{
			TRAP(err, ReadL( backuplist.NextL() ) );
			TESTPRINTI( KErrNone == err, k );
			TESTPRINTI( CheckAllFieldsL( KNullDesC ), k );
			CloseL( ETrue );
			}
		}
	else
		{
		iIterate->Reset();
		//all updated contact should contain the appropriated updated field value
		for(k = 0; k < updated; ++k)
			{
			ReadL( iIterate->NextL() );
			TESTPRINTI(iFields->Count() == fieldsCount, k);
			//if reading first contact read first field from stringfields section
			//otherwise read next field in list of fields stored in ini file secion
			TESTPRINTI( CheckAllFieldsL( GetNextFieldL(KStringFields, KField, 0 == k ) ), k );
			CloseL( ETrue );
			}
			
		//all newly	added contacts, should contain empty fields only.
		for(k = 0; k < added; ++k)
			{
			ReadL( iIterate->NextL() );
			TESTPRINTI( iFields->Count() == fieldsCount, k);
			TESTPRINTI( CheckAllFieldsL( KNullDesC ), k);
			CloseL( ETrue );
			}
		}
	}