|
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". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // f32test\server\b_rand.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 #include <f32file.h> |
|
19 #include <e32math.h> |
|
20 #include <e32test.h> |
|
21 #include "t_server.h" |
|
22 |
|
23 const TInt64 KInitialSeedL=24; |
|
24 const TInt KInitialSeed5=42; |
|
25 const TInt KMaxStream=0x1000; |
|
26 |
|
27 class RStream |
|
28 { |
|
29 public: |
|
30 RStream(); |
|
31 void Set(RFile& aFile); |
|
32 TInt Read(TDes8& aDes); |
|
33 private: |
|
34 const TText8* iNext; |
|
35 const TText8* iEnd; |
|
36 TBool iEOF; |
|
37 RFile iFile; |
|
38 TBuf8<KMaxStream> iStream; |
|
39 }; |
|
40 |
|
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; |
|
59 LOCAL_D RStream sBuf; |
|
60 LOCAL_D TPtrC testDir(_S("\\F32-TST\\")); |
|
61 |
|
62 LOCAL_C void TestRet(TInt aRet) |
|
63 // |
|
64 // Display error value if aRet!=KErrNone |
|
65 // |
|
66 { |
|
67 |
|
68 if (aRet==KErrNone) |
|
69 return; |
|
70 test.Printf(_L("Error: %d\n"),aRet); |
|
71 test(EFalse); |
|
72 } |
|
73 |
|
74 #if defined(__SLOW_TEST__) |
|
75 LOCAL_C void CheckFile(const RFile& aFile,const TChar aChar) |
|
76 // |
|
77 // Check that aFile only contains aChar and '\n' characters |
|
78 // |
|
79 { |
|
80 |
|
81 TBuf8<0x400> buf(0x400); |
|
82 TInt pos=0; |
|
83 TInt r=aFile.Seek(ESeekStart,pos); |
|
84 TestRet(r); |
|
85 while(buf.Length()==buf.MaxLength()) |
|
86 { |
|
87 r=aFile.Read(buf); |
|
88 TestRet(r); |
|
89 TInt len=buf.Length(); |
|
90 while(len--) |
|
91 test(buf[len]=='\n' || aChar==buf[len]); |
|
92 } |
|
93 } |
|
94 #else |
|
95 LOCAL_C void CheckFile(const RFile& /*aFile*/,const TChar /*aChar*/) |
|
96 { |
|
97 } |
|
98 #endif |
|
99 |
|
100 LOCAL_C void CheckFile1() |
|
101 {CheckFile(TheFile1,'A');} |
|
102 LOCAL_C void CheckFile2() |
|
103 {CheckFile(TheFile2,'B');} |
|
104 |
|
105 RStream::RStream() |
|
106 // |
|
107 // Constructor. |
|
108 // |
|
109 { |
|
110 } |
|
111 |
|
112 void RStream::Set(RFile& aFile) |
|
113 // |
|
114 // Initialize the stream on a file. |
|
115 // |
|
116 { |
|
117 |
|
118 iEOF=EFalse; |
|
119 iFile=aFile; |
|
120 iStream.Zero(); |
|
121 iNext=iStream.Ptr(); |
|
122 iEnd=iNext; |
|
123 } |
|
124 |
|
125 TInt RStream::Read(TDes8& aDes) |
|
126 // |
|
127 // Read from the stream. |
|
128 // |
|
129 { |
|
130 |
|
131 TText8* pD=(TText8*)aDes.Ptr(); |
|
132 TInt len=aDes.MaxLength(); |
|
133 TInt newLen=0; |
|
134 while (newLen<len) |
|
135 { |
|
136 if (iNext>=iEnd) |
|
137 { |
|
138 if (iEOF) |
|
139 { |
|
140 if (newLen==0) |
|
141 return(KErrEof); |
|
142 aDes.SetLength(newLen); |
|
143 return(KErrNone); |
|
144 } |
|
145 TInt r=iFile.Read(iStream); |
|
146 if (r!=KErrNone) |
|
147 return(r); |
|
148 if (iStream.Length()!=iStream.MaxLength()) |
|
149 iEOF=ETrue; |
|
150 iNext=iStream.Ptr(); |
|
151 iEnd=iNext+iStream.Length(); |
|
152 continue; |
|
153 } |
|
154 TUint c=(*iNext++); |
|
155 if (c=='\n') |
|
156 { |
|
157 aDes.SetLength(newLen); |
|
158 return(KErrNone); |
|
159 } |
|
160 *pD++=(TText8)c; |
|
161 newLen++; |
|
162 } |
|
163 return(KErrTooBig); |
|
164 } |
|
165 |
|
166 |
|
167 GLDEF_C void CallTestsL(void) |
|
168 // |
|
169 // Do tests relative to session path |
|
170 // |
|
171 { |
|
172 |
|
173 TTime timerC; |
|
174 timerC.HomeTime(); |
|
175 |
|
176 test.Next(_L("Make test directory")); |
|
177 // |
|
178 TInt n_times=400; |
|
179 testPat2.Fill('B',testPat2.MaxLength()); |
|
180 testPat3.Fill('C',testPat3.MaxLength()); |
|
181 testPat4.Fill('D',testPat4.MaxLength()); |
|
182 // |
|
183 TInt r=TheFile1.Temp(TheFs,testDir,nameBuf1,EFileStream|EFileWrite); |
|
184 TestRet(r); |
|
185 test.Printf(_L("Created1: %S\n"),&nameBuf1); |
|
186 TInt sum1=0; |
|
187 // |
|
188 r=TheFile2.Temp(TheFs,testDir,nameBuf2,EFileStreamText|EFileWrite); |
|
189 TestRet(r); |
|
190 test.Printf(_L("Created2: %S\n"),&nameBuf2); |
|
191 TInt sum2=0; |
|
192 // |
|
193 r=TheFile3.Temp(TheFs,testDir,nameBuf3,EFileStream|EFileWrite); |
|
194 TestRet(r); |
|
195 test.Printf(_L("Created3: %S\n"),&nameBuf3); |
|
196 TInt sum3=0; |
|
197 // |
|
198 r=TheFile4.Temp(TheFs,testDir,nameBuf4,EFileStream|EFileWrite); |
|
199 TestRet(r); |
|
200 test.Printf(_L("Created4: %S\n"),&nameBuf4); |
|
201 TInt sum4=0; |
|
202 // |
|
203 r=TheFile5.Temp(TheFs,testDir,nameBuf5,EFileStreamText|EFileWrite); |
|
204 TestRet(r); |
|
205 test.Printf(_L("Created5: %S\n"),&nameBuf5); |
|
206 TInt sum5=0; |
|
207 TheFile5.Close(); |
|
208 // |
|
209 TInt64 seed5=KInitialSeed5; |
|
210 TInt64 seedL=KInitialSeedL; |
|
211 TBuf<0x100> pBuf; |
|
212 for (TInt rep=0;rep<n_times;rep++) |
|
213 { |
|
214 pBuf.Zero(); |
|
215 pBuf.Format(_L("RAND(%03u) "),rep); |
|
216 sum1++; |
|
217 pBuf.Append(_L("W1->F1 ")); // Write 1 byte to file1 |
|
218 TPtrC8 pA=_L8("A"); |
|
219 r=TheFile1.Write(pA); |
|
220 TestRet(r); |
|
221 CheckFile1(); |
|
222 |
|
223 CheckFile2(); |
|
224 TInt len=(Math::Rand(seedL)&0xff); // 0 to 255 |
|
225 sum2+=len; |
|
226 pBuf.AppendFormat(_L("W%03u->F2 "),len); // Write len bytes to file2 |
|
227 r=TheFile2.Write(testPat2,len); |
|
228 TestRet(r); |
|
229 r=TheFile2.Write(_L8("\n")); |
|
230 TestRet(r); |
|
231 CheckFile2(); |
|
232 |
|
233 if (Math::Rand(seedL)&0x10) |
|
234 { |
|
235 CheckFile2(); |
|
236 len=(Math::Rand(seedL)&0x2ff); |
|
237 sum3+=len; |
|
238 pBuf.AppendFormat(_L("W%03u->F3 "),len); // Write len bytes to file3 |
|
239 r=TheFile3.Write(testPat3,len); |
|
240 TestRet(r); |
|
241 CheckFile2(); |
|
242 } |
|
243 |
|
244 if (Math::Rand(seedL)&0x10) |
|
245 { |
|
246 len=(Math::Rand(seedL)&0x3ff); |
|
247 sum4+=len; |
|
248 pBuf.AppendFormat(_L("W%04u->F4 "),len); // Write len bytes to file4 |
|
249 r=TheFile4.Write(testPat4,len); |
|
250 TestRet(r); |
|
251 // CheckFile4(); |
|
252 } |
|
253 |
|
254 if ((Math::Rand(seedL)&0x70)==0x70) |
|
255 { |
|
256 r=TheFile5.Open(TheFs,nameBuf5,EFileStreamText|EFileWrite); |
|
257 TestRet(r); |
|
258 TInt pos=0; |
|
259 r=TheFile5.Seek(ESeekEnd,pos); |
|
260 TestRet(r); |
|
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); |
|
264 TestRet(r); |
|
265 TheFile5.Close(); |
|
266 sum5+=8; |
|
267 } |
|
268 test.Printf(pBuf); |
|
269 |
|
270 if ((Math::Rand(seedL)&0xf0)==0xf0) |
|
271 { |
|
272 test.Printf(_L(" DELETE F3")); |
|
273 TheFile3.Close(); |
|
274 r=TheFs.Delete(nameBuf3); |
|
275 TestRet(r); |
|
276 r=TheFile3.Temp(TheFs,testDir,nameBuf3,EFileStream|EFileWrite); |
|
277 TestRet(r); |
|
278 sum3=0L; |
|
279 } |
|
280 if ((Math::Rand(seedL)&0xf0)==0xf0) |
|
281 { |
|
282 test.Printf(_L(" DELETE F4")); |
|
283 TheFile4.Close(); |
|
284 r=TheFs.Delete(nameBuf4); |
|
285 TestRet(r); |
|
286 r=TheFile4.Temp(TheFs,testDir,nameBuf4,EFileStream|EFileWrite); |
|
287 TestRet(r); |
|
288 sum4=0L; |
|
289 } |
|
290 if ((Math::Rand(seedL)&0x1f0)==0x1f0) |
|
291 { |
|
292 test.Printf(_L(" REPLACE F3")); |
|
293 TheFile3.Close(); |
|
294 TestRet(r); |
|
295 r=TheFile3.Replace(TheFs,nameBuf3,EFileStream|EFileWrite); |
|
296 TestRet(r); |
|
297 sum3=0L; |
|
298 } |
|
299 if ((Math::Rand(seedL)&0x1f0)==0x1f0) |
|
300 { |
|
301 test.Printf(_L(" REPLACE F4")); |
|
302 TheFile4.Close(); |
|
303 r=TheFile4.Replace(TheFs,nameBuf4,EFileStream|EFileWrite); |
|
304 TestRet(r); |
|
305 sum4=0L; |
|
306 } |
|
307 if ((Math::Rand(seedL)&0x1f0)==0x1f0) |
|
308 { |
|
309 test.Printf(_L(" TRUNCATE F3 to zero")); |
|
310 r=TheFile3.SetSize(0); |
|
311 TestRet(r); |
|
312 sum3=0L; |
|
313 } |
|
314 if ((Math::Rand(seedL)&0x1f0)==0x1f0) |
|
315 { |
|
316 test.Printf(_L(" TRUNCATE F4 to zero")); |
|
317 r=TheFile4.SetSize(0); |
|
318 TestRet(r); |
|
319 sum4=0L; |
|
320 } |
|
321 if ((Math::Rand(seedL)&0x70)==0x70) |
|
322 { |
|
323 sum3=Math::Rand(seedL)&0x3fff; |
|
324 test.Printf(_L(" SET SIZE F3 to %u"),sum3); |
|
325 r=TheFile3.SetSize(sum3); |
|
326 TestRet(r); |
|
327 TInt pos=0; |
|
328 r=TheFile3.Seek(ESeekEnd,pos); |
|
329 TestRet(r); |
|
330 test(pos==sum3); |
|
331 } |
|
332 if ((Math::Rand(seedL)&0x70)==0x70) |
|
333 { |
|
334 sum4=Math::Rand(seedL)&0x3fff; |
|
335 test.Printf(_L(" SET SIZE F4 to %u"),sum4); |
|
336 r=TheFile4.SetSize(sum4); |
|
337 TestRet(r); |
|
338 TInt pos=0; |
|
339 r=TheFile4.Seek(ESeekEnd,pos); |
|
340 TestRet(r); |
|
341 test(pos==sum4); |
|
342 } |
|
343 if ((Math::Rand(seedL)&0x70)==0x70) |
|
344 { |
|
345 test.Printf(_L(" CHECKING F1")); |
|
346 TInt pos=0; |
|
347 r=TheFile1.Seek(ESeekStart,pos); |
|
348 TestRet(r); |
|
349 test(pos==0); |
|
350 TInt sum=0; |
|
351 buf.Fill('A',0x200); |
|
352 do |
|
353 { |
|
354 r=TheFile1.Read(chkPat); |
|
355 TestRet(r); |
|
356 if (chkPat.Length()<chkPat.MaxLength()) |
|
357 buf.SetLength(chkPat.Length()); |
|
358 test(buf==chkPat); |
|
359 sum+=chkPat.Length(); |
|
360 } while (chkPat.Length()==chkPat.MaxLength()); |
|
361 test(sum==sum1); |
|
362 } |
|
363 if ((Math::Rand(seedL)&0x70)==0x70) |
|
364 { |
|
365 test.Printf(_L(" CHECKING F2")); |
|
366 TInt pos=0; |
|
367 r=TheFile2.Seek(ESeekStart,pos); |
|
368 TestRet(r); |
|
369 test(pos==0); |
|
370 TInt sum=0; |
|
371 sBuf.Set(TheFile2); |
|
372 FOREVER |
|
373 { |
|
374 r=sBuf.Read(chkPat); |
|
375 if (r!=KErrNone) |
|
376 { |
|
377 if (r==KErrEof) |
|
378 break; |
|
379 test.Panic(r,_L("Read text failed")); |
|
380 } |
|
381 testPat2.SetLength(chkPat.Length()); |
|
382 test(chkPat==testPat2); |
|
383 sum+=chkPat.Length(); |
|
384 } |
|
385 testPat2.SetLength(testPat2.MaxLength()); |
|
386 test(sum==sum2); |
|
387 } |
|
388 if ((Math::Rand(seedL)&0x70)==0x70) |
|
389 { |
|
390 pBuf.Zero(); |
|
391 pBuf.Format(_L(" CHECKING F3 ")); |
|
392 TheFile3.Close(); |
|
393 TEntry e; |
|
394 r=TheFs.Entry(nameBuf3,e); |
|
395 TestRet(r); |
|
396 pBuf.AppendFormat(_L("Info=%u sum3=%u"),e.iSize,sum3); |
|
397 test.Printf(pBuf); |
|
398 r=TheFile3.Open(TheFs,nameBuf3,EFileStream|EFileWrite); |
|
399 TestRet(r); |
|
400 TInt pos=0; |
|
401 r=TheFile3.Seek(ESeekEnd,pos); |
|
402 TestRet(r); |
|
403 test(pos==sum3); |
|
404 } |
|
405 if ((Math::Rand(seedL)&0x70)==0x70) |
|
406 { |
|
407 pBuf.Format(_L(" CHECKING F4 ")); |
|
408 TheFile4.Close(); |
|
409 TEntry e; |
|
410 r=TheFs.Entry(nameBuf4,e); |
|
411 TestRet(r); |
|
412 pBuf.AppendFormat(_L("Info=%u sum4=%u"),e.iSize,sum4); |
|
413 test.Printf(pBuf); |
|
414 r=TheFile4.Open(TheFs,nameBuf4,EFileStream|EFileWrite); |
|
415 TestRet(r); |
|
416 TInt pos=sum4; |
|
417 r=TheFile4.Seek(ESeekStart,pos); |
|
418 TestRet(r); |
|
419 test(pos==sum4); |
|
420 } |
|
421 if ((Math::Rand(seedL)&0x1f0)==0x1f0) |
|
422 { |
|
423 test.Printf(_L(" CHECKING F5")); |
|
424 r=TheFile5.Open(TheFs,nameBuf5,EFileStreamText|EFileWrite); |
|
425 TestRet(r); |
|
426 TInt64 seed=KInitialSeed5; |
|
427 TInt sum=0; |
|
428 sBuf.Set(TheFile5); |
|
429 FOREVER |
|
430 { |
|
431 chkPat.Format(_L8("%8x"),Math::Rand(seed)); |
|
432 r=sBuf.Read(testPat5); |
|
433 if (r!=KErrNone) |
|
434 { |
|
435 if (r==KErrEof) |
|
436 break; |
|
437 test.Panic(r,_L("Read text failed")); |
|
438 } |
|
439 test(testPat5.Length()==8); |
|
440 sum+=testPat5.Length(); |
|
441 test(chkPat==testPat5); |
|
442 } |
|
443 test(sum==sum5); |
|
444 TheFile5.Close(); |
|
445 } |
|
446 } |
|
447 TheFile1.Close(); |
|
448 TheFile2.Close(); |
|
449 TheFile3.Close(); |
|
450 TheFile4.Close(); |
|
451 TheFs.Delete(nameBuf1); |
|
452 TheFs.Delete(nameBuf2); |
|
453 TheFs.Delete(nameBuf3); |
|
454 TheFs.Delete(nameBuf4); |
|
455 TheFs.Delete(nameBuf5); |
|
456 |
|
457 TTime endTimeC; |
|
458 endTimeC.HomeTime(); |
|
459 TTimeIntervalSeconds timeTakenC; |
|
460 r=endTimeC.SecondsFrom(timerC,timeTakenC); |
|
461 TestRet(r); |
|
462 test.Printf(_L("Time taken for test = %d secs\n"),timeTakenC.Int()); |
|
463 } |