os/kernelhwsrv/kerneltest/e32test/buffer/t_bseg.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) 1994-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
// e32test\buffer\t_bseg.cpp
sl@0
    15
// Overview:
sl@0
    16
// Test all aspects of the CBufSeg class.
sl@0
    17
// API Information:
sl@0
    18
// CBufSeg.
sl@0
    19
// Details:
sl@0
    20
// - Create a segmented dynamic buffer of specified size.
sl@0
    21
// - Test all operations of CBufSeg class and see if methods are implemented -- 
sl@0
    22
// including NewL, Reset, Size, InsertL, Delete, Ptr, BackPtr, Read, Write and Compress.
sl@0
    23
// - Test CBufSeg constructor is as expected.
sl@0
    24
// - Insert data into the segmented dynamic buffer and verify that InsertL method
sl@0
    25
// is as expected using the Read and Write methods.
sl@0
    26
// - Delete all data using Reset method and check size is zero.
sl@0
    27
// - Test InsertL, Read, Write and Length methods.
sl@0
    28
// - Test Ptr, Free, Size, Backptr and SetReserveL methods are as expected.
sl@0
    29
// - Check self consistancy of segment lengths, and check contents of segmented 
sl@0
    30
// buffer using Ptr() and BackPtr().
sl@0
    31
// - Verify the size of the of the test buffers are correct.
sl@0
    32
// - Insert data into the buffer, delete some data from the beginning, middle and end
sl@0
    33
// then check for results as expected. 
sl@0
    34
// - Verify the data in the buffer before and after Compress method is as expected.
sl@0
    35
// Platforms/Drives/Compatibility:
sl@0
    36
// All 
sl@0
    37
// Assumptions/Requirement/Pre-requisites:
sl@0
    38
// Failures and causes:
sl@0
    39
// Base Port information:
sl@0
    40
// 
sl@0
    41
//
sl@0
    42
sl@0
    43
#include <e32test.h>
sl@0
    44
sl@0
    45
class TBufSegLink : public TDblQueLink
sl@0
    46
	{
sl@0
    47
public:
sl@0
    48
	inline TBufSegLink() : iLen(0) {}
sl@0
    49
	inline TBufSegLink* Next() const {return((TBufSegLink*)iNext);}
sl@0
    50
	inline TBufSegLink* Prev() const {return((TBufSegLink*)iPrev);}
sl@0
    51
public:
sl@0
    52
	TInt iLen;
sl@0
    53
	};
sl@0
    54
sl@0
    55
class TestCBufSeg
sl@0
    56
	{
sl@0
    57
public:
sl@0
    58
	TestCBufSeg() : iDummy(0) {};
sl@0
    59
	void Test1(); 	// Test all operations of the class.
sl@0
    60
	void Test2();	// Inherited Methods
sl@0
    61
	void Test3();	// Insert	
sl@0
    62
	void Test4();	// Delete
sl@0
    63
	void Test5();	// Compress
sl@0
    64
	void Test6L();	// borderlines
sl@0
    65
protected:
sl@0
    66
	TInt iDummy;
sl@0
    67
	static const TInt SegLen;
sl@0
    68
	void CheckSeg(const CBufSeg*);
sl@0
    69
	void CheckContents1(CBufSeg* const);
sl@0
    70
	void CheckContents2(CBufSeg* const);
sl@0
    71
	};
sl@0
    72
sl@0
    73
const TInt TestCBufSeg::SegLen=64;
sl@0
    74
sl@0
    75
LOCAL_D RTest test(_L("T_BSEG"));
sl@0
    76
sl@0
    77
class CSpy : public CBufBase
sl@0
    78
	{
sl@0
    79
public:
sl@0
    80
	~CSpy();
sl@0
    81
protected:
sl@0
    82
	CSpy(TInt anExpandSize);
sl@0
    83
public:
sl@0
    84
    TDblQue<TBufSegLink> iQue;
sl@0
    85
	TBufSegLink* iSeg;
sl@0
    86
	TInt iBase;
sl@0
    87
	TInt iOffset;
sl@0
    88
	};
sl@0
    89
sl@0
    90
GLDEF_C void TestCBufSeg::CheckSeg(const CBufSeg* bf)
sl@0
    91
//
sl@0
    92
// Check Self consistancy of segment lengths
sl@0
    93
//
sl@0
    94
	{
sl@0
    95
sl@0
    96
	if (bf->Size()==0)
sl@0
    97
		return;
sl@0
    98
	TInt sum=0;
sl@0
    99
	TBufSegLink* p1=((CSpy*)bf)->iQue.First();
sl@0
   100
	while (!((CSpy*)bf)->iQue.IsHead(p1))
sl@0
   101
		{
sl@0
   102
		test(p1->iLen<=bf->Size());
sl@0
   103
		sum+=p1->iLen;
sl@0
   104
		p1=p1->Next();
sl@0
   105
		} 
sl@0
   106
	test(sum==bf->Size());
sl@0
   107
	sum=0;
sl@0
   108
	p1=((CSpy*)bf)->iQue.Last();
sl@0
   109
	while (!((CSpy*)bf)->iQue.IsHead(p1))
sl@0
   110
		{
sl@0
   111
		test(p1->iLen<=bf->Size());
sl@0
   112
		sum+=p1->iLen;
sl@0
   113
		p1=p1->Prev();
sl@0
   114
		}
sl@0
   115
	test(sum==bf->Size());
sl@0
   116
	}
sl@0
   117
sl@0
   118
GLDEF_C void TestCBufSeg::CheckContents1(CBufSeg* const bf)
sl@0
   119
//
sl@0
   120
// Check contents of segmented buffer using Ptr()
sl@0
   121
//
sl@0
   122
	{
sl@0
   123
sl@0
   124
	TInt sum=0;
sl@0
   125
	TInt nbytes=bf->Size();
sl@0
   126
	for (TInt pos=0;pos<nbytes;)
sl@0
   127
		{
sl@0
   128
		TPtr8 p=bf->Ptr(pos);
sl@0
   129
		TInt len=p.Length();
sl@0
   130
		TInt8* pT=(TInt8*)p.Ptr();
sl@0
   131
		for (TInt i=0;i<len;i++)
sl@0
   132
			sum+=*pT++;
sl@0
   133
		test(sum==0);
sl@0
   134
		pos+=len;
sl@0
   135
		}
sl@0
   136
	}
sl@0
   137
sl@0
   138
GLDEF_C void TestCBufSeg::CheckContents2(CBufSeg* const bf)
sl@0
   139
//
sl@0
   140
// Check contents of segmented buffer using BackPtr()
sl@0
   141
//
sl@0
   142
	{
sl@0
   143
sl@0
   144
	TInt sum=0;
sl@0
   145
	TInt nbytes=bf->Size();
sl@0
   146
	for(TInt pos=nbytes;pos>0;)
sl@0
   147
		{
sl@0
   148
		TPtr8 p=bf->BackPtr(pos);
sl@0
   149
		TInt len=p.Length();
sl@0
   150
		TInt8* pT=(TInt8*)p.Ptr();
sl@0
   151
		for (TInt i=0;i<len;i++)
sl@0
   152
			sum+=*pT++;
sl@0
   153
		pos-=len;
sl@0
   154
		}
sl@0
   155
	test(sum==0);
sl@0
   156
	}
sl@0
   157
sl@0
   158
GLDEF_C void TestCBufSeg::Test1()
sl@0
   159
//
sl@0
   160
// Test all operations of BufSeg class
sl@0
   161
//
sl@0
   162
	{
sl@0
   163
sl@0
   164
	test.Start(_L("Test all operations of CBufSeg"));
sl@0
   165
	CBufSeg* bf=(CBufSeg*)CBufSeg::NewL(100);
sl@0
   166
	bf->Reset();
sl@0
   167
	bf->Size();
sl@0
   168
	bf->InsertL(0,TPtrC8((TText8*)"Hello World"));
sl@0
   169
	bf->Delete(0,5);
sl@0
   170
	TText8* tp=(TText8*)bf->Ptr(3).Ptr();
sl@0
   171
	tp=(TText8*)bf->BackPtr(3).Ptr();
sl@0
   172
	TBuf8<0x20> txt;
sl@0
   173
	bf->Read(2,txt,2);
sl@0
   174
	bf->Write(2,txt,2);
sl@0
   175
	bf->Compress();
sl@0
   176
	delete bf;
sl@0
   177
	test.End();
sl@0
   178
	}
sl@0
   179
			
sl@0
   180
GLDEF_C void TestCBufSeg::Test2()
sl@0
   181
//
sl@0
   182
// Test all inherited methods
sl@0
   183
//
sl@0
   184
	{
sl@0
   185
sl@0
   186
	TBuf8<0x40> tb1=(TText8*)"Hello World";
sl@0
   187
	TBuf8<0x40> tb2=(TText8*)"String number two";
sl@0
   188
	TBuf8<0x40> tb3;
sl@0
   189
	test.Start(_L("Free,Size,Read,Write,Reset"));
sl@0
   190
	CBufSeg* bf=(CBufSeg*)CBufSeg::NewL(SegLen);
sl@0
   191
	test(bf->Size()==0);
sl@0
   192
	bf->InsertL(0,tb1);
sl@0
   193
	test(bf->Size()==tb1.Length());
sl@0
   194
	bf->Read(6,tb3,5);
sl@0
   195
	test(tb3==tb1.Right(5));
sl@0
   196
	bf->Write(1,tb2,6);
sl@0
   197
	bf->Read(0,tb3,bf->Size());
sl@0
   198
	test(tb3==TPtrC8((TText8*)"HStringorld"));
sl@0
   199
	bf->Reset();
sl@0
   200
	test(bf->Size()==0);
sl@0
   201
	while (bf->Size()<400)
sl@0
   202
		{
sl@0
   203
		bf->InsertL(bf->Size(),tb1);
sl@0
   204
		bf->InsertL(bf->Size(),tb2);
sl@0
   205
		}
sl@0
   206
	TInt i=0;
sl@0
   207
	while (i<400)
sl@0
   208
		{
sl@0
   209
		bf->Read(i,tb3,tb1.Size());
sl@0
   210
		test(tb3==tb1);
sl@0
   211
		i+=tb1.Length();
sl@0
   212
		bf->Read(i,tb3,tb2.Size());
sl@0
   213
		test(tb3==tb2);
sl@0
   214
		i+=tb2.Length();
sl@0
   215
		}
sl@0
   216
	i=0;
sl@0
   217
	while (i<400)
sl@0
   218
		{
sl@0
   219
		bf->Write(i,tb2);
sl@0
   220
		i+=tb2.Length();
sl@0
   221
		bf->Write(i,tb1);
sl@0
   222
		i+=tb1.Length();
sl@0
   223
		}
sl@0
   224
	i=0;
sl@0
   225
	while (i<400)
sl@0
   226
		{
sl@0
   227
		bf->Read(i,tb3,tb2.Size());
sl@0
   228
		test(tb3==tb2);
sl@0
   229
		i+=tb2.Length();
sl@0
   230
		bf->Read(i,tb3,tb1.Size());
sl@0
   231
		test(tb3==tb1);
sl@0
   232
		i+=tb1.Length();
sl@0
   233
		}
sl@0
   234
	delete bf;
sl@0
   235
	test.End();
sl@0
   236
	}
sl@0
   237
sl@0
   238
GLDEF_C void TestCBufSeg::Test3()
sl@0
   239
//
sl@0
   240
//	Test input methods
sl@0
   241
//
sl@0
   242
	{
sl@0
   243
sl@0
   244
	TInt8 bb[1000];
sl@0
   245
	TInt nbytes;
sl@0
   246
	test.Start(_L("InsertL"));
sl@0
   247
	CBufSeg* bf1=(CBufSeg*)CBufSeg::NewL(SegLen);
sl@0
   248
	CBufSeg* bf2=(CBufSeg*)CBufSeg::NewL(SegLen);
sl@0
   249
	CBufSeg* bf3=(CBufSeg*)CBufSeg::NewL(SegLen);
sl@0
   250
	nbytes=0;
sl@0
   251
	TInt k;
sl@0
   252
	for(TInt j=0;j<20;j++)
sl@0
   253
		{
sl@0
   254
		for(TInt i=0;i<10*j;i+=2)
sl@0
   255
			{
sl@0
   256
			k=i%128;
sl@0
   257
			bb[i]=(TInt8)k;
sl@0
   258
			bb[i+1]=(TInt8)-k;
sl@0
   259
			}
sl@0
   260
		bf1->InsertL(bf1->Size()/3*2,&bb[0],10*j);
sl@0
   261
		CheckSeg(bf1);
sl@0
   262
		CheckContents1(bf1);
sl@0
   263
		CheckContents2(bf1);
sl@0
   264
		bf2->InsertL(bf2->Size(),&bb[0],10*j);
sl@0
   265
		CheckSeg(bf2);
sl@0
   266
		CheckContents1(bf2);
sl@0
   267
		CheckContents2(bf2);
sl@0
   268
		bf3->InsertL(0,&bb[0],10*j);
sl@0
   269
		CheckSeg(bf3);
sl@0
   270
		CheckContents1(bf3);
sl@0
   271
		CheckContents2(bf3);
sl@0
   272
		nbytes+=10*j;
sl@0
   273
		}
sl@0
   274
	test(nbytes==bf1->Size());
sl@0
   275
	test(nbytes==bf2->Size());
sl@0
   276
	test(nbytes==bf3->Size());
sl@0
   277
	delete bf1;
sl@0
   278
	delete bf2;
sl@0
   279
	delete bf3;
sl@0
   280
	test.End();
sl@0
   281
	}
sl@0
   282
sl@0
   283
GLDEF_C void TestCBufSeg::Test4()
sl@0
   284
//
sl@0
   285
// Delete
sl@0
   286
// 
sl@0
   287
	{
sl@0
   288
	TInt8 bb[1000];
sl@0
   289
sl@0
   290
	test.Start(_L("Delete"));
sl@0
   291
	CBufSeg* bf1=(CBufSeg*)CBufSeg::NewL(SegLen);
sl@0
   292
	CBufSeg* bf2=(CBufSeg*)CBufSeg::NewL(SegLen);
sl@0
   293
	CBufSeg* bf3=(CBufSeg*)CBufSeg::NewL(SegLen);
sl@0
   294
	TInt nbytes=0;
sl@0
   295
	TInt k;
sl@0
   296
	for(TInt j=0;j<20;j++)
sl@0
   297
		{
sl@0
   298
		for(TInt i=0;i<10*j;i+=2)
sl@0
   299
			{
sl@0
   300
			k=i%128;
sl@0
   301
			bb[i]=(TInt8)k;
sl@0
   302
			bb[i+1]=(TInt8)-k;
sl@0
   303
			}
sl@0
   304
		bf1->InsertL(bf1->Size()/3*2,&bb[0],10*j);
sl@0
   305
		bf2->InsertL(bf2->Size(),&bb[0],10*j);
sl@0
   306
		bf3->InsertL(0,&bb[0],10*j);
sl@0
   307
		nbytes+=10*j;
sl@0
   308
		}
sl@0
   309
	TInt len=34;
sl@0
   310
	TInt aLength;
sl@0
   311
	while (nbytes>len)
sl@0
   312
		{
sl@0
   313
		for (TInt pos=0;pos<nbytes;pos+=44)
sl@0
   314
		{
sl@0
   315
			len=((len+17)%23)*2;
sl@0
   316
			if (len>nbytes-pos)
sl@0
   317
				len=nbytes-pos;
sl@0
   318
			bf1->Delete(pos,len);
sl@0
   319
			CheckSeg(bf1);
sl@0
   320
			CheckContents1(bf1);
sl@0
   321
			CheckContents2(bf1);
sl@0
   322
			aLength=bf2->Ptr(0).Length();
sl@0
   323
			aLength=((aLength>len) ? aLength : len);
sl@0
   324
			bf2->Delete(aLength-len,len);
sl@0
   325
			CheckSeg(bf2);
sl@0
   326
			CheckContents1(bf2);
sl@0
   327
			CheckContents2(bf2);
sl@0
   328
			bf3->Delete(0,len);
sl@0
   329
			CheckSeg(bf3);
sl@0
   330
			CheckContents1(bf3);
sl@0
   331
			CheckContents2(bf3);
sl@0
   332
			nbytes-=len;
sl@0
   333
			test(nbytes==bf1->Size());
sl@0
   334
			test(nbytes==bf2->Size());
sl@0
   335
			test(nbytes==bf3->Size());
sl@0
   336
			}
sl@0
   337
		}
sl@0
   338
	delete bf1;
sl@0
   339
	delete bf2;
sl@0
   340
	delete bf3;
sl@0
   341
	test.End();
sl@0
   342
	}
sl@0
   343
sl@0
   344
GLDEF_C void TestCBufSeg::Test5()
sl@0
   345
//
sl@0
   346
// Compress
sl@0
   347
// 
sl@0
   348
	{
sl@0
   349
	TInt8 bb[1000];
sl@0
   350
sl@0
   351
	test.Start(_L("Compress"));
sl@0
   352
	CBufSeg* bf1=(CBufSeg*)CBufSeg::NewL(SegLen);
sl@0
   353
	CBufSeg* bf2=(CBufSeg*)CBufSeg::NewL(SegLen);
sl@0
   354
	CBufSeg* bf3=(CBufSeg*)CBufSeg::NewL(SegLen);
sl@0
   355
	TInt nbytes=0;
sl@0
   356
	TInt k;
sl@0
   357
	for(TInt j=0;j<20;j++)
sl@0
   358
		{
sl@0
   359
		for(TInt i=0;i<10*j;i+=2)
sl@0
   360
			{
sl@0
   361
			k=i%128;
sl@0
   362
			bb[i]=(TInt8)k;
sl@0
   363
			bb[i+1]=(TInt8)-k;
sl@0
   364
			}
sl@0
   365
		bf1->InsertL(bf1->Size()/3*2,&bb[0],10*j);
sl@0
   366
		bf2->InsertL(bf2->Size(),&bb[0],10*j);
sl@0
   367
		bf3->InsertL(0,&bb[0],10*j);
sl@0
   368
		nbytes+=10*j;
sl@0
   369
		}
sl@0
   370
	TInt len=34;
sl@0
   371
	TInt aLength;
sl@0
   372
	while (nbytes>len)
sl@0
   373
		{
sl@0
   374
		for (TInt pos=0;pos<nbytes;pos+=44)
sl@0
   375
		{
sl@0
   376
			if (len>nbytes-pos)
sl@0
   377
				len=nbytes-pos;
sl@0
   378
			bf1->Delete(pos,len);
sl@0
   379
			bf1->Compress();
sl@0
   380
			CheckSeg(bf1);
sl@0
   381
			CheckContents1(bf1);
sl@0
   382
			CheckContents2(bf1);
sl@0
   383
			aLength=bf2->Ptr(0).Length();
sl@0
   384
			aLength=((aLength>len)? aLength : len);
sl@0
   385
			bf2->Delete(aLength-len,len);
sl@0
   386
			bf2->Compress();
sl@0
   387
			CheckSeg(bf2);
sl@0
   388
			CheckContents1(bf2);
sl@0
   389
			CheckContents2(bf2);
sl@0
   390
			bf3->Delete(0,len);
sl@0
   391
			bf3->Compress();
sl@0
   392
			CheckSeg(bf3);
sl@0
   393
			CheckContents1(bf3);
sl@0
   394
			CheckContents2(bf3);
sl@0
   395
			nbytes-=len;
sl@0
   396
			test(nbytes==bf1->Size());
sl@0
   397
			test(nbytes==bf2->Size());
sl@0
   398
			test(nbytes==bf3->Size());
sl@0
   399
			}
sl@0
   400
		}
sl@0
   401
	delete bf1;
sl@0
   402
	delete bf2;
sl@0
   403
	delete bf3;
sl@0
   404
	test.End();
sl@0
   405
	}
sl@0
   406
sl@0
   407
void TestCBufSeg::Test6L()
sl@0
   408
	{
sl@0
   409
	test.Start(_L("Test compress frees empty cells"));
sl@0
   410
	__UHEAP_MARK;
sl@0
   411
	TUint8 alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
sl@0
   412
	CBufSeg* buf = CBufSeg::NewL(10);
sl@0
   413
	CleanupStack::PushL(buf);
sl@0
   414
	buf->InsertL(0, alphabet, 16);	// "abcdefghij" ++ "klmnop"
sl@0
   415
	buf->Delete(5, 5);			// "abcde" ++ "klmnop"
sl@0
   416
	buf->Delete(10, 1);			// "abcde" ++ "klmno"
sl@0
   417
	buf->Compress();				// "abcdefklmno". i.e. empty cell should be freed.
sl@0
   418
	CleanupStack::PopAndDestroy(buf);
sl@0
   419
	__UHEAP_MARKEND;
sl@0
   420
	test.End();
sl@0
   421
	}
sl@0
   422
sl@0
   423
LOCAL_C void test_CBufSeg()
sl@0
   424
//
sl@0
   425
// Test the BufSeg class
sl@0
   426
//
sl@0
   427
	{
sl@0
   428
sl@0
   429
	TestCBufSeg b;
sl@0
   430
	test.Start(_L("All operations"));
sl@0
   431
	b.Test1();
sl@0
   432
	test.Next(_L("Inherited Methods"));
sl@0
   433
	b.Test2();
sl@0
   434
	test.Next(_L("Insert"));
sl@0
   435
	b.Test3();
sl@0
   436
	test.Next(_L("Delete"));
sl@0
   437
	b.Test4();
sl@0
   438
	test.Next(_L("Compress"));
sl@0
   439
	b.Test5();
sl@0
   440
	test.Next(_L("Bordeline cases"));
sl@0
   441
	TRAPD(r,b.Test6L());
sl@0
   442
	test(r==KErrNone);
sl@0
   443
	//
sl@0
   444
	test.End();
sl@0
   445
	}
sl@0
   446
sl@0
   447
GLDEF_C TInt E32Main()
sl@0
   448
//
sl@0
   449
// Test the ADT segmented varray.
sl@0
   450
//
sl@0
   451
	{
sl@0
   452
	
sl@0
   453
	test.Title();
sl@0
   454
	__UHEAP_MARK;
sl@0
   455
//
sl@0
   456
// Install a trap handler
sl@0
   457
//
sl@0
   458
	CTrapCleanup* trapHandler=CTrapCleanup::New();
sl@0
   459
	test(trapHandler!=NULL);
sl@0
   460
//	CleanupStack::NextLevel();
sl@0
   461
	test.Start(_L("class CBufSeg"));
sl@0
   462
	test_CBufSeg();
sl@0
   463
	delete trapHandler;
sl@0
   464
	__UHEAP_MARKEND;
sl@0
   465
	test.End();
sl@0
   466
	return(0);
sl@0
   467
	}
sl@0
   468