Update contrib.
1 // Copyright (c) 2002-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 // Tests reading from files
19 #include <e32std_private.h>
27 RTest test( _L("T_ROFSFILE") );
30 _LIT( KSpotFile1, "root.txt" );
31 _LIT( KSpotFile2, "Dir1\\level1.txt" );
32 _LIT( KSpotFile3, "Dir1\\Dir2\\level2.txt" );
33 _LIT( KSpotFile4, "Dir1\\Dir2\\Dir3\\level3.txt" );
34 _LIT( KSpotFile5, "Dir1\\Dir2\\Dir3\\Dir4\\level4.txt" );
35 _LIT( KSpotFile6, "Dir1\\Dir2\\Dir3\\Dir4\\Dir5\\level5.txt" );
36 _LIT( KSpotFile7, "Dir1\\Dir2\\Dir3\\Dir4\\Dir5\\Dir6\\level6.txt" );
37 _LIT( KSpotFile8, "Dir1\\Dir2\\Dir3\\Dir4\\Dir5\\Dir6\\Dir7\\level7.txt" );
38 _LIT( KSpotFile9, "DeepDir1\\DeepDir2\\DeepDir3\\DeepDir4\\DeepDir5\\DeepDir6\\DeepDir7\\DeepDir8\\DeepDir9\\DeepDir10\\DeepDir11\\DeepDir12\\DeepDir13\\DeepDir14\\file.txt" );
39 _LIT( KSpotFile10, "Parent\\parfile.txt" );
40 _LIT( KSpotFile11, "Parent\\SubDir1A\\subfileA.txt" );
41 _LIT( KSpotFile12, "Mixed\\par1.txt" );
42 _LIT( KSpotFile13, "Mixed\\SubDir1\\sub1.txt" );
43 _LIT( KSpotFile14, "Mixed\\SubDir2\\sub2.txt" );
44 _LIT( KSpotFile15, "Dir1\\level1_ext.txt" );
47 _LIT( KSameDirFile1, "ReadTest\\A\\file1" );
48 _LIT( KSameDirFile2, "ReadTest\\A\\file2" );
49 _LIT( KSameDirFile3, "ReadTest\\A\\file3" );
50 _LIT( KSameDirFile4, "ReadTest\\A\\file4" );
51 const TInt KSameDirFileLength = 256;
52 const TUint KSameDirFile1Seed = 0xEF1113BC;
53 const TUint KSameDirFile2Seed = 0x04082195;
54 const TUint KSameDirFile3Seed = 0xEC36D359;
55 const TUint KSameDirFile4Seed = 0x07D4DAC2;
57 _LIT( KSameNameFile1, "ReadTest\\B\\B1\\testfile" );
58 _LIT( KSameNameFile2, "ReadTest\\B\\B2\\testfile" );
59 _LIT( KSameNameFile3, "ReadTest\\B\\B3\\testfile" );
60 _LIT( KSameNameFile4, "ReadTest\\B\\B4\\testfile" );
61 const TInt KSameNameFileLength = 256; // we will read this much of the file
62 const TUint KSameNameFile1Seed = 0x8DA9AA5A;
63 const TUint KSameNameFile2Seed = 0x735AA240;
64 const TUint KSameNameFile3Seed = 0x42D4BF02;
65 const TUint KSameNameFile4Seed = 0x47C728FB;
67 _LIT( KRandomReadFile, "ReadTest\\C\\seektest" );
68 const TInt KRandomReadFileSize = 17466;
69 const TUint KRandomReadFileSeed = 0x8DA9AA5A;
71 _LIT( KEofFile1, "ReadTest\\D\\eoftest1" );
72 _LIT( KEofFile2, "ReadTest\\D\\eoftest2" );
73 _LIT( KEofFile3, "ReadTest\\D\\eoftest3" );
74 _LIT( KEofFile4, "ReadTest\\D\\eoftest4" );
75 _LIT( KEofFile5, "ReadTest\\D\\eoftest5" );
76 _LIT( KEofFile6, "ReadTest\\D\\eoftest6" );
79 _LIT( KStreamFile1, "ReadTest\\E\\stream1" );
80 _LIT( KStreamFile2, "ReadTest\\E\\stream2" );
81 _LIT( KStreamFile3, "ReadTest\\E\\stream3" );
82 _LIT( KStreamFile4, "ReadTest\\E\\stream4" );
84 _LIT( KTestFileExt, "ext.txt" );
86 const TUint KStreamReadSeed = 0x8DA9AA5A;
88 _LIT( KDriveBase, " :\\" );
91 const TInt KHello8FileSize = 5;
92 _LIT8( KHello8FileContent, "hello" );
95 // A large buffer for file reads
96 LOCAL_D TBuf8<18000> gFileBuffer;
99 LOCAL_C void SpotCheckFilesL(TInt aDriveToTest, TBool aExtension)
101 // Tests a few files for size & content. These are all the hello8.txt test file
104 const TDesC* const fileArray[15] =
106 &KSpotFile1, &KSpotFile2, &KSpotFile3, &KSpotFile4, &KSpotFile5,
107 &KSpotFile6, &KSpotFile7, &KSpotFile8, &KSpotFile9, &KSpotFile10,
108 &KSpotFile11, &KSpotFile12, &KSpotFile13, &KSpotFile14, &KSpotFile15
111 test.Next( _L("Spot-check some files") );
112 TFileName name(KDriveBase);
113 name[0] = TText('A' + aDriveToTest);
116 if(aExtension) index =1;
117 for( TInt i = index; i < 14; i++ )
119 name.SetLength( 3 ); // trim back to drive specifier
120 if(aExtension &&(i==1))
121 name.Append( *fileArray[i+13] );
123 name.Append( *fileArray[i] );
125 test.Printf( _L("Opening file %S"), &name );
127 TInt r = file.Open( TheFs, name, EFileRead );
131 r = file.Size( fileSize );
133 TEST_FOR_MATCH( fileSize, KHello8FileSize );
136 r = file.Read( buf );
138 TEST_FOR_MATCH( buf.Length(), KHello8FileSize );
139 test(buf == KHello8FileContent);
145 LOCAL_C void FillRandomBuffer( TDes8& aDes, TRandomGenerator& aRand, TInt aLength )
147 aDes.SetLength( aLength );
148 TUint* ptr = (TUint*)aDes.Ptr();
149 while( aLength >= 4 )
151 TUint v = aRand.Next();
157 TUint v = aRand.Next();
158 TUint8* p8 = (TUint8*)ptr;
161 *p8++ = (TUint8)(v & 0xFF);
168 LOCAL_C TBool ValidateRandomBuffer( const TDes8& aDes, TUint aSeed )
170 const TUint* ptr = (const TUint*)aDes.Ptr();
171 TInt length = aDes.Length();
173 TRandomGenerator rand;
174 rand.SetSeed( aSeed );
177 TUint v = rand.Next();
186 TUint v = rand.Next();
187 TUint8* p8 = (TUint8*)ptr;
190 if( *p8++ != (TUint8)(v & 0xFF) )
203 LOCAL_C void TestFilesInSameDirectoryL(TInt aDriveToTest)
205 // Tests opening different files in the same directory
208 test.Next( _L("Testing files in same directory") );
213 TRandomGenerator iRand;
217 TFileName name(KDriveBase);
218 name[0] = TText('A' + aDriveToTest);
220 name.Append( KSameDirFile1 );
221 TInt r = files[0].iFile.Open( TheFs, name, EFileRead );
225 name.Append( KSameDirFile2 );
226 r = files[1].iFile.Open( TheFs, name, EFileRead );
230 name.Append( KSameDirFile3 );
231 r = files[2].iFile.Open( TheFs, name, EFileRead );
235 name.Append( KSameDirFile4 );
236 r = files[3].iFile.Open( TheFs, name, EFileRead );
239 const TInt KReadLen = KSameDirFileLength / 16;
240 TBuf8<KReadLen> readBuf;
241 TBuf8<KReadLen> randomBuf;
243 files[0].iRand.SetSeed( KSameDirFile1Seed );
244 files[1].iRand.SetSeed( KSameDirFile2Seed );
245 files[2].iRand.SetSeed( KSameDirFile3Seed );
246 files[3].iRand.SetSeed( KSameDirFile4Seed );
248 for( TInt fragment = 0; fragment < (KSameDirFileLength / KReadLen); ++fragment )
250 // read next fragment from each file and compare
251 for( TInt j = 0; j < 4; ++j )
253 FillRandomBuffer( randomBuf, files[j].iRand, KReadLen );
254 r = files[j].iFile.Read( readBuf );
256 TEST_FOR_MATCH( readBuf.Length(), KReadLen );
257 test( readBuf == randomBuf );
260 files[0].iFile.Close();
261 files[1].iFile.Close();
262 files[2].iFile.Close();
263 files[3].iFile.Close();
267 LOCAL_C void TestFilesSameNameDifferentDirectoryL(TInt aDriveToTest)
269 // Tests opening files with the same name but in different directories
272 test.Next( _L("Testing files with same name in different directories") );
277 TRandomGenerator iRand;
281 TFileName name(KDriveBase);
282 name[0] = TText('A' + aDriveToTest);
284 name.Append( KSameNameFile1 );
285 TInt r = files[0].iFile.Open( TheFs, name, EFileRead );
289 name.Append( KSameNameFile2 );
290 r = files[1].iFile.Open( TheFs, name, EFileRead );
294 name.Append( KSameNameFile3 );
295 r = files[2].iFile.Open( TheFs, name, EFileRead );
299 name.Append( KSameNameFile4 );
300 r = files[3].iFile.Open( TheFs, name, EFileRead );
303 const TInt KReadLen = KSameNameFileLength / 16;
304 TBuf8<KReadLen> readBuf;
305 TBuf8<KReadLen> randomBuf;
307 files[0].iRand.SetSeed( KSameNameFile1Seed );
308 files[1].iRand.SetSeed( KSameNameFile2Seed );
309 files[2].iRand.SetSeed( KSameNameFile3Seed );
310 files[3].iRand.SetSeed( KSameNameFile4Seed );
312 for( TInt fragment = 0; fragment < (KSameDirFileLength / KReadLen); ++fragment )
314 // read next fragment from each file and compare
315 for( TInt j = 0; j < 4; ++j )
317 FillRandomBuffer( randomBuf, files[j].iRand, KReadLen );
318 r = files[j].iFile.Read( readBuf );
320 TEST_FOR_MATCH( readBuf.Length(), KReadLen );
321 test( readBuf == randomBuf );
324 files[0].iFile.Close();
325 files[1].iFile.Close();
326 files[2].iFile.Close();
327 files[3].iFile.Close();
333 LOCAL_C void TestRandomSeekL(TInt aDriveToTest)
335 // Tests reading from random positions in a file
338 test.Next( _L("Testing random read position") );
340 TFileName name(KDriveBase);
341 name[0] = TText('A' + aDriveToTest);
343 name.Append( KRandomReadFile );
345 TInt r = file.Open( TheFs, name, EFileRead );
350 r = file.Size( fileSize ),
352 TEST_FOR_MATCH( fileSize, KRandomReadFileSize );
354 // Fill buffer with expected file contents
355 TRandomGenerator rand;
356 rand.SetSeed( KRandomReadFileSeed );
357 FillRandomBuffer( gFileBuffer, rand, KRandomReadFileSize );
360 // Read from random positions within the file
361 rand.SetSeed( 0xA6E320F1 );
362 const TInt KMaxRead = 256;
364 const TInt maxRead = Min( fileSize, buf.MaxLength() );
365 for( TInt readCount = 0; readCount < 100; ++readCount )
367 TInt readLen = rand.Next() % (maxRead+1);
368 if( readLen > buf.MaxLength() )
370 readLen = buf.MaxLength();
372 TInt readPos = rand.Next() % (fileSize - readLen);
374 _LIT( KMessage, "Reading %d bytes from 0x%x" );
375 test.Printf( KMessage, readLen, readPos );
377 const TUint8* pExpected = gFileBuffer.Ptr() + readPos;
378 r = file.Seek( ESeekStart, readPos );
381 r = file.Read( buf, readLen );
383 TEST_FOR_MATCH( buf.Length(), readLen );
385 const TUint8* pRead = buf.Ptr();
386 test( 0 == Mem::Compare( pExpected, readLen, pRead, readLen ) );
391 LOCAL_C void TestEofReadL(TInt aDriveToTest)
393 // Test reading off the end of a file
396 test.Next( _L("Testing reading past EOF") );
399 const TDesC* const fileArray[6] =
401 &KEofFile1, &KEofFile2, &KEofFile3, &KEofFile4, &KEofFile5, &KEofFile6
404 TFileName name(KDriveBase);
405 name[0] = TText('A' + aDriveToTest);
407 TRandomGenerator rand;
408 rand.SetSeed( 0x1001A005 );
409 for( TInt i = 0; i < 6; i++ )
411 name.SetLength( 3 ); // trim back to drive specifier
412 name.Append( *fileArray[i] );
413 test.Printf( _L("Opening file %S"), &name );
415 TInt r = file.Open( TheFs, name, EFileRead );
419 r = file.Size( fileSize ),
421 test( fileSize > 0 );
423 TInt readPos = fileSize - ( rand.Next() % 12);
428 r = file.Seek( ESeekStart, readPos );
432 r = file.Read( buf );
434 TEST_FOR_MATCH( buf.Length(), fileSize - readPos );
442 LOCAL_C void TestStreamReadL(TInt aDriveToTest)
444 // Reads the whole file into a buffer in a single read
445 // and validates the data
448 test.Next( _L("Testing whole file read") );
450 const TDesC* const fileArray[4] =
452 &KStreamFile1, &KStreamFile2, &KStreamFile3, &KStreamFile4
455 TFileName name(KDriveBase);
456 name[0] = TText('A' + aDriveToTest);
458 for( TInt i = 0; i < 4; ++i )
461 name.Append( *fileArray[i] );
463 test.Printf( _L("... file %S"), &name );
466 TInt r = file.Open( TheFs, name, EFileRead );
470 r = file.Size( fileSize ),
472 test( fileSize > 0 );
474 r = file.Read( gFileBuffer );
476 TEST_FOR_MATCH( fileSize, gFileBuffer.Length() );
478 test( ValidateRandomBuffer( gFileBuffer, KStreamReadSeed ) );
484 //************************
487 void DoTestL(TInt aDriveToTest)
490 test.Start( _L("Testing ROFS file reading") );
492 test.Printf( _L("Looking for ROFS extension\n"));
493 TBool extension = EFalse;
494 TFileName name(KDriveBase);
495 name[0] = TText('A' + aDriveToTest);
497 name.SetLength( 3 ); // trim back to drive specifier
498 name.Append( KTestFileExt );
501 test.Printf( _L("Opening file %S"), &name );
502 TInt r = file.Open( TheFs, name, EFileRead );
507 test.Printf( _L("ROFS extension found\n"));
509 else if(r==KErrNotFound)
511 test.Printf( _L("Not found, ROFS extension not present\n"));
514 SpotCheckFilesL(aDriveToTest, extension);
515 TestFilesInSameDirectoryL(aDriveToTest);
516 TestFilesSameNameDifferentDirectoryL(aDriveToTest);
517 TestRandomSeekL(aDriveToTest);
518 TestEofReadL(aDriveToTest);
519 TestStreamReadL(aDriveToTest);