sl@0: // Copyright (c) 1994-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_farray.cpp sl@0: // Overview: sl@0: // Test the functionality of CArrayFixFlat, CArrayPtrFlat, CArrayFixSeg classes. sl@0: // API Information: sl@0: // CArrayFixFlat, CArrayPtrFlat, CArrayFixSeg. sl@0: // Details: sl@0: // - Create an array of fixed length buffer descriptor objects contained within a flat sl@0: // dynamic and segmented buffer and verify that sl@0: // - number of elements held in the array is 0. sl@0: // - length of an element is as specified. 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 into the array at specified position and filling a blank space sl@0: // at the beginning is as expected. sl@0: // - return value is 0 when available element is searched 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 element onto sl@0: // the end of the array. sl@0: // - the position of specified element is found successfully sl@0: // - resetting the array is as expected sl@0: // - End and Back methods are as expected. sl@0: // - Create an array of fixed length text character objects contained within a flat dynamic sl@0: // and segmented buffer. sl@0: // - append a text onto the end of the array, check the contents and number of elements sl@0: // held in the array are as expected. sl@0: // - insert a text and verify that the change in the content of array and number of sl@0: // elements held in the array are as expected. sl@0: // - remove a single character and multiple characters from the array and verify that sl@0: // the Delete method is as expected. Compress the array. sl@0: // - Create an array of fixed length text character objects contained within a flat dynamic sl@0: // and segmented buffer. sl@0: // - append strings of specified length onto the end of the array and verify that the sl@0: // number of elements held in the array is as expected. sl@0: // - insert strings at specified location and check that the contents are as expected. sl@0: // - reset the array, append a string, compress the array and verify that content is as sl@0: // expected. sl@0: // - sort the array and verify that content is as expected. sl@0: // - verify the correct position of the element and return value is zero when an element sl@0: // is found using binary and sequential search technique and nonzero if not present in sl@0: // the array. sl@0: // - Create an array of fixed length text character objects contained within a flat dynamic sl@0: // and segmented buffer. sl@0: // - Insert some elements into the array at specified positions determined by key of sl@0: // type TInt and verify that KErrAlreadyExists is returned if an element with the sl@0: // same key already exists within the array. sl@0: // - Create an array of pointers to objects implemented using a flat dynamic buffer, insert one sl@0: // element into the array at the specified position and destroy the object whose pointer form sl@0: // the element of the array, before resetting the array. sl@0: // - Create and delete an array of CBase objects contained within a flat dynamic buffer. 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: sl@0: class MyCBase : public CBase sl@0: { sl@0: }; sl@0: sl@0: const TInt KTestGranularity=0x02; sl@0: sl@0: LOCAL_D RTest test(_L("T_FARRAY")); sl@0: sl@0: template sl@0: class TArr sl@0: { sl@0: public: sl@0: TArr() {} sl@0: TInt Count() const {return S;} sl@0: T& operator[](TInt anIndex) {return iArr[anIndex];} sl@0: const T& operator[](TInt anIndex) const {return iArr[anIndex];} sl@0: private: sl@0: T iArr[S]; sl@0: }; sl@0: sl@0: LOCAL_C void testFix(CArrayFix >& aFix) sl@0: // sl@0: // Test all methods sl@0: // sl@0: { sl@0: test.Next(_L("Test all methods")); sl@0: test(aFix.Count()==0); sl@0: test(aFix.Length()==sizeof(TBuf<0x10>)); sl@0: aFix.Compress(); sl@0: test(TRUE); sl@0: aFix.Reset(); sl@0: test(TRUE); sl@0: TKeyArrayFix kk(0,ECmpNormal,0x10); sl@0: test(TRUE); sl@0: aFix.Sort(kk); sl@0: test(TRUE); sl@0: TBuf<0x10> aa(_L("aaaaa")); sl@0: aFix.InsertL(0,aa); sl@0: test(TRUE); sl@0: aFix[0].Fill(' '); sl@0: test(TRUE); sl@0: TBuf<0x10> z(aFix[0]); sl@0: z.Length(); sl@0: test(TRUE); sl@0: aFix[0].Fill('a'); sl@0: test(TRUE); sl@0: TInt pp; sl@0: test(aFix.Find(aa,kk,pp)==0); sl@0: test(pp==0); sl@0: aFix.Delete(0); sl@0: TBuf<0x10> bb(_L("bbbbb")); sl@0: aFix.AppendL(bb); sl@0: test(aFix.Count()==1); sl@0: test(aFix.InsertIsqAllowDuplicatesL(aa,kk)==0); sl@0: test(aFix.InsertIsqAllowDuplicatesL(bb,kk)==2); sl@0: test(aFix.FindIsq(aa,kk,pp)==0); sl@0: test(pp==0); sl@0: aFix.Reset(); sl@0: for(TInt index=0;index *end=NULL; sl@0: const TBuf<0x10> *ptr=NULL; sl@0: for(TInt index2=0;index2 *bak=NULL; sl@0: ptr=NULL; sl@0: for(TInt index3=KTestGranularity*7/2;index3>0;index3--) sl@0: { sl@0: if (bak==ptr) sl@0: { sl@0: bak=aFix.Back(index3); sl@0: ptr=&aFix[index3-1]+1; sl@0: TInt seglen=ptr-bak; sl@0: test(seglen==KTestGranularity || seglen==index3 || seglen==index3%KTestGranularity); sl@0: } sl@0: test(&aFix[index3-1]==--ptr); sl@0: } sl@0: } sl@0: sl@0: LOCAL_C void test1(CArrayFix& aFix) sl@0: // sl@0: { sl@0: test.Next(_L("AppendL and InsertL single chars")); sl@0: aFix.AppendL(_S("abcd"),4); sl@0: test(aFix[0]=='a'); sl@0: test(aFix[1]=='b'); sl@0: test(aFix[3]=='d'); sl@0: test(aFix.Count()==4); sl@0: aFix.InsertL(2,_S("ef"),2); sl@0: test(aFix[1]=='b'); sl@0: test(aFix[2]=='e'); sl@0: test(aFix[4]=='c'); sl@0: test(aFix.Count()==6); sl@0: aFix.AppendL(TText('z')); sl@0: test(aFix[6]=='z'); sl@0: aFix.InsertL(0,TText('y')); sl@0: test(aFix[0]=='y'); sl@0: test(aFix[1]=='a'); sl@0: test(aFix.Count()==8); sl@0: test.Next(_L("Delete single chars")); sl@0: aFix.Delete(3); sl@0: test(aFix[2]=='b'); sl@0: test(aFix[3]=='f'); sl@0: test(aFix[4]=='c'); sl@0: aFix.Delete(1,2); sl@0: test(aFix[0]=='y'); sl@0: test(aFix[1]=='f'); sl@0: test(aFix[2]=='c'); sl@0: test(aFix.Count()==5); sl@0: aFix.Compress(); sl@0: } sl@0: sl@0: LOCAL_C void test2(CArrayFix >& aFix) sl@0: // sl@0: { sl@0: test(aFix.Length()==sizeof(TArr)); sl@0: test.Next(_L("AppendL and insert strings of length 4")); sl@0: TPtrC des1=_L("abcd"); sl@0: TPtrC des2=_L("efgh"); sl@0: aFix.AppendL(*(const TArr*)des1.Ptr()); sl@0: aFix.AppendL(*(const TArr*)des2.Ptr()); sl@0: test(aFix.Count()==2); sl@0: TPtrC des3(&aFix[0][0],4); sl@0: TPtrC des4(&aFix[1][0],4); sl@0: test(des3==_L("abcd")); sl@0: test(des4==_L("efgh")); sl@0: aFix.InsertL(1,*(const TArr*)_S("ijkl")); sl@0: test(aFix.Count()==3); sl@0: TPtrC des5(&aFix[2][0],4); sl@0: test(des3==_L("abcd")); sl@0: test(des4==_L("ijkl")); sl@0: test(des5==_L("efgh")); sl@0: sl@0: test.Next(_L("Reset and Compress")); sl@0: aFix.Reset(); sl@0: TBuf<0x10> buf1=_L("abcdefgh"); sl@0: aFix.AppendL((const TArr*)buf1.Ptr(),2); sl@0: aFix.Compress(); sl@0: TPtrC des6(&aFix[0][0],4); sl@0: test(des6==_L("abcd")); sl@0: TPtrC des7(&aFix[1][0],4); sl@0: test(des7==_L("efgh")); sl@0: buf1=_L("ghighhxy"); sl@0: aFix.InsertL(1,(const TArr*)buf1.Ptr(),2); sl@0: aFix.Compress(); sl@0: TPtrC des8(&aFix[0][0],4); sl@0: test(des8==_L("abcd")); sl@0: TPtrC des9(&aFix[1][0],4); sl@0: test(des9==_L("ghig")); sl@0: TPtrC des10(&aFix[2][0],4); sl@0: test(des10==_L("hhxy")); sl@0: TPtrC des11(&aFix[3][0],4); sl@0: test(des11==_L("efgh")); sl@0: sl@0: test.Next(_L("Sort strings")); sl@0: TKeyArrayFix kk(0,ECmpNormal,0x04); sl@0: aFix.Sort(kk); sl@0: TPtrC des12(&aFix[0][0],4); sl@0: test(des12==_L("abcd")); sl@0: TPtrC des13(&aFix[1][0],4); sl@0: test(des13==_L("efgh")); sl@0: TPtrC des14(&aFix[2][0],4); sl@0: test(des14==_L("ghig")); sl@0: TPtrC des15(&aFix[3][0],4); sl@0: test(des15==_L("hhxy")); sl@0: sl@0: test.Next(_L("Find and FindIsq")); sl@0: aFix.Compress(); sl@0: test(aFix.InsertIsqL(*(const TArr*)_S("ffff"),kk)==2); sl@0: aFix.Compress(); sl@0: test(aFix.InsertIsqAllowDuplicatesL(*(const TArr*)_S("ffff"),kk)==3); sl@0: aFix.Compress(); sl@0: TRAPD(r,aFix.InsertIsqL(*(const TArr*)_S("ffff"),kk)) sl@0: test(r==KErrAlreadyExists); sl@0: TInt aPos=0; sl@0: test(aFix.Find(*(const TArr*)_S("xxxx"),kk,aPos)==1); sl@0: test(aPos==6); sl@0: test(aFix.Find(*(const TArr*)_S("abcd"),kk,aPos)==0); sl@0: test(aPos==0); sl@0: test(aFix.Find(*(const TArr*)_S("ghig"),kk,aPos)==0); sl@0: test(aPos==4); sl@0: test(aFix.Find(*(const TArr*)_S("ffff"),kk,aPos)==0); sl@0: test(aPos==2); sl@0: test(aFix.Find(*(const TArr*)_S("hhxy"),kk,aPos)==0); sl@0: test(aPos==5); sl@0: test(aFix.FindIsq(*(const TArr*)_S("bbbb"),kk,aPos)!=0); sl@0: test(aPos==1); sl@0: test(aFix.FindIsq(*(const TArr*)_S("abcd"),kk,aPos)==0); sl@0: test(aPos==0); sl@0: test(aFix.FindIsq(*(const TArr*)_S("ghig"),kk,aPos)==0); sl@0: test(aPos==4); sl@0: test(aFix.FindIsq(*(const TArr*)_S("ffff"),kk,aPos)==0); sl@0: test(aPos==2); sl@0: test(aFix.InsertIsqL(*(const TArr*)_S("fghz"),kk)==4); sl@0: test(aFix.FindIsq(*(const TArr*)_S("fghz"),kk,aPos)==0); sl@0: test(aPos==4); sl@0: test(aFix.FindIsq(*(const TArr*)_S("hhxy"),kk,aPos)==0); sl@0: test(aPos==6); sl@0: } sl@0: sl@0: LOCAL_C void test3(CArrayFix& aFix) sl@0: { sl@0: sl@0: test.Next(_L("InsertIsqL")); sl@0: TKeyArrayFix kk(0,ECmpTInt); sl@0: sl@0: TInt pos=0; sl@0: TInt mod=47; sl@0: TInt inc=23; sl@0: TInt i=0; sl@0: sl@0: FOREVER sl@0: { sl@0: TInt ret; sl@0: if (i&1) sl@0: TRAP(ret,aFix.InsertIsqL(i,kk)) sl@0: else sl@0: { sl@0: TRAP(ret,pos=aFix.InsertIsqL(i,kk)) sl@0: if (ret==KErrNone) sl@0: test(aFix[pos]==i); sl@0: } sl@0: if (ret==KErrAlreadyExists) sl@0: break; sl@0: i=(i+inc)%mod; sl@0: } sl@0: sl@0: for(i=0;i >* pFixFlat=new CArrayFixFlat >(KTestGranularity); sl@0: if (pFixFlat==NULL) sl@0: test.Panic(_L("Allocating array")); sl@0: testFix(*pFixFlat); sl@0: delete pFixFlat; sl@0: sl@0: CArrayFixFlat* pFixFlatChar=new CArrayFixFlat(KTestGranularity); sl@0: test1(*pFixFlatChar); sl@0: delete pFixFlatChar; sl@0: sl@0: CArrayFixFlat >* pFixFlatArr=new CArrayFixFlat >(KTestGranularity); sl@0: test2(*pFixFlatArr); sl@0: delete pFixFlatArr; sl@0: sl@0: CArrayFixFlat* pFixFlatInt=new CArrayFixFlat(KTestGranularity); sl@0: test3(*pFixFlatInt); sl@0: delete pFixFlatInt; sl@0: sl@0: test.Next(_L("class CArrayPtrFlat of CBase")); sl@0: sl@0: CArrayPtrFlat* pPtrFlatCBase=new CArrayPtrFlat(KTestGranularity); sl@0: if (pPtrFlatCBase==NULL) sl@0: test.Panic(_L("Allocating array of CBase*")); sl@0: MyCBase* c1 = new MyCBase(); sl@0: pPtrFlatCBase->InsertL(0,&c1,1); sl@0: pPtrFlatCBase->ResetAndDestroy(); sl@0: // test(pFixFlatCBase->iBase==0); sl@0: pPtrFlatCBase->ResetAndDestroy(); sl@0: delete pPtrFlatCBase; sl@0: sl@0: test.Next(_L("class CArrayFixFlat of CBase")); sl@0: sl@0: CArrayFixFlat* pFixFlatCBase=new CArrayFixFlat(KTestGranularity); sl@0: if (pFixFlatCBase==NULL) sl@0: test.Panic(_L("Allocating array of CBase")); sl@0: delete pFixFlatCBase; sl@0: sl@0: test.Next(_L("class CArrayFixSeg")); sl@0: CArrayFixSeg >* pFixSeg=new CArrayFixSeg >(KTestGranularity); sl@0: if (pFixSeg==NULL) sl@0: test.Panic(_L("Allocating array")); sl@0: testFix(*pFixSeg); sl@0: delete pFixSeg; sl@0: sl@0: CArrayFixSeg* pFixSegChar=new CArrayFixSeg(KTestGranularity); sl@0: test1(*pFixSegChar); sl@0: delete pFixSegChar; sl@0: sl@0: CArrayFixSeg >* pFixSegArr=new CArrayFixSeg >(KTestGranularity); sl@0: test2(*pFixSegArr); sl@0: delete pFixSegArr; sl@0: sl@0: CArrayFixSeg* pFixSegInt=new CArrayFixSeg(KTestGranularity); sl@0: test3(*pFixSegInt); sl@0: delete pFixSegInt; sl@0: sl@0: test.End(); sl@0: __UHEAP_MARKEND; sl@0: return(0); sl@0: } sl@0: