os/persistentdata/persistentstorage/store/TSTOR/t_storiter.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) 2004-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 "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
// RPermanentFileStoreIter functionality tests
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32test.h>
sl@0
    19
#include <s32file.h>
sl@0
    20
#include <s32fileiter.h>
sl@0
    21
sl@0
    22
LOCAL_D RTest test(_L("t_storiter"));
sl@0
    23
LOCAL_D CTrapCleanup *TheTrapCleanup;
sl@0
    24
LOCAL_D RFs TheFs;
sl@0
    25
sl@0
    26
// This is a path specification and should not be used as is
sl@0
    27
_LIT(KFileLocationSpec, "Z:\\T_ITER1.DAT");
sl@0
    28
const TInt KMaxTestStreamIds = 512;
sl@0
    29
LOCAL_D TStreamId StreamIds[KMaxTestStreamIds];//Test stream IDs - used in test functions
sl@0
    30
sl@0
    31
//aCount - there should be aCount valid stream IDs in StreamIds array.
sl@0
    32
//The function aborts program's execution if aStreamId is not in StreamIds array.
sl@0
    33
LOCAL_C void AssertStreamId(TInt aCount, TStreamId aStreamId)
sl@0
    34
	{
sl@0
    35
	__ASSERT_DEBUG(aCount >= 0 && aCount < KMaxTestStreamIds, User::Invariant());
sl@0
    36
	TInt i = 0;
sl@0
    37
	for(;(i<aCount)&&(aStreamId!=::StreamIds[i]);i++)
sl@0
    38
		{
sl@0
    39
		}
sl@0
    40
	test(i < aCount);
sl@0
    41
	}
sl@0
    42
sl@0
    43
//aStoreStreamIds array has aCount valid elements.
sl@0
    44
//The function aborts program's execution if aStoreStreamIds array has duplicated elements.
sl@0
    45
LOCAL_C void AssertStreamIdDuplication(TStreamId aStoreStreamIds[], TInt aCount)
sl@0
    46
	{
sl@0
    47
	for(TInt i=0;i<aCount;i++)
sl@0
    48
		{
sl@0
    49
		for(TInt j=(i+1);j<aCount;j++)
sl@0
    50
			{
sl@0
    51
			test(aStoreStreamIds[i] != aStoreStreamIds[j]);
sl@0
    52
			}
sl@0
    53
		}
sl@0
    54
	}
sl@0
    55
sl@0
    56
//The function leaves if it fails to open aStore's stream with aId stream ID.
sl@0
    57
LOCAL_C void AssertValidStreamIdL(TStreamId aId, CPermanentFileStore& aStore)
sl@0
    58
	{
sl@0
    59
	RStoreReadStream in;
sl@0
    60
	in.OpenLC(aStore, aId);
sl@0
    61
	CleanupStack::PopAndDestroy(&in);
sl@0
    62
	}
sl@0
    63
sl@0
    64
//aCount - there should be aCount valid stream Ids in StreamIds array.
sl@0
    65
//The function aborts program's execution if StreamIds items and count don't match
sl@0
    66
//aStore stream IDs and count or there are duplicated stream IDs.
sl@0
    67
//The function leaves if it fails to open aStore's streams.
sl@0
    68
LOCAL_C void AssertStreamIdsL(CPermanentFileStore& aStore, TInt aCount)
sl@0
    69
	{
sl@0
    70
	__ASSERT_DEBUG(aCount >= 0 && aCount < KMaxTestStreamIds, User::Invariant());
sl@0
    71
	TStreamId storeStreamIds[KMaxTestStreamIds];
sl@0
    72
	Mem::FillZ(storeStreamIds, sizeof(storeStreamIds));
sl@0
    73
	RPermanentFileStoreIter iter;
sl@0
    74
	::CleanupClosePushL(iter);
sl@0
    75
	TInt count = 0;
sl@0
    76
	iter.ResetL(aStore);
sl@0
    77
	TStreamId id;
sl@0
    78
	while((id = iter.NextL()) != KNullStreamIdValue)
sl@0
    79
		{
sl@0
    80
		//verifying that it is a valid stream ID
sl@0
    81
		::AssertValidStreamIdL(id, aStore);
sl@0
    82
		//Save stream ID in storeStreamIds array
sl@0
    83
		storeStreamIds[count++] = id;
sl@0
    84
		//Assert stream ID
sl@0
    85
		::AssertStreamId(aCount, id);
sl@0
    86
		}
sl@0
    87
	test(count == aCount);
sl@0
    88
	//Assert duplications
sl@0
    89
	::AssertStreamIdDuplication(storeStreamIds, count);
sl@0
    90
	CleanupStack::PopAndDestroy(&iter);
sl@0
    91
	}
sl@0
    92
sl@0
    93
/**
sl@0
    94
@SYMTestCaseID          SYSLIB-STORE-CT-1219
sl@0
    95
@SYMTestCaseDesc	    CPermanentFileStore-StreamId iteration tests
sl@0
    96
@SYMTestPriority 	    High
sl@0
    97
@SYMTestActions  	    Tests by creating file store instance with stream count to zero.
sl@0
    98
						Assert if StreamIds items and count don't match
sl@0
    99
@SYMTestExpectedResults Test must not fail
sl@0
   100
@SYMREQ                 REQ0000
sl@0
   101
*/
sl@0
   102
LOCAL_C void Test1L()
sl@0
   103
	{
sl@0
   104
	test.Start(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1219 CPermanentFileStore-StreamId iteration tests - 1 "));
sl@0
   105
	//Create CPermanentFileStore instance. Stream count should be 0
sl@0
   106
	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
sl@0
   107
	TParse parse;
sl@0
   108
	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
sl@0
   109
	
sl@0
   110
	RFile file;
sl@0
   111
	test(file.Create(::TheFs, parse.FullName(), EFileWrite| EFileRead) == KErrNone);
sl@0
   112
	CPermanentFileStore* store = CPermanentFileStore::NewLC(file);
sl@0
   113
	store->SetTypeL(KPermanentFileStoreLayoutUid);
sl@0
   114
	store->CommitL();
sl@0
   115
	::AssertStreamIdsL(*store, 0);
sl@0
   116
	//Create 1 stream. Stream count: 1
sl@0
   117
	RStoreWriteStream out;
sl@0
   118
	::StreamIds[0] = out.CreateLC(*store);
sl@0
   119
	out.CommitL();
sl@0
   120
	store->CommitL();
sl@0
   121
	::AssertStreamIdsL(*store, 1);
sl@0
   122
	//Create 2 new streams. Stream count: 1 + 2
sl@0
   123
	RStoreWriteStream out2;
sl@0
   124
	::StreamIds[1] = out2.CreateLC(*store);
sl@0
   125
	out2.CommitL();
sl@0
   126
	RStoreWriteStream out3;
sl@0
   127
	::StreamIds[2] = out3.CreateLC(*store);
sl@0
   128
	out3.CommitL();
sl@0
   129
	store->CommitL();
sl@0
   130
	::AssertStreamIdsL(*store, 3);
sl@0
   131
	//Close 1 stream. Stream count: 3
sl@0
   132
	CleanupStack::PopAndDestroy(&out3);
sl@0
   133
	::AssertStreamIdsL(*store, 3);
sl@0
   134
	//Delete 1 stream. Stream count: 2
sl@0
   135
	store->DeleteL(::StreamIds[2]);
sl@0
   136
	store->CommitL();
sl@0
   137
	::AssertStreamIdsL(*store, 2);
sl@0
   138
	//Cleanup
sl@0
   139
	CleanupStack::PopAndDestroy(&out2);
sl@0
   140
	CleanupStack::PopAndDestroy(&out);
sl@0
   141
	CleanupStack::PopAndDestroy(store);
sl@0
   142
	}
sl@0
   143
sl@0
   144
/**
sl@0
   145
@SYMTestCaseID          SYSLIB-STORE-CT-1220
sl@0
   146
@SYMTestCaseDesc	    CPermanentFileStore-StreamId iteration tests
sl@0
   147
@SYMTestPriority 	    High
sl@0
   148
@SYMTestActions  	    Test by creating file store instance with stream count to two.
sl@0
   149
                        Assert if StreamIds items and count don't match
sl@0
   150
@SYMTestExpectedResults Test must not fail
sl@0
   151
@SYMREQ                 REQ0000
sl@0
   152
*/
sl@0
   153
LOCAL_C void Test2L()
sl@0
   154
	{
sl@0
   155
	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1220 CPermanentFileStore-StreamId iteration tests - 2 "));
sl@0
   156
sl@0
   157
	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
sl@0
   158
	TParse parse;
sl@0
   159
	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
sl@0
   160
	
sl@0
   161
	//Create CPermanentFileStore instance with an existing store file . Stream count: 2
sl@0
   162
	CPermanentFileStore* store = CPermanentFileStore::OpenLC(::TheFs, parse.FullName(), EFileWrite | EFileRead);
sl@0
   163
	store->SetTypeL(KPermanentFileStoreLayoutUid);
sl@0
   164
	::AssertStreamIdsL(*store, 2);
sl@0
   165
	//Create new stream. Stream count: 3
sl@0
   166
	RStoreWriteStream out;
sl@0
   167
	::StreamIds[2] = out.CreateLC(*store);
sl@0
   168
	out.CommitL();
sl@0
   169
	store->CommitL();
sl@0
   170
	::AssertStreamIdsL(*store, 3);
sl@0
   171
	//Create 2 new streams. Stream count: 3 + 2
sl@0
   172
	RStoreWriteStream out2;
sl@0
   173
	::StreamIds[3] = out2.CreateLC(*store);
sl@0
   174
	out2.CommitL();
sl@0
   175
	RStoreWriteStream out3;
sl@0
   176
	::StreamIds[4] = out3.CreateLC(*store);
sl@0
   177
	out3.CommitL();
sl@0
   178
	store->CommitL();
sl@0
   179
	::AssertStreamIdsL(*store, 5);
sl@0
   180
	//Cleanup
sl@0
   181
	CleanupStack::PopAndDestroy(&out3);
sl@0
   182
	CleanupStack::PopAndDestroy(&out2);
sl@0
   183
	CleanupStack::PopAndDestroy(&out);
sl@0
   184
	CleanupStack::PopAndDestroy(store);
sl@0
   185
	}
sl@0
   186
sl@0
   187
/**
sl@0
   188
@SYMTestCaseID          SYSLIB-STORE-CT-1221
sl@0
   189
@SYMTestCaseDesc	    CPermanentFileStore-StreamId iteration tests
sl@0
   190
@SYMTestPriority 	    High
sl@0
   191
@SYMTestActions  	    Test by creating file store instance with stream count to five.
sl@0
   192
                        Assert if StreamIds items and count don't match.Read and test data from the streams
sl@0
   193
@SYMTestExpectedResults Test must not fail
sl@0
   194
@SYMREQ                 REQ0000
sl@0
   195
*/
sl@0
   196
LOCAL_C void Test3L()
sl@0
   197
	{
sl@0
   198
	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1221 CPermanentFileStore-StreamId iteration tests - 3 "));
sl@0
   199
sl@0
   200
	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
sl@0
   201
	TParse parse;
sl@0
   202
	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
sl@0
   203
	
sl@0
   204
	//Create CPermanentFileStore instance with an existing store file . Stream count: 5
sl@0
   205
	CPermanentFileStore* store = CPermanentFileStore::OpenLC(::TheFs, parse.FullName(), EFileWrite | EFileRead);
sl@0
   206
	store->SetTypeL(KPermanentFileStoreLayoutUid);
sl@0
   207
	::AssertStreamIdsL(*store, 5);
sl@0
   208
	//Save some data to the streams. Stream count: 5
sl@0
   209
	RPermanentFileStoreIter iter;
sl@0
   210
	iter.ResetLC(*store);
sl@0
   211
	TStreamId id;
sl@0
   212
	TInt i = 0;
sl@0
   213
	while((id = iter.NextL()) != KNullStreamIdValue)
sl@0
   214
		{
sl@0
   215
		RStoreWriteStream out;
sl@0
   216
		out.ReplaceLC(*store, id);
sl@0
   217
		out << TInt32(++i);
sl@0
   218
		out.CommitL();
sl@0
   219
		CleanupStack::PopAndDestroy(&out);
sl@0
   220
		}
sl@0
   221
	store->CommitL();
sl@0
   222
	::AssertStreamIdsL(*store, 5);
sl@0
   223
	CleanupStack::PopAndDestroy(&iter);
sl@0
   224
	//Read and test data from the streams. Stream count: 5
sl@0
   225
	i = 0;
sl@0
   226
	iter.ResetLC(*store);
sl@0
   227
	while((id = iter.NextL()) != KNullStreamIdValue)
sl@0
   228
		{
sl@0
   229
		RStoreReadStream in;
sl@0
   230
		in.OpenLC(*store, id);
sl@0
   231
		TInt32 v = 0;
sl@0
   232
		in >> v;
sl@0
   233
		test(v == ++i);
sl@0
   234
		CleanupStack::PopAndDestroy(&in);
sl@0
   235
		}
sl@0
   236
	//Cleanup
sl@0
   237
	CleanupStack::PopAndDestroy(&iter);
sl@0
   238
	CleanupStack::PopAndDestroy(store);
sl@0
   239
	}
sl@0
   240
sl@0
   241
/**
sl@0
   242
@SYMTestCaseID          SYSLIB-STORE-CT-1222
sl@0
   243
@SYMTestCaseDesc	    CPermanentFileStore-StreamId iteration tests
sl@0
   244
@SYMTestPriority 	    High
sl@0
   245
@SYMTestActions  	    Test by creating file store instance with stream count to five and delete all the streams.
sl@0
   246
						Assert if StreamIds items and count don't match
sl@0
   247
@SYMTestExpectedResults Test must not fail
sl@0
   248
@SYMREQ                 REQ0000
sl@0
   249
*/
sl@0
   250
LOCAL_C void Test4L()
sl@0
   251
	{
sl@0
   252
	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1222 CPermanentFileStore-StreamId iteration tests - 4 "));
sl@0
   253
sl@0
   254
	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
sl@0
   255
	TParse parse;
sl@0
   256
	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
sl@0
   257
	
sl@0
   258
	//Create CPermanentFileStore instance with an existing store file . Stream count: 5
sl@0
   259
	CPermanentFileStore* store = CPermanentFileStore::OpenLC(::TheFs, parse.FullName(), EFileWrite | EFileRead);
sl@0
   260
	store->SetTypeL(KPermanentFileStoreLayoutUid);
sl@0
   261
	::AssertStreamIdsL(*store, 5);
sl@0
   262
	TInt i;
sl@0
   263
	//Delete all streams
sl@0
   264
	for(i=0;i<5;i++)
sl@0
   265
		{
sl@0
   266
		store->DeleteL(::StreamIds[i]);
sl@0
   267
		}
sl@0
   268
	store->CommitL();
sl@0
   269
	::AssertStreamIdsL(*store, 0);
sl@0
   270
	//Create KMaxTestStreamIds/4 streams
sl@0
   271
	for(i=0;i<(KMaxTestStreamIds/4);i++)
sl@0
   272
		{
sl@0
   273
		RStoreWriteStream out;
sl@0
   274
		::StreamIds[i] = out.CreateLC(*store);
sl@0
   275
		out.CommitL();
sl@0
   276
		CleanupStack::PopAndDestroy(&out);
sl@0
   277
		}
sl@0
   278
	store->CommitL();
sl@0
   279
	::AssertStreamIdsL(*store, KMaxTestStreamIds / 4);
sl@0
   280
	//Delete last KMaxTestStreamIds/8 streams
sl@0
   281
	for(i=(KMaxTestStreamIds/8);i<(KMaxTestStreamIds/4);i++)
sl@0
   282
		{
sl@0
   283
		store->DeleteL(::StreamIds[i]);
sl@0
   284
		}
sl@0
   285
	store->CommitL();
sl@0
   286
	//There are KMaxTestStreamIds/8 streams in store
sl@0
   287
	::AssertStreamIdsL(*store, KMaxTestStreamIds / 8);
sl@0
   288
	//Create KMaxTestStreamIds/2 streams
sl@0
   289
	for(i=(KMaxTestStreamIds/8);i<(KMaxTestStreamIds/8+KMaxTestStreamIds/2);i++)
sl@0
   290
		{
sl@0
   291
		RStoreWriteStream out;
sl@0
   292
		::StreamIds[i] = out.CreateLC(*store);
sl@0
   293
		out.CommitL();
sl@0
   294
		CleanupStack::PopAndDestroy(&out);
sl@0
   295
		}
sl@0
   296
	store->CommitL();
sl@0
   297
	//There are KMaxTestStreamIds / 2 + KMaxTestStreamIds / 8 streams in store
sl@0
   298
	::AssertStreamIdsL(*store, KMaxTestStreamIds / 2 + KMaxTestStreamIds / 8);
sl@0
   299
	//Delete first KMaxTestStreamIds/4 streams
sl@0
   300
	for(i=0;i<(KMaxTestStreamIds/4);i++)
sl@0
   301
		{
sl@0
   302
		store->DeleteL(::StreamIds[i]);
sl@0
   303
		}
sl@0
   304
	store->CommitL();
sl@0
   305
	//There are KMaxTestStreamIds / 8 + KMaxTestStreamIds / 4 streams in store
sl@0
   306
	Mem::Move(::StreamIds, &::StreamIds[KMaxTestStreamIds / 4], sizeof(TStreamId) * (KMaxTestStreamIds / 8 + KMaxTestStreamIds / 4));
sl@0
   307
	::AssertStreamIdsL(*store, KMaxTestStreamIds / 8 + KMaxTestStreamIds / 4);
sl@0
   308
	CleanupStack::PopAndDestroy(store);
sl@0
   309
	}
sl@0
   310
sl@0
   311
//Run tests
sl@0
   312
LOCAL_C void MainL()
sl@0
   313
	{
sl@0
   314
	::Test1L();
sl@0
   315
	::Test2L();
sl@0
   316
	::Test3L();
sl@0
   317
	::Test4L();
sl@0
   318
	test.End();
sl@0
   319
	}
sl@0
   320
sl@0
   321
//Delete created/used store files.
sl@0
   322
LOCAL_C void Cleanup()
sl@0
   323
	{
sl@0
   324
	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
sl@0
   325
	TParse parse;
sl@0
   326
	parse.Set(drive.Name(), &KFileLocationSpec, NULL);
sl@0
   327
	::TheFs.Delete(parse.FullName());
sl@0
   328
	}
sl@0
   329
sl@0
   330
GLDEF_C TInt E32Main()
sl@0
   331
    {
sl@0
   332
	test.Title();
sl@0
   333
	::TheTrapCleanup = CTrapCleanup::New();
sl@0
   334
sl@0
   335
	__UHEAP_MARK;
sl@0
   336
	test(::TheFs.Connect() == KErrNone);
sl@0
   337
sl@0
   338
	::Cleanup();
sl@0
   339
	TRAPD(err, ::MainL());
sl@0
   340
	test(err == KErrNone);
sl@0
   341
	test.Close();
sl@0
   342
	::Cleanup();
sl@0
   343
sl@0
   344
	::TheFs.Close();
sl@0
   345
	__UHEAP_MARKEND;
sl@0
   346
	delete ::TheTrapCleanup;
sl@0
   347
	return 0;
sl@0
   348
    }
sl@0
   349