First public contribution.
1 // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32test\fsstress\t_ramstr.cpp
24 GLDEF_D RTest test(_L("T_RAMSTR"));
26 LOCAL_D TBuf8<512> testBuf(512);
27 LOCAL_D TInt64 TheSeed=917824;
28 LOCAL_D TInt KMaxIteration;
29 LOCAL_D const TInt KMaxFiles=4;
30 LOCAL_D const TInt KMaxLengthIncrement=7770;
31 LOCAL_D const TInt mult[] = { 1, 5, 13, 37};
32 LOCAL_D const TInt KReduceSizeFrequency=20; // 1 reduce in ?? iterations
33 LOCAL_D const TInt KCheckFileFrequency=20000; // 1 check in ?? iterations
34 LOCAL_D const TInt KMaxBufferLength=0x8000;
36 LOCAL_C TInt CreateFileEx(const TDesC& aBaseName,TInt aX, TInt anInitialSize)
38 // Create a single cluster file
41 TBuf<128> fileName=aBaseName;
42 fileName.AppendNum(aX);
44 TInt r=file.Replace(TheFs,fileName,EFileWrite);
49 test.Printf(_L("ERROR:: Replace returned %d\n"),r);
54 r=file.SetSize(anInitialSize);
61 test.Printf(_L("ERROR:: SetSize returned %d\n"),r);
66 // r=TheFs.CheckDisk(fileName);
67 // if (r!=KErrNone && r!=KErrNotSupported)
69 // test.Printf(_L("ERROR:: CheckDisk returned %d\n"),r);
71 // return(KErrDiskFull);
73 test.Printf(_L("Created file %d size %d\n"),aX, anInitialSize);
77 LOCAL_C TInt DeleteFileEx(TBuf<128>& aBaseName,TInt aX)
83 TBuf<128> fileName=aBaseName;
84 fileName.AppendNum(aX);
85 TInt r=TheFs.Delete(fileName);
87 // r=TheFs.CheckDisk(fileName);
88 // if (r!=KErrNone && r!=KErrNotSupported)
90 // test.Printf(_L("ERROR:: CheckDisk returned %d\n"),r);
93 test.Printf(_L("Deleted File %d\n"),aX);
97 LOCAL_C void WriteCluster(RFile& aFile,TInt aCluster)
99 // Extend aFile by 1 cluster
102 TUint8* bufPtr=(TUint8*)testBuf.Ptr();
103 testBuf.SetLength(testBuf.MaxSize());
104 Mem::Fill(bufPtr,testBuf.MaxSize(),aCluster);
105 TInt r=aFile.Write(testBuf);
109 LOCAL_C void SeekToCluster(RFile& aFile,TInt aCluster)
111 // Seek to aCluster and check it is found correctly
114 TBuf8<508> seekBuf(508);
115 TInt r=aFile.Read(aCluster*testBuf.MaxSize(),seekBuf);
117 test(seekBuf[0]==(TUint8)aCluster && seekBuf[507]==(TUint8)aCluster);
120 LOCAL_C void SeekToCluster(RFile& aFile,TInt aCluster1,TInt aCluster2)
122 // Seek to aCluster and check it is found correctly
125 TBuf8<508> seekBuf(508);
126 TInt r=aFile.Read(aCluster1*testBuf.MaxSize(),seekBuf);
128 test(seekBuf[0]==(TUint8)aCluster1 && seekBuf[507]==(TUint8)aCluster1);
129 r=aFile.Read(aCluster2*testBuf.MaxSize(),seekBuf);
131 test(seekBuf[0]==(TUint8)aCluster2 && seekBuf[507]==(TUint8)aCluster2);
134 LOCAL_C void ExhaustiveTest(RFile& aFile,TInt aCount1)
136 // Test every possible seeking combination
140 for(k=0;k<aCount1;k++)
142 for(i=aCount1-1;i>0;i--)
144 SeekToCluster(aFile,i);
145 SeekToCluster(aFile,k);
147 test.Printf(_L("Seek from %d \r"),k);
149 test.Printf(_L("\n"));
154 // Test opening a large file
157 test.Next(_L("Create interleaved files"));
160 TInt r=f1.Replace(TheFs,_L("BIGFILE1.TST"),EFileWrite);
162 r=f2.Replace(TheFs,_L("BIGFILE2.TST"),EFileWrite);
165 TInt maxListLength=4;
169 for (k=0;k<maxListLength;k++)
171 for (i=0;i<maxListLength;i++)
175 WriteCluster(f1,countf1++);
177 WriteCluster(f2,countf2++);
178 test.Printf(_L("Written %d to file1 %d to file2\n"),i+1,k+1);
182 ExhaustiveTest(f1,countf1);
183 ExhaustiveTest(f2,countf2);
185 SeekToCluster(f1,1,10);
186 SeekToCluster(f1,6,3);
187 SeekToCluster(f1,8,4);
188 SeekToCluster(f1,12,3);
189 SeekToCluster(f1,23,32);
190 SeekToCluster(f1,5,8);
191 SeekToCluster(f1,7,9);
192 SeekToCluster(f1,12,1);
193 SeekToCluster(f1,2,32);
194 SeekToCluster(f1,16,8);
195 SeekToCluster(f1,9,5);
196 SeekToCluster(f1,33,6);
197 SeekToCluster(f1,13,7);
198 SeekToCluster(f1,9,17);
199 SeekToCluster(f1,4,5);
200 SeekToCluster(f1,5,31);
201 SeekToCluster(f1,11,10);
202 SeekToCluster(f1,1,2);
203 SeekToCluster(f1,5,5);
207 r=TheFs.Delete(_L("BIGFile1.tst"));
209 r=TheFs.Delete(_L("BIGFile2.tst"));
216 // Reproduce old bugs
219 test.Next(_L("Regression Protection"));
222 TInt r=f1.Replace(TheFs,_L("BIGFILE1.TST"),EFileWrite);
224 r=f2.Replace(TheFs,_L("BIGFILE2.TST"),EFileWrite);
241 r=TheFs.Delete(_L("BIGFile1.tst"));
243 r=TheFs.Delete(_L("BIGFile2.tst"));
250 // Change file size while seeking
254 test.Next(_L("Alter filesize"));
258 TInt r=f1.Replace(TheFs,_L("BIGFILE1.TST"),EFileWrite);
269 TInt cluster1=Math::Rand(TheSeed)%15;
270 TInt cluster2=Math::Rand(TheSeed)%15;
271 SeekToCluster(f1,cluster2,cluster1);
274 test.Next(_L("Increase Size"));
275 r=f1.SetSize(1048577);
276 test(r==KErrNone || r==KErrDiskFull);
279 test.Printf(_L("File too big\n"));
284 test.Next(_L("Test data still present"));
287 TInt cluster1=Math::Rand(TheSeed)%15;
288 TInt cluster2=Math::Rand(TheSeed)%15;
289 SeekToCluster(f1,cluster2,cluster1);
293 r=f1.Seek(ESeekStart,newPos);
296 test.Next(_L("Write more data"));
300 test.Next(_L("Seek to new data"));
303 TInt cluster1=Math::Rand(TheSeed)%83;
304 TInt cluster2=Math::Rand(TheSeed)%83;
305 SeekToCluster(f1,cluster2,cluster1);
308 test.Next(_L("Reduce file size"));
309 r=f1.SetSize(135000);
312 test.Next(_L("Test data still present"));
315 TInt cluster1=Math::Rand(TheSeed)%31;
316 TInt cluster2=Math::Rand(TheSeed)%31;
317 SeekToCluster(f1,cluster2,cluster1);
326 TFileReader(RFile* aFile);
327 void Next(TUint8& aVal,TInt& aLength);
328 TBool Compare(TUint8 aVal,TInt aLength);
335 TFileReader::TFileReader(RFile* aFile)
339 : iFile(*aFile), iPos(0)
342 TInt r=iFile.Read(0,iData);
346 void TFileReader::Next(TUint8& aVal,TInt& aLength)
348 // Read aLength contiguous bytes with aVal
352 if (iPos==iData.Length())
354 TInt r=iFile.Read(iData);
357 if (iData.Length()==0)
366 while(iPos<iData.Length())
368 if (iData[iPos]!=aVal)
375 TBool TFileReader::Compare(TUint8 aVal, TInt aLength)
377 // Compare file contents == aVal for aLength bytes
383 if(iPos==iData.Length())
385 TInt r=iFile.Read(iData);
388 test.Printf(_L("READ error %d\n"),r);
392 test.Printf(_L("connect returned %d\n"),r);
398 if (iData.Length()==0)
400 test.Printf(_L("\nFound Error\n"));
405 while(iPos<iData.Length())
407 if (iData[iPos]!=aVal)
409 test.Printf(_L("\nFound Error\n"));
421 LOCAL_C void CheckFileContents(RFile* aFile)
423 // Check all files have consistent contents
427 TFileReader f0(aFile);
428 TFileReader f1(aFile+1);
429 TFileReader f2(aFile+2);
430 TFileReader f3(aFile+3);
439 test(f1.Compare(val,length*mult[1]));
440 test(f2.Compare(val,length*mult[2]));
441 test(f3.Compare(val,length*mult[3]));
449 test.Printf(_L("\nFound Error\n"));
456 test.Printf(_L("\nFound Error\n"));
463 test.Printf(_L("\nFound Error\n"));
471 // Read, write and resize 4 interleaved files
476 HBufC8* dataBuf=HBufC8::NewL(KMaxBufferLength);
478 TInt r=f[0].Replace(TheFs,_L("TEST1.DAT"),EFileWrite);
480 r=f[1].Replace(TheFs,_L("TEST2.DAT"),EFileWrite);
482 r=f[2].Replace(TheFs,_L("TEST3.DAT"),EFileWrite);
484 r=f[3].Replace(TheFs,_L("TEST4.DAT"),EFileWrite);
493 TInt pos=(size) ? Math::Rand(TheSeed)%size : 0;
494 TInt len=Math::Rand(TheSeed)%KMaxLengthIncrement;
495 TInt order=Math::Rand(TheSeed)%KMaxFiles;
496 TInt value=Math::Rand(TheSeed)%KMaxTUint8;
498 TUint8* data=(TUint8*)dataBuf->Ptr();
499 Mem::Fill(data,KMaxBufferLength,value);
504 for (TInt i=0;i<KMaxFiles;i++)
506 TInt fileNum=(order+i)%KMaxFiles;
507 TInt s=len*mult[fileNum];
508 TInt filePos=pos*mult[fileNum];
509 r=f[fileNum].Seek(ESeekStart,filePos);
514 TInt l=(s>KMaxBufferLength) ? KMaxBufferLength : s;
515 dataBuf->Des().SetLength(l);
516 r=f[fileNum].Write(*dataBuf);
525 if ((iteration%KCheckFileFrequency)==0)
526 CheckFileContents(&f[0]);
528 test.Printf(_L("Iteration %d, size %d \r"),iteration,size);
529 if (iteration==KMaxIteration)
532 if ((iteration%KReduceSizeFrequency)==0)
534 size=(size) ? Math::Rand(TheSeed)%size : 0;
535 test.Printf(_L("\nReduceSize newsize=%d\n"),size);
536 for (TInt i=0;i<KMaxFiles;i++)
538 TInt fileNum=(order+i)%KMaxFiles;
539 r=f[fileNum].SetSize(size*mult[fileNum]);
542 CheckFileContents(&f[0]);
547 for (TInt i=0;i<KMaxFiles;i++)
549 test.Printf(_L("\n"));
552 LOCAL_C void MultipleFiles(TInt numberOfFiles,TInt anInitialSize)
554 // Test filling the disk with files, and various manipulations thereof
557 test.Start(_L("Test multiple file creation, deletion and resize operations"));
558 TInt r=TheFs.MkDirAll(_L("\\F32-TST\\BIGDIRECTORY\\"));
559 test(r==KErrNone || r==KErrAlreadyExists);
560 TFileName sessionPath;
561 r=TheFs.SessionPath(sessionPath);
565 TBuf<128> fileName=_L("\\F32-TST\\BIGDIRECTORY\\FILE");
567 for (;index<numberOfFiles;index++)
569 r=CreateFileEx(fileName,index,anInitialSize);
575 #if defined(__WINS__)
576 if (index==32 && sessionPath[0]=='C')
581 if ((r==KErrNone)||(r==KErrDiskFull))
582 r=TheFs.CheckDisk(fileName);
584 if (r!=KErrNone && r!=KErrNotSupported)
586 test.Printf(_L("ERROR:: CheckDisk returned %d\n"),r);
590 // Delete half of the files
591 TInt halfCount=index/2;
593 for (; index>halfCount; index--)
594 DeleteFileEx(fileName,index);
598 // Double the size of those files remaining
599 for (index=0; index<halfCount; index++)
602 baseName.AppendNum(index);
603 r=file.Open(TheFs,baseName,EFileRead|EFileWrite);
605 test.Printf(_L("Resized %S from %d to %d\n"),&baseName,anInitialSize,anInitialSize*2);
606 r=file.SetSize(2*anInitialSize);
607 test((r==KErrNone)||(r==KErrDiskFull));
611 r=TheFs.CheckDisk(fileName);
613 if (r!=KErrNone && r!=KErrNotSupported)
615 test.Printf(_L("ERROR:: CheckDisk returned %d\n"),r);
620 for (index=0; index<halfCount; index++)
623 baseName.AppendNum(index);
624 test.Printf(_L("Deleted %S\n"),&baseName);
625 r=TheFs.Delete(baseName);
626 test((r==KErrNone)||(r==KErrNotFound));
629 r=TheFs.CheckDisk(fileName);
631 if (r!=KErrNone && r!=KErrNotSupported)
633 test.Printf(_L("ERROR:: CheckDisk returned %d\n"),r);
641 GLDEF_C void CallTestsL()
647 #if defined(__WINS__)
648 if (gSessionPath[0]=='C')
651 if (gSessionPath[0]=='C' || gSessionPath[0]=='Y')
655 CreateTestDirectory(_L("\\TRAND\\"));
656 test.Printf(_L("\nThis test never stops. It is designed to create, delete and resize \n"));
657 test.Printf(_L("multiple small files on the EPOC machine whilst testing that soft reset\n"));
658 test.Printf(_L("events are not catastrophic to the filesystem. Once the test is running, \n"));
659 test.Printf(_L("push the grey button to reset the machine at random intervals and then\n"));
660 test.Printf(_L("restart the test. \n"));
661 test.Printf(_L("\nPress any key to continue... \n"));
665 MultipleFiles(50,0x200);
666 MultipleFiles(50,0x400);
667 MultipleFiles(50,0x1FF);
668 MultipleFiles(50,0x201);
674 //DeleteTestDirectory();