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\server\t_rand.cpp
24 GLDEF_D RTest test(_L("T_RAND"));
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 void WriteCluster(RFile& aFile,TInt aCluster)
38 // Extend aFile by 1 cluster
42 TUint8* bufPtr=(TUint8*)testBuf.Ptr();
43 testBuf.SetLength(testBuf.MaxSize());
44 Mem::Fill(bufPtr,testBuf.MaxSize(),aCluster);
45 TInt r=aFile.Write(testBuf);
49 LOCAL_C void SeekToCluster(RFile& aFile,TInt aCluster)
51 // Seek to aCluster and check it is found correctly
54 TBuf8<508> seekBuf(508);
55 TInt r=aFile.Read(aCluster*testBuf.MaxSize(),seekBuf);
57 test(seekBuf[0]==(TUint8)aCluster && seekBuf[507]==(TUint8)aCluster);
60 LOCAL_C void SeekToCluster(RFile& aFile,TInt aCluster1,TInt aCluster2)
62 // Seek to aCluster and check it is found correctly
65 TBuf8<508> seekBuf(508);
66 TInt r=aFile.Read(aCluster1*testBuf.MaxSize(),seekBuf);
68 test(seekBuf[0]==(TUint8)aCluster1 && seekBuf[507]==(TUint8)aCluster1);
69 r=aFile.Read(aCluster2*testBuf.MaxSize(),seekBuf);
71 test(seekBuf[0]==(TUint8)aCluster2 && seekBuf[507]==(TUint8)aCluster2);
74 LOCAL_C void ExhaustiveTest(RFile& aFile,TInt aCount1)
76 // Test every possible seeking combination
81 for(k=0;k<aCount1;k++)
83 for(i=aCount1-1;i>0;i--)
85 SeekToCluster(aFile,i);
86 SeekToCluster(aFile,k);
88 test.Printf(_L("Seek from %d \r"),k);
90 test.Printf(_L("\n"));
95 // Test openning a large file
99 test.Next(_L("Create interleaved files"));
102 TInt r=f1.Replace(TheFs,_L("BIGFILE1.TST"),EFileWrite);
104 r=f2.Replace(TheFs,_L("BIGFILE2.TST"),EFileWrite);
107 TInt maxListLength=4;
111 for (k=0;k<maxListLength;k++)
113 for (i=0;i<maxListLength;i++)
117 WriteCluster(f1,countf1++);
119 WriteCluster(f2,countf2++);
120 test.Printf(_L("Written %d to file1 %d to file2\n"),i+1,k+1);
124 ExhaustiveTest(f1,countf1);
125 ExhaustiveTest(f2,countf2);
127 SeekToCluster(f1,1,10);
128 SeekToCluster(f1,6,3);
129 SeekToCluster(f1,8,4);
130 SeekToCluster(f1,12,3);
131 SeekToCluster(f1,23,32);
132 SeekToCluster(f1,5,8);
133 SeekToCluster(f1,7,9);
134 SeekToCluster(f1,12,1);
135 SeekToCluster(f1,2,32);
136 SeekToCluster(f1,16,8);
137 SeekToCluster(f1,9,5);
138 SeekToCluster(f1,33,6);
139 SeekToCluster(f1,13,7);
140 SeekToCluster(f1,9,17);
141 SeekToCluster(f1,4,5);
142 SeekToCluster(f1,5,31);
143 SeekToCluster(f1,11,10);
144 SeekToCluster(f1,1,2);
145 SeekToCluster(f1,5,5);
149 r=TheFs.Delete(_L("BIGFile1.tst"));
151 r=TheFs.Delete(_L("BIGFile2.tst"));
158 // Reproduce old bugs
162 test.Next(_L("Regression Protection"));
165 TInt r=f1.Replace(TheFs,_L("BIGFILE1.TST"),EFileWrite);
167 r=f2.Replace(TheFs,_L("BIGFILE2.TST"),EFileWrite);
184 r=TheFs.Delete(_L("BIGFile1.tst"));
186 r=TheFs.Delete(_L("BIGFile2.tst"));
193 // Change file size while seeking
197 test.Next(_L("Alter filesize"));
202 TInt r=f1.Replace(TheFs,_L("BIGFILE1.TST"),EFileWrite);
213 TInt cluster1=Math::Rand(TheSeed)%15;
214 TInt cluster2=Math::Rand(TheSeed)%15;
215 SeekToCluster(f1,cluster2,cluster1);
218 test.Next(_L("Increase Size"));
219 r=f1.SetSize(1048577);
220 test(r==KErrNone || r==KErrDiskFull);
223 test.Printf(_L("File too big\n"));
228 test.Next(_L("Test data still present"));
231 TInt cluster1=Math::Rand(TheSeed)%15;
232 TInt cluster2=Math::Rand(TheSeed)%15;
233 SeekToCluster(f1,cluster2,cluster1);
237 r=f1.Seek(ESeekStart,newPos);
240 test.Next(_L("Write more data"));
244 test.Next(_L("Seek to new data"));
247 TInt cluster1=Math::Rand(TheSeed)%83;
248 TInt cluster2=Math::Rand(TheSeed)%83;
249 SeekToCluster(f1,cluster2,cluster1);
252 test.Next(_L("Reduce file size"));
253 r=f1.SetSize(135000);
256 test.Next(_L("Test data still present"));
259 TInt cluster1=Math::Rand(TheSeed)%31;
260 TInt cluster2=Math::Rand(TheSeed)%31;
261 SeekToCluster(f1,cluster2,cluster1);
270 TFileReader(RFile* aFile);
271 void Next(TUint8& aVal,TInt& aLength);
272 TBool Compare(TUint8 aVal,TInt aLength);
279 TFileReader::TFileReader(RFile* aFile)
283 : iFile(*aFile), iPos(0)
286 TInt r=iFile.Read(0,iData);
290 void TFileReader::Next(TUint8& aVal,TInt& aLength)
292 // Read aLength contiguous bytes with aVal
296 if (iPos==iData.Length())
298 TInt r=iFile.Read(iData);
301 if (iData.Length()==0)
310 while(iPos<iData.Length())
312 if (iData[iPos]!=aVal)
319 TBool TFileReader::Compare(TUint8 aVal, TInt aLength)
321 // Compare file contents == aVal for aLength bytes
327 if(iPos==iData.Length())
329 TInt r=iFile.Read(iData);
332 test.Printf(_L("READ error %d\n"),r);
336 test.Printf(_L("connect returned %d\n"),r);
343 if (iData.Length()==0)
345 test.Printf(_L("\nFound Error\n"));
351 while(iPos<iData.Length())
353 if (iData[iPos]!=aVal)
355 test.Printf(_L("\nFound Error\n"));
368 LOCAL_C void CheckFileContents(RFile* aFile)
370 // Check all files have consistent contents
374 TFileReader f0(aFile);
375 TFileReader f1(aFile+1);
376 TFileReader f2(aFile+2);
377 TFileReader f3(aFile+3);
386 test(f1.Compare(val,length*mult[1]));
387 test(f2.Compare(val,length*mult[2]));
388 test(f3.Compare(val,length*mult[3]));
396 test.Printf(_L("\nFound Error\n"));
404 test.Printf(_L("\nFound Error\n"));
412 test.Printf(_L("\nFound Error\n"));
421 // Read, write and resize 4 interleaved files
426 HBufC8* dataBuf=HBufC8::NewL(KMaxBufferLength);
428 TInt r=f[0].Replace(TheFs,_L("TEST1.DAT"),EFileWrite);
430 r=f[1].Replace(TheFs,_L("TEST2.DAT"),EFileWrite);
432 r=f[2].Replace(TheFs,_L("TEST3.DAT"),EFileWrite);
434 r=f[3].Replace(TheFs,_L("TEST4.DAT"),EFileWrite);
443 TInt pos=(size) ? Math::Rand(TheSeed)%size : 0;
444 TInt len=Math::Rand(TheSeed)%KMaxLengthIncrement;
445 TInt order=Math::Rand(TheSeed)%KMaxFiles;
446 TInt value=Math::Rand(TheSeed)%KMaxTUint8;
448 TUint8* data=(TUint8*)dataBuf->Ptr();
449 Mem::Fill(data,KMaxBufferLength,value);
454 for (TInt i=0;i<KMaxFiles;i++)
456 TInt fileNum=(order+i)%KMaxFiles;
457 TInt s=len*mult[fileNum];
458 TInt filePos=pos*mult[fileNum];
459 r=f[fileNum].Seek(ESeekStart,filePos);
464 TInt l=(s>KMaxBufferLength) ? KMaxBufferLength : s;
465 dataBuf->Des().SetLength(l);
466 r=f[fileNum].Write(*dataBuf);
468 // Flush if write caching enabled to ensure we get disk space notifications
469 if ((gDriveCacheFlags & EFileCacheWriteOn) && (r == KErrNone))
470 r = f[fileNum].Flush();
480 if ((iteration%KCheckFileFrequency)==0)
481 CheckFileContents(&f[0]);
483 test.Printf(_L("Iteration %d, size %d \r"),iteration,size);
484 if (iteration==KMaxIteration)
487 if ((iteration%KReduceSizeFrequency)==0)
489 size=(size) ? Math::Rand(TheSeed)%size : 0;
490 test.Printf(_L("\nReduceSize newsize=%d\n"),size);
491 for (TInt i=0;i<KMaxFiles;i++)
493 TInt fileNum=(order+i)%KMaxFiles;
494 r=f[fileNum].SetSize(size*mult[fileNum]);
497 CheckFileContents(&f[0]);
502 for (TInt i=0;i<KMaxFiles;i++)
504 test.Printf(_L("\n"));
507 GLDEF_C void CallTestsL()
513 #if defined(__WINS__)
514 if (gSessionPath[0]=='C')
517 if (gSessionPath[0]=='C' || gSessionPath[0]=='Y')
521 CreateTestDirectory(_L("\\TRAND\\"));
526 DeleteTestDirectory();