kerneltest/f32test/server/t_rand.cpp
changeset 0 a41df078684a
child 43 c1f20ce4abcf
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // f32test\server\t_rand.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <f32file.h>
       
    19 #include <e32test.h>
       
    20 #include <e32math.h>
       
    21 #include <e32hal.h>
       
    22 #include "t_server.h"
       
    23 
       
    24 GLDEF_D RTest test(_L("T_RAND"));
       
    25 
       
    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;
       
    35 
       
    36 LOCAL_C void WriteCluster(RFile& aFile,TInt aCluster)
       
    37 //
       
    38 // Extend aFile by 1 cluster
       
    39 //
       
    40 	{
       
    41 
       
    42 	TUint8* bufPtr=(TUint8*)testBuf.Ptr();
       
    43 	testBuf.SetLength(testBuf.MaxSize());
       
    44 	Mem::Fill(bufPtr,testBuf.MaxSize(),aCluster);
       
    45 	TInt r=aFile.Write(testBuf);
       
    46 	test(r==KErrNone);
       
    47 	}
       
    48 
       
    49 LOCAL_C void SeekToCluster(RFile& aFile,TInt aCluster)
       
    50 //
       
    51 // Seek to aCluster and check it is found correctly
       
    52 //
       
    53 	{
       
    54 	TBuf8<508> seekBuf(508);
       
    55 	TInt r=aFile.Read(aCluster*testBuf.MaxSize(),seekBuf);
       
    56 	test(r==KErrNone);
       
    57 	test(seekBuf[0]==(TUint8)aCluster && seekBuf[507]==(TUint8)aCluster);
       
    58 	}
       
    59 
       
    60 LOCAL_C void SeekToCluster(RFile& aFile,TInt aCluster1,TInt aCluster2)
       
    61 //
       
    62 // Seek to aCluster and check it is found correctly
       
    63 //
       
    64 	{
       
    65 	TBuf8<508> seekBuf(508);
       
    66 	TInt r=aFile.Read(aCluster1*testBuf.MaxSize(),seekBuf);
       
    67 	test(r==KErrNone);
       
    68 	test(seekBuf[0]==(TUint8)aCluster1 && seekBuf[507]==(TUint8)aCluster1);
       
    69 	r=aFile.Read(aCluster2*testBuf.MaxSize(),seekBuf);
       
    70 	test(r==KErrNone);
       
    71 	test(seekBuf[0]==(TUint8)aCluster2 && seekBuf[507]==(TUint8)aCluster2);
       
    72 	}
       
    73 
       
    74 LOCAL_C void ExhaustiveTest(RFile& aFile,TInt aCount1)
       
    75 //
       
    76 // Test every possible seeking combination
       
    77 //
       
    78 	{
       
    79 
       
    80 	TInt i=0,k=0;
       
    81 	for(k=0;k<aCount1;k++)
       
    82 		{
       
    83 		for(i=aCount1-1;i>0;i--)
       
    84 			{
       
    85 			SeekToCluster(aFile,i);
       
    86 			SeekToCluster(aFile,k);
       
    87 			}
       
    88 		test.Printf(_L("Seek from %d          \r"),k);
       
    89 		}
       
    90 		test.Printf(_L("\n"));
       
    91 	}
       
    92 
       
    93 LOCAL_C void Test1()
       
    94 //
       
    95 // Test openning a large file
       
    96 //
       
    97 	{
       
    98 
       
    99 	test.Next(_L("Create interleaved files"));
       
   100 	RFile f1,f2;
       
   101 //
       
   102 	TInt r=f1.Replace(TheFs,_L("BIGFILE1.TST"),EFileWrite);
       
   103 	test(r==KErrNone);
       
   104 	r=f2.Replace(TheFs,_L("BIGFILE2.TST"),EFileWrite);
       
   105 	test(r==KErrNone);
       
   106 //
       
   107 	TInt maxListLength=4;
       
   108 	TInt i=0,k=0;
       
   109 	TInt countf1=0;
       
   110 	TInt countf2=0;
       
   111 	for (k=0;k<maxListLength;k++)
       
   112 		{
       
   113 		for (i=0;i<maxListLength;i++)
       
   114 			{
       
   115 			TInt j;
       
   116 			for (j=0;j<=i;j++)
       
   117 				WriteCluster(f1,countf1++);
       
   118 			for (j=0;j<=k;j++)
       
   119 				WriteCluster(f2,countf2++);
       
   120 			test.Printf(_L("Written %d to file1 %d to file2\n"),i+1,k+1);
       
   121 			}
       
   122 		}
       
   123 
       
   124 	ExhaustiveTest(f1,countf1);
       
   125 	ExhaustiveTest(f2,countf2);
       
   126 
       
   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);
       
   146 
       
   147 	f1.Close();
       
   148 	f2.Close();
       
   149 	r=TheFs.Delete(_L("BIGFile1.tst"));
       
   150 	test(r==KErrNone);
       
   151 	r=TheFs.Delete(_L("BIGFile2.tst"));
       
   152 	test(r==KErrNone);
       
   153 	CheckDisk();
       
   154 	}
       
   155 
       
   156 LOCAL_C void Test2()
       
   157 //
       
   158 // Reproduce old bugs
       
   159 //
       
   160 	{
       
   161 
       
   162 	test.Next(_L("Regression Protection"));
       
   163 	RFile f1,f2;
       
   164 //
       
   165 	TInt r=f1.Replace(TheFs,_L("BIGFILE1.TST"),EFileWrite);
       
   166 	test(r==KErrNone);
       
   167 	r=f2.Replace(TheFs,_L("BIGFILE2.TST"),EFileWrite);
       
   168 	test(r==KErrNone);
       
   169 //
       
   170 	WriteCluster(f1,0);
       
   171 	WriteCluster(f1,1);
       
   172 	WriteCluster(f1,2);
       
   173 	WriteCluster(f1,3);
       
   174 	WriteCluster(f1,4);
       
   175 	WriteCluster(f1,5);
       
   176 	WriteCluster(f2,0);
       
   177 	WriteCluster(f1,6);
       
   178 //
       
   179 	SeekToCluster(f1,6);
       
   180 	SeekToCluster(f1,4);
       
   181 //
       
   182 	f1.Close();
       
   183 	f2.Close();
       
   184 	r=TheFs.Delete(_L("BIGFile1.tst"));
       
   185 	test(r==KErrNone);
       
   186 	r=TheFs.Delete(_L("BIGFile2.tst"));
       
   187 	test(r==KErrNone);
       
   188 	CheckDisk();
       
   189 	}
       
   190 
       
   191 LOCAL_C void Test3()
       
   192 //
       
   193 // Change file size while seeking
       
   194 //
       
   195 	{
       
   196 
       
   197 	test.Next(_L("Alter filesize"));
       
   198 	RFile f1;
       
   199 	TheSeed=917824;
       
   200 	TInt i=0,j=0;
       
   201 //
       
   202 	TInt r=f1.Replace(TheFs,_L("BIGFILE1.TST"),EFileWrite);
       
   203 	test(r==KErrNone);
       
   204 	
       
   205 	r=f1.SetSize(65534);
       
   206 	test(r==KErrNone);
       
   207 
       
   208 	for(i=0;i<=15;i++)
       
   209 		WriteCluster(f1,i);
       
   210 
       
   211 	for (j=0;j<100;j++)
       
   212 		{
       
   213 		TInt cluster1=Math::Rand(TheSeed)%15;
       
   214 		TInt cluster2=Math::Rand(TheSeed)%15;
       
   215 		SeekToCluster(f1,cluster2,cluster1);
       
   216 		}
       
   217 
       
   218 	test.Next(_L("Increase Size"));
       
   219 	r=f1.SetSize(1048577);
       
   220 	test(r==KErrNone || r==KErrDiskFull);
       
   221 	if (r==KErrDiskFull)
       
   222 		{
       
   223 		test.Printf(_L("File too big\n"));
       
   224 		f1.Close();
       
   225 		return;
       
   226 		}
       
   227 
       
   228 	test.Next(_L("Test data still present"));
       
   229 	for (j=0;j<200;j++)
       
   230 		{
       
   231 		TInt cluster1=Math::Rand(TheSeed)%15;
       
   232 		TInt cluster2=Math::Rand(TheSeed)%15;
       
   233 		SeekToCluster(f1,cluster2,cluster1);
       
   234 		}
       
   235 
       
   236 	TInt newPos=8192;
       
   237 	r=f1.Seek(ESeekStart,newPos);
       
   238 	test(r==KErrNone);
       
   239 
       
   240 	test.Next(_L("Write more data"));
       
   241 	for(i=16;i<83;i++)
       
   242 		WriteCluster(f1,i);
       
   243 
       
   244 	test.Next(_L("Seek to new data"));
       
   245 	for (j=0;j<200;j++)
       
   246 		{
       
   247 		TInt cluster1=Math::Rand(TheSeed)%83;
       
   248 		TInt cluster2=Math::Rand(TheSeed)%83;
       
   249 		SeekToCluster(f1,cluster2,cluster1);
       
   250 		}
       
   251 
       
   252 	test.Next(_L("Reduce file size"));
       
   253 	r=f1.SetSize(135000);
       
   254 	test(r==KErrNone);
       
   255 
       
   256 	test.Next(_L("Test data still present"));
       
   257 	for (j=0;j<200;j++)
       
   258 		{
       
   259 		TInt cluster1=Math::Rand(TheSeed)%31;
       
   260 		TInt cluster2=Math::Rand(TheSeed)%31;
       
   261 		SeekToCluster(f1,cluster2,cluster1);
       
   262 		}
       
   263 
       
   264 	f1.Close();
       
   265 	}
       
   266 
       
   267 class TFileReader
       
   268 	{
       
   269 public:
       
   270 	TFileReader(RFile* aFile);
       
   271 	void Next(TUint8& aVal,TInt& aLength);
       
   272 	TBool Compare(TUint8 aVal,TInt aLength);
       
   273 private:
       
   274 	RFile iFile;
       
   275 	TBuf8<512> iData;
       
   276 	TInt iPos;
       
   277 	};
       
   278 
       
   279 TFileReader::TFileReader(RFile* aFile)
       
   280 //
       
   281 // Constructor
       
   282 //
       
   283 	: iFile(*aFile), iPos(0)
       
   284 	{
       
   285 
       
   286 	TInt r=iFile.Read(0,iData);
       
   287 	test(r==KErrNone);
       
   288 	}
       
   289 
       
   290 void TFileReader::Next(TUint8& aVal,TInt& aLength)
       
   291 //
       
   292 // Read aLength contiguous bytes with aVal
       
   293 //
       
   294 	{
       
   295 
       
   296 	if (iPos==iData.Length())
       
   297 		{
       
   298 		TInt r=iFile.Read(iData);
       
   299 		test(r==KErrNone);
       
   300 		iPos=0;
       
   301 		if (iData.Length()==0)
       
   302 			{
       
   303 			aLength=0;
       
   304 			return;
       
   305 			}
       
   306 		}
       
   307 
       
   308 	aVal=iData[iPos];
       
   309 	aLength=0;
       
   310 	while(iPos<iData.Length())
       
   311 		{
       
   312 		if (iData[iPos]!=aVal)
       
   313 			break;
       
   314 		iPos++;
       
   315 		aLength++;
       
   316 		}
       
   317 	}
       
   318 
       
   319 TBool TFileReader::Compare(TUint8 aVal, TInt aLength)
       
   320 //
       
   321 // Compare file contents == aVal for aLength bytes
       
   322 //
       
   323 	{
       
   324 
       
   325 	FOREVER
       
   326 		{
       
   327 		if(iPos==iData.Length())
       
   328 			{
       
   329 			TInt r=iFile.Read(iData);
       
   330 			if (r!=KErrNone)
       
   331 				{
       
   332 				test.Printf(_L("READ error %d\n"),r);
       
   333 				//test.Getch();
       
   334 				RFs fs;
       
   335 				r=fs.Connect();
       
   336 				test.Printf(_L("connect returned %d\n"),r);
       
   337 				//test.Getch();
       
   338 				fs.Close();
       
   339 				test(0);
       
   340 				return(EFalse);
       
   341 				}
       
   342 			iPos=0;
       
   343 			if (iData.Length()==0)
       
   344 				{
       
   345 				test.Printf(_L("\nFound Error\n"));
       
   346 				test(0);
       
   347 				//test.Getch();
       
   348 				return(EFalse);
       
   349 				}
       
   350 			}
       
   351 		while(iPos<iData.Length())
       
   352 			{
       
   353 			if (iData[iPos]!=aVal)
       
   354 				{
       
   355 				test.Printf(_L("\nFound Error\n"));
       
   356 				test(0);
       
   357 				//test.Getch();
       
   358 				return(EFalse);
       
   359 				}
       
   360 			iPos++;
       
   361 			aLength--;
       
   362 			if (aLength==0)
       
   363 				return(ETrue);
       
   364 			}
       
   365 		}
       
   366 	}
       
   367 
       
   368 LOCAL_C void CheckFileContents(RFile* aFile)
       
   369 //
       
   370 // Check all files have consistent contents
       
   371 //
       
   372 	{
       
   373 
       
   374 	TFileReader f0(aFile);
       
   375 	TFileReader f1(aFile+1);
       
   376 	TFileReader f2(aFile+2);
       
   377 	TFileReader f3(aFile+3);
       
   378 
       
   379 	FOREVER
       
   380 		{
       
   381 		TUint8 val;
       
   382 		TInt length;
       
   383 		f0.Next(val,length);
       
   384 		if (length==0)
       
   385 			break;
       
   386 		test(f1.Compare(val,length*mult[1]));
       
   387 		test(f2.Compare(val,length*mult[2]));
       
   388 		test(f3.Compare(val,length*mult[3]));
       
   389 		}
       
   390 
       
   391 	TUint8 val;
       
   392 	TInt length;
       
   393 	f1.Next(val,length);
       
   394 	if (length!=0)
       
   395 		{
       
   396 		test.Printf(_L("\nFound Error\n"));
       
   397 		test(0);
       
   398 		//test.Getch();
       
   399 		}
       
   400 	test(length==0);
       
   401 	f2.Next(val,length);
       
   402 	if (length!=0)
       
   403 		{
       
   404 		test.Printf(_L("\nFound Error\n"));
       
   405 		test(0);
       
   406 		//test.Getch();
       
   407 		}
       
   408 	test(length==0);
       
   409 	f3.Next(val,length);
       
   410 	if (length!=0)
       
   411 		{
       
   412 		test.Printf(_L("\nFound Error\n"));
       
   413 		test(0);
       
   414 		//test.Getch();
       
   415 		}
       
   416 	test(length==0);
       
   417 	}		
       
   418 		
       
   419 LOCAL_C void Test4()
       
   420 //
       
   421 // Read, write and resize 4 interleaved files
       
   422 //
       
   423 	{
       
   424 
       
   425 	RFile f[KMaxFiles];
       
   426 	HBufC8* dataBuf=HBufC8::NewL(KMaxBufferLength);
       
   427 
       
   428 	TInt r=f[0].Replace(TheFs,_L("TEST1.DAT"),EFileWrite);
       
   429 	test(r==KErrNone);
       
   430 	r=f[1].Replace(TheFs,_L("TEST2.DAT"),EFileWrite);
       
   431 	test(r==KErrNone);
       
   432 	r=f[2].Replace(TheFs,_L("TEST3.DAT"),EFileWrite);
       
   433 	test(r==KErrNone);
       
   434 	r=f[3].Replace(TheFs,_L("TEST4.DAT"),EFileWrite);
       
   435 	test(r==KErrNone);
       
   436 	
       
   437 	TInt size=0;
       
   438 	TInt iteration=0;
       
   439 
       
   440 	FOREVER
       
   441 		{
       
   442 		iteration++;
       
   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;
       
   447 	
       
   448 		TUint8* data=(TUint8*)dataBuf->Ptr();
       
   449 		Mem::Fill(data,KMaxBufferLength,value);
       
   450 
       
   451 		if (pos+len>size)
       
   452 			size=pos+len;
       
   453 
       
   454 		for (TInt i=0;i<KMaxFiles;i++)
       
   455 			{
       
   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);
       
   460 			test(r==KErrNone);
       
   461 
       
   462 			while(s>0)
       
   463 				{
       
   464 				TInt l=(s>KMaxBufferLength) ? KMaxBufferLength : s;
       
   465 				dataBuf->Des().SetLength(l);
       
   466 				r=f[fileNum].Write(*dataBuf);
       
   467 
       
   468 				// Flush if write caching enabled to ensure we get disk space notifications
       
   469 				if ((gDriveCacheFlags & EFileCacheWriteOn) && (r == KErrNone))
       
   470 					r = f[fileNum].Flush();
       
   471 			
       
   472 				if (r==KErrDiskFull)
       
   473 					goto End;
       
   474 				test(r==KErrNone);
       
   475 				s-=l;
       
   476 				}
       
   477 			
       
   478 			}
       
   479 
       
   480 		if ((iteration%KCheckFileFrequency)==0)
       
   481 			CheckFileContents(&f[0]);
       
   482 
       
   483 		test.Printf(_L("Iteration %d, size %d       \r"),iteration,size);
       
   484 		if (iteration==KMaxIteration)
       
   485 			break;
       
   486 		
       
   487 		if ((iteration%KReduceSizeFrequency)==0)
       
   488 			{
       
   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++)
       
   492 				{
       
   493 				TInt fileNum=(order+i)%KMaxFiles;
       
   494 				r=f[fileNum].SetSize(size*mult[fileNum]);
       
   495 				test(r==KErrNone);
       
   496 				}
       
   497 			CheckFileContents(&f[0]);
       
   498 			}
       
   499 		}
       
   500 End:
       
   501 	delete dataBuf;
       
   502 	for (TInt i=0;i<KMaxFiles;i++)
       
   503 		f[i].Close();
       
   504 	test.Printf(_L("\n"));
       
   505 	}
       
   506 
       
   507 GLDEF_C void CallTestsL()
       
   508 //
       
   509 // Call all tests
       
   510 //
       
   511 	{
       
   512 
       
   513 #if defined(__WINS__)
       
   514 	if (gSessionPath[0]=='C')
       
   515 		return;
       
   516 #endif
       
   517 	if (gSessionPath[0]=='C' || gSessionPath[0]=='Y')
       
   518 		KMaxIteration=100;
       
   519 	else
       
   520 		KMaxIteration=100;
       
   521 	CreateTestDirectory(_L("\\TRAND\\"));
       
   522 	Test1();
       
   523 	Test2();
       
   524 	Test3();
       
   525 	Test4();
       
   526 	DeleteTestDirectory();
       
   527 	}