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