symport/e32test/buffer/t_varray.cpp
changeset 1 0a7b44b10206
child 2 806186ab5e14
equal deleted inserted replaced
0:c55016431358 1:0a7b44b10206
       
     1 // Copyright (c) 1995-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 "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\buffer\t_varray.cpp
       
    15 // Overview:
       
    16 // Test variable record length array classes.
       
    17 // API Information:
       
    18 // CArrayVarFlat, CArrayVarSeg.
       
    19 // Details:
       
    20 // - Create an array of variable length text using a flat dynamic & segmented
       
    21 // buffer and verify that:
       
    22 // - number of elements held in the array is 0.
       
    23 // - array is compressed and reset as expected.
       
    24 // - the elements of the array are sorted as expected.
       
    25 // - insertion of a text character into the array at specified position 
       
    26 // and length of the array is as expected.
       
    27 // - return value is 0 when available element is searched using sequential 
       
    28 // search technique 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 
       
    31 // element at the end of empty array.
       
    32 // - insertion of a single element with specified key is successful.
       
    33 // - the element is found when searched using binary search technique
       
    34 // - KErrAlreadyExists is returned if an element is inserted with the same 
       
    35 // key already exists within the array.
       
    36 // - Create an array of variable length text character implemented using a flat 
       
    37 // dynamic & segmented buffer.
       
    38 // - append some strings onto the end of the array, check the contents and 
       
    39 // number of elements held in the array are as expected.
       
    40 // - insert some strings and verify that the change in the content of array 
       
    41 // and number of elements held in the array are as expected.
       
    42 // - remove a single, multiple elements from the array and verify that the 
       
    43 // Delete method is as expected.
       
    44 // - Create an array of variable length text character contained within a flat 
       
    45 // dynamic & segmented buffer.
       
    46 // - append some strings of specified length onto the end of the array, compress 
       
    47 // the array and verify that the number of elements held in the array is as specified.
       
    48 // - insert a string at specified location, check the contents and reset the array.
       
    49 // - append some strings at the end, insert some strings at specified position, 
       
    50 // compress the array and verify that content, number of strings in the array 
       
    51 // are as expected.
       
    52 // - test that the number of elements and contents of the array are sorted as expected.
       
    53 // - verify the correct position of the element and return value is zero when 
       
    54 // an element is found using binary, sequential search technique and nonzero 
       
    55 // if not present in the array.
       
    56 // - insert some strings at the specified location and check that correct position
       
    57 // is returned when searched using binary search technique.
       
    58 // - Create an array of variable length integer contained within a flat dynamic &
       
    59 // segmented buffer.
       
    60 // - insert some elements with same key which is already present within the array 
       
    61 // and check that KErrAlreadyExists is returned. 
       
    62 // - Test whether the heap has been corrupted by all the tests.
       
    63 // Platforms/Drives/Compatibility:
       
    64 // All 
       
    65 // Assumptions/Requirement/Pre-requisites:
       
    66 // Failures and causes:
       
    67 // Base Port information:
       
    68 // 
       
    69 //
       
    70 
       
    71 #include <e32std.h>
       
    72 #include <e32base.h>
       
    73 #include <e32test.h>
       
    74 #include <e32svr.h>
       
    75 #include <e32ver.h>
       
    76 
       
    77 const TInt KTestGranularity=0x02;
       
    78 
       
    79 LOCAL_D RTest test(_L("T_VARRAY"));
       
    80 
       
    81 LOCAL_C void testAllMethods(CArrayVar<TText>& aVar)
       
    82     {
       
    83 	test.Next(_L("Test all methods"));
       
    84 	test(aVar.Count()==0);
       
    85 	aVar.Compress();
       
    86 	test(TRUE);
       
    87 	aVar.Reset();
       
    88 	test(TRUE);
       
    89 	TKeyArrayVar kk(sizeof(TText),ECmpNormal,0);
       
    90 	test(TRUE);
       
    91 	aVar.Sort(kk);
       
    92 	test(TRUE);
       
    93 	const TText* aa=_S("a");
       
    94 	aVar.InsertL(0,*aa,sizeof(TText));
       
    95 	test(aVar.Length(0)==sizeof(TText));
       
    96 	test(TRUE);
       
    97 	TInt pp;
       
    98 	test(aVar.Find(*aa,kk,pp)==0);
       
    99 	test(pp==0);
       
   100 	aVar.Delete(0);
       
   101 	aVar.AppendL(*aa,1);
       
   102 	test(aVar.Count()==1);
       
   103 	aVar.InsertIsqAllowDuplicatesL(*aa,0,kk);
       
   104 	test(TRUE);
       
   105 	test(aVar.FindIsq(*aa,kk,pp)==0);
       
   106 	test(pp==0);
       
   107 	TRAPD(r,aVar.InsertIsqL(*aa,0,kk));
       
   108 	test(r==KErrAlreadyExists);
       
   109     }
       
   110 
       
   111 LOCAL_C void test1(CArrayVar<TText>& aVar)
       
   112 //
       
   113 	{
       
   114 	test.Next(_L("AppendL and InsertL chars"));
       
   115 	aVar.AppendL(*_S("abcd"),5*sizeof(TText)); // abcd
       
   116 	TBuf<0x10> des1(&aVar[0]);
       
   117 	test(des1==_L("abcd"));
       
   118 	test(aVar.Count()==1);
       
   119 	aVar.AppendL(*_S("wxyz"),5*sizeof(TText)); // abcd wxyz
       
   120 	des1=&aVar[1];
       
   121 	test(des1==_L("wxyz"));
       
   122 	test(aVar.Count()==2);
       
   123 	aVar.InsertL(1,*_S("ef"),3*sizeof(TText)); // abcd ef wxyz
       
   124 	des1=&aVar[1];
       
   125 	test(des1==_L("ef"));
       
   126 	test(aVar.Count()==3);	
       
   127 	aVar.AppendL(*_S("z"),2*sizeof(TText)); // abcd ef wxyz z
       
   128 	des1=&aVar[3];
       
   129 	test(des1==_L("z"));
       
   130 	aVar.InsertL(0,*_S("y"),2*sizeof(TText)); // y abcd ef wxyz z
       
   131 	des1=&aVar[0];
       
   132 	test(des1==_L("y"));
       
   133 	test(aVar.Length(0)==2*sizeof(TText));
       
   134 	test(aVar.Length(1)==5*sizeof(TText));
       
   135 	test(aVar.Length(2)==3*sizeof(TText));
       
   136 	test(aVar.Length(3)==5*sizeof(TText));
       
   137 	test(aVar.Length(4)==2*sizeof(TText));
       
   138 	des1=&aVar[1];
       
   139 	test(des1==_L("abcd"));
       
   140 	test(aVar.Count()==5);
       
   141 //
       
   142 	test.Next(_L("Delete chars"));
       
   143 	aVar.Delete(3,1); // y abcd ef z
       
   144 	des1=&aVar[2];
       
   145 	test(des1==_L("ef"));
       
   146 	des1=&aVar[1];
       
   147 	test(des1==_L("abcd"));
       
   148 	des1=&aVar[3];
       
   149 	test(des1==_L("z"));
       
   150 	aVar.Delete(1,2); // y z
       
   151 	des1=&aVar[0];
       
   152 	test(des1==_L("y"));
       
   153 	des1=&aVar[1];
       
   154 	test(des1==_L("z"));
       
   155 	test(aVar.Count()==2);
       
   156 	}
       
   157 
       
   158 LOCAL_C void test2(CArrayVar<TText>& aVar)
       
   159 //
       
   160 	{
       
   161 	test.Next(_L("Reset and Compress"));
       
   162 	TBuf<0x10> des1(_L("abcde"));
       
   163 	TBuf<0x10> des2(_L("fgh"));
       
   164 	TBuf<0x10> des3(_L("wxyz"));
       
   165 	aVar.AppendL(*(TText*)des1.Ptr(),des1.Size());
       
   166 	aVar.AppendL(*(TText*)des2.Ptr(),des2.Size());
       
   167 	aVar.Compress();
       
   168 	test(aVar.Count()==2);
       
   169 	TPtrC des4((TText*)&aVar[0],des1.Length());
       
   170 	test(des1==des4);
       
   171 	TPtrC des5((TText*)&aVar[1],des2.Length());
       
   172 	test(des2==des5);
       
   173 	aVar.InsertL(1,*(TText*)des3.Ptr(),des3.Size());
       
   174 	test(aVar.Count()==3);	
       
   175 	TPtrC des6((TText*)&aVar[0],des1.Length());
       
   176 	test(des1==des6);
       
   177 	TPtrC des7((TText*)&aVar[2],des2.Length());
       
   178 	test(des2==des7);
       
   179 	TPtrC des8((TText*)&aVar[1],des3.Length());
       
   180 	test(des3==des8);
       
   181 	aVar.Reset();
       
   182 
       
   183 	TBuf<0x10> buf1=_L("abcdef");
       
   184 	TBuf<0x10> buf2=_L("wxyz");
       
   185 	TBuf<0x10> buf3=_L("lmnop");
       
   186 	TBuf<0x10> buf4=_L("aa");
       
   187 	aVar.AppendL(*buf1.Ptr(),buf1.Size());
       
   188 	aVar.InsertL(0,*buf2.Ptr(),buf2.Size());
       
   189 	aVar.AppendL(*buf3.Ptr(),buf3.Size());
       
   190 	aVar.InsertL(1,*buf4.Ptr(),buf4.Size());
       
   191 	aVar.Compress();
       
   192 	TPtrC rd1((TText*)&aVar[2],buf1.Length());
       
   193 	test(buf1==rd1);
       
   194 	TPtrC rd2((TText*)&aVar[0],buf2.Length());
       
   195 	test(buf2==rd2);
       
   196 	TPtrC rd3((TText*)&aVar[3],buf3.Length());
       
   197 	test(buf3==rd3);
       
   198 	TPtrC rd4((TText*)&aVar[1],buf4.Length());
       
   199 	test(buf4==rd4);
       
   200 	test(aVar.Count()==4);
       
   201 	
       
   202 	TKeyArrayVar kk(0,ECmpNormal,3);		// Compare 3 characters
       
   203 	TKeyArrayVar kk1(0,ECmpNormal,2);	// Compare 2 characters
       
   204 	test.Next(_L("Sort"));
       
   205 	aVar.Sort(kk);
       
   206 	TPtrC rd5((TText*)&aVar[1],buf1.Length());
       
   207 	test(buf1==rd5);
       
   208 	TPtrC rd6((TText*)&aVar[3],buf2.Length());
       
   209 	test(buf2==rd6);
       
   210 	TPtrC rd7((TText*)&aVar[2],buf3.Length());
       
   211 	test(buf3==rd7);
       
   212 	TPtrC rd8((TText*)&aVar[0],buf4.Length());
       
   213 	test(buf4==rd8);
       
   214 	test(aVar.Count()==4);	
       
   215 
       
   216 	test.Next(_L("Find and FindIsq"));
       
   217 	TBuf<0x10> buf5=_L("ffff");
       
   218 	test(aVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==2);
       
   219 	TRAPD(r,aVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk))
       
   220 	test(r==KErrAlreadyExists);
       
   221 	test(aVar.InsertIsqAllowDuplicatesL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==3);
       
   222 	TInt aPos;
       
   223 	test(aVar.Find(*_S("abc"),kk,aPos)==0);	// Second parameter 'aLength' is unused. 
       
   224 	test(aPos==1);
       
   225 	test(aVar.Find(*_S("aa"),kk1,aPos)==0);
       
   226 	test(aPos==0);
       
   227 	test(aVar.Find(*_S("wxyz"),kk,aPos)==0);
       
   228 	test(aPos==5);
       
   229 	test(aVar.Find(*_S("fgh"),kk,aPos)!=0);		// Returns !=0 if string not found.
       
   230 	test(aPos==6);								// Not present in list, aPos set to last position
       
   231 	test(aVar.Find(*_S("ffff"),kk,aPos)==0);	
       
   232 	test(aPos==2);
       
   233 	test(aVar.Find(*_S("lmn"),kk,aPos)==0);
       
   234 	test(aPos==4);
       
   235 	test(aVar.FindIsq(*_S("abc"),kk,aPos)==0);
       
   236 	test(aPos==1);
       
   237 	test(aVar.FindIsq(*_S("aa"),kk1,aPos)==0);
       
   238 	test(aPos==0);
       
   239 	test(aVar.FindIsq(*_S("wxyz"),kk,aPos)==0);
       
   240 	test(aPos==5);
       
   241 	test(aVar.FindIsq(*_S("fgh"),kk,aPos)!=0);	// Returns result of last test
       
   242 	test(aPos==4);		  // Not present in list, aPos set to last position tested
       
   243 	TBuf<0x10> buf7=_L("fgh");
       
   244 	test(aVar.InsertIsqL(*(TText*)buf7.Ptr(),buf7.Size(),kk)==4);
       
   245 	test(aVar.FindIsq(*_S("fgh"),kk,aPos)==0);	// Returns result of last test
       
   246 	test(aPos==4);
       
   247 									
       
   248 	test(aVar.FindIsq(*_S("ffff"),kk,aPos)==0);
       
   249 	test(aPos==3);
       
   250 	test(aVar.FindIsq(*_S("lmn"),kk,aPos)==0);
       
   251 	test(aPos==5); 
       
   252 	}
       
   253 
       
   254 LOCAL_C void test3(CArrayVar<TInt>& aVar)
       
   255 	{
       
   256 
       
   257 	test.Next(_L("InsertIsqL"));
       
   258 	TKeyArrayVar kk(0,ECmpTInt);
       
   259 
       
   260 	TInt pos=0;
       
   261 	TInt mod=47;
       
   262 	TInt inc=23;
       
   263 
       
   264 	TInt i=0;	
       
   265 	FOREVER
       
   266 		{
       
   267 		TInt ret;
       
   268 		if (i&1)
       
   269 			TRAP(ret,aVar.InsertIsqL(i,sizeof(TInt),kk))
       
   270 		else
       
   271 			{
       
   272 			TRAP(ret,pos=aVar.InsertIsqL(i,sizeof(TInt),kk))
       
   273 			if (ret==KErrNone)
       
   274 				test(aVar[pos]==i);
       
   275 			}
       
   276 		if (ret==KErrAlreadyExists)
       
   277 			break;
       
   278 		i=(i+inc)%mod;
       
   279 		}
       
   280 	for(i=0;i<mod;i++)
       
   281 		{
       
   282 		pos=(-1);
       
   283 		test(aVar.FindIsq(i,kk,pos)==0);
       
   284 		test(pos==i);
       
   285 		TRAPD(r,aVar.InsertIsqL(i,sizeof(TInt),kk))
       
   286 		test(r==KErrAlreadyExists);
       
   287 		}
       
   288 	}
       
   289 
       
   290 GLDEF_C TInt E32Main()
       
   291 //
       
   292 // Test the variable record length array classes.
       
   293 //
       
   294     {
       
   295 
       
   296 	test.Title();
       
   297 	__UHEAP_MARK;
       
   298 	test.Start(_L("class CArrayFixFlat"));
       
   299 //
       
   300 	CArrayVarFlat<TText>* pVarFlat=new CArrayVarFlat<TText>(KTestGranularity);
       
   301 	if (pVarFlat==NULL)
       
   302 		test.Panic(_L("Allocating array"));
       
   303     testAllMethods(*pVarFlat);
       
   304     delete pVarFlat;
       
   305 //
       
   306 	CArrayVarFlat<TText>* pVarFlatChar=new CArrayVarFlat<TText>(KTestGranularity);
       
   307 	test1(*pVarFlatChar);
       
   308 	delete pVarFlatChar; 
       
   309 //		
       
   310 	CArrayVarFlat<TText>* pVarFlatArr=new CArrayVarFlat<TText>(KTestGranularity);
       
   311 	test2(*pVarFlatArr);
       
   312 	delete pVarFlatArr;
       
   313 //
       
   314 	CArrayVarFlat<TInt>* pVarFlatInt=new CArrayVarFlat<TInt>(KTestGranularity);
       
   315 	test3(*pVarFlatInt);
       
   316 	delete pVarFlatInt;
       
   317 //	
       
   318 	test.Next(_L("class CArrayVarSeg"));
       
   319 	CArrayVarSeg<TText>* pVarSeg=new CArrayVarSeg<TText>(KTestGranularity);
       
   320 	if (pVarSeg==NULL)
       
   321 		test.Panic(_L("Allocating array"));
       
   322 	testAllMethods(*pVarSeg);
       
   323 	delete pVarSeg;
       
   324 //
       
   325 	CArrayVarSeg<TText>* pVarSegChar=new CArrayVarSeg<TText>(KTestGranularity);
       
   326 	test1(*pVarSegChar);
       
   327 	delete pVarSegChar;
       
   328 //		
       
   329 	CArrayVarSeg<TText>* pVarSegArr=new CArrayVarSeg<TText>(KTestGranularity);
       
   330 	test2(*pVarSegArr);
       
   331 	delete pVarSegArr;
       
   332 //
       
   333 	CArrayVarSeg<TInt>* pVarSegInt=new CArrayVarSeg<TInt>(KTestGranularity);
       
   334 	test3(*pVarSegInt);
       
   335 	delete pVarSegInt;
       
   336 //
       
   337 	test.End();
       
   338 	__UHEAP_MARKEND;
       
   339 	return(0);
       
   340     }
       
   341