os/kernelhwsrv/kerneltest/e32test/buffer/t_parray.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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_parray.cpp
    15 // Overview:
    16 // Test the variable record length array classes.
    17 // Test only covers the Flat type implementation.
    18 // API Information:
    19 // CArrayPakFlat.
    20 // Details:
    21 // - Create an array of variable length text character objects packed into a flat buffer.
    22 // - check the number of elements held in the array is 0.
    23 // - test that Compress and Reset methods are as expected.
    24 // - sort the array and check that no error is returned.
    25 // - insert an element and check the length is as expected.
    26 // - search for the inserted element and check it is found successfully.
    27 // - remove all the elements, append an element and verify that number of elements
    28 // held in the array is 1.
    29 // - insert an element into the array, search for that element using sequential,
    30 // binary search technique and verify that it is found at the expected position.
    31 // - insert another element with the same key and check that KErrAlreadyExists is 
    32 // returned.
    33 // - Create an array of variable length text character objects packed into a flat buffer.
    34 // - append some strings at the end, insert some strings at the specified location and
    35 // verify that the length of each string and number of strings held in the array are 
    36 // as expected.
    37 // - delete some strings and check the remaining strings in the array are as expected.
    38 // - Create an array of variable length text character objects packed into a flat buffer.
    39 // - append some strings at the end and insert some stings at specified location.
    40 // - compress the array and verify strings and the number of strings held in the 
    41 // array are as expected.
    42 // - reset the array and verify the number of elements held in the array is 0.
    43 // - sort the array and check that array is sorted as expected.
    44 // - test that KErrAlreadyExists is returned if another element with the same key 
    45 // type is inserted.
    46 // - search for strings using sequential, binary search and verify that 0 is returned 
    47 // if found else nonzero.
    48 // - delete some text from the array and check the results are as expected.
    49 // - Create an array of variable length integer objects packed into a flat buffer.
    50 // - Insert some elements with same key which already exists within the array and 
    51 // check that KErrAlreadyExists is returned.
    52 // - Test whether the heap has been corrupted by all the tests.
    53 // Platforms/Drives/Compatibility:
    54 // All 
    55 // Assumptions/Requirement/Pre-requisites:
    56 // Failures and causes:
    57 // Base Port information:
    58 // 
    59 //
    60 
    61 #include <e32std.h>
    62 #include <e32std_private.h>
    63 #include <e32base.h>
    64 #include <e32base_private.h>
    65 #include <e32test.h>
    66 #include <e32svr.h>
    67 #include <e32ver.h>
    68 
    69 const TInt KTestGranularity=0x10;
    70 
    71 LOCAL_D RTest test(_L("T_PARRAY"));
    72 
    73 LOCAL_C void testAllMethods(CArrayPak<TText>& aPakVar)
    74     {
    75 	test.Next(_L("Test all methods"));
    76 	test(aPakVar.Count()==0);
    77 	aPakVar.Compress();
    78 	test(TRUE);
    79 	aPakVar.Reset();
    80 	test(TRUE);
    81 	TKeyArrayPak kk(sizeof(TText),ECmpNormal,0);
    82 	TKeyArrayVar vv(sizeof(TText),ECmpNormal,0);
    83 	test(TRUE);
    84 	TRAPD(res,aPakVar.SortL(vv));
    85 	test(res==KErrNone);
    86 	const TText* aa=_S("a");
    87 	aPakVar.InsertL(0,*aa,sizeof(TText));
    88 	TBuf<0x10> des1(1);
    89 	des1[0]=aPakVar[0];
    90 	test(des1==_L("a"));
    91 	test(aPakVar.Length(0)==sizeof(TText));
    92 	test(TRUE);
    93 	TInt pp;
    94 	test(aPakVar.Find(*aa,kk,pp)==0);
    95 	test(pp==0);
    96 	aPakVar.Delete(0);
    97 	aPakVar.AppendL(*aa,1);
    98 	test(aPakVar.Count()==1);
    99 	aPakVar.InsertIsqAllowDuplicatesL(*aa,0,kk);
   100 	test(TRUE);
   101 	test(aPakVar.FindIsq(*aa,kk,pp)==0);
   102 	test(pp==0);
   103 	TRAPD(r,aPakVar.InsertIsqL(*aa,0,kk));
   104 	test(r==KErrAlreadyExists);
   105 	}
   106 
   107 LOCAL_C void test1(CArrayPak<TText>& aPakVar)
   108 //
   109 	{
   110 	test.Next(_L("AppendL and InsertL chars"));
   111 	aPakVar.AppendL(*_S("abcd"),5*sizeof(TText)); // abcd
   112 	TBuf<0x10> des1(&aPakVar[0]);
   113 	test(des1==_L("abcd"));
   114 	test(aPakVar.Count()==1);
   115 	aPakVar.AppendL(*_S("wxyz"),5*sizeof(TText)); // abcd wxyz
   116 	des1=&aPakVar[1];
   117 	test(des1==_L("wxyz"));
   118 	test(aPakVar.Count()==2);
   119 	aPakVar.InsertL(1,*_S("ef"),3*sizeof(TText)); // abcd ef wxyz
   120 	des1=&aPakVar[1];
   121 	test(des1==_L("ef"));
   122 	test(aPakVar.Count()==3);	
   123 	aPakVar.AppendL(*_S("z"),2*sizeof(TText)); // abcd ef wxyz z
   124 	des1=&aPakVar[3];
   125 	test(des1==_L("z"));
   126 	aPakVar.InsertL(0,*_S("y"),2*sizeof(TText)); // y abcd ef wxyz z
   127 	des1=&aPakVar[0];
   128 	test(des1==_L("y"));
   129 	test(aPakVar.Length(0)==2*sizeof(TText));
   130 	test(aPakVar.Length(1)==5*sizeof(TText));
   131 	test(aPakVar.Length(2)==3*sizeof(TText));
   132 	test(aPakVar.Length(3)==5*sizeof(TText));
   133 	test(aPakVar.Length(4)==2*sizeof(TText));
   134 	des1=&aPakVar[1];
   135 	test(des1==_L("abcd"));
   136 	test(aPakVar.Count()==5);
   137 //
   138 	test.Next(_L("Delete chars"));
   139 	aPakVar.Delete(3,1); // y abcd ef z
   140 	des1=&aPakVar[2];
   141 	test(des1==_L("ef"));
   142 	des1=&aPakVar[1];
   143 	test(des1==_L("abcd"));
   144 	des1=&aPakVar[3];
   145 	test(des1==_L("z"));
   146 	aPakVar.Delete(1,2); // y z
   147 	des1=&aPakVar[0];
   148 	test(des1==_L("y"));
   149 	des1=&aPakVar[1];
   150 	test(des1==_L("z"));
   151 	test(aPakVar.Count()==2);
   152 	}
   153 
   154 LOCAL_C void test2(CArrayPak<TText>& aPakVar)
   155 //
   156 	{
   157 	test.Next(_L("Reset and Compress"));
   158 	TBuf<0x10> des1(_L("abcde"));
   159 	TBuf<0x10> des2(_L("fgh"));
   160 	TBuf<0x10> des3(_L("wxyz"));
   161 	aPakVar.AppendL(*(TText*)des1.Ptr(),des1.Size());
   162 	aPakVar.AppendL(*(TText*)des2.Ptr(),des2.Size());
   163 	aPakVar.Compress();
   164 	test(aPakVar.Count()==2);
   165 	TPtrC des4((TText*)&aPakVar[0],des1.Length());
   166 	test(des1==des4);
   167 	TPtrC des5((TText*)&aPakVar[1],des2.Length());
   168 	test(des2==des5);
   169 	aPakVar.InsertL(1,*(TText*)des3.Ptr(),des3.Size());
   170 	test(aPakVar.Count()==3);	
   171 	TPtrC des6((TText*)&aPakVar[0],des1.Length());
   172 	test(des1==des6);
   173 	TPtrC des7((TText*)&aPakVar[2],des2.Length());
   174 	test(des2==des7);
   175 	TPtrC des8((TText*)&aPakVar[1],des3.Length());
   176 	test(des3==des8);
   177 	aPakVar.Reset();
   178 	// So nothing in this array
   179 	test(aPakVar.Count()==0);
   180 	TKeyArrayPak kk(0,ECmpNormal,3);		// Compare 3 characters
   181 	TKeyArrayPak kk1(0,ECmpNormal,2);	// Compare 2 characters
   182 	TKeyArrayVar vv(0,ECmpNormal,3);		// Compare 3 characters
   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 	aPakVar.AppendL(*buf1.Ptr(),buf1.Size());
   188 	aPakVar.InsertL(0,*buf2.Ptr(),buf2.Size());
   189 	aPakVar.AppendL(*buf3.Ptr(),buf3.Size());
   190 	aPakVar.InsertL(1,*buf4.Ptr(),buf4.Size());
   191 	aPakVar.Compress();
   192 	TPtrC rd1((TText*)&aPakVar[2],buf1.Length());
   193 	test(buf1==rd1);
   194 	TPtrC rd2((TText*)&aPakVar[0],buf2.Length());
   195 	test(buf2==rd2);
   196 	TPtrC rd3((TText*)&aPakVar[3],buf3.Length());
   197 	test(buf3==rd3);
   198 	TPtrC rd4((TText*)&aPakVar[1],buf4.Length());
   199 	test(buf4==rd4);
   200 	test(aPakVar.Count()==4);
   201 
   202 	test.Next(_L("Sort"));
   203 	TRAPD(res,aPakVar.SortL(vv));
   204 	test(res==KErrNone);
   205 	/**/
   206 	TPtrC rd5((TText*)&aPakVar[1],buf1.Length());
   207 	test(buf1==rd5);
   208 	TPtrC rd6((TText*)&aPakVar[3],buf2.Length());
   209 	test(buf2==rd6);
   210 	TPtrC rd7((TText*)&aPakVar[2],buf3.Length());
   211 	test(buf3==rd7);
   212 	TPtrC rd8((TText*)&aPakVar[0],buf4.Length());
   213 	test(buf4==rd8);
   214 	test(aPakVar.Count()==4);	
   215 	/**/
   216 	test.Next(_L("Find and FindIsq"));
   217 	TBuf<0x10> buf5=_L("ffff");
   218 	test(aPakVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==2);
   219 	TRAPD(r,aPakVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk))
   220 	test(r==KErrAlreadyExists);
   221 	test(aPakVar.InsertIsqAllowDuplicatesL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==3);
   222 	TInt aPos;
   223 	test(aPakVar.Find(*_S("abc"),kk,aPos)==0);	// Second parameter 'aLength' is unused. 
   224 	test(aPos==1);
   225 	test(aPakVar.Find(*_S("aa"),kk1,aPos)==0);
   226 	test(aPos==0);
   227 	test(aPakVar.Find(*_S("wxyz"),kk,aPos)==0);
   228 	test(aPos==5);
   229 	test(aPakVar.Find(*_S("fgh"),kk,aPos)!=0);		// Returns non zero if string not found.
   230 	test(aPos==6);								// Not present in list, aPos set to last position
   231 	test(aPakVar.Find(*_S("ffff"),kk,aPos)==0);	
   232 	test(aPos==2);
   233 	test(aPakVar.Find(*_S("lmn"),kk,aPos)==0);
   234 	test(aPos==4);			  //15
   235 	test(aPakVar.FindIsq(*_S("abc"),kk,aPos)==0);
   236 	test(aPos==1);
   237 	test(aPakVar.FindIsq(*_S("aa"),kk1,aPos)==0);
   238 	test(aPos==0);
   239 	test(aPakVar.FindIsq(*_S("wxyz"),kk,aPos)==0);
   240 	test(aPakVar.FindIsq(*_S("fgh"),kk,aPos)!=0);	// 22 Returns result of last test
   241 	test(aPos==4);		// Not present in list, aPos set to last position tested
   242 	//This test shows problem with BinarySearch
   243 	TBuf<0x10> buf7=_L("fghij");
   244 	test(aPakVar.InsertIsqL(*(TText*)buf7.Ptr(),buf7.Size(),kk)==4);
   245 	test(aPakVar.FindIsq(*_S("fgh"),kk,aPos)==0);	// Returns result of last test
   246 	test(aPos==4);
   247 	test(aPakVar.FindIsq(*_S("ffff"),kk,aPos)==0);
   248 	test(aPos==3);
   249 	test(aPakVar.FindIsq(*_S("lmn"),kk,aPos)==0);
   250 	test(aPos==5); 
   251 	aPakVar.Delete(4,1);
   252 	test(aPakVar.Find(*_S("wxyz"),kk,aPos)==0);
   253 	test(aPos==5);
   254 	}
   255 
   256 LOCAL_C void test3(CArrayPak<TInt>& aPak)
   257 
   258 	{
   259 	test.Next(_L("InsertIsqL"));
   260 	TKeyArrayPak 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,aPak.InsertIsqL(i,sizeof(TInt),kk))
   272 		else
   273 			{
   274 			TRAP(ret,pos=aPak.InsertIsqL(i,sizeof(TInt),kk))
   275 			if (ret==KErrNone)
   276 				test(aPak[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(aPak.FindIsq(i,kk,pos)==0);
   286 		test(pos==i);
   287 		TRAPD(r,aPak.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 // Initially test only covers the Flat type implementation.
   296 //
   297     {
   298 	test.Title();
   299 	__UHEAP_MARK;
   300 	test.Start(_L("class CArrayPakFlat"));
   301 //
   302     CArrayPakFlat<TText>* pPakFlat=new CArrayPakFlat<TText>(KTestGranularity);
   303 	testAllMethods(*pPakFlat);
   304     delete pPakFlat;
   305 //
   306 	CArrayPakFlat<TText>* pPakFlatChar=new CArrayPakFlat<TText>(KTestGranularity);
   307 	test1(*pPakFlatChar);
   308 	delete pPakFlatChar; 
   309 //
   310  	CArrayPakFlat<TText>* pPakFlatArr=new CArrayPakFlat<TText>(KTestGranularity);
   311 	test2(*pPakFlatArr);
   312 	delete pPakFlatArr;
   313 //
   314 	CArrayPakFlat<TInt>* pPakFlatInt=new CArrayPakFlat<TInt>(KTestGranularity);
   315 	test3(*pPakFlatInt);
   316 	delete pPakFlatInt;
   317 //
   318 	test.End();
   319 	__UHEAP_MARKEND;
   320 	return(0);
   321     }
   322