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_rand.cpp
23 const TInt64 KInitialSeedL=24;
24 const TInt KInitialSeed5=42;
25 const TInt KMaxStream=0x1000;
31 void Set(RFile& aFile);
32 TInt Read(TDes8& aDes);
38 TBuf8<KMaxStream> iStream;
41 GLDEF_D RTest test(_L("B_RAND"));
42 LOCAL_D RFile TheFile1;
43 LOCAL_D RFile TheFile2;
44 LOCAL_D RFile TheFile3;
45 LOCAL_D RFile TheFile4;
46 LOCAL_D RFile TheFile5;
47 LOCAL_D TFileName fBuf;
48 LOCAL_D TFileName nameBuf1;
49 LOCAL_D TFileName nameBuf2;
50 LOCAL_D TFileName nameBuf3;
51 LOCAL_D TFileName nameBuf4;
52 LOCAL_D TFileName nameBuf5;
53 LOCAL_D TBuf8<0x200> chkPat;
54 LOCAL_D TBuf8<0x200> testPat2;
55 LOCAL_D TBuf8<0x400> testPat3;
56 LOCAL_D TBuf8<0x400> testPat4;
57 LOCAL_D TBuf8<0x40> testPat5;
58 LOCAL_D TBuf8<0x400> buf;
60 LOCAL_D TPtrC testDir(_S("\\F32-TST\\"));
62 LOCAL_C void TestRet(TInt aRet)
64 // Display error value if aRet!=KErrNone
70 test.Printf(_L("Error: %d\n"),aRet);
74 #if defined(__SLOW_TEST__)
75 LOCAL_C void CheckFile(const RFile& aFile,const TChar aChar)
77 // Check that aFile only contains aChar and '\n' characters
81 TBuf8<0x400> buf(0x400);
83 TInt r=aFile.Seek(ESeekStart,pos);
85 while(buf.Length()==buf.MaxLength())
89 TInt len=buf.Length();
91 test(buf[len]=='\n' || aChar==buf[len]);
95 LOCAL_C void CheckFile(const RFile& /*aFile*/,const TChar /*aChar*/)
100 LOCAL_C void CheckFile1()
101 {CheckFile(TheFile1,'A');}
102 LOCAL_C void CheckFile2()
103 {CheckFile(TheFile2,'B');}
112 void RStream::Set(RFile& aFile)
114 // Initialize the stream on a file.
125 TInt RStream::Read(TDes8& aDes)
127 // Read from the stream.
131 TText8* pD=(TText8*)aDes.Ptr();
132 TInt len=aDes.MaxLength();
142 aDes.SetLength(newLen);
145 TInt r=iFile.Read(iStream);
148 if (iStream.Length()!=iStream.MaxLength())
151 iEnd=iNext+iStream.Length();
157 aDes.SetLength(newLen);
167 GLDEF_C void CallTestsL(void)
169 // Do tests relative to session path
176 test.Next(_L("Make test directory"));
179 testPat2.Fill('B',testPat2.MaxLength());
180 testPat3.Fill('C',testPat3.MaxLength());
181 testPat4.Fill('D',testPat4.MaxLength());
183 TInt r=TheFile1.Temp(TheFs,testDir,nameBuf1,EFileStream|EFileWrite);
185 test.Printf(_L("Created1: %S\n"),&nameBuf1);
188 r=TheFile2.Temp(TheFs,testDir,nameBuf2,EFileStreamText|EFileWrite);
190 test.Printf(_L("Created2: %S\n"),&nameBuf2);
193 r=TheFile3.Temp(TheFs,testDir,nameBuf3,EFileStream|EFileWrite);
195 test.Printf(_L("Created3: %S\n"),&nameBuf3);
198 r=TheFile4.Temp(TheFs,testDir,nameBuf4,EFileStream|EFileWrite);
200 test.Printf(_L("Created4: %S\n"),&nameBuf4);
203 r=TheFile5.Temp(TheFs,testDir,nameBuf5,EFileStreamText|EFileWrite);
205 test.Printf(_L("Created5: %S\n"),&nameBuf5);
209 TInt64 seed5=KInitialSeed5;
210 TInt64 seedL=KInitialSeedL;
212 for (TInt rep=0;rep<n_times;rep++)
215 pBuf.Format(_L("RAND(%03u) "),rep);
217 pBuf.Append(_L("W1->F1 ")); // Write 1 byte to file1
219 r=TheFile1.Write(pA);
224 TInt len=(Math::Rand(seedL)&0xff); // 0 to 255
226 pBuf.AppendFormat(_L("W%03u->F2 "),len); // Write len bytes to file2
227 r=TheFile2.Write(testPat2,len);
229 r=TheFile2.Write(_L8("\n"));
233 if (Math::Rand(seedL)&0x10)
236 len=(Math::Rand(seedL)&0x2ff);
238 pBuf.AppendFormat(_L("W%03u->F3 "),len); // Write len bytes to file3
239 r=TheFile3.Write(testPat3,len);
244 if (Math::Rand(seedL)&0x10)
246 len=(Math::Rand(seedL)&0x3ff);
248 pBuf.AppendFormat(_L("W%04u->F4 "),len); // Write len bytes to file4
249 r=TheFile4.Write(testPat4,len);
254 if ((Math::Rand(seedL)&0x70)==0x70)
256 r=TheFile5.Open(TheFs,nameBuf5,EFileStreamText|EFileWrite);
259 r=TheFile5.Seek(ESeekEnd,pos);
261 testPat5.Format(_L8("%8x\n"),Math::Rand(seed5));
262 pBuf.Append(_L("W8->F5")); // Write 8 bytes to file5
263 r=TheFile5.Write(testPat5);
270 if ((Math::Rand(seedL)&0xf0)==0xf0)
272 test.Printf(_L(" DELETE F3"));
274 r=TheFs.Delete(nameBuf3);
276 r=TheFile3.Temp(TheFs,testDir,nameBuf3,EFileStream|EFileWrite);
280 if ((Math::Rand(seedL)&0xf0)==0xf0)
282 test.Printf(_L(" DELETE F4"));
284 r=TheFs.Delete(nameBuf4);
286 r=TheFile4.Temp(TheFs,testDir,nameBuf4,EFileStream|EFileWrite);
290 if ((Math::Rand(seedL)&0x1f0)==0x1f0)
292 test.Printf(_L(" REPLACE F3"));
295 r=TheFile3.Replace(TheFs,nameBuf3,EFileStream|EFileWrite);
299 if ((Math::Rand(seedL)&0x1f0)==0x1f0)
301 test.Printf(_L(" REPLACE F4"));
303 r=TheFile4.Replace(TheFs,nameBuf4,EFileStream|EFileWrite);
307 if ((Math::Rand(seedL)&0x1f0)==0x1f0)
309 test.Printf(_L(" TRUNCATE F3 to zero"));
310 r=TheFile3.SetSize(0);
314 if ((Math::Rand(seedL)&0x1f0)==0x1f0)
316 test.Printf(_L(" TRUNCATE F4 to zero"));
317 r=TheFile4.SetSize(0);
321 if ((Math::Rand(seedL)&0x70)==0x70)
323 sum3=Math::Rand(seedL)&0x3fff;
324 test.Printf(_L(" SET SIZE F3 to %u"),sum3);
325 r=TheFile3.SetSize(sum3);
328 r=TheFile3.Seek(ESeekEnd,pos);
332 if ((Math::Rand(seedL)&0x70)==0x70)
334 sum4=Math::Rand(seedL)&0x3fff;
335 test.Printf(_L(" SET SIZE F4 to %u"),sum4);
336 r=TheFile4.SetSize(sum4);
339 r=TheFile4.Seek(ESeekEnd,pos);
343 if ((Math::Rand(seedL)&0x70)==0x70)
345 test.Printf(_L(" CHECKING F1"));
347 r=TheFile1.Seek(ESeekStart,pos);
354 r=TheFile1.Read(chkPat);
356 if (chkPat.Length()<chkPat.MaxLength())
357 buf.SetLength(chkPat.Length());
359 sum+=chkPat.Length();
360 } while (chkPat.Length()==chkPat.MaxLength());
363 if ((Math::Rand(seedL)&0x70)==0x70)
365 test.Printf(_L(" CHECKING F2"));
367 r=TheFile2.Seek(ESeekStart,pos);
379 test.Panic(r,_L("Read text failed"));
381 testPat2.SetLength(chkPat.Length());
382 test(chkPat==testPat2);
383 sum+=chkPat.Length();
385 testPat2.SetLength(testPat2.MaxLength());
388 if ((Math::Rand(seedL)&0x70)==0x70)
391 pBuf.Format(_L(" CHECKING F3 "));
394 r=TheFs.Entry(nameBuf3,e);
396 pBuf.AppendFormat(_L("Info=%u sum3=%u"),e.iSize,sum3);
398 r=TheFile3.Open(TheFs,nameBuf3,EFileStream|EFileWrite);
401 r=TheFile3.Seek(ESeekEnd,pos);
405 if ((Math::Rand(seedL)&0x70)==0x70)
407 pBuf.Format(_L(" CHECKING F4 "));
410 r=TheFs.Entry(nameBuf4,e);
412 pBuf.AppendFormat(_L("Info=%u sum4=%u"),e.iSize,sum4);
414 r=TheFile4.Open(TheFs,nameBuf4,EFileStream|EFileWrite);
417 r=TheFile4.Seek(ESeekStart,pos);
421 if ((Math::Rand(seedL)&0x1f0)==0x1f0)
423 test.Printf(_L(" CHECKING F5"));
424 r=TheFile5.Open(TheFs,nameBuf5,EFileStreamText|EFileWrite);
426 TInt64 seed=KInitialSeed5;
431 chkPat.Format(_L8("%8x"),Math::Rand(seed));
432 r=sBuf.Read(testPat5);
437 test.Panic(r,_L("Read text failed"));
439 test(testPat5.Length()==8);
440 sum+=testPat5.Length();
441 test(chkPat==testPat5);
451 TheFs.Delete(nameBuf1);
452 TheFs.Delete(nameBuf2);
453 TheFs.Delete(nameBuf3);
454 TheFs.Delete(nameBuf4);
455 TheFs.Delete(nameBuf5);
459 TTimeIntervalSeconds timeTakenC;
460 r=endTimeC.SecondsFrom(timerC,timeTakenC);
462 test.Printf(_L("Time taken for test = %d secs\n"),timeTakenC.Int());