symport/e32test/buffer/t_parray.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_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 <e32base.h>
       
    63 #include <e32test.h>
       
    64 #include <e32svr.h>
       
    65 #include <e32ver.h>
       
    66 
       
    67 const TInt KTestGranularity=0x10;
       
    68 
       
    69 LOCAL_D RTest test(_L("T_PARRAY"));
       
    70 
       
    71 LOCAL_C void testAllMethods(CArrayPak<TText>& aPakVar)
       
    72     {
       
    73 	test.Next(_L("Test all methods"));
       
    74 	test(aPakVar.Count()==0);
       
    75 	aPakVar.Compress();
       
    76 	test(TRUE);
       
    77 	aPakVar.Reset();
       
    78 	test(TRUE);
       
    79 	TKeyArrayPak kk(sizeof(TText),ECmpNormal,0);
       
    80 	TKeyArrayVar vv(sizeof(TText),ECmpNormal,0);
       
    81 	test(TRUE);
       
    82 	TRAPD(res,aPakVar.SortL(vv));
       
    83 	test(res==KErrNone);
       
    84 	const TText* aa=_S("a");
       
    85 	aPakVar.InsertL(0,*aa,sizeof(TText));
       
    86 	TBuf<0x10> des1(1);
       
    87 	des1[0]=aPakVar[0];
       
    88 	test(des1==_L("a"));
       
    89 	test(aPakVar.Length(0)==sizeof(TText));
       
    90 	test(TRUE);
       
    91 	TInt pp;
       
    92 	test(aPakVar.Find(*aa,kk,pp)==0);
       
    93 	test(pp==0);
       
    94 	aPakVar.Delete(0);
       
    95 	aPakVar.AppendL(*aa,1);
       
    96 	test(aPakVar.Count()==1);
       
    97 	aPakVar.InsertIsqAllowDuplicatesL(*aa,0,kk);
       
    98 	test(TRUE);
       
    99 	test(aPakVar.FindIsq(*aa,kk,pp)==0);
       
   100 	test(pp==0);
       
   101 	TRAPD(r,aPakVar.InsertIsqL(*aa,0,kk));
       
   102 	test(r==KErrAlreadyExists);
       
   103 	}
       
   104 
       
   105 LOCAL_C void test1(CArrayPak<TText>& aPakVar)
       
   106 //
       
   107 	{
       
   108 	test.Next(_L("AppendL and InsertL chars"));
       
   109 	aPakVar.AppendL(*_S("abcd"),5*sizeof(TText)); // abcd
       
   110 	TBuf<0x10> des1(&aPakVar[0]);
       
   111 	test(des1==_L("abcd"));
       
   112 	test(aPakVar.Count()==1);
       
   113 	aPakVar.AppendL(*_S("wxyz"),5*sizeof(TText)); // abcd wxyz
       
   114 	des1=&aPakVar[1];
       
   115 	test(des1==_L("wxyz"));
       
   116 	test(aPakVar.Count()==2);
       
   117 	aPakVar.InsertL(1,*_S("ef"),3*sizeof(TText)); // abcd ef wxyz
       
   118 	des1=&aPakVar[1];
       
   119 	test(des1==_L("ef"));
       
   120 	test(aPakVar.Count()==3);	
       
   121 	aPakVar.AppendL(*_S("z"),2*sizeof(TText)); // abcd ef wxyz z
       
   122 	des1=&aPakVar[3];
       
   123 	test(des1==_L("z"));
       
   124 	aPakVar.InsertL(0,*_S("y"),2*sizeof(TText)); // y abcd ef wxyz z
       
   125 	des1=&aPakVar[0];
       
   126 	test(des1==_L("y"));
       
   127 	test(aPakVar.Length(0)==2*sizeof(TText));
       
   128 	test(aPakVar.Length(1)==5*sizeof(TText));
       
   129 	test(aPakVar.Length(2)==3*sizeof(TText));
       
   130 	test(aPakVar.Length(3)==5*sizeof(TText));
       
   131 	test(aPakVar.Length(4)==2*sizeof(TText));
       
   132 	des1=&aPakVar[1];
       
   133 	test(des1==_L("abcd"));
       
   134 	test(aPakVar.Count()==5);
       
   135 //
       
   136 	test.Next(_L("Delete chars"));
       
   137 	aPakVar.Delete(3,1); // y abcd ef z
       
   138 	des1=&aPakVar[2];
       
   139 	test(des1==_L("ef"));
       
   140 	des1=&aPakVar[1];
       
   141 	test(des1==_L("abcd"));
       
   142 	des1=&aPakVar[3];
       
   143 	test(des1==_L("z"));
       
   144 	aPakVar.Delete(1,2); // y z
       
   145 	des1=&aPakVar[0];
       
   146 	test(des1==_L("y"));
       
   147 	des1=&aPakVar[1];
       
   148 	test(des1==_L("z"));
       
   149 	test(aPakVar.Count()==2);
       
   150 	}
       
   151 
       
   152 LOCAL_C void test2(CArrayPak<TText>& aPakVar)
       
   153 //
       
   154 	{
       
   155 	test.Next(_L("Reset and Compress"));
       
   156 	TBuf<0x10> des1(_L("abcde"));
       
   157 	TBuf<0x10> des2(_L("fgh"));
       
   158 	TBuf<0x10> des3(_L("wxyz"));
       
   159 	aPakVar.AppendL(*(TText*)des1.Ptr(),des1.Size());
       
   160 	aPakVar.AppendL(*(TText*)des2.Ptr(),des2.Size());
       
   161 	aPakVar.Compress();
       
   162 	test(aPakVar.Count()==2);
       
   163 	TPtrC des4((TText*)&aPakVar[0],des1.Length());
       
   164 	test(des1==des4);
       
   165 	TPtrC des5((TText*)&aPakVar[1],des2.Length());
       
   166 	test(des2==des5);
       
   167 	aPakVar.InsertL(1,*(TText*)des3.Ptr(),des3.Size());
       
   168 	test(aPakVar.Count()==3);	
       
   169 	TPtrC des6((TText*)&aPakVar[0],des1.Length());
       
   170 	test(des1==des6);
       
   171 	TPtrC des7((TText*)&aPakVar[2],des2.Length());
       
   172 	test(des2==des7);
       
   173 	TPtrC des8((TText*)&aPakVar[1],des3.Length());
       
   174 	test(des3==des8);
       
   175 	aPakVar.Reset();
       
   176 	// So nothing in this array
       
   177 	test(aPakVar.Count()==0);
       
   178 	TKeyArrayPak kk(0,ECmpNormal,3);		// Compare 3 characters
       
   179 	TKeyArrayPak kk1(0,ECmpNormal,2);	// Compare 2 characters
       
   180 	TKeyArrayVar vv(0,ECmpNormal,3);		// Compare 3 characters
       
   181 	TBuf<0x10> buf1=_L("abcdef");
       
   182 	TBuf<0x10> buf2=_L("wxyz");
       
   183 	TBuf<0x10> buf3=_L("lmnop");
       
   184 	TBuf<0x10> buf4=_L("aa");
       
   185 	aPakVar.AppendL(*buf1.Ptr(),buf1.Size());
       
   186 	aPakVar.InsertL(0,*buf2.Ptr(),buf2.Size());
       
   187 	aPakVar.AppendL(*buf3.Ptr(),buf3.Size());
       
   188 	aPakVar.InsertL(1,*buf4.Ptr(),buf4.Size());
       
   189 	aPakVar.Compress();
       
   190 	TPtrC rd1((TText*)&aPakVar[2],buf1.Length());
       
   191 	test(buf1==rd1);
       
   192 	TPtrC rd2((TText*)&aPakVar[0],buf2.Length());
       
   193 	test(buf2==rd2);
       
   194 	TPtrC rd3((TText*)&aPakVar[3],buf3.Length());
       
   195 	test(buf3==rd3);
       
   196 	TPtrC rd4((TText*)&aPakVar[1],buf4.Length());
       
   197 	test(buf4==rd4);
       
   198 	test(aPakVar.Count()==4);
       
   199 
       
   200 	test.Next(_L("Sort"));
       
   201 	TRAPD(res,aPakVar.SortL(vv));
       
   202 	test(res==KErrNone);
       
   203 	/**/
       
   204 	TPtrC rd5((TText*)&aPakVar[1],buf1.Length());
       
   205 	test(buf1==rd5);
       
   206 	TPtrC rd6((TText*)&aPakVar[3],buf2.Length());
       
   207 	test(buf2==rd6);
       
   208 	TPtrC rd7((TText*)&aPakVar[2],buf3.Length());
       
   209 	test(buf3==rd7);
       
   210 	TPtrC rd8((TText*)&aPakVar[0],buf4.Length());
       
   211 	test(buf4==rd8);
       
   212 	test(aPakVar.Count()==4);	
       
   213 	/**/
       
   214 	test.Next(_L("Find and FindIsq"));
       
   215 	TBuf<0x10> buf5=_L("ffff");
       
   216 	test(aPakVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==2);
       
   217 	TRAPD(r,aPakVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk))
       
   218 	test(r==KErrAlreadyExists);
       
   219 	test(aPakVar.InsertIsqAllowDuplicatesL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==3);
       
   220 	TInt aPos;
       
   221 	test(aPakVar.Find(*_S("abc"),kk,aPos)==0);	// Second parameter 'aLength' is unused. 
       
   222 	test(aPos==1);
       
   223 	test(aPakVar.Find(*_S("aa"),kk1,aPos)==0);
       
   224 	test(aPos==0);
       
   225 	test(aPakVar.Find(*_S("wxyz"),kk,aPos)==0);
       
   226 	test(aPos==5);
       
   227 	test(aPakVar.Find(*_S("fgh"),kk,aPos)!=0);		// Returns non zero if string not found.
       
   228 	test(aPos==6);								// Not present in list, aPos set to last position
       
   229 	test(aPakVar.Find(*_S("ffff"),kk,aPos)==0);	
       
   230 	test(aPos==2);
       
   231 	test(aPakVar.Find(*_S("lmn"),kk,aPos)==0);
       
   232 	test(aPos==4);			  //15
       
   233 	test(aPakVar.FindIsq(*_S("abc"),kk,aPos)==0);
       
   234 	test(aPos==1);
       
   235 	test(aPakVar.FindIsq(*_S("aa"),kk1,aPos)==0);
       
   236 	test(aPos==0);
       
   237 	test(aPakVar.FindIsq(*_S("wxyz"),kk,aPos)==0);
       
   238 	test(aPakVar.FindIsq(*_S("fgh"),kk,aPos)!=0);	// 22 Returns result of last test
       
   239 	test(aPos==4);		// Not present in list, aPos set to last position tested
       
   240 	//This test shows problem with BinarySearch
       
   241 	TBuf<0x10> buf7=_L("fghij");
       
   242 	test(aPakVar.InsertIsqL(*(TText*)buf7.Ptr(),buf7.Size(),kk)==4);
       
   243 	test(aPakVar.FindIsq(*_S("fgh"),kk,aPos)==0);	// Returns result of last test
       
   244 	test(aPos==4);
       
   245 	test(aPakVar.FindIsq(*_S("ffff"),kk,aPos)==0);
       
   246 	test(aPos==3);
       
   247 	test(aPakVar.FindIsq(*_S("lmn"),kk,aPos)==0);
       
   248 	test(aPos==5); 
       
   249 	aPakVar.Delete(4,1);
       
   250 	test(aPakVar.Find(*_S("wxyz"),kk,aPos)==0);
       
   251 	test(aPos==5);
       
   252 	}
       
   253 
       
   254 LOCAL_C void test3(CArrayPak<TInt>& aPak)
       
   255 
       
   256 	{
       
   257 	test.Next(_L("InsertIsqL"));
       
   258 	TKeyArrayPak 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,aPak.InsertIsqL(i,sizeof(TInt),kk))
       
   270 		else
       
   271 			{
       
   272 			TRAP(ret,pos=aPak.InsertIsqL(i,sizeof(TInt),kk))
       
   273 			if (ret==KErrNone)
       
   274 				test(aPak[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(aPak.FindIsq(i,kk,pos)==0);
       
   284 		test(pos==i);
       
   285 		TRAPD(r,aPak.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 // Initially test only covers the Flat type implementation.
       
   294 //
       
   295     {
       
   296 	test.Title();
       
   297 	__UHEAP_MARK;
       
   298 	test.Start(_L("class CArrayPakFlat"));
       
   299 //
       
   300     CArrayPakFlat<TText>* pPakFlat=new CArrayPakFlat<TText>(KTestGranularity);
       
   301 	testAllMethods(*pPakFlat);
       
   302     delete pPakFlat;
       
   303 //
       
   304 	CArrayPakFlat<TText>* pPakFlatChar=new CArrayPakFlat<TText>(KTestGranularity);
       
   305 	test1(*pPakFlatChar);
       
   306 	delete pPakFlatChar; 
       
   307 //
       
   308  	CArrayPakFlat<TText>* pPakFlatArr=new CArrayPakFlat<TText>(KTestGranularity);
       
   309 	test2(*pPakFlatArr);
       
   310 	delete pPakFlatArr;
       
   311 //
       
   312 	CArrayPakFlat<TInt>* pPakFlatInt=new CArrayPakFlat<TInt>(KTestGranularity);
       
   313 	test3(*pPakFlatInt);
       
   314 	delete pPakFlatInt;
       
   315 //
       
   316 	test.End();
       
   317 	__UHEAP_MARKEND;
       
   318 	return(0);
       
   319     }
       
   320