os/kernelhwsrv/kerneltest/e32test/buffer/t_farray.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_farray.cpp
sl@0
    15
// Overview:
sl@0
    16
// Test the functionality of CArrayFixFlat,  CArrayPtrFlat,  CArrayFixSeg classes.
sl@0
    17
// API Information:
sl@0
    18
// CArrayFixFlat, CArrayPtrFlat, CArrayFixSeg.
sl@0
    19
// Details:
sl@0
    20
// - Create an array of fixed length buffer descriptor objects contained within a flat
sl@0
    21
// dynamic and segmented buffer and verify that
sl@0
    22
// - number of elements held in the array is 0.
sl@0
    23
// - length of an element is as specified.
sl@0
    24
// - array is compressed and reset as expected.
sl@0
    25
// - the elements of the array are sorted as expected.
sl@0
    26
// - insertion of a text into the array at specified position and filling a blank space
sl@0
    27
// at the beginning is as expected.
sl@0
    28
// - return value is 0 when available element is searched within the array.
sl@0
    29
// - removal of first element from the array is successful.
sl@0
    30
// - number of elements held in the array is 1 after appending a single element onto 
sl@0
    31
// the end of the array.
sl@0
    32
// - the position of specified element is found successfully
sl@0
    33
// - resetting the array is as expected
sl@0
    34
// - End and Back methods are as expected.
sl@0
    35
// - Create an array of fixed length text character objects contained within a flat dynamic 
sl@0
    36
// and segmented buffer.
sl@0
    37
// - append a text onto the end of the array, check the contents and number of elements 
sl@0
    38
// held in the array are as expected.
sl@0
    39
// - insert a text and verify that the change in the content of array and number of 
sl@0
    40
// elements held in the array are as expected.
sl@0
    41
// - remove a single character and multiple characters from the array and verify that
sl@0
    42
// the Delete method is as expected. Compress the array.
sl@0
    43
// - Create an array of fixed length text character objects contained within a flat dynamic
sl@0
    44
// and segmented buffer.
sl@0
    45
// - append strings of specified length onto the end of the array and verify that the 
sl@0
    46
// number of elements held in the array is as expected.
sl@0
    47
// - insert strings at specified location and check that the contents are as expected.
sl@0
    48
// - reset the array, append a string, compress the array and verify that content is as 
sl@0
    49
// expected.
sl@0
    50
// - sort the array and verify that content is as expected.
sl@0
    51
// - verify the correct position of the element and return value is zero when an element
sl@0
    52
// is found using binary and sequential search technique and nonzero if not present in 
sl@0
    53
// the array.
sl@0
    54
// - Create an array of fixed length text character objects contained within a flat dynamic 
sl@0
    55
// and segmented buffer.
sl@0
    56
// - Insert some elements into the array at specified positions determined by key of 
sl@0
    57
// type TInt and verify that KErrAlreadyExists is returned if an element with the 
sl@0
    58
// same key already exists within the array.
sl@0
    59
// - Create an array of pointers to objects implemented using a flat dynamic buffer, insert one 
sl@0
    60
// element into the array at the specified position and destroy the object whose pointer form 
sl@0
    61
// the element of the array, before resetting the array.
sl@0
    62
// - Create and delete an array of CBase objects contained within a flat dynamic buffer.
sl@0
    63
// - Test whether the heap has been corrupted by all the tests.
sl@0
    64
// Platforms/Drives/Compatibility:
sl@0
    65
// All 
sl@0
    66
// Assumptions/Requirement/Pre-requisites:
sl@0
    67
// Failures and causes:
sl@0
    68
// Base Port information:
sl@0
    69
// 
sl@0
    70
//
sl@0
    71
sl@0
    72
#include <e32test.h>
sl@0
    73
sl@0
    74
class MyCBase : public CBase
sl@0
    75
	{
sl@0
    76
	};
sl@0
    77
sl@0
    78
const TInt KTestGranularity=0x02;
sl@0
    79
sl@0
    80
LOCAL_D RTest test(_L("T_FARRAY"));
sl@0
    81
sl@0
    82
template <class T,TInt S>
sl@0
    83
class TArr
sl@0
    84
	{
sl@0
    85
public:
sl@0
    86
	TArr() {}
sl@0
    87
	TInt Count() const {return S;}
sl@0
    88
	T& operator[](TInt anIndex) {return iArr[anIndex];}
sl@0
    89
	const T& operator[](TInt anIndex) const {return iArr[anIndex];}
sl@0
    90
private:
sl@0
    91
	T iArr[S];
sl@0
    92
	};
sl@0
    93
sl@0
    94
LOCAL_C void testFix(CArrayFix<TBuf<0x10> >& aFix)
sl@0
    95
//
sl@0
    96
// Test all methods
sl@0
    97
//
sl@0
    98
	{
sl@0
    99
	test.Next(_L("Test all methods"));
sl@0
   100
	test(aFix.Count()==0);
sl@0
   101
	test(aFix.Length()==sizeof(TBuf<0x10>));
sl@0
   102
	aFix.Compress();
sl@0
   103
	test(TRUE);
sl@0
   104
	aFix.Reset();
sl@0
   105
	test(TRUE);
sl@0
   106
	TKeyArrayFix kk(0,ECmpNormal,0x10);
sl@0
   107
	test(TRUE);
sl@0
   108
	aFix.Sort(kk);
sl@0
   109
	test(TRUE);
sl@0
   110
	TBuf<0x10> aa(_L("aaaaa"));
sl@0
   111
	aFix.InsertL(0,aa);
sl@0
   112
	test(TRUE);
sl@0
   113
	aFix[0].Fill(' ');
sl@0
   114
	test(TRUE);
sl@0
   115
	TBuf<0x10> z(aFix[0]);
sl@0
   116
    z.Length();
sl@0
   117
	test(TRUE);
sl@0
   118
	aFix[0].Fill('a');
sl@0
   119
	test(TRUE);
sl@0
   120
	TInt pp;
sl@0
   121
	test(aFix.Find(aa,kk,pp)==0);
sl@0
   122
	test(pp==0);
sl@0
   123
	aFix.Delete(0);
sl@0
   124
	TBuf<0x10> bb(_L("bbbbb"));
sl@0
   125
	aFix.AppendL(bb);
sl@0
   126
	test(aFix.Count()==1);
sl@0
   127
	test(aFix.InsertIsqAllowDuplicatesL(aa,kk)==0);
sl@0
   128
	test(aFix.InsertIsqAllowDuplicatesL(bb,kk)==2);
sl@0
   129
	test(aFix.FindIsq(aa,kk,pp)==0);
sl@0
   130
	test(pp==0);
sl@0
   131
	aFix.Reset();
sl@0
   132
	for(TInt index=0;index<KTestGranularity*7/2;index++)
sl@0
   133
		aFix.AppendL(aa);
sl@0
   134
	const TBuf<0x10> *end=NULL;
sl@0
   135
	const TBuf<0x10> *ptr=NULL;
sl@0
   136
	for(TInt index2=0;index2<KTestGranularity*7/2;index2++)
sl@0
   137
		{
sl@0
   138
		if (end==ptr)
sl@0
   139
			{
sl@0
   140
			end=aFix.End(index2);
sl@0
   141
			ptr=&aFix[index2];
sl@0
   142
			TInt seglen=end-ptr;
sl@0
   143
			test(seglen==KTestGranularity || seglen==(aFix.Count()-index2));
sl@0
   144
			}
sl@0
   145
		test(&aFix[index2]==ptr++);
sl@0
   146
		}
sl@0
   147
	const TBuf<0x10> *bak=NULL;
sl@0
   148
	ptr=NULL;
sl@0
   149
	for(TInt index3=KTestGranularity*7/2;index3>0;index3--)
sl@0
   150
		{
sl@0
   151
		if (bak==ptr)
sl@0
   152
			{
sl@0
   153
			bak=aFix.Back(index3);
sl@0
   154
			ptr=&aFix[index3-1]+1;
sl@0
   155
			TInt seglen=ptr-bak;
sl@0
   156
			test(seglen==KTestGranularity || seglen==index3 || seglen==index3%KTestGranularity);
sl@0
   157
			}
sl@0
   158
		test(&aFix[index3-1]==--ptr);
sl@0
   159
		}
sl@0
   160
	}
sl@0
   161
sl@0
   162
LOCAL_C void test1(CArrayFix<TText>& aFix)
sl@0
   163
//
sl@0
   164
	{
sl@0
   165
	test.Next(_L("AppendL and InsertL single chars"));
sl@0
   166
	aFix.AppendL(_S("abcd"),4);
sl@0
   167
	test(aFix[0]=='a');
sl@0
   168
	test(aFix[1]=='b');
sl@0
   169
	test(aFix[3]=='d');
sl@0
   170
	test(aFix.Count()==4);
sl@0
   171
	aFix.InsertL(2,_S("ef"),2);
sl@0
   172
	test(aFix[1]=='b');
sl@0
   173
	test(aFix[2]=='e');
sl@0
   174
	test(aFix[4]=='c');
sl@0
   175
	test(aFix.Count()==6);	
sl@0
   176
	aFix.AppendL(TText('z'));
sl@0
   177
	test(aFix[6]=='z');
sl@0
   178
	aFix.InsertL(0,TText('y'));
sl@0
   179
	test(aFix[0]=='y');
sl@0
   180
	test(aFix[1]=='a');
sl@0
   181
	test(aFix.Count()==8);
sl@0
   182
	test.Next(_L("Delete single chars"));
sl@0
   183
	aFix.Delete(3);
sl@0
   184
	test(aFix[2]=='b');
sl@0
   185
	test(aFix[3]=='f');
sl@0
   186
	test(aFix[4]=='c');
sl@0
   187
	aFix.Delete(1,2);
sl@0
   188
	test(aFix[0]=='y');
sl@0
   189
	test(aFix[1]=='f');
sl@0
   190
	test(aFix[2]=='c');
sl@0
   191
	test(aFix.Count()==5);
sl@0
   192
	aFix.Compress();
sl@0
   193
	}
sl@0
   194
sl@0
   195
LOCAL_C void test2(CArrayFix<TArr<TText,4> >& aFix)
sl@0
   196
//
sl@0
   197
	{
sl@0
   198
	test(aFix.Length()==sizeof(TArr<TText,4>));
sl@0
   199
	test.Next(_L("AppendL and insert strings of length 4"));
sl@0
   200
	TPtrC des1=_L("abcd");
sl@0
   201
	TPtrC des2=_L("efgh");
sl@0
   202
	aFix.AppendL(*(const TArr<TText,4>*)des1.Ptr());
sl@0
   203
	aFix.AppendL(*(const TArr<TText,4>*)des2.Ptr());
sl@0
   204
	test(aFix.Count()==2);
sl@0
   205
	TPtrC des3(&aFix[0][0],4);
sl@0
   206
	TPtrC des4(&aFix[1][0],4);
sl@0
   207
	test(des3==_L("abcd"));
sl@0
   208
	test(des4==_L("efgh"));
sl@0
   209
	aFix.InsertL(1,*(const TArr<TText,4>*)_S("ijkl"));
sl@0
   210
	test(aFix.Count()==3);	
sl@0
   211
	TPtrC des5(&aFix[2][0],4);
sl@0
   212
	test(des3==_L("abcd"));
sl@0
   213
	test(des4==_L("ijkl"));
sl@0
   214
	test(des5==_L("efgh"));
sl@0
   215
sl@0
   216
	test.Next(_L("Reset and Compress"));
sl@0
   217
	aFix.Reset();
sl@0
   218
	TBuf<0x10> buf1=_L("abcdefgh");
sl@0
   219
	aFix.AppendL((const TArr<TText,4>*)buf1.Ptr(),2);
sl@0
   220
	aFix.Compress();
sl@0
   221
	TPtrC des6(&aFix[0][0],4);
sl@0
   222
	test(des6==_L("abcd"));
sl@0
   223
	TPtrC des7(&aFix[1][0],4);
sl@0
   224
	test(des7==_L("efgh"));
sl@0
   225
	buf1=_L("ghighhxy");
sl@0
   226
	aFix.InsertL(1,(const TArr<TText,4>*)buf1.Ptr(),2);
sl@0
   227
	aFix.Compress();
sl@0
   228
	TPtrC des8(&aFix[0][0],4);
sl@0
   229
	test(des8==_L("abcd"));
sl@0
   230
	TPtrC des9(&aFix[1][0],4);
sl@0
   231
	test(des9==_L("ghig"));
sl@0
   232
	TPtrC des10(&aFix[2][0],4);
sl@0
   233
	test(des10==_L("hhxy"));
sl@0
   234
	TPtrC des11(&aFix[3][0],4);
sl@0
   235
	test(des11==_L("efgh"));
sl@0
   236
sl@0
   237
	test.Next(_L("Sort strings"));
sl@0
   238
	TKeyArrayFix kk(0,ECmpNormal,0x04);
sl@0
   239
	aFix.Sort(kk);
sl@0
   240
	TPtrC des12(&aFix[0][0],4);
sl@0
   241
	test(des12==_L("abcd"));
sl@0
   242
	TPtrC des13(&aFix[1][0],4);
sl@0
   243
	test(des13==_L("efgh"));
sl@0
   244
	TPtrC des14(&aFix[2][0],4);
sl@0
   245
	test(des14==_L("ghig"));
sl@0
   246
	TPtrC des15(&aFix[3][0],4);
sl@0
   247
	test(des15==_L("hhxy"));
sl@0
   248
	
sl@0
   249
	test.Next(_L("Find and FindIsq"));
sl@0
   250
	aFix.Compress();
sl@0
   251
	test(aFix.InsertIsqL(*(const TArr<TText,4>*)_S("ffff"),kk)==2);
sl@0
   252
	aFix.Compress();
sl@0
   253
	test(aFix.InsertIsqAllowDuplicatesL(*(const TArr<TText,4>*)_S("ffff"),kk)==3);
sl@0
   254
	aFix.Compress();
sl@0
   255
	TRAPD(r,aFix.InsertIsqL(*(const TArr<TText,4>*)_S("ffff"),kk))
sl@0
   256
	test(r==KErrAlreadyExists);
sl@0
   257
	TInt aPos=0;
sl@0
   258
	test(aFix.Find(*(const TArr<TText,4>*)_S("xxxx"),kk,aPos)==1);
sl@0
   259
	test(aPos==6);
sl@0
   260
	test(aFix.Find(*(const TArr<TText,4>*)_S("abcd"),kk,aPos)==0);
sl@0
   261
	test(aPos==0);
sl@0
   262
	test(aFix.Find(*(const TArr<TText,4>*)_S("ghig"),kk,aPos)==0);
sl@0
   263
	test(aPos==4);
sl@0
   264
	test(aFix.Find(*(const TArr<TText,4>*)_S("ffff"),kk,aPos)==0);
sl@0
   265
	test(aPos==2);
sl@0
   266
	test(aFix.Find(*(const TArr<TText,4>*)_S("hhxy"),kk,aPos)==0);
sl@0
   267
	test(aPos==5);
sl@0
   268
	test(aFix.FindIsq(*(const TArr<TText,4>*)_S("bbbb"),kk,aPos)!=0);
sl@0
   269
	test(aPos==1);
sl@0
   270
	test(aFix.FindIsq(*(const TArr<TText,4>*)_S("abcd"),kk,aPos)==0);
sl@0
   271
	test(aPos==0);
sl@0
   272
	test(aFix.FindIsq(*(const TArr<TText,4>*)_S("ghig"),kk,aPos)==0);
sl@0
   273
	test(aPos==4);
sl@0
   274
	test(aFix.FindIsq(*(const TArr<TText,4>*)_S("ffff"),kk,aPos)==0);
sl@0
   275
	test(aPos==2);
sl@0
   276
	test(aFix.InsertIsqL(*(const TArr<TText,4>*)_S("fghz"),kk)==4);
sl@0
   277
	test(aFix.FindIsq(*(const TArr<TText,4>*)_S("fghz"),kk,aPos)==0);
sl@0
   278
	test(aPos==4);
sl@0
   279
	test(aFix.FindIsq(*(const TArr<TText,4>*)_S("hhxy"),kk,aPos)==0);
sl@0
   280
	test(aPos==6);
sl@0
   281
	}
sl@0
   282
sl@0
   283
LOCAL_C void test3(CArrayFix<TInt>& aFix)
sl@0
   284
	{
sl@0
   285
sl@0
   286
	test.Next(_L("InsertIsqL"));
sl@0
   287
	TKeyArrayFix kk(0,ECmpTInt);
sl@0
   288
sl@0
   289
	TInt pos=0;
sl@0
   290
	TInt mod=47;
sl@0
   291
	TInt inc=23;
sl@0
   292
	TInt i=0;
sl@0
   293
sl@0
   294
	FOREVER
sl@0
   295
		{
sl@0
   296
		TInt ret;
sl@0
   297
		if (i&1)
sl@0
   298
			TRAP(ret,aFix.InsertIsqL(i,kk))
sl@0
   299
		else
sl@0
   300
			{
sl@0
   301
			TRAP(ret,pos=aFix.InsertIsqL(i,kk))
sl@0
   302
			if (ret==KErrNone)
sl@0
   303
				test(aFix[pos]==i);
sl@0
   304
			}
sl@0
   305
		if (ret==KErrAlreadyExists)
sl@0
   306
			break;
sl@0
   307
		i=(i+inc)%mod;
sl@0
   308
		}
sl@0
   309
sl@0
   310
	for(i=0;i<mod;i++)
sl@0
   311
		{
sl@0
   312
		test(aFix.FindIsq(i,kk,pos)==0);
sl@0
   313
		test(pos==i);
sl@0
   314
		TRAPD(r,aFix.InsertIsqL(i,kk))
sl@0
   315
		test(r==KErrAlreadyExists);
sl@0
   316
		}
sl@0
   317
	}
sl@0
   318
	
sl@0
   319
GLDEF_C TInt E32Main()
sl@0
   320
//
sl@0
   321
// Test the Array classes.
sl@0
   322
//
sl@0
   323
    {
sl@0
   324
sl@0
   325
	test.Title();
sl@0
   326
	__UHEAP_MARK;
sl@0
   327
	test.Start(_L("class CArrayFixFlat"));
sl@0
   328
	CArrayFixFlat<TBuf<0x10> >* pFixFlat=new CArrayFixFlat<TBuf<0x10> >(KTestGranularity);
sl@0
   329
	if (pFixFlat==NULL)
sl@0
   330
		test.Panic(_L("Allocating array"));
sl@0
   331
	testFix(*pFixFlat);
sl@0
   332
	delete pFixFlat;
sl@0
   333
sl@0
   334
	CArrayFixFlat<TText>* pFixFlatChar=new CArrayFixFlat<TText>(KTestGranularity);
sl@0
   335
	test1(*pFixFlatChar);
sl@0
   336
	delete pFixFlatChar; 
sl@0
   337
		
sl@0
   338
	CArrayFixFlat<TArr<TText,4> >* pFixFlatArr=new CArrayFixFlat<TArr<TText,4> >(KTestGranularity);
sl@0
   339
	test2(*pFixFlatArr);
sl@0
   340
	delete pFixFlatArr;
sl@0
   341
sl@0
   342
	CArrayFixFlat<TInt>* pFixFlatInt=new CArrayFixFlat<TInt>(KTestGranularity);
sl@0
   343
	test3(*pFixFlatInt);
sl@0
   344
	delete pFixFlatInt;
sl@0
   345
sl@0
   346
	test.Next(_L("class CArrayPtrFlat of CBase"));
sl@0
   347
	
sl@0
   348
	CArrayPtrFlat<MyCBase>* pPtrFlatCBase=new CArrayPtrFlat<MyCBase>(KTestGranularity); 
sl@0
   349
	if (pPtrFlatCBase==NULL)
sl@0
   350
		test.Panic(_L("Allocating array of CBase*"));
sl@0
   351
	MyCBase* c1 = new MyCBase();
sl@0
   352
	pPtrFlatCBase->InsertL(0,&c1,1);
sl@0
   353
	pPtrFlatCBase->ResetAndDestroy();
sl@0
   354
//	test(pFixFlatCBase->iBase==0);
sl@0
   355
	pPtrFlatCBase->ResetAndDestroy();
sl@0
   356
	delete pPtrFlatCBase;
sl@0
   357
sl@0
   358
	test.Next(_L("class CArrayFixFlat of CBase"));
sl@0
   359
	
sl@0
   360
	CArrayFixFlat<MyCBase>* pFixFlatCBase=new CArrayFixFlat<MyCBase>(KTestGranularity); 
sl@0
   361
	if (pFixFlatCBase==NULL)
sl@0
   362
		test.Panic(_L("Allocating array of CBase"));
sl@0
   363
	delete pFixFlatCBase;
sl@0
   364
sl@0
   365
	test.Next(_L("class CArrayFixSeg"));
sl@0
   366
	CArrayFixSeg<TBuf<0x10> >* pFixSeg=new CArrayFixSeg<TBuf<0x10> >(KTestGranularity);
sl@0
   367
	if (pFixSeg==NULL)
sl@0
   368
		test.Panic(_L("Allocating array"));
sl@0
   369
	testFix(*pFixSeg);
sl@0
   370
	delete pFixSeg;
sl@0
   371
sl@0
   372
	CArrayFixSeg<TText>* pFixSegChar=new CArrayFixSeg<TText>(KTestGranularity);
sl@0
   373
	test1(*pFixSegChar);
sl@0
   374
	delete pFixSegChar; 
sl@0
   375
		
sl@0
   376
	CArrayFixSeg<TArr<TText,4> >* pFixSegArr=new CArrayFixSeg<TArr<TText,4> >(KTestGranularity);
sl@0
   377
	test2(*pFixSegArr);
sl@0
   378
	delete pFixSegArr;
sl@0
   379
sl@0
   380
	CArrayFixSeg<TInt>* pFixSegInt=new CArrayFixSeg<TInt>(KTestGranularity);
sl@0
   381
	test3(*pFixSegInt);
sl@0
   382
	delete pFixSegInt;
sl@0
   383
sl@0
   384
	test.End();
sl@0
   385
	__UHEAP_MARKEND;
sl@0
   386
	return(0);
sl@0
   387
    }
sl@0
   388