sl@0: // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\buffer\t_varray.cpp sl@0: // Overview: sl@0: // Test variable record length array classes. sl@0: // API Information: sl@0: // CArrayVarFlat, CArrayVarSeg. sl@0: // Details: sl@0: // - Create an array of variable length text using a flat dynamic & segmented sl@0: // buffer and verify that: sl@0: // - number of elements held in the array is 0. sl@0: // - array is compressed and reset as expected. sl@0: // - the elements of the array are sorted as expected. sl@0: // - insertion of a text character into the array at specified position sl@0: // and length of the array is as expected. sl@0: // - return value is 0 when available element is searched using sequential sl@0: // search technique within the array. sl@0: // - removal of first element from the array is successful. sl@0: // - number of elements held in the array is 1 after appending a single sl@0: // element at the end of empty array. sl@0: // - insertion of a single element with specified key is successful. sl@0: // - the element is found when searched using binary search technique sl@0: // - KErrAlreadyExists is returned if an element is inserted with the same sl@0: // key already exists within the array. sl@0: // - Create an array of variable length text character implemented using a flat sl@0: // dynamic & segmented buffer. sl@0: // - append some strings onto the end of the array, check the contents and sl@0: // number of elements held in the array are as expected. sl@0: // - insert some strings and verify that the change in the content of array sl@0: // and number of elements held in the array are as expected. sl@0: // - remove a single, multiple elements from the array and verify that the sl@0: // Delete method is as expected. sl@0: // - Create an array of variable length text character contained within a flat sl@0: // dynamic & segmented buffer. sl@0: // - append some strings of specified length onto the end of the array, compress sl@0: // the array and verify that the number of elements held in the array is as specified. sl@0: // - insert a string at specified location, check the contents and reset the array. sl@0: // - append some strings at the end, insert some strings at specified position, sl@0: // compress the array and verify that content, number of strings in the array sl@0: // are as expected. sl@0: // - test that the number of elements and contents of the array are sorted as expected. sl@0: // - verify the correct position of the element and return value is zero when sl@0: // an element is found using binary, sequential search technique and nonzero sl@0: // if not present in the array. sl@0: // - insert some strings at the specified location and check that correct position sl@0: // is returned when searched using binary search technique. sl@0: // - Create an array of variable length integer contained within a flat dynamic & sl@0: // segmented buffer. sl@0: // - insert some elements with same key which is already present within the array sl@0: // and check that KErrAlreadyExists is returned. sl@0: // - Test whether the heap has been corrupted by all the tests. sl@0: // Platforms/Drives/Compatibility: sl@0: // All sl@0: // Assumptions/Requirement/Pre-requisites: sl@0: // Failures and causes: sl@0: // Base Port information: sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: const TInt KTestGranularity=0x02; sl@0: sl@0: LOCAL_D RTest test(_L("T_VARRAY")); sl@0: sl@0: LOCAL_C void testAllMethods(CArrayVar& aVar) sl@0: { sl@0: test.Next(_L("Test all methods")); sl@0: test(aVar.Count()==0); sl@0: aVar.Compress(); sl@0: test(TRUE); sl@0: aVar.Reset(); sl@0: test(TRUE); sl@0: TKeyArrayVar kk(sizeof(TText),ECmpNormal,0); sl@0: test(TRUE); sl@0: aVar.Sort(kk); sl@0: test(TRUE); sl@0: const TText* aa=_S("a"); sl@0: aVar.InsertL(0,*aa,sizeof(TText)); sl@0: test(aVar.Length(0)==sizeof(TText)); sl@0: test(TRUE); sl@0: TInt pp; sl@0: test(aVar.Find(*aa,kk,pp)==0); sl@0: test(pp==0); sl@0: aVar.Delete(0); sl@0: aVar.AppendL(*aa,1); sl@0: test(aVar.Count()==1); sl@0: aVar.InsertIsqAllowDuplicatesL(*aa,0,kk); sl@0: test(TRUE); sl@0: test(aVar.FindIsq(*aa,kk,pp)==0); sl@0: test(pp==0); sl@0: TRAPD(r,aVar.InsertIsqL(*aa,0,kk)); sl@0: test(r==KErrAlreadyExists); sl@0: } sl@0: sl@0: LOCAL_C void test1(CArrayVar& aVar) sl@0: // sl@0: { sl@0: test.Next(_L("AppendL and InsertL chars")); sl@0: aVar.AppendL(*_S("abcd"),5*sizeof(TText)); // abcd sl@0: TBuf<0x10> des1(&aVar[0]); sl@0: test(des1==_L("abcd")); sl@0: test(aVar.Count()==1); sl@0: aVar.AppendL(*_S("wxyz"),5*sizeof(TText)); // abcd wxyz sl@0: des1=&aVar[1]; sl@0: test(des1==_L("wxyz")); sl@0: test(aVar.Count()==2); sl@0: aVar.InsertL(1,*_S("ef"),3*sizeof(TText)); // abcd ef wxyz sl@0: des1=&aVar[1]; sl@0: test(des1==_L("ef")); sl@0: test(aVar.Count()==3); sl@0: aVar.AppendL(*_S("z"),2*sizeof(TText)); // abcd ef wxyz z sl@0: des1=&aVar[3]; sl@0: test(des1==_L("z")); sl@0: aVar.InsertL(0,*_S("y"),2*sizeof(TText)); // y abcd ef wxyz z sl@0: des1=&aVar[0]; sl@0: test(des1==_L("y")); sl@0: test(aVar.Length(0)==2*sizeof(TText)); sl@0: test(aVar.Length(1)==5*sizeof(TText)); sl@0: test(aVar.Length(2)==3*sizeof(TText)); sl@0: test(aVar.Length(3)==5*sizeof(TText)); sl@0: test(aVar.Length(4)==2*sizeof(TText)); sl@0: des1=&aVar[1]; sl@0: test(des1==_L("abcd")); sl@0: test(aVar.Count()==5); sl@0: // sl@0: test.Next(_L("Delete chars")); sl@0: aVar.Delete(3,1); // y abcd ef z sl@0: des1=&aVar[2]; sl@0: test(des1==_L("ef")); sl@0: des1=&aVar[1]; sl@0: test(des1==_L("abcd")); sl@0: des1=&aVar[3]; sl@0: test(des1==_L("z")); sl@0: aVar.Delete(1,2); // y z sl@0: des1=&aVar[0]; sl@0: test(des1==_L("y")); sl@0: des1=&aVar[1]; sl@0: test(des1==_L("z")); sl@0: test(aVar.Count()==2); sl@0: } sl@0: sl@0: LOCAL_C void test2(CArrayVar& aVar) sl@0: // sl@0: { sl@0: test.Next(_L("Reset and Compress")); sl@0: TBuf<0x10> des1(_L("abcde")); sl@0: TBuf<0x10> des2(_L("fgh")); sl@0: TBuf<0x10> des3(_L("wxyz")); sl@0: aVar.AppendL(*(TText*)des1.Ptr(),des1.Size()); sl@0: aVar.AppendL(*(TText*)des2.Ptr(),des2.Size()); sl@0: aVar.Compress(); sl@0: test(aVar.Count()==2); sl@0: TPtrC des4((TText*)&aVar[0],des1.Length()); sl@0: test(des1==des4); sl@0: TPtrC des5((TText*)&aVar[1],des2.Length()); sl@0: test(des2==des5); sl@0: aVar.InsertL(1,*(TText*)des3.Ptr(),des3.Size()); sl@0: test(aVar.Count()==3); sl@0: TPtrC des6((TText*)&aVar[0],des1.Length()); sl@0: test(des1==des6); sl@0: TPtrC des7((TText*)&aVar[2],des2.Length()); sl@0: test(des2==des7); sl@0: TPtrC des8((TText*)&aVar[1],des3.Length()); sl@0: test(des3==des8); sl@0: aVar.Reset(); sl@0: sl@0: TBuf<0x10> buf1=_L("abcdef"); sl@0: TBuf<0x10> buf2=_L("wxyz"); sl@0: TBuf<0x10> buf3=_L("lmnop"); sl@0: TBuf<0x10> buf4=_L("aa"); sl@0: aVar.AppendL(*buf1.Ptr(),buf1.Size()); sl@0: aVar.InsertL(0,*buf2.Ptr(),buf2.Size()); sl@0: aVar.AppendL(*buf3.Ptr(),buf3.Size()); sl@0: aVar.InsertL(1,*buf4.Ptr(),buf4.Size()); sl@0: aVar.Compress(); sl@0: TPtrC rd1((TText*)&aVar[2],buf1.Length()); sl@0: test(buf1==rd1); sl@0: TPtrC rd2((TText*)&aVar[0],buf2.Length()); sl@0: test(buf2==rd2); sl@0: TPtrC rd3((TText*)&aVar[3],buf3.Length()); sl@0: test(buf3==rd3); sl@0: TPtrC rd4((TText*)&aVar[1],buf4.Length()); sl@0: test(buf4==rd4); sl@0: test(aVar.Count()==4); sl@0: sl@0: TKeyArrayVar kk(0,ECmpNormal,3); // Compare 3 characters sl@0: TKeyArrayVar kk1(0,ECmpNormal,2); // Compare 2 characters sl@0: test.Next(_L("Sort")); sl@0: aVar.Sort(kk); sl@0: TPtrC rd5((TText*)&aVar[1],buf1.Length()); sl@0: test(buf1==rd5); sl@0: TPtrC rd6((TText*)&aVar[3],buf2.Length()); sl@0: test(buf2==rd6); sl@0: TPtrC rd7((TText*)&aVar[2],buf3.Length()); sl@0: test(buf3==rd7); sl@0: TPtrC rd8((TText*)&aVar[0],buf4.Length()); sl@0: test(buf4==rd8); sl@0: test(aVar.Count()==4); sl@0: sl@0: test.Next(_L("Find and FindIsq")); sl@0: TBuf<0x10> buf5=_L("ffff"); sl@0: test(aVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==2); sl@0: TRAPD(r,aVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk)) sl@0: test(r==KErrAlreadyExists); sl@0: test(aVar.InsertIsqAllowDuplicatesL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==3); sl@0: TInt aPos; sl@0: test(aVar.Find(*_S("abc"),kk,aPos)==0); // Second parameter 'aLength' is unused. sl@0: test(aPos==1); sl@0: test(aVar.Find(*_S("aa"),kk1,aPos)==0); sl@0: test(aPos==0); sl@0: test(aVar.Find(*_S("wxyz"),kk,aPos)==0); sl@0: test(aPos==5); sl@0: test(aVar.Find(*_S("fgh"),kk,aPos)!=0); // Returns !=0 if string not found. sl@0: test(aPos==6); // Not present in list, aPos set to last position sl@0: test(aVar.Find(*_S("ffff"),kk,aPos)==0); sl@0: test(aPos==2); sl@0: test(aVar.Find(*_S("lmn"),kk,aPos)==0); sl@0: test(aPos==4); sl@0: test(aVar.FindIsq(*_S("abc"),kk,aPos)==0); sl@0: test(aPos==1); sl@0: test(aVar.FindIsq(*_S("aa"),kk1,aPos)==0); sl@0: test(aPos==0); sl@0: test(aVar.FindIsq(*_S("wxyz"),kk,aPos)==0); sl@0: test(aPos==5); sl@0: test(aVar.FindIsq(*_S("fgh"),kk,aPos)!=0); // Returns result of last test sl@0: test(aPos==4); // Not present in list, aPos set to last position tested sl@0: TBuf<0x10> buf7=_L("fgh"); sl@0: test(aVar.InsertIsqL(*(TText*)buf7.Ptr(),buf7.Size(),kk)==4); sl@0: test(aVar.FindIsq(*_S("fgh"),kk,aPos)==0); // Returns result of last test sl@0: test(aPos==4); sl@0: sl@0: test(aVar.FindIsq(*_S("ffff"),kk,aPos)==0); sl@0: test(aPos==3); sl@0: test(aVar.FindIsq(*_S("lmn"),kk,aPos)==0); sl@0: test(aPos==5); sl@0: } sl@0: sl@0: LOCAL_C void test3(CArrayVar& aVar) sl@0: { sl@0: sl@0: test.Next(_L("InsertIsqL")); sl@0: TKeyArrayVar kk(0,ECmpTInt); sl@0: sl@0: TInt pos=0; sl@0: TInt mod=47; sl@0: TInt inc=23; sl@0: sl@0: TInt i=0; sl@0: FOREVER sl@0: { sl@0: TInt ret; sl@0: if (i&1) sl@0: TRAP(ret,aVar.InsertIsqL(i,sizeof(TInt),kk)) sl@0: else sl@0: { sl@0: TRAP(ret,pos=aVar.InsertIsqL(i,sizeof(TInt),kk)) sl@0: if (ret==KErrNone) sl@0: test(aVar[pos]==i); sl@0: } sl@0: if (ret==KErrAlreadyExists) sl@0: break; sl@0: i=(i+inc)%mod; sl@0: } sl@0: for(i=0;i* pVarFlat=new CArrayVarFlat(KTestGranularity); sl@0: if (pVarFlat==NULL) sl@0: test.Panic(_L("Allocating array")); sl@0: testAllMethods(*pVarFlat); sl@0: delete pVarFlat; sl@0: // sl@0: CArrayVarFlat* pVarFlatChar=new CArrayVarFlat(KTestGranularity); sl@0: test1(*pVarFlatChar); sl@0: delete pVarFlatChar; sl@0: // sl@0: CArrayVarFlat* pVarFlatArr=new CArrayVarFlat(KTestGranularity); sl@0: test2(*pVarFlatArr); sl@0: delete pVarFlatArr; sl@0: // sl@0: CArrayVarFlat* pVarFlatInt=new CArrayVarFlat(KTestGranularity); sl@0: test3(*pVarFlatInt); sl@0: delete pVarFlatInt; sl@0: // sl@0: test.Next(_L("class CArrayVarSeg")); sl@0: CArrayVarSeg* pVarSeg=new CArrayVarSeg(KTestGranularity); sl@0: if (pVarSeg==NULL) sl@0: test.Panic(_L("Allocating array")); sl@0: testAllMethods(*pVarSeg); sl@0: delete pVarSeg; sl@0: // sl@0: CArrayVarSeg* pVarSegChar=new CArrayVarSeg(KTestGranularity); sl@0: test1(*pVarSegChar); sl@0: delete pVarSegChar; sl@0: // sl@0: CArrayVarSeg* pVarSegArr=new CArrayVarSeg(KTestGranularity); sl@0: test2(*pVarSegArr); sl@0: delete pVarSegArr; sl@0: // sl@0: CArrayVarSeg* pVarSegInt=new CArrayVarSeg(KTestGranularity); sl@0: test3(*pVarSegInt); sl@0: delete pVarSegInt; sl@0: // sl@0: test.End(); sl@0: __UHEAP_MARKEND; sl@0: return(0); sl@0: } sl@0: