os/kernelhwsrv/kerneltest/e32test/buffer/t_varray.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/buffer/t_varray.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,343 @@
     1.4 +// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\buffer\t_varray.cpp
    1.18 +// Overview:
    1.19 +// Test variable record length array classes.
    1.20 +// API Information:
    1.21 +// CArrayVarFlat, CArrayVarSeg.
    1.22 +// Details:
    1.23 +// - Create an array of variable length text using a flat dynamic & segmented
    1.24 +// buffer and verify that:
    1.25 +// - number of elements held in the array is 0.
    1.26 +// - array is compressed and reset as expected.
    1.27 +// - the elements of the array are sorted as expected.
    1.28 +// - insertion of a text character into the array at specified position 
    1.29 +// and length of the array is as expected.
    1.30 +// - return value is 0 when available element is searched using sequential 
    1.31 +// search technique within the array.
    1.32 +// - removal of first element from the array is successful.
    1.33 +// - number of elements held in the array is 1 after appending a single 
    1.34 +// element at the end of empty array.
    1.35 +// - insertion of a single element with specified key is successful.
    1.36 +// - the element is found when searched using binary search technique
    1.37 +// - KErrAlreadyExists is returned if an element is inserted with the same 
    1.38 +// key already exists within the array.
    1.39 +// - Create an array of variable length text character implemented using a flat 
    1.40 +// dynamic & segmented buffer.
    1.41 +// - append some strings onto the end of the array, check the contents and 
    1.42 +// number of elements held in the array are as expected.
    1.43 +// - insert some strings and verify that the change in the content of array 
    1.44 +// and number of elements held in the array are as expected.
    1.45 +// - remove a single, multiple elements from the array and verify that the 
    1.46 +// Delete method is as expected.
    1.47 +// - Create an array of variable length text character contained within a flat 
    1.48 +// dynamic & segmented buffer.
    1.49 +// - append some strings of specified length onto the end of the array, compress 
    1.50 +// the array and verify that the number of elements held in the array is as specified.
    1.51 +// - insert a string at specified location, check the contents and reset the array.
    1.52 +// - append some strings at the end, insert some strings at specified position, 
    1.53 +// compress the array and verify that content, number of strings in the array 
    1.54 +// are as expected.
    1.55 +// - test that the number of elements and contents of the array are sorted as expected.
    1.56 +// - verify the correct position of the element and return value is zero when 
    1.57 +// an element is found using binary, sequential search technique and nonzero 
    1.58 +// if not present in the array.
    1.59 +// - insert some strings at the specified location and check that correct position
    1.60 +// is returned when searched using binary search technique.
    1.61 +// - Create an array of variable length integer contained within a flat dynamic &
    1.62 +// segmented buffer.
    1.63 +// - insert some elements with same key which is already present within the array 
    1.64 +// and check that KErrAlreadyExists is returned. 
    1.65 +// - Test whether the heap has been corrupted by all the tests.
    1.66 +// Platforms/Drives/Compatibility:
    1.67 +// All 
    1.68 +// Assumptions/Requirement/Pre-requisites:
    1.69 +// Failures and causes:
    1.70 +// Base Port information:
    1.71 +// 
    1.72 +//
    1.73 +
    1.74 +#include <e32std.h>
    1.75 +#include <e32std_private.h>
    1.76 +#include <e32base.h>
    1.77 +#include <e32base_private.h>
    1.78 +#include <e32test.h>
    1.79 +#include <e32svr.h>
    1.80 +#include <e32ver.h>
    1.81 +
    1.82 +const TInt KTestGranularity=0x02;
    1.83 +
    1.84 +LOCAL_D RTest test(_L("T_VARRAY"));
    1.85 +
    1.86 +LOCAL_C void testAllMethods(CArrayVar<TText>& aVar)
    1.87 +    {
    1.88 +	test.Next(_L("Test all methods"));
    1.89 +	test(aVar.Count()==0);
    1.90 +	aVar.Compress();
    1.91 +	test(TRUE);
    1.92 +	aVar.Reset();
    1.93 +	test(TRUE);
    1.94 +	TKeyArrayVar kk(sizeof(TText),ECmpNormal,0);
    1.95 +	test(TRUE);
    1.96 +	aVar.Sort(kk);
    1.97 +	test(TRUE);
    1.98 +	const TText* aa=_S("a");
    1.99 +	aVar.InsertL(0,*aa,sizeof(TText));
   1.100 +	test(aVar.Length(0)==sizeof(TText));
   1.101 +	test(TRUE);
   1.102 +	TInt pp;
   1.103 +	test(aVar.Find(*aa,kk,pp)==0);
   1.104 +	test(pp==0);
   1.105 +	aVar.Delete(0);
   1.106 +	aVar.AppendL(*aa,1);
   1.107 +	test(aVar.Count()==1);
   1.108 +	aVar.InsertIsqAllowDuplicatesL(*aa,0,kk);
   1.109 +	test(TRUE);
   1.110 +	test(aVar.FindIsq(*aa,kk,pp)==0);
   1.111 +	test(pp==0);
   1.112 +	TRAPD(r,aVar.InsertIsqL(*aa,0,kk));
   1.113 +	test(r==KErrAlreadyExists);
   1.114 +    }
   1.115 +
   1.116 +LOCAL_C void test1(CArrayVar<TText>& aVar)
   1.117 +//
   1.118 +	{
   1.119 +	test.Next(_L("AppendL and InsertL chars"));
   1.120 +	aVar.AppendL(*_S("abcd"),5*sizeof(TText)); // abcd
   1.121 +	TBuf<0x10> des1(&aVar[0]);
   1.122 +	test(des1==_L("abcd"));
   1.123 +	test(aVar.Count()==1);
   1.124 +	aVar.AppendL(*_S("wxyz"),5*sizeof(TText)); // abcd wxyz
   1.125 +	des1=&aVar[1];
   1.126 +	test(des1==_L("wxyz"));
   1.127 +	test(aVar.Count()==2);
   1.128 +	aVar.InsertL(1,*_S("ef"),3*sizeof(TText)); // abcd ef wxyz
   1.129 +	des1=&aVar[1];
   1.130 +	test(des1==_L("ef"));
   1.131 +	test(aVar.Count()==3);	
   1.132 +	aVar.AppendL(*_S("z"),2*sizeof(TText)); // abcd ef wxyz z
   1.133 +	des1=&aVar[3];
   1.134 +	test(des1==_L("z"));
   1.135 +	aVar.InsertL(0,*_S("y"),2*sizeof(TText)); // y abcd ef wxyz z
   1.136 +	des1=&aVar[0];
   1.137 +	test(des1==_L("y"));
   1.138 +	test(aVar.Length(0)==2*sizeof(TText));
   1.139 +	test(aVar.Length(1)==5*sizeof(TText));
   1.140 +	test(aVar.Length(2)==3*sizeof(TText));
   1.141 +	test(aVar.Length(3)==5*sizeof(TText));
   1.142 +	test(aVar.Length(4)==2*sizeof(TText));
   1.143 +	des1=&aVar[1];
   1.144 +	test(des1==_L("abcd"));
   1.145 +	test(aVar.Count()==5);
   1.146 +//
   1.147 +	test.Next(_L("Delete chars"));
   1.148 +	aVar.Delete(3,1); // y abcd ef z
   1.149 +	des1=&aVar[2];
   1.150 +	test(des1==_L("ef"));
   1.151 +	des1=&aVar[1];
   1.152 +	test(des1==_L("abcd"));
   1.153 +	des1=&aVar[3];
   1.154 +	test(des1==_L("z"));
   1.155 +	aVar.Delete(1,2); // y z
   1.156 +	des1=&aVar[0];
   1.157 +	test(des1==_L("y"));
   1.158 +	des1=&aVar[1];
   1.159 +	test(des1==_L("z"));
   1.160 +	test(aVar.Count()==2);
   1.161 +	}
   1.162 +
   1.163 +LOCAL_C void test2(CArrayVar<TText>& aVar)
   1.164 +//
   1.165 +	{
   1.166 +	test.Next(_L("Reset and Compress"));
   1.167 +	TBuf<0x10> des1(_L("abcde"));
   1.168 +	TBuf<0x10> des2(_L("fgh"));
   1.169 +	TBuf<0x10> des3(_L("wxyz"));
   1.170 +	aVar.AppendL(*(TText*)des1.Ptr(),des1.Size());
   1.171 +	aVar.AppendL(*(TText*)des2.Ptr(),des2.Size());
   1.172 +	aVar.Compress();
   1.173 +	test(aVar.Count()==2);
   1.174 +	TPtrC des4((TText*)&aVar[0],des1.Length());
   1.175 +	test(des1==des4);
   1.176 +	TPtrC des5((TText*)&aVar[1],des2.Length());
   1.177 +	test(des2==des5);
   1.178 +	aVar.InsertL(1,*(TText*)des3.Ptr(),des3.Size());
   1.179 +	test(aVar.Count()==3);	
   1.180 +	TPtrC des6((TText*)&aVar[0],des1.Length());
   1.181 +	test(des1==des6);
   1.182 +	TPtrC des7((TText*)&aVar[2],des2.Length());
   1.183 +	test(des2==des7);
   1.184 +	TPtrC des8((TText*)&aVar[1],des3.Length());
   1.185 +	test(des3==des8);
   1.186 +	aVar.Reset();
   1.187 +
   1.188 +	TBuf<0x10> buf1=_L("abcdef");
   1.189 +	TBuf<0x10> buf2=_L("wxyz");
   1.190 +	TBuf<0x10> buf3=_L("lmnop");
   1.191 +	TBuf<0x10> buf4=_L("aa");
   1.192 +	aVar.AppendL(*buf1.Ptr(),buf1.Size());
   1.193 +	aVar.InsertL(0,*buf2.Ptr(),buf2.Size());
   1.194 +	aVar.AppendL(*buf3.Ptr(),buf3.Size());
   1.195 +	aVar.InsertL(1,*buf4.Ptr(),buf4.Size());
   1.196 +	aVar.Compress();
   1.197 +	TPtrC rd1((TText*)&aVar[2],buf1.Length());
   1.198 +	test(buf1==rd1);
   1.199 +	TPtrC rd2((TText*)&aVar[0],buf2.Length());
   1.200 +	test(buf2==rd2);
   1.201 +	TPtrC rd3((TText*)&aVar[3],buf3.Length());
   1.202 +	test(buf3==rd3);
   1.203 +	TPtrC rd4((TText*)&aVar[1],buf4.Length());
   1.204 +	test(buf4==rd4);
   1.205 +	test(aVar.Count()==4);
   1.206 +	
   1.207 +	TKeyArrayVar kk(0,ECmpNormal,3);		// Compare 3 characters
   1.208 +	TKeyArrayVar kk1(0,ECmpNormal,2);	// Compare 2 characters
   1.209 +	test.Next(_L("Sort"));
   1.210 +	aVar.Sort(kk);
   1.211 +	TPtrC rd5((TText*)&aVar[1],buf1.Length());
   1.212 +	test(buf1==rd5);
   1.213 +	TPtrC rd6((TText*)&aVar[3],buf2.Length());
   1.214 +	test(buf2==rd6);
   1.215 +	TPtrC rd7((TText*)&aVar[2],buf3.Length());
   1.216 +	test(buf3==rd7);
   1.217 +	TPtrC rd8((TText*)&aVar[0],buf4.Length());
   1.218 +	test(buf4==rd8);
   1.219 +	test(aVar.Count()==4);	
   1.220 +
   1.221 +	test.Next(_L("Find and FindIsq"));
   1.222 +	TBuf<0x10> buf5=_L("ffff");
   1.223 +	test(aVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==2);
   1.224 +	TRAPD(r,aVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk))
   1.225 +	test(r==KErrAlreadyExists);
   1.226 +	test(aVar.InsertIsqAllowDuplicatesL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==3);
   1.227 +	TInt aPos;
   1.228 +	test(aVar.Find(*_S("abc"),kk,aPos)==0);	// Second parameter 'aLength' is unused. 
   1.229 +	test(aPos==1);
   1.230 +	test(aVar.Find(*_S("aa"),kk1,aPos)==0);
   1.231 +	test(aPos==0);
   1.232 +	test(aVar.Find(*_S("wxyz"),kk,aPos)==0);
   1.233 +	test(aPos==5);
   1.234 +	test(aVar.Find(*_S("fgh"),kk,aPos)!=0);		// Returns !=0 if string not found.
   1.235 +	test(aPos==6);								// Not present in list, aPos set to last position
   1.236 +	test(aVar.Find(*_S("ffff"),kk,aPos)==0);	
   1.237 +	test(aPos==2);
   1.238 +	test(aVar.Find(*_S("lmn"),kk,aPos)==0);
   1.239 +	test(aPos==4);
   1.240 +	test(aVar.FindIsq(*_S("abc"),kk,aPos)==0);
   1.241 +	test(aPos==1);
   1.242 +	test(aVar.FindIsq(*_S("aa"),kk1,aPos)==0);
   1.243 +	test(aPos==0);
   1.244 +	test(aVar.FindIsq(*_S("wxyz"),kk,aPos)==0);
   1.245 +	test(aPos==5);
   1.246 +	test(aVar.FindIsq(*_S("fgh"),kk,aPos)!=0);	// Returns result of last test
   1.247 +	test(aPos==4);		  // Not present in list, aPos set to last position tested
   1.248 +	TBuf<0x10> buf7=_L("fgh");
   1.249 +	test(aVar.InsertIsqL(*(TText*)buf7.Ptr(),buf7.Size(),kk)==4);
   1.250 +	test(aVar.FindIsq(*_S("fgh"),kk,aPos)==0);	// Returns result of last test
   1.251 +	test(aPos==4);
   1.252 +									
   1.253 +	test(aVar.FindIsq(*_S("ffff"),kk,aPos)==0);
   1.254 +	test(aPos==3);
   1.255 +	test(aVar.FindIsq(*_S("lmn"),kk,aPos)==0);
   1.256 +	test(aPos==5); 
   1.257 +	}
   1.258 +
   1.259 +LOCAL_C void test3(CArrayVar<TInt>& aVar)
   1.260 +	{
   1.261 +
   1.262 +	test.Next(_L("InsertIsqL"));
   1.263 +	TKeyArrayVar kk(0,ECmpTInt);
   1.264 +
   1.265 +	TInt pos=0;
   1.266 +	TInt mod=47;
   1.267 +	TInt inc=23;
   1.268 +
   1.269 +	TInt i=0;	
   1.270 +	FOREVER
   1.271 +		{
   1.272 +		TInt ret;
   1.273 +		if (i&1)
   1.274 +			TRAP(ret,aVar.InsertIsqL(i,sizeof(TInt),kk))
   1.275 +		else
   1.276 +			{
   1.277 +			TRAP(ret,pos=aVar.InsertIsqL(i,sizeof(TInt),kk))
   1.278 +			if (ret==KErrNone)
   1.279 +				test(aVar[pos]==i);
   1.280 +			}
   1.281 +		if (ret==KErrAlreadyExists)
   1.282 +			break;
   1.283 +		i=(i+inc)%mod;
   1.284 +		}
   1.285 +	for(i=0;i<mod;i++)
   1.286 +		{
   1.287 +		pos=(-1);
   1.288 +		test(aVar.FindIsq(i,kk,pos)==0);
   1.289 +		test(pos==i);
   1.290 +		TRAPD(r,aVar.InsertIsqL(i,sizeof(TInt),kk))
   1.291 +		test(r==KErrAlreadyExists);
   1.292 +		}
   1.293 +	}
   1.294 +
   1.295 +GLDEF_C TInt E32Main()
   1.296 +//
   1.297 +// Test the variable record length array classes.
   1.298 +//
   1.299 +    {
   1.300 +
   1.301 +	test.Title();
   1.302 +	__UHEAP_MARK;
   1.303 +	test.Start(_L("class CArrayFixFlat"));
   1.304 +//
   1.305 +	CArrayVarFlat<TText>* pVarFlat=new CArrayVarFlat<TText>(KTestGranularity);
   1.306 +	if (pVarFlat==NULL)
   1.307 +		test.Panic(_L("Allocating array"));
   1.308 +    testAllMethods(*pVarFlat);
   1.309 +    delete pVarFlat;
   1.310 +//
   1.311 +	CArrayVarFlat<TText>* pVarFlatChar=new CArrayVarFlat<TText>(KTestGranularity);
   1.312 +	test1(*pVarFlatChar);
   1.313 +	delete pVarFlatChar; 
   1.314 +//		
   1.315 +	CArrayVarFlat<TText>* pVarFlatArr=new CArrayVarFlat<TText>(KTestGranularity);
   1.316 +	test2(*pVarFlatArr);
   1.317 +	delete pVarFlatArr;
   1.318 +//
   1.319 +	CArrayVarFlat<TInt>* pVarFlatInt=new CArrayVarFlat<TInt>(KTestGranularity);
   1.320 +	test3(*pVarFlatInt);
   1.321 +	delete pVarFlatInt;
   1.322 +//	
   1.323 +	test.Next(_L("class CArrayVarSeg"));
   1.324 +	CArrayVarSeg<TText>* pVarSeg=new CArrayVarSeg<TText>(KTestGranularity);
   1.325 +	if (pVarSeg==NULL)
   1.326 +		test.Panic(_L("Allocating array"));
   1.327 +	testAllMethods(*pVarSeg);
   1.328 +	delete pVarSeg;
   1.329 +//
   1.330 +	CArrayVarSeg<TText>* pVarSegChar=new CArrayVarSeg<TText>(KTestGranularity);
   1.331 +	test1(*pVarSegChar);
   1.332 +	delete pVarSegChar;
   1.333 +//		
   1.334 +	CArrayVarSeg<TText>* pVarSegArr=new CArrayVarSeg<TText>(KTestGranularity);
   1.335 +	test2(*pVarSegArr);
   1.336 +	delete pVarSegArr;
   1.337 +//
   1.338 +	CArrayVarSeg<TInt>* pVarSegInt=new CArrayVarSeg<TInt>(KTestGranularity);
   1.339 +	test3(*pVarSegInt);
   1.340 +	delete pVarSegInt;
   1.341 +//
   1.342 +	test.End();
   1.343 +	__UHEAP_MARKEND;
   1.344 +	return(0);
   1.345 +    }
   1.346 +