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