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_parray.cpp sl@0: // Overview: sl@0: // Test the variable record length array classes. sl@0: // Test only covers the Flat type implementation. sl@0: // API Information: sl@0: // CArrayPakFlat. sl@0: // Details: sl@0: // - Create an array of variable length text character objects packed into a flat buffer. sl@0: // - check the number of elements held in the array is 0. sl@0: // - test that Compress and Reset methods are as expected. sl@0: // - sort the array and check that no error is returned. sl@0: // - insert an element and check the length is as expected. sl@0: // - search for the inserted element and check it is found successfully. sl@0: // - remove all the elements, append an element and verify that number of elements sl@0: // held in the array is 1. sl@0: // - insert an element into the array, search for that element using sequential, sl@0: // binary search technique and verify that it is found at the expected position. sl@0: // - insert another element with the same key and check that KErrAlreadyExists is sl@0: // returned. sl@0: // - Create an array of variable length text character objects packed into a flat buffer. sl@0: // - append some strings at the end, insert some strings at the specified location and sl@0: // verify that the length of each string and number of strings held in the array are sl@0: // as expected. sl@0: // - delete some strings and check the remaining strings in the array are as expected. sl@0: // - Create an array of variable length text character objects packed into a flat buffer. sl@0: // - append some strings at the end and insert some stings at specified location. sl@0: // - compress the array and verify strings and the number of strings held in the sl@0: // array are as expected. sl@0: // - reset the array and verify the number of elements held in the array is 0. sl@0: // - sort the array and check that array is sorted as expected. sl@0: // - test that KErrAlreadyExists is returned if another element with the same key sl@0: // type is inserted. sl@0: // - search for strings using sequential, binary search and verify that 0 is returned sl@0: // if found else nonzero. sl@0: // - delete some text from the array and check the results are as expected. sl@0: // - Create an array of variable length integer objects packed into a flat buffer. sl@0: // - Insert some elements with same key which already exists within the array and sl@0: // 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=0x10; sl@0: sl@0: LOCAL_D RTest test(_L("T_PARRAY")); sl@0: sl@0: LOCAL_C void testAllMethods(CArrayPak& aPakVar) sl@0: { sl@0: test.Next(_L("Test all methods")); sl@0: test(aPakVar.Count()==0); sl@0: aPakVar.Compress(); sl@0: test(TRUE); sl@0: aPakVar.Reset(); sl@0: test(TRUE); sl@0: TKeyArrayPak kk(sizeof(TText),ECmpNormal,0); sl@0: TKeyArrayVar vv(sizeof(TText),ECmpNormal,0); sl@0: test(TRUE); sl@0: TRAPD(res,aPakVar.SortL(vv)); sl@0: test(res==KErrNone); sl@0: const TText* aa=_S("a"); sl@0: aPakVar.InsertL(0,*aa,sizeof(TText)); sl@0: TBuf<0x10> des1(1); sl@0: des1[0]=aPakVar[0]; sl@0: test(des1==_L("a")); sl@0: test(aPakVar.Length(0)==sizeof(TText)); sl@0: test(TRUE); sl@0: TInt pp; sl@0: test(aPakVar.Find(*aa,kk,pp)==0); sl@0: test(pp==0); sl@0: aPakVar.Delete(0); sl@0: aPakVar.AppendL(*aa,1); sl@0: test(aPakVar.Count()==1); sl@0: aPakVar.InsertIsqAllowDuplicatesL(*aa,0,kk); sl@0: test(TRUE); sl@0: test(aPakVar.FindIsq(*aa,kk,pp)==0); sl@0: test(pp==0); sl@0: TRAPD(r,aPakVar.InsertIsqL(*aa,0,kk)); sl@0: test(r==KErrAlreadyExists); sl@0: } sl@0: sl@0: LOCAL_C void test1(CArrayPak& aPakVar) sl@0: // sl@0: { sl@0: test.Next(_L("AppendL and InsertL chars")); sl@0: aPakVar.AppendL(*_S("abcd"),5*sizeof(TText)); // abcd sl@0: TBuf<0x10> des1(&aPakVar[0]); sl@0: test(des1==_L("abcd")); sl@0: test(aPakVar.Count()==1); sl@0: aPakVar.AppendL(*_S("wxyz"),5*sizeof(TText)); // abcd wxyz sl@0: des1=&aPakVar[1]; sl@0: test(des1==_L("wxyz")); sl@0: test(aPakVar.Count()==2); sl@0: aPakVar.InsertL(1,*_S("ef"),3*sizeof(TText)); // abcd ef wxyz sl@0: des1=&aPakVar[1]; sl@0: test(des1==_L("ef")); sl@0: test(aPakVar.Count()==3); sl@0: aPakVar.AppendL(*_S("z"),2*sizeof(TText)); // abcd ef wxyz z sl@0: des1=&aPakVar[3]; sl@0: test(des1==_L("z")); sl@0: aPakVar.InsertL(0,*_S("y"),2*sizeof(TText)); // y abcd ef wxyz z sl@0: des1=&aPakVar[0]; sl@0: test(des1==_L("y")); sl@0: test(aPakVar.Length(0)==2*sizeof(TText)); sl@0: test(aPakVar.Length(1)==5*sizeof(TText)); sl@0: test(aPakVar.Length(2)==3*sizeof(TText)); sl@0: test(aPakVar.Length(3)==5*sizeof(TText)); sl@0: test(aPakVar.Length(4)==2*sizeof(TText)); sl@0: des1=&aPakVar[1]; sl@0: test(des1==_L("abcd")); sl@0: test(aPakVar.Count()==5); sl@0: // sl@0: test.Next(_L("Delete chars")); sl@0: aPakVar.Delete(3,1); // y abcd ef z sl@0: des1=&aPakVar[2]; sl@0: test(des1==_L("ef")); sl@0: des1=&aPakVar[1]; sl@0: test(des1==_L("abcd")); sl@0: des1=&aPakVar[3]; sl@0: test(des1==_L("z")); sl@0: aPakVar.Delete(1,2); // y z sl@0: des1=&aPakVar[0]; sl@0: test(des1==_L("y")); sl@0: des1=&aPakVar[1]; sl@0: test(des1==_L("z")); sl@0: test(aPakVar.Count()==2); sl@0: } sl@0: sl@0: LOCAL_C void test2(CArrayPak& aPakVar) 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: aPakVar.AppendL(*(TText*)des1.Ptr(),des1.Size()); sl@0: aPakVar.AppendL(*(TText*)des2.Ptr(),des2.Size()); sl@0: aPakVar.Compress(); sl@0: test(aPakVar.Count()==2); sl@0: TPtrC des4((TText*)&aPakVar[0],des1.Length()); sl@0: test(des1==des4); sl@0: TPtrC des5((TText*)&aPakVar[1],des2.Length()); sl@0: test(des2==des5); sl@0: aPakVar.InsertL(1,*(TText*)des3.Ptr(),des3.Size()); sl@0: test(aPakVar.Count()==3); sl@0: TPtrC des6((TText*)&aPakVar[0],des1.Length()); sl@0: test(des1==des6); sl@0: TPtrC des7((TText*)&aPakVar[2],des2.Length()); sl@0: test(des2==des7); sl@0: TPtrC des8((TText*)&aPakVar[1],des3.Length()); sl@0: test(des3==des8); sl@0: aPakVar.Reset(); sl@0: // So nothing in this array sl@0: test(aPakVar.Count()==0); sl@0: TKeyArrayPak kk(0,ECmpNormal,3); // Compare 3 characters sl@0: TKeyArrayPak kk1(0,ECmpNormal,2); // Compare 2 characters sl@0: TKeyArrayVar vv(0,ECmpNormal,3); // Compare 3 characters 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: aPakVar.AppendL(*buf1.Ptr(),buf1.Size()); sl@0: aPakVar.InsertL(0,*buf2.Ptr(),buf2.Size()); sl@0: aPakVar.AppendL(*buf3.Ptr(),buf3.Size()); sl@0: aPakVar.InsertL(1,*buf4.Ptr(),buf4.Size()); sl@0: aPakVar.Compress(); sl@0: TPtrC rd1((TText*)&aPakVar[2],buf1.Length()); sl@0: test(buf1==rd1); sl@0: TPtrC rd2((TText*)&aPakVar[0],buf2.Length()); sl@0: test(buf2==rd2); sl@0: TPtrC rd3((TText*)&aPakVar[3],buf3.Length()); sl@0: test(buf3==rd3); sl@0: TPtrC rd4((TText*)&aPakVar[1],buf4.Length()); sl@0: test(buf4==rd4); sl@0: test(aPakVar.Count()==4); sl@0: sl@0: test.Next(_L("Sort")); sl@0: TRAPD(res,aPakVar.SortL(vv)); sl@0: test(res==KErrNone); sl@0: /**/ sl@0: TPtrC rd5((TText*)&aPakVar[1],buf1.Length()); sl@0: test(buf1==rd5); sl@0: TPtrC rd6((TText*)&aPakVar[3],buf2.Length()); sl@0: test(buf2==rd6); sl@0: TPtrC rd7((TText*)&aPakVar[2],buf3.Length()); sl@0: test(buf3==rd7); sl@0: TPtrC rd8((TText*)&aPakVar[0],buf4.Length()); sl@0: test(buf4==rd8); sl@0: test(aPakVar.Count()==4); sl@0: /**/ sl@0: test.Next(_L("Find and FindIsq")); sl@0: TBuf<0x10> buf5=_L("ffff"); sl@0: test(aPakVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==2); sl@0: TRAPD(r,aPakVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk)) sl@0: test(r==KErrAlreadyExists); sl@0: test(aPakVar.InsertIsqAllowDuplicatesL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==3); sl@0: TInt aPos; sl@0: test(aPakVar.Find(*_S("abc"),kk,aPos)==0); // Second parameter 'aLength' is unused. sl@0: test(aPos==1); sl@0: test(aPakVar.Find(*_S("aa"),kk1,aPos)==0); sl@0: test(aPos==0); sl@0: test(aPakVar.Find(*_S("wxyz"),kk,aPos)==0); sl@0: test(aPos==5); sl@0: test(aPakVar.Find(*_S("fgh"),kk,aPos)!=0); // Returns non zero if string not found. sl@0: test(aPos==6); // Not present in list, aPos set to last position sl@0: test(aPakVar.Find(*_S("ffff"),kk,aPos)==0); sl@0: test(aPos==2); sl@0: test(aPakVar.Find(*_S("lmn"),kk,aPos)==0); sl@0: test(aPos==4); //15 sl@0: test(aPakVar.FindIsq(*_S("abc"),kk,aPos)==0); sl@0: test(aPos==1); sl@0: test(aPakVar.FindIsq(*_S("aa"),kk1,aPos)==0); sl@0: test(aPos==0); sl@0: test(aPakVar.FindIsq(*_S("wxyz"),kk,aPos)==0); sl@0: test(aPakVar.FindIsq(*_S("fgh"),kk,aPos)!=0); // 22 Returns result of last test sl@0: test(aPos==4); // Not present in list, aPos set to last position tested sl@0: //This test shows problem with BinarySearch sl@0: TBuf<0x10> buf7=_L("fghij"); sl@0: test(aPakVar.InsertIsqL(*(TText*)buf7.Ptr(),buf7.Size(),kk)==4); sl@0: test(aPakVar.FindIsq(*_S("fgh"),kk,aPos)==0); // Returns result of last test sl@0: test(aPos==4); sl@0: test(aPakVar.FindIsq(*_S("ffff"),kk,aPos)==0); sl@0: test(aPos==3); sl@0: test(aPakVar.FindIsq(*_S("lmn"),kk,aPos)==0); sl@0: test(aPos==5); sl@0: aPakVar.Delete(4,1); sl@0: test(aPakVar.Find(*_S("wxyz"),kk,aPos)==0); sl@0: test(aPos==5); sl@0: } sl@0: sl@0: LOCAL_C void test3(CArrayPak& aPak) sl@0: sl@0: { sl@0: test.Next(_L("InsertIsqL")); sl@0: TKeyArrayPak 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,aPak.InsertIsqL(i,sizeof(TInt),kk)) sl@0: else sl@0: { sl@0: TRAP(ret,pos=aPak.InsertIsqL(i,sizeof(TInt),kk)) sl@0: if (ret==KErrNone) sl@0: test(aPak[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* pPakFlat=new CArrayPakFlat(KTestGranularity); sl@0: testAllMethods(*pPakFlat); sl@0: delete pPakFlat; sl@0: // sl@0: CArrayPakFlat* pPakFlatChar=new CArrayPakFlat(KTestGranularity); sl@0: test1(*pPakFlatChar); sl@0: delete pPakFlatChar; sl@0: // sl@0: CArrayPakFlat* pPakFlatArr=new CArrayPakFlat(KTestGranularity); sl@0: test2(*pPakFlatArr); sl@0: delete pPakFlatArr; sl@0: // sl@0: CArrayPakFlat* pPakFlatInt=new CArrayPakFlat(KTestGranularity); sl@0: test3(*pPakFlatInt); sl@0: delete pPakFlatInt; sl@0: // sl@0: test.End(); sl@0: __UHEAP_MARKEND; sl@0: return(0); sl@0: } sl@0: