1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/store/TSTOR/t_storiter.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,349 @@
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 "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 +// RPermanentFileStoreIter functionality tests
1.18 +//
1.19 +//
1.20 +
1.21 +#include <e32test.h>
1.22 +#include <s32file.h>
1.23 +#include <s32fileiter.h>
1.24 +
1.25 +LOCAL_D RTest test(_L("t_storiter"));
1.26 +LOCAL_D CTrapCleanup *TheTrapCleanup;
1.27 +LOCAL_D RFs TheFs;
1.28 +
1.29 +// This is a path specification and should not be used as is
1.30 +_LIT(KFileLocationSpec, "Z:\\T_ITER1.DAT");
1.31 +const TInt KMaxTestStreamIds = 512;
1.32 +LOCAL_D TStreamId StreamIds[KMaxTestStreamIds];//Test stream IDs - used in test functions
1.33 +
1.34 +//aCount - there should be aCount valid stream IDs in StreamIds array.
1.35 +//The function aborts program's execution if aStreamId is not in StreamIds array.
1.36 +LOCAL_C void AssertStreamId(TInt aCount, TStreamId aStreamId)
1.37 + {
1.38 + __ASSERT_DEBUG(aCount >= 0 && aCount < KMaxTestStreamIds, User::Invariant());
1.39 + TInt i = 0;
1.40 + for(;(i<aCount)&&(aStreamId!=::StreamIds[i]);i++)
1.41 + {
1.42 + }
1.43 + test(i < aCount);
1.44 + }
1.45 +
1.46 +//aStoreStreamIds array has aCount valid elements.
1.47 +//The function aborts program's execution if aStoreStreamIds array has duplicated elements.
1.48 +LOCAL_C void AssertStreamIdDuplication(TStreamId aStoreStreamIds[], TInt aCount)
1.49 + {
1.50 + for(TInt i=0;i<aCount;i++)
1.51 + {
1.52 + for(TInt j=(i+1);j<aCount;j++)
1.53 + {
1.54 + test(aStoreStreamIds[i] != aStoreStreamIds[j]);
1.55 + }
1.56 + }
1.57 + }
1.58 +
1.59 +//The function leaves if it fails to open aStore's stream with aId stream ID.
1.60 +LOCAL_C void AssertValidStreamIdL(TStreamId aId, CPermanentFileStore& aStore)
1.61 + {
1.62 + RStoreReadStream in;
1.63 + in.OpenLC(aStore, aId);
1.64 + CleanupStack::PopAndDestroy(&in);
1.65 + }
1.66 +
1.67 +//aCount - there should be aCount valid stream Ids in StreamIds array.
1.68 +//The function aborts program's execution if StreamIds items and count don't match
1.69 +//aStore stream IDs and count or there are duplicated stream IDs.
1.70 +//The function leaves if it fails to open aStore's streams.
1.71 +LOCAL_C void AssertStreamIdsL(CPermanentFileStore& aStore, TInt aCount)
1.72 + {
1.73 + __ASSERT_DEBUG(aCount >= 0 && aCount < KMaxTestStreamIds, User::Invariant());
1.74 + TStreamId storeStreamIds[KMaxTestStreamIds];
1.75 + Mem::FillZ(storeStreamIds, sizeof(storeStreamIds));
1.76 + RPermanentFileStoreIter iter;
1.77 + ::CleanupClosePushL(iter);
1.78 + TInt count = 0;
1.79 + iter.ResetL(aStore);
1.80 + TStreamId id;
1.81 + while((id = iter.NextL()) != KNullStreamIdValue)
1.82 + {
1.83 + //verifying that it is a valid stream ID
1.84 + ::AssertValidStreamIdL(id, aStore);
1.85 + //Save stream ID in storeStreamIds array
1.86 + storeStreamIds[count++] = id;
1.87 + //Assert stream ID
1.88 + ::AssertStreamId(aCount, id);
1.89 + }
1.90 + test(count == aCount);
1.91 + //Assert duplications
1.92 + ::AssertStreamIdDuplication(storeStreamIds, count);
1.93 + CleanupStack::PopAndDestroy(&iter);
1.94 + }
1.95 +
1.96 +/**
1.97 +@SYMTestCaseID SYSLIB-STORE-CT-1219
1.98 +@SYMTestCaseDesc CPermanentFileStore-StreamId iteration tests
1.99 +@SYMTestPriority High
1.100 +@SYMTestActions Tests by creating file store instance with stream count to zero.
1.101 + Assert if StreamIds items and count don't match
1.102 +@SYMTestExpectedResults Test must not fail
1.103 +@SYMREQ REQ0000
1.104 +*/
1.105 +LOCAL_C void Test1L()
1.106 + {
1.107 + test.Start(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1219 CPermanentFileStore-StreamId iteration tests - 1 "));
1.108 + //Create CPermanentFileStore instance. Stream count should be 0
1.109 + TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));
1.110 + TParse parse;
1.111 + parse.Set(drive.Name(), &KFileLocationSpec, NULL);
1.112 +
1.113 + RFile file;
1.114 + test(file.Create(::TheFs, parse.FullName(), EFileWrite| EFileRead) == KErrNone);
1.115 + CPermanentFileStore* store = CPermanentFileStore::NewLC(file);
1.116 + store->SetTypeL(KPermanentFileStoreLayoutUid);
1.117 + store->CommitL();
1.118 + ::AssertStreamIdsL(*store, 0);
1.119 + //Create 1 stream. Stream count: 1
1.120 + RStoreWriteStream out;
1.121 + ::StreamIds[0] = out.CreateLC(*store);
1.122 + out.CommitL();
1.123 + store->CommitL();
1.124 + ::AssertStreamIdsL(*store, 1);
1.125 + //Create 2 new streams. Stream count: 1 + 2
1.126 + RStoreWriteStream out2;
1.127 + ::StreamIds[1] = out2.CreateLC(*store);
1.128 + out2.CommitL();
1.129 + RStoreWriteStream out3;
1.130 + ::StreamIds[2] = out3.CreateLC(*store);
1.131 + out3.CommitL();
1.132 + store->CommitL();
1.133 + ::AssertStreamIdsL(*store, 3);
1.134 + //Close 1 stream. Stream count: 3
1.135 + CleanupStack::PopAndDestroy(&out3);
1.136 + ::AssertStreamIdsL(*store, 3);
1.137 + //Delete 1 stream. Stream count: 2
1.138 + store->DeleteL(::StreamIds[2]);
1.139 + store->CommitL();
1.140 + ::AssertStreamIdsL(*store, 2);
1.141 + //Cleanup
1.142 + CleanupStack::PopAndDestroy(&out2);
1.143 + CleanupStack::PopAndDestroy(&out);
1.144 + CleanupStack::PopAndDestroy(store);
1.145 + }
1.146 +
1.147 +/**
1.148 +@SYMTestCaseID SYSLIB-STORE-CT-1220
1.149 +@SYMTestCaseDesc CPermanentFileStore-StreamId iteration tests
1.150 +@SYMTestPriority High
1.151 +@SYMTestActions Test by creating file store instance with stream count to two.
1.152 + Assert if StreamIds items and count don't match
1.153 +@SYMTestExpectedResults Test must not fail
1.154 +@SYMREQ REQ0000
1.155 +*/
1.156 +LOCAL_C void Test2L()
1.157 + {
1.158 + test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1220 CPermanentFileStore-StreamId iteration tests - 2 "));
1.159 +
1.160 + TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));
1.161 + TParse parse;
1.162 + parse.Set(drive.Name(), &KFileLocationSpec, NULL);
1.163 +
1.164 + //Create CPermanentFileStore instance with an existing store file . Stream count: 2
1.165 + CPermanentFileStore* store = CPermanentFileStore::OpenLC(::TheFs, parse.FullName(), EFileWrite | EFileRead);
1.166 + store->SetTypeL(KPermanentFileStoreLayoutUid);
1.167 + ::AssertStreamIdsL(*store, 2);
1.168 + //Create new stream. Stream count: 3
1.169 + RStoreWriteStream out;
1.170 + ::StreamIds[2] = out.CreateLC(*store);
1.171 + out.CommitL();
1.172 + store->CommitL();
1.173 + ::AssertStreamIdsL(*store, 3);
1.174 + //Create 2 new streams. Stream count: 3 + 2
1.175 + RStoreWriteStream out2;
1.176 + ::StreamIds[3] = out2.CreateLC(*store);
1.177 + out2.CommitL();
1.178 + RStoreWriteStream out3;
1.179 + ::StreamIds[4] = out3.CreateLC(*store);
1.180 + out3.CommitL();
1.181 + store->CommitL();
1.182 + ::AssertStreamIdsL(*store, 5);
1.183 + //Cleanup
1.184 + CleanupStack::PopAndDestroy(&out3);
1.185 + CleanupStack::PopAndDestroy(&out2);
1.186 + CleanupStack::PopAndDestroy(&out);
1.187 + CleanupStack::PopAndDestroy(store);
1.188 + }
1.189 +
1.190 +/**
1.191 +@SYMTestCaseID SYSLIB-STORE-CT-1221
1.192 +@SYMTestCaseDesc CPermanentFileStore-StreamId iteration tests
1.193 +@SYMTestPriority High
1.194 +@SYMTestActions Test by creating file store instance with stream count to five.
1.195 + Assert if StreamIds items and count don't match.Read and test data from the streams
1.196 +@SYMTestExpectedResults Test must not fail
1.197 +@SYMREQ REQ0000
1.198 +*/
1.199 +LOCAL_C void Test3L()
1.200 + {
1.201 + test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1221 CPermanentFileStore-StreamId iteration tests - 3 "));
1.202 +
1.203 + TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));
1.204 + TParse parse;
1.205 + parse.Set(drive.Name(), &KFileLocationSpec, NULL);
1.206 +
1.207 + //Create CPermanentFileStore instance with an existing store file . Stream count: 5
1.208 + CPermanentFileStore* store = CPermanentFileStore::OpenLC(::TheFs, parse.FullName(), EFileWrite | EFileRead);
1.209 + store->SetTypeL(KPermanentFileStoreLayoutUid);
1.210 + ::AssertStreamIdsL(*store, 5);
1.211 + //Save some data to the streams. Stream count: 5
1.212 + RPermanentFileStoreIter iter;
1.213 + iter.ResetLC(*store);
1.214 + TStreamId id;
1.215 + TInt i = 0;
1.216 + while((id = iter.NextL()) != KNullStreamIdValue)
1.217 + {
1.218 + RStoreWriteStream out;
1.219 + out.ReplaceLC(*store, id);
1.220 + out << TInt32(++i);
1.221 + out.CommitL();
1.222 + CleanupStack::PopAndDestroy(&out);
1.223 + }
1.224 + store->CommitL();
1.225 + ::AssertStreamIdsL(*store, 5);
1.226 + CleanupStack::PopAndDestroy(&iter);
1.227 + //Read and test data from the streams. Stream count: 5
1.228 + i = 0;
1.229 + iter.ResetLC(*store);
1.230 + while((id = iter.NextL()) != KNullStreamIdValue)
1.231 + {
1.232 + RStoreReadStream in;
1.233 + in.OpenLC(*store, id);
1.234 + TInt32 v = 0;
1.235 + in >> v;
1.236 + test(v == ++i);
1.237 + CleanupStack::PopAndDestroy(&in);
1.238 + }
1.239 + //Cleanup
1.240 + CleanupStack::PopAndDestroy(&iter);
1.241 + CleanupStack::PopAndDestroy(store);
1.242 + }
1.243 +
1.244 +/**
1.245 +@SYMTestCaseID SYSLIB-STORE-CT-1222
1.246 +@SYMTestCaseDesc CPermanentFileStore-StreamId iteration tests
1.247 +@SYMTestPriority High
1.248 +@SYMTestActions Test by creating file store instance with stream count to five and delete all the streams.
1.249 + Assert if StreamIds items and count don't match
1.250 +@SYMTestExpectedResults Test must not fail
1.251 +@SYMREQ REQ0000
1.252 +*/
1.253 +LOCAL_C void Test4L()
1.254 + {
1.255 + test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1222 CPermanentFileStore-StreamId iteration tests - 4 "));
1.256 +
1.257 + TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));
1.258 + TParse parse;
1.259 + parse.Set(drive.Name(), &KFileLocationSpec, NULL);
1.260 +
1.261 + //Create CPermanentFileStore instance with an existing store file . Stream count: 5
1.262 + CPermanentFileStore* store = CPermanentFileStore::OpenLC(::TheFs, parse.FullName(), EFileWrite | EFileRead);
1.263 + store->SetTypeL(KPermanentFileStoreLayoutUid);
1.264 + ::AssertStreamIdsL(*store, 5);
1.265 + TInt i;
1.266 + //Delete all streams
1.267 + for(i=0;i<5;i++)
1.268 + {
1.269 + store->DeleteL(::StreamIds[i]);
1.270 + }
1.271 + store->CommitL();
1.272 + ::AssertStreamIdsL(*store, 0);
1.273 + //Create KMaxTestStreamIds/4 streams
1.274 + for(i=0;i<(KMaxTestStreamIds/4);i++)
1.275 + {
1.276 + RStoreWriteStream out;
1.277 + ::StreamIds[i] = out.CreateLC(*store);
1.278 + out.CommitL();
1.279 + CleanupStack::PopAndDestroy(&out);
1.280 + }
1.281 + store->CommitL();
1.282 + ::AssertStreamIdsL(*store, KMaxTestStreamIds / 4);
1.283 + //Delete last KMaxTestStreamIds/8 streams
1.284 + for(i=(KMaxTestStreamIds/8);i<(KMaxTestStreamIds/4);i++)
1.285 + {
1.286 + store->DeleteL(::StreamIds[i]);
1.287 + }
1.288 + store->CommitL();
1.289 + //There are KMaxTestStreamIds/8 streams in store
1.290 + ::AssertStreamIdsL(*store, KMaxTestStreamIds / 8);
1.291 + //Create KMaxTestStreamIds/2 streams
1.292 + for(i=(KMaxTestStreamIds/8);i<(KMaxTestStreamIds/8+KMaxTestStreamIds/2);i++)
1.293 + {
1.294 + RStoreWriteStream out;
1.295 + ::StreamIds[i] = out.CreateLC(*store);
1.296 + out.CommitL();
1.297 + CleanupStack::PopAndDestroy(&out);
1.298 + }
1.299 + store->CommitL();
1.300 + //There are KMaxTestStreamIds / 2 + KMaxTestStreamIds / 8 streams in store
1.301 + ::AssertStreamIdsL(*store, KMaxTestStreamIds / 2 + KMaxTestStreamIds / 8);
1.302 + //Delete first KMaxTestStreamIds/4 streams
1.303 + for(i=0;i<(KMaxTestStreamIds/4);i++)
1.304 + {
1.305 + store->DeleteL(::StreamIds[i]);
1.306 + }
1.307 + store->CommitL();
1.308 + //There are KMaxTestStreamIds / 8 + KMaxTestStreamIds / 4 streams in store
1.309 + Mem::Move(::StreamIds, &::StreamIds[KMaxTestStreamIds / 4], sizeof(TStreamId) * (KMaxTestStreamIds / 8 + KMaxTestStreamIds / 4));
1.310 + ::AssertStreamIdsL(*store, KMaxTestStreamIds / 8 + KMaxTestStreamIds / 4);
1.311 + CleanupStack::PopAndDestroy(store);
1.312 + }
1.313 +
1.314 +//Run tests
1.315 +LOCAL_C void MainL()
1.316 + {
1.317 + ::Test1L();
1.318 + ::Test2L();
1.319 + ::Test3L();
1.320 + ::Test4L();
1.321 + test.End();
1.322 + }
1.323 +
1.324 +//Delete created/used store files.
1.325 +LOCAL_C void Cleanup()
1.326 + {
1.327 + TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));
1.328 + TParse parse;
1.329 + parse.Set(drive.Name(), &KFileLocationSpec, NULL);
1.330 + ::TheFs.Delete(parse.FullName());
1.331 + }
1.332 +
1.333 +GLDEF_C TInt E32Main()
1.334 + {
1.335 + test.Title();
1.336 + ::TheTrapCleanup = CTrapCleanup::New();
1.337 +
1.338 + __UHEAP_MARK;
1.339 + test(::TheFs.Connect() == KErrNone);
1.340 +
1.341 + ::Cleanup();
1.342 + TRAPD(err, ::MainL());
1.343 + test(err == KErrNone);
1.344 + test.Close();
1.345 + ::Cleanup();
1.346 +
1.347 + ::TheFs.Close();
1.348 + __UHEAP_MARKEND;
1.349 + delete ::TheTrapCleanup;
1.350 + return 0;
1.351 + }
1.352 +