First public contribution.
1 // Copyright (c) 1995-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\b_file.cpp
23 GLDEF_D RTest test(_L("B_FILE"));
25 LOCAL_D RFile TheFile;
26 LOCAL_D TInt bret; // Expected error return
27 LOCAL_D TInt aret; // Actual error return
28 LOCAL_D TBuf8<10000> tbuf; // Test buffer
29 LOCAL_D TPtrC tbin(_S("\\F32-TST\\TEST.BIN"));
30 LOCAL_D TPtrC rndm(_S("\\F32-TST\\RANDOM.TST"));
31 LOCAL_D TPtrC tzzz(_S("\\F32-TST\\ZZZZZZ.ZZZ"));
32 LOCAL_D const TInt KRandomNumbers=1024;
34 LOCAL_C void bopen(TUint aMode)
36 // Open the binary file.
40 aret=TheFile.Open(TheFs,tbin,aMode);
44 LOCAL_C void bcreate(TUint aMode)
46 // Open the binary file.
50 aret=TheFile.Create(TheFs,tbin,aMode);
54 LOCAL_C void breplace(TUint aMode)
56 // Open the binary file.
60 aret=TheFile.Replace(TheFs,tbin,aMode);
64 LOCAL_C void bwrite(TInt aLength)
66 // Write aLength bytes of test data at current position.
71 test.Printf(_L("bwrite1,len=%u\n"),aLength);
72 TInt pos=0; // Relative position zero
73 aret=TheFile.Seek(ESeekCurrent,pos);
74 test.Printf(_L("bwrite2,pos=%u\n"),pos);
77 tbuf.SetLength(aLength);
78 TText8* p=(TText8*)tbuf.Ptr();
85 test.Printf(_L("bwrite3\n"));
86 aret=TheFile.Write(tbuf);
87 test.Printf(_L("bwrite4\n"));
92 LOCAL_C void bread(TInt aLength)
94 // Read and check aLength bytes of test data at current position.
99 TInt pos=0; // Relative position zero
100 aret=TheFile.Seek(ESeekCurrent,pos);
101 test(aret==KErrNone);
103 aret=TheFile.Read(tbuf,aLength);
109 test(((TInt)tbuf.Length())==bret);
110 const TText8* p=tbuf.Ptr();
111 const TText8* pE=p+bret;
115 test.Panic(_L("bread data different"));
121 LOCAL_C void bposa(TInt aPos)
123 // Position absolute.
129 aret=TheFile.Seek(ESeekStart,newpos);
130 test(aret==KErrNone);
135 LOCAL_C void bclose()
146 LOCAL_C void btest1(TUint aMode)
148 // Binary file tests.
152 test.Start(_L("BTEST1..."));
154 breplace(aMode|EFileWrite);
156 bret=0; bwrite(1); bposa(0l);
161 bret=0; bread(1); bret=0;
163 bret=KErrAlreadyExists;bcreate(aMode|EFileWrite);bret=0;
164 bopen(aMode|EFileRead);
165 bret=KErrAccessDenied; bwrite(1); bret=0;
167 aret=TheFile.Open(TheFs,tzzz,EFileRead);
168 test(aret==KErrNotFound);
172 LOCAL_C void btest2(TUint aMode)
174 // Binary file tests.
178 test.Start(_L("BTEST2..."));
180 breplace(aMode|EFileWrite);
183 bret=5; bread(5); bret=0;
186 bret=45; bread(45); bret=0;
189 bret=300; bread(300); bret=0;
191 bopen(aMode|EFileWrite);
194 bret=1000; bread(1000); bret=0;
196 bopen(aMode|EFileWrite);
200 bret=1100; bread(1100); bret=0;
201 aret=TheFile.Flush();
202 test(aret==KErrNone);
203 aret=TheFile.SetSize(2000);
204 test(aret==KErrNone);
206 aret=TheFile.Seek(ESeekCurrent,pos);
207 test(aret==KErrNone && pos==1100);
209 aret=TheFile.Seek(ESeekEnd,pos);
210 test(aret==KErrNone && pos==2000);
215 LOCAL_C void rndtest(TUint aMode)
217 // Tests the file handling by writing a file of random numbers,
218 // closing the file, reseeding the random number generator with
219 // the same number, then reading the file back again, checking
220 // it against the generator.
225 test.Start(_L("RNDTEST..."));
226 TInt64 seed(0),zero(0);
227 aret=TheFile.Replace(TheFs,rndm,EFileWrite|aMode);
228 test(aret==KErrNone);
229 for (cnt=0;cnt<KRandomNumbers;cnt++)
232 b.Format(TPtrC8((TUint8*)"%8x"),Math::Rand(seed));
233 aret=TheFile.Write(b);
234 test(aret==KErrNone);
238 test.Next(_L("Reading back"));
240 aret=TheFile.Open(TheFs,rndm,aMode);
241 test(aret==KErrNone);
242 for (cnt=0;cnt<KRandomNumbers;cnt++)
245 b.Format(TPtrC8((TUint8*)"%8x"),Math::Rand(seed));
247 aret=TheFile.Read(r);
248 test(aret==KErrNone);
252 aret=TheFs.Delete(rndm);
253 test(aret==KErrNone);
258 LOCAL_C void testAutoClose()
260 // Tests TAutoClose template class
264 test.Start(_L("TAutoClose..."));
266 aret=f.iObj.Replace(TheFs,rndm,EFileWrite);
267 test(aret==KErrNone);
269 for (TInt cnt=0;cnt<KRandomNumbers;cnt++)
272 b.Format(TPtrC8((TUint8*)"%8x"),Math::Rand(seed));
273 aret=f.iObj.Write(b);
274 test(aret==KErrNone);
279 LOCAL_C void readWithNegativeLengthTest()
281 test.Start(_L("Read with Negative Length Test..."));
283 TRequestStatus status = KRequestPending;
284 TheFile.Open(TheFs,tbin,EFileRead);
285 ret = TheFile.Read(0,tbuf,-1); // sync
286 test ( ret == KErrArgument);
287 TheFile.Read(0,tbuf,-1,status); // async
288 User::WaitForRequest(status);
289 test(status.Int() == KErrArgument);
294 LOCAL_C void readWithNegativeLengthTestForEmptyFile()
298 test.Start(_L("Read with Negative Length Test(For EmptyFile)..."));
300 MakeFile(_L("C:\\F32-TST\\TFILE\\hello2.txt"));
301 TInt r=f.Open(TheFs,_L("C:\\F32-TST\\TFILE\\hello2.txt"),EFileRead);
305 test.Next(_L("Check Negative length when file is empty"));
307 test(r==KErrArgument);
309 test(r==KErrArgument);
311 test(r==KErrArgument);
312 TRequestStatus stat1;
313 f.Read(0,a,-5,stat1);
314 User::WaitForRequest(stat1);
315 test(stat1.Int() == KErrArgument);
317 User::WaitForRequest(stat1);
318 test(stat1.Int() == KErrArgument);
324 GLDEF_C void CallTestsL()
326 // Call tests that may leave
333 btest1(EFileStreamText);
334 btest2(EFileStreamText);
335 rndtest(EFileStream);
336 readWithNegativeLengthTest();
337 readWithNegativeLengthTestForEmptyFile();