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