kerneltest/e32test/buffer/t_varray.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     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 "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_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 <e32std_private.h>
       
    73 #include <e32base.h>
       
    74 #include <e32base_private.h>
       
    75 #include <e32test.h>
       
    76 #include <e32svr.h>
       
    77 #include <e32ver.h>
       
    78 
       
    79 const TInt KTestGranularity=0x02;
       
    80 
       
    81 LOCAL_D RTest test(_L("T_VARRAY"));
       
    82 
       
    83 LOCAL_C void testAllMethods(CArrayVar<TText>& aVar)
       
    84     {
       
    85 	test.Next(_L("Test all methods"));
       
    86 	test(aVar.Count()==0);
       
    87 	aVar.Compress();
       
    88 	test(TRUE);
       
    89 	aVar.Reset();
       
    90 	test(TRUE);
       
    91 	TKeyArrayVar kk(sizeof(TText),ECmpNormal,0);
       
    92 	test(TRUE);
       
    93 	aVar.Sort(kk);
       
    94 	test(TRUE);
       
    95 	const TText* aa=_S("a");
       
    96 	aVar.InsertL(0,*aa,sizeof(TText));
       
    97 	test(aVar.Length(0)==sizeof(TText));
       
    98 	test(TRUE);
       
    99 	TInt pp;
       
   100 	test(aVar.Find(*aa,kk,pp)==0);
       
   101 	test(pp==0);
       
   102 	aVar.Delete(0);
       
   103 	aVar.AppendL(*aa,1);
       
   104 	test(aVar.Count()==1);
       
   105 	aVar.InsertIsqAllowDuplicatesL(*aa,0,kk);
       
   106 	test(TRUE);
       
   107 	test(aVar.FindIsq(*aa,kk,pp)==0);
       
   108 	test(pp==0);
       
   109 	TRAPD(r,aVar.InsertIsqL(*aa,0,kk));
       
   110 	test(r==KErrAlreadyExists);
       
   111     }
       
   112 
       
   113 LOCAL_C void test1(CArrayVar<TText>& aVar)
       
   114 //
       
   115 	{
       
   116 	test.Next(_L("AppendL and InsertL chars"));
       
   117 	aVar.AppendL(*_S("abcd"),5*sizeof(TText)); // abcd
       
   118 	TBuf<0x10> des1(&aVar[0]);
       
   119 	test(des1==_L("abcd"));
       
   120 	test(aVar.Count()==1);
       
   121 	aVar.AppendL(*_S("wxyz"),5*sizeof(TText)); // abcd wxyz
       
   122 	des1=&aVar[1];
       
   123 	test(des1==_L("wxyz"));
       
   124 	test(aVar.Count()==2);
       
   125 	aVar.InsertL(1,*_S("ef"),3*sizeof(TText)); // abcd ef wxyz
       
   126 	des1=&aVar[1];
       
   127 	test(des1==_L("ef"));
       
   128 	test(aVar.Count()==3);	
       
   129 	aVar.AppendL(*_S("z"),2*sizeof(TText)); // abcd ef wxyz z
       
   130 	des1=&aVar[3];
       
   131 	test(des1==_L("z"));
       
   132 	aVar.InsertL(0,*_S("y"),2*sizeof(TText)); // y abcd ef wxyz z
       
   133 	des1=&aVar[0];
       
   134 	test(des1==_L("y"));
       
   135 	test(aVar.Length(0)==2*sizeof(TText));
       
   136 	test(aVar.Length(1)==5*sizeof(TText));
       
   137 	test(aVar.Length(2)==3*sizeof(TText));
       
   138 	test(aVar.Length(3)==5*sizeof(TText));
       
   139 	test(aVar.Length(4)==2*sizeof(TText));
       
   140 	des1=&aVar[1];
       
   141 	test(des1==_L("abcd"));
       
   142 	test(aVar.Count()==5);
       
   143 //
       
   144 	test.Next(_L("Delete chars"));
       
   145 	aVar.Delete(3,1); // y abcd ef z
       
   146 	des1=&aVar[2];
       
   147 	test(des1==_L("ef"));
       
   148 	des1=&aVar[1];
       
   149 	test(des1==_L("abcd"));
       
   150 	des1=&aVar[3];
       
   151 	test(des1==_L("z"));
       
   152 	aVar.Delete(1,2); // y z
       
   153 	des1=&aVar[0];
       
   154 	test(des1==_L("y"));
       
   155 	des1=&aVar[1];
       
   156 	test(des1==_L("z"));
       
   157 	test(aVar.Count()==2);
       
   158 	}
       
   159 
       
   160 LOCAL_C void test2(CArrayVar<TText>& aVar)
       
   161 //
       
   162 	{
       
   163 	test.Next(_L("Reset and Compress"));
       
   164 	TBuf<0x10> des1(_L("abcde"));
       
   165 	TBuf<0x10> des2(_L("fgh"));
       
   166 	TBuf<0x10> des3(_L("wxyz"));
       
   167 	aVar.AppendL(*(TText*)des1.Ptr(),des1.Size());
       
   168 	aVar.AppendL(*(TText*)des2.Ptr(),des2.Size());
       
   169 	aVar.Compress();
       
   170 	test(aVar.Count()==2);
       
   171 	TPtrC des4((TText*)&aVar[0],des1.Length());
       
   172 	test(des1==des4);
       
   173 	TPtrC des5((TText*)&aVar[1],des2.Length());
       
   174 	test(des2==des5);
       
   175 	aVar.InsertL(1,*(TText*)des3.Ptr(),des3.Size());
       
   176 	test(aVar.Count()==3);	
       
   177 	TPtrC des6((TText*)&aVar[0],des1.Length());
       
   178 	test(des1==des6);
       
   179 	TPtrC des7((TText*)&aVar[2],des2.Length());
       
   180 	test(des2==des7);
       
   181 	TPtrC des8((TText*)&aVar[1],des3.Length());
       
   182 	test(des3==des8);
       
   183 	aVar.Reset();
       
   184 
       
   185 	TBuf<0x10> buf1=_L("abcdef");
       
   186 	TBuf<0x10> buf2=_L("wxyz");
       
   187 	TBuf<0x10> buf3=_L("lmnop");
       
   188 	TBuf<0x10> buf4=_L("aa");
       
   189 	aVar.AppendL(*buf1.Ptr(),buf1.Size());
       
   190 	aVar.InsertL(0,*buf2.Ptr(),buf2.Size());
       
   191 	aVar.AppendL(*buf3.Ptr(),buf3.Size());
       
   192 	aVar.InsertL(1,*buf4.Ptr(),buf4.Size());
       
   193 	aVar.Compress();
       
   194 	TPtrC rd1((TText*)&aVar[2],buf1.Length());
       
   195 	test(buf1==rd1);
       
   196 	TPtrC rd2((TText*)&aVar[0],buf2.Length());
       
   197 	test(buf2==rd2);
       
   198 	TPtrC rd3((TText*)&aVar[3],buf3.Length());
       
   199 	test(buf3==rd3);
       
   200 	TPtrC rd4((TText*)&aVar[1],buf4.Length());
       
   201 	test(buf4==rd4);
       
   202 	test(aVar.Count()==4);
       
   203 	
       
   204 	TKeyArrayVar kk(0,ECmpNormal,3);		// Compare 3 characters
       
   205 	TKeyArrayVar kk1(0,ECmpNormal,2);	// Compare 2 characters
       
   206 	test.Next(_L("Sort"));
       
   207 	aVar.Sort(kk);
       
   208 	TPtrC rd5((TText*)&aVar[1],buf1.Length());
       
   209 	test(buf1==rd5);
       
   210 	TPtrC rd6((TText*)&aVar[3],buf2.Length());
       
   211 	test(buf2==rd6);
       
   212 	TPtrC rd7((TText*)&aVar[2],buf3.Length());
       
   213 	test(buf3==rd7);
       
   214 	TPtrC rd8((TText*)&aVar[0],buf4.Length());
       
   215 	test(buf4==rd8);
       
   216 	test(aVar.Count()==4);	
       
   217 
       
   218 	test.Next(_L("Find and FindIsq"));
       
   219 	TBuf<0x10> buf5=_L("ffff");
       
   220 	test(aVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==2);
       
   221 	TRAPD(r,aVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk))
       
   222 	test(r==KErrAlreadyExists);
       
   223 	test(aVar.InsertIsqAllowDuplicatesL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==3);
       
   224 	TInt aPos;
       
   225 	test(aVar.Find(*_S("abc"),kk,aPos)==0);	// Second parameter 'aLength' is unused. 
       
   226 	test(aPos==1);
       
   227 	test(aVar.Find(*_S("aa"),kk1,aPos)==0);
       
   228 	test(aPos==0);
       
   229 	test(aVar.Find(*_S("wxyz"),kk,aPos)==0);
       
   230 	test(aPos==5);
       
   231 	test(aVar.Find(*_S("fgh"),kk,aPos)!=0);		// Returns !=0 if string not found.
       
   232 	test(aPos==6);								// Not present in list, aPos set to last position
       
   233 	test(aVar.Find(*_S("ffff"),kk,aPos)==0);	
       
   234 	test(aPos==2);
       
   235 	test(aVar.Find(*_S("lmn"),kk,aPos)==0);
       
   236 	test(aPos==4);
       
   237 	test(aVar.FindIsq(*_S("abc"),kk,aPos)==0);
       
   238 	test(aPos==1);
       
   239 	test(aVar.FindIsq(*_S("aa"),kk1,aPos)==0);
       
   240 	test(aPos==0);
       
   241 	test(aVar.FindIsq(*_S("wxyz"),kk,aPos)==0);
       
   242 	test(aPos==5);
       
   243 	test(aVar.FindIsq(*_S("fgh"),kk,aPos)!=0);	// Returns result of last test
       
   244 	test(aPos==4);		  // Not present in list, aPos set to last position tested
       
   245 	TBuf<0x10> buf7=_L("fgh");
       
   246 	test(aVar.InsertIsqL(*(TText*)buf7.Ptr(),buf7.Size(),kk)==4);
       
   247 	test(aVar.FindIsq(*_S("fgh"),kk,aPos)==0);	// Returns result of last test
       
   248 	test(aPos==4);
       
   249 									
       
   250 	test(aVar.FindIsq(*_S("ffff"),kk,aPos)==0);
       
   251 	test(aPos==3);
       
   252 	test(aVar.FindIsq(*_S("lmn"),kk,aPos)==0);
       
   253 	test(aPos==5); 
       
   254 	}
       
   255 
       
   256 LOCAL_C void test3(CArrayVar<TInt>& aVar)
       
   257 	{
       
   258 
       
   259 	test.Next(_L("InsertIsqL"));
       
   260 	TKeyArrayVar kk(0,ECmpTInt);
       
   261 
       
   262 	TInt pos=0;
       
   263 	TInt mod=47;
       
   264 	TInt inc=23;
       
   265 
       
   266 	TInt i=0;	
       
   267 	FOREVER
       
   268 		{
       
   269 		TInt ret;
       
   270 		if (i&1)
       
   271 			TRAP(ret,aVar.InsertIsqL(i,sizeof(TInt),kk))
       
   272 		else
       
   273 			{
       
   274 			TRAP(ret,pos=aVar.InsertIsqL(i,sizeof(TInt),kk))
       
   275 			if (ret==KErrNone)
       
   276 				test(aVar[pos]==i);
       
   277 			}
       
   278 		if (ret==KErrAlreadyExists)
       
   279 			break;
       
   280 		i=(i+inc)%mod;
       
   281 		}
       
   282 	for(i=0;i<mod;i++)
       
   283 		{
       
   284 		pos=(-1);
       
   285 		test(aVar.FindIsq(i,kk,pos)==0);
       
   286 		test(pos==i);
       
   287 		TRAPD(r,aVar.InsertIsqL(i,sizeof(TInt),kk))
       
   288 		test(r==KErrAlreadyExists);
       
   289 		}
       
   290 	}
       
   291 
       
   292 GLDEF_C TInt E32Main()
       
   293 //
       
   294 // Test the variable record length array classes.
       
   295 //
       
   296     {
       
   297 
       
   298 	test.Title();
       
   299 	__UHEAP_MARK;
       
   300 	test.Start(_L("class CArrayFixFlat"));
       
   301 //
       
   302 	CArrayVarFlat<TText>* pVarFlat=new CArrayVarFlat<TText>(KTestGranularity);
       
   303 	if (pVarFlat==NULL)
       
   304 		test.Panic(_L("Allocating array"));
       
   305     testAllMethods(*pVarFlat);
       
   306     delete pVarFlat;
       
   307 //
       
   308 	CArrayVarFlat<TText>* pVarFlatChar=new CArrayVarFlat<TText>(KTestGranularity);
       
   309 	test1(*pVarFlatChar);
       
   310 	delete pVarFlatChar; 
       
   311 //		
       
   312 	CArrayVarFlat<TText>* pVarFlatArr=new CArrayVarFlat<TText>(KTestGranularity);
       
   313 	test2(*pVarFlatArr);
       
   314 	delete pVarFlatArr;
       
   315 //
       
   316 	CArrayVarFlat<TInt>* pVarFlatInt=new CArrayVarFlat<TInt>(KTestGranularity);
       
   317 	test3(*pVarFlatInt);
       
   318 	delete pVarFlatInt;
       
   319 //	
       
   320 	test.Next(_L("class CArrayVarSeg"));
       
   321 	CArrayVarSeg<TText>* pVarSeg=new CArrayVarSeg<TText>(KTestGranularity);
       
   322 	if (pVarSeg==NULL)
       
   323 		test.Panic(_L("Allocating array"));
       
   324 	testAllMethods(*pVarSeg);
       
   325 	delete pVarSeg;
       
   326 //
       
   327 	CArrayVarSeg<TText>* pVarSegChar=new CArrayVarSeg<TText>(KTestGranularity);
       
   328 	test1(*pVarSegChar);
       
   329 	delete pVarSegChar;
       
   330 //		
       
   331 	CArrayVarSeg<TText>* pVarSegArr=new CArrayVarSeg<TText>(KTestGranularity);
       
   332 	test2(*pVarSegArr);
       
   333 	delete pVarSegArr;
       
   334 //
       
   335 	CArrayVarSeg<TInt>* pVarSegInt=new CArrayVarSeg<TInt>(KTestGranularity);
       
   336 	test3(*pVarSegInt);
       
   337 	delete pVarSegInt;
       
   338 //
       
   339 	test.End();
       
   340 	__UHEAP_MARKEND;
       
   341 	return(0);
       
   342     }
       
   343