os/kernelhwsrv/kerneltest/f32test/server/b_file.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/server/b_file.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,339 @@
     1.4 +// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// f32test\server\b_file.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <f32file.h>
    1.22 +#include <e32math.h>
    1.23 +#include <e32test.h>
    1.24 +#include "t_server.h"
    1.25 +
    1.26 +GLDEF_D RTest test(_L("B_FILE"));
    1.27 +
    1.28 +LOCAL_D RFile TheFile;
    1.29 +LOCAL_D TInt bret; // Expected error return
    1.30 +LOCAL_D TInt aret; // Actual error return
    1.31 +LOCAL_D TBuf8<10000> tbuf; // Test buffer
    1.32 +LOCAL_D TPtrC tbin(_S("\\F32-TST\\TEST.BIN"));
    1.33 +LOCAL_D TPtrC rndm(_S("\\F32-TST\\RANDOM.TST"));
    1.34 +LOCAL_D TPtrC tzzz(_S("\\F32-TST\\ZZZZZZ.ZZZ"));
    1.35 +LOCAL_D const TInt KRandomNumbers=1024;
    1.36 +
    1.37 +LOCAL_C void bopen(TUint aMode)
    1.38 +//
    1.39 +// Open the binary file.
    1.40 +//
    1.41 +    {
    1.42 +
    1.43 +    aret=TheFile.Open(TheFs,tbin,aMode);
    1.44 +    test(aret==bret);
    1.45 +    }
    1.46 +
    1.47 +LOCAL_C void bcreate(TUint aMode)
    1.48 +//
    1.49 +// Open the binary file.
    1.50 +//
    1.51 +    {
    1.52 +
    1.53 +    aret=TheFile.Create(TheFs,tbin,aMode);
    1.54 +    test(aret==bret);
    1.55 +    }
    1.56 +
    1.57 +LOCAL_C void breplace(TUint aMode)
    1.58 +//
    1.59 +// Open the binary file.
    1.60 +//
    1.61 +    {
    1.62 +
    1.63 +    aret=TheFile.Replace(TheFs,tbin,aMode);
    1.64 +    test(aret==bret);
    1.65 +    }
    1.66 +
    1.67 +LOCAL_C void bwrite(TInt aLength)
    1.68 +//
    1.69 +// Write aLength bytes of test data at current position.
    1.70 +//
    1.71 +    {
    1.72 +
    1.73 +	CheckDisk();
    1.74 +    test.Printf(_L("bwrite1,len=%u\n"),aLength);
    1.75 +    TInt pos=0; // Relative position zero
    1.76 +    aret=TheFile.Seek(ESeekCurrent,pos);
    1.77 +    test.Printf(_L("bwrite2,pos=%u\n"),pos);
    1.78 +    test(aret==KErrNone);
    1.79 +    TInt count=pos&0xff;
    1.80 +	tbuf.SetLength(aLength);
    1.81 +	TText8* p=(TText8*)tbuf.Ptr();
    1.82 +	TText8* pE=p+aLength;
    1.83 +    while (p<pE)
    1.84 +        {
    1.85 +        *p++=(TText8)count++;
    1.86 +        count&=0xff;
    1.87 +        }
    1.88 +    test.Printf(_L("bwrite3\n"));
    1.89 +    aret=TheFile.Write(tbuf);
    1.90 +    test.Printf(_L("bwrite4\n"));
    1.91 +    test(aret==bret);
    1.92 +	CheckDisk();
    1.93 +    }
    1.94 +
    1.95 +LOCAL_C void bread(TInt aLength)
    1.96 +//
    1.97 +// Read and check aLength bytes of test data at current position.
    1.98 +//
    1.99 +    {
   1.100 +
   1.101 +	CheckDisk();
   1.102 +    TInt pos=0; // Relative position zero
   1.103 +    aret=TheFile.Seek(ESeekCurrent,pos);
   1.104 +    test(aret==KErrNone);
   1.105 +    TInt count=pos&0xff;
   1.106 +    aret=TheFile.Read(tbuf,aLength);
   1.107 +    if (bret<KErrNone)
   1.108 +		{
   1.109 +        test(bret==aret);
   1.110 +		return;
   1.111 +		}
   1.112 +    test(((TInt)tbuf.Length())==bret);
   1.113 +	const TText8* p=tbuf.Ptr();
   1.114 +	const TText8* pE=p+bret;
   1.115 +    while (p<pE)
   1.116 +        {
   1.117 +        if (*p++!=count++)
   1.118 +			test.Panic(_L("bread data different"));
   1.119 +        count&=0xff;
   1.120 +        }
   1.121 +	CheckDisk();
   1.122 +    }
   1.123 +
   1.124 +LOCAL_C void bposa(TInt aPos)
   1.125 +//
   1.126 +// Position absolute.
   1.127 +//
   1.128 +    {
   1.129 +
   1.130 +	CheckDisk();
   1.131 +    TInt newpos=aPos;
   1.132 +    aret=TheFile.Seek(ESeekStart,newpos);
   1.133 +    test(aret==KErrNone);
   1.134 +    test(newpos==aPos);
   1.135 +	CheckDisk();
   1.136 +    }
   1.137 +
   1.138 +LOCAL_C void bclose()
   1.139 +//
   1.140 +// Close the file.
   1.141 +//
   1.142 +    {
   1.143 +
   1.144 +	CheckDisk();
   1.145 +    TheFile.Close();
   1.146 +	CheckDisk();
   1.147 +    }
   1.148 +
   1.149 +LOCAL_C void btest1(TUint aMode)
   1.150 +//
   1.151 +// Binary file tests.
   1.152 +//
   1.153 +    {
   1.154 +
   1.155 +    test.Start(_L("BTEST1..."));
   1.156 +    bret=0;
   1.157 +    breplace(aMode|EFileWrite);
   1.158 +    bret=0; bread(1);
   1.159 +    bret=0; bwrite(1); bposa(0l);
   1.160 +    bret=1; bread(2);
   1.161 +    bret=0; bread(1);
   1.162 +    bret=0; bposa(0l);
   1.163 +    bret=1; bread(1);
   1.164 +    bret=0; bread(1); bret=0;
   1.165 +    bclose();
   1.166 +    bret=KErrAlreadyExists;bcreate(aMode|EFileWrite);bret=0;
   1.167 +    bopen(aMode|EFileRead);
   1.168 +    bret=KErrAccessDenied; bwrite(1); bret=0;
   1.169 +    bclose();
   1.170 +	aret=TheFile.Open(TheFs,tzzz,EFileRead);
   1.171 +	test(aret==KErrNotFound);
   1.172 +	test.End();
   1.173 +    }
   1.174 +
   1.175 +LOCAL_C void btest2(TUint aMode)
   1.176 +//
   1.177 +// Binary file tests.
   1.178 +//
   1.179 +    {
   1.180 +
   1.181 +    test.Start(_L("BTEST2..."));
   1.182 +    bret=0;
   1.183 +    breplace(aMode|EFileWrite);
   1.184 +    bwrite(11);
   1.185 +    bposa(0);
   1.186 +    bret=5; bread(5); bret=0;
   1.187 +    bwrite(45);
   1.188 +    bposa(5);
   1.189 +    bret=45; bread(45); bret=0;
   1.190 +    bwrite(1000);
   1.191 +    bposa(600);
   1.192 +    bret=300; bread(300); bret=0;
   1.193 +    bclose();
   1.194 +    bopen(aMode|EFileWrite);
   1.195 +    bposa(5);
   1.196 +    bret=5; bread(5); 
   1.197 +    bret=1000; bread(1000); bret=0;
   1.198 +    bclose();
   1.199 +    bopen(aMode|EFileWrite);
   1.200 +    bposa(KMaxTInt);
   1.201 +    bwrite(50);
   1.202 +    bposa(0);
   1.203 +    bret=1100; bread(1100); bret=0;
   1.204 +    aret=TheFile.Flush();
   1.205 +    test(aret==KErrNone);
   1.206 +    aret=TheFile.SetSize(2000);
   1.207 +    test(aret==KErrNone);
   1.208 +    TInt pos=0;
   1.209 +    aret=TheFile.Seek(ESeekCurrent,pos);
   1.210 +    test(aret==KErrNone && pos==1100);
   1.211 +    pos=0;
   1.212 +    aret=TheFile.Seek(ESeekEnd,pos);
   1.213 +    test(aret==KErrNone && pos==2000);
   1.214 +    bclose();
   1.215 +	test.End();
   1.216 +    }
   1.217 +
   1.218 +LOCAL_C void rndtest(TUint aMode)
   1.219 +//
   1.220 +// Tests the file handling by writing a file of random numbers, 
   1.221 +// closing the file, reseeding the random number generator with
   1.222 +// the same number, then reading the file back again, checking
   1.223 +// it against the generator.
   1.224 +//
   1.225 +    {
   1.226 +
   1.227 +    TInt cnt;
   1.228 +    test.Start(_L("RNDTEST..."));
   1.229 +    TInt64 seed(0),zero(0);
   1.230 +	aret=TheFile.Replace(TheFs,rndm,EFileWrite|aMode);
   1.231 +	test(aret==KErrNone);
   1.232 +    for (cnt=0;cnt<KRandomNumbers;cnt++)
   1.233 +        {
   1.234 +		TBuf8<0x10> b;
   1.235 +		b.Format(TPtrC8((TUint8*)"%8x"),Math::Rand(seed));
   1.236 +		aret=TheFile.Write(b);
   1.237 +		test(aret==KErrNone);
   1.238 +        }
   1.239 +	TheFile.Close();
   1.240 +//
   1.241 +    test.Next(_L("Reading back"));
   1.242 +    seed=zero;
   1.243 +	aret=TheFile.Open(TheFs,rndm,aMode);
   1.244 +	test(aret==KErrNone);
   1.245 +    for (cnt=0;cnt<KRandomNumbers;cnt++)
   1.246 +        {
   1.247 +		TBuf8<8> b;
   1.248 +		b.Format(TPtrC8((TUint8*)"%8x"),Math::Rand(seed));
   1.249 +		TBuf8<8> r;
   1.250 +		aret=TheFile.Read(r);
   1.251 +		test(aret==KErrNone);
   1.252 +		test(b==r);
   1.253 +        }
   1.254 +	TheFile.Close();
   1.255 +    aret=TheFs.Delete(rndm);
   1.256 +	test(aret==KErrNone);
   1.257 +//
   1.258 +	test.End();
   1.259 +    }
   1.260 +
   1.261 +LOCAL_C void testAutoClose()
   1.262 +//
   1.263 +// Tests TAutoClose template class
   1.264 +//
   1.265 +    {
   1.266 +
   1.267 +    test.Start(_L("TAutoClose..."));
   1.268 +	TAutoClose<RFile> f;
   1.269 +	aret=f.iObj.Replace(TheFs,rndm,EFileWrite);
   1.270 +	test(aret==KErrNone);
   1.271 +    TInt64 seed;
   1.272 +    for (TInt cnt=0;cnt<KRandomNumbers;cnt++)
   1.273 +        {
   1.274 +		TBuf8<0x10> b;
   1.275 +		b.Format(TPtrC8((TUint8*)"%8x"),Math::Rand(seed));
   1.276 +		aret=f.iObj.Write(b);
   1.277 +		test(aret==KErrNone);
   1.278 +        }
   1.279 +	test.End();
   1.280 +    }
   1.281 +
   1.282 +LOCAL_C void readWithNegativeLengthTest()
   1.283 +{
   1.284 +    test.Start(_L("Read with Negative Length Test..."));
   1.285 +    TInt	ret;
   1.286 +	TRequestStatus status = KRequestPending;
   1.287 +	TheFile.Open(TheFs,tbin,EFileRead);
   1.288 + 	ret = TheFile.Read(0,tbuf,-1);			// sync
   1.289 + 	test ( ret == KErrArgument);
   1.290 + 	TheFile.Read(0,tbuf,-1,status);		// async
   1.291 + 	User::WaitForRequest(status);
   1.292 + 	test(status.Int() == KErrArgument);
   1.293 +	TheFile.Close();
   1.294 +	test.End();
   1.295 +}
   1.296 +
   1.297 +LOCAL_C void readWithNegativeLengthTestForEmptyFile() 
   1.298 +	
   1.299 +	{
   1.300 +
   1.301 +	test.Start(_L("Read with Negative Length Test(For EmptyFile)..."));
   1.302 +	RFile f;             
   1.303 +	MakeFile(_L("C:\\F32-TST\\TFILE\\hello2.txt"));
   1.304 +	TInt r=f.Open(TheFs,_L("C:\\F32-TST\\TFILE\\hello2.txt"),EFileRead); 
   1.305 +	test(r==KErrNone);
   1.306 +
   1.307 +	TBuf8<0x100> a;
   1.308 +	test.Next(_L("Check Negative length when file is empty"));
   1.309 +	r=f.Read(a, -10);
   1.310 +	test(r==KErrArgument);
   1.311 +	r=f.Read(0,a, -1);
   1.312 +	test(r==KErrArgument);
   1.313 +	r=f.Read(0,a, -10);
   1.314 +	test(r==KErrArgument);
   1.315 +	TRequestStatus	stat1;
   1.316 +	f.Read(0,a,-5,stat1);
   1.317 +	User::WaitForRequest(stat1);
   1.318 +	test(stat1.Int() == KErrArgument);
   1.319 +	f.Read(a,-5,stat1);
   1.320 +	User::WaitForRequest(stat1);
   1.321 +	test(stat1.Int() == KErrArgument);
   1.322 +	f.Close();	
   1.323 +	test.End();
   1.324 +	
   1.325 +	}
   1.326 +
   1.327 +GLDEF_C void CallTestsL()
   1.328 +//
   1.329 +// Call tests that may leave
   1.330 +//
   1.331 +	{
   1.332 +	
   1.333 +	testAutoClose();
   1.334 +    btest1(EFileStream);
   1.335 +    btest2(EFileStream);
   1.336 +    btest1(EFileStreamText);
   1.337 +    btest2(EFileStreamText);
   1.338 +    rndtest(EFileStream);
   1.339 +	readWithNegativeLengthTest();
   1.340 +	readWithNegativeLengthTestForEmptyFile();
   1.341 +	
   1.342 +	}