Update contrib.
1 // Copyright (c) 1994-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 "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\buffer\t_farray.cpp
16 // Test the functionality of CArrayFixFlat, CArrayPtrFlat, CArrayFixSeg classes.
18 // CArrayFixFlat, CArrayPtrFlat, CArrayFixSeg.
20 // - Create an array of fixed length buffer descriptor objects contained within a flat
21 // dynamic and segmented buffer and verify that
22 // - number of elements held in the array is 0.
23 // - length of an element is as specified.
24 // - array is compressed and reset as expected.
25 // - the elements of the array are sorted as expected.
26 // - insertion of a text into the array at specified position and filling a blank space
27 // at the beginning is as expected.
28 // - return value is 0 when available element is searched within the array.
29 // - removal of first element from the array is successful.
30 // - number of elements held in the array is 1 after appending a single element onto
31 // the end of the array.
32 // - the position of specified element is found successfully
33 // - resetting the array is as expected
34 // - End and Back methods are as expected.
35 // - Create an array of fixed length text character objects contained within a flat dynamic
36 // and segmented buffer.
37 // - append a text onto the end of the array, check the contents and number of elements
38 // held in the array are as expected.
39 // - insert a text and verify that the change in the content of array and number of
40 // elements held in the array are as expected.
41 // - remove a single character and multiple characters from the array and verify that
42 // the Delete method is as expected. Compress the array.
43 // - Create an array of fixed length text character objects contained within a flat dynamic
44 // and segmented buffer.
45 // - append strings of specified length onto the end of the array and verify that the
46 // number of elements held in the array is as expected.
47 // - insert strings at specified location and check that the contents are as expected.
48 // - reset the array, append a string, compress the array and verify that content is as
50 // - sort the array and verify that content is as expected.
51 // - verify the correct position of the element and return value is zero when an element
52 // is found using binary and sequential search technique and nonzero if not present in
54 // - Create an array of fixed length text character objects contained within a flat dynamic
55 // and segmented buffer.
56 // - Insert some elements into the array at specified positions determined by key of
57 // type TInt and verify that KErrAlreadyExists is returned if an element with the
58 // same key already exists within the array.
59 // - Create an array of pointers to objects implemented using a flat dynamic buffer, insert one
60 // element into the array at the specified position and destroy the object whose pointer form
61 // the element of the array, before resetting the array.
62 // - Create and delete an array of CBase objects contained within a flat dynamic buffer.
63 // - Test whether the heap has been corrupted by all the tests.
64 // Platforms/Drives/Compatibility:
66 // Assumptions/Requirement/Pre-requisites:
67 // Failures and causes:
68 // Base Port information:
74 class MyCBase : public CBase
78 const TInt KTestGranularity=0x02;
80 LOCAL_D RTest test(_L("T_FARRAY"));
82 template <class T,TInt S>
87 TInt Count() const {return S;}
88 T& operator[](TInt anIndex) {return iArr[anIndex];}
89 const T& operator[](TInt anIndex) const {return iArr[anIndex];}
94 LOCAL_C void testFix(CArrayFix<TBuf<0x10> >& aFix)
99 test.Next(_L("Test all methods"));
100 test(aFix.Count()==0);
101 test(aFix.Length()==sizeof(TBuf<0x10>));
106 TKeyArrayFix kk(0,ECmpNormal,0x10);
110 TBuf<0x10> aa(_L("aaaaa"));
115 TBuf<0x10> z(aFix[0]);
121 test(aFix.Find(aa,kk,pp)==0);
124 TBuf<0x10> bb(_L("bbbbb"));
126 test(aFix.Count()==1);
127 test(aFix.InsertIsqAllowDuplicatesL(aa,kk)==0);
128 test(aFix.InsertIsqAllowDuplicatesL(bb,kk)==2);
129 test(aFix.FindIsq(aa,kk,pp)==0);
132 for(TInt index=0;index<KTestGranularity*7/2;index++)
134 const TBuf<0x10> *end=NULL;
135 const TBuf<0x10> *ptr=NULL;
136 for(TInt index2=0;index2<KTestGranularity*7/2;index2++)
140 end=aFix.End(index2);
143 test(seglen==KTestGranularity || seglen==(aFix.Count()-index2));
145 test(&aFix[index2]==ptr++);
147 const TBuf<0x10> *bak=NULL;
149 for(TInt index3=KTestGranularity*7/2;index3>0;index3--)
153 bak=aFix.Back(index3);
154 ptr=&aFix[index3-1]+1;
156 test(seglen==KTestGranularity || seglen==index3 || seglen==index3%KTestGranularity);
158 test(&aFix[index3-1]==--ptr);
162 LOCAL_C void test1(CArrayFix<TText>& aFix)
165 test.Next(_L("AppendL and InsertL single chars"));
166 aFix.AppendL(_S("abcd"),4);
170 test(aFix.Count()==4);
171 aFix.InsertL(2,_S("ef"),2);
175 test(aFix.Count()==6);
176 aFix.AppendL(TText('z'));
178 aFix.InsertL(0,TText('y'));
181 test(aFix.Count()==8);
182 test.Next(_L("Delete single chars"));
191 test(aFix.Count()==5);
195 LOCAL_C void test2(CArrayFix<TArr<TText,4> >& aFix)
198 test(aFix.Length()==sizeof(TArr<TText,4>));
199 test.Next(_L("AppendL and insert strings of length 4"));
200 TPtrC des1=_L("abcd");
201 TPtrC des2=_L("efgh");
202 aFix.AppendL(*(const TArr<TText,4>*)des1.Ptr());
203 aFix.AppendL(*(const TArr<TText,4>*)des2.Ptr());
204 test(aFix.Count()==2);
205 TPtrC des3(&aFix[0][0],4);
206 TPtrC des4(&aFix[1][0],4);
207 test(des3==_L("abcd"));
208 test(des4==_L("efgh"));
209 aFix.InsertL(1,*(const TArr<TText,4>*)_S("ijkl"));
210 test(aFix.Count()==3);
211 TPtrC des5(&aFix[2][0],4);
212 test(des3==_L("abcd"));
213 test(des4==_L("ijkl"));
214 test(des5==_L("efgh"));
216 test.Next(_L("Reset and Compress"));
218 TBuf<0x10> buf1=_L("abcdefgh");
219 aFix.AppendL((const TArr<TText,4>*)buf1.Ptr(),2);
221 TPtrC des6(&aFix[0][0],4);
222 test(des6==_L("abcd"));
223 TPtrC des7(&aFix[1][0],4);
224 test(des7==_L("efgh"));
226 aFix.InsertL(1,(const TArr<TText,4>*)buf1.Ptr(),2);
228 TPtrC des8(&aFix[0][0],4);
229 test(des8==_L("abcd"));
230 TPtrC des9(&aFix[1][0],4);
231 test(des9==_L("ghig"));
232 TPtrC des10(&aFix[2][0],4);
233 test(des10==_L("hhxy"));
234 TPtrC des11(&aFix[3][0],4);
235 test(des11==_L("efgh"));
237 test.Next(_L("Sort strings"));
238 TKeyArrayFix kk(0,ECmpNormal,0x04);
240 TPtrC des12(&aFix[0][0],4);
241 test(des12==_L("abcd"));
242 TPtrC des13(&aFix[1][0],4);
243 test(des13==_L("efgh"));
244 TPtrC des14(&aFix[2][0],4);
245 test(des14==_L("ghig"));
246 TPtrC des15(&aFix[3][0],4);
247 test(des15==_L("hhxy"));
249 test.Next(_L("Find and FindIsq"));
251 test(aFix.InsertIsqL(*(const TArr<TText,4>*)_S("ffff"),kk)==2);
253 test(aFix.InsertIsqAllowDuplicatesL(*(const TArr<TText,4>*)_S("ffff"),kk)==3);
255 TRAPD(r,aFix.InsertIsqL(*(const TArr<TText,4>*)_S("ffff"),kk))
256 test(r==KErrAlreadyExists);
258 test(aFix.Find(*(const TArr<TText,4>*)_S("xxxx"),kk,aPos)==1);
260 test(aFix.Find(*(const TArr<TText,4>*)_S("abcd"),kk,aPos)==0);
262 test(aFix.Find(*(const TArr<TText,4>*)_S("ghig"),kk,aPos)==0);
264 test(aFix.Find(*(const TArr<TText,4>*)_S("ffff"),kk,aPos)==0);
266 test(aFix.Find(*(const TArr<TText,4>*)_S("hhxy"),kk,aPos)==0);
268 test(aFix.FindIsq(*(const TArr<TText,4>*)_S("bbbb"),kk,aPos)!=0);
270 test(aFix.FindIsq(*(const TArr<TText,4>*)_S("abcd"),kk,aPos)==0);
272 test(aFix.FindIsq(*(const TArr<TText,4>*)_S("ghig"),kk,aPos)==0);
274 test(aFix.FindIsq(*(const TArr<TText,4>*)_S("ffff"),kk,aPos)==0);
276 test(aFix.InsertIsqL(*(const TArr<TText,4>*)_S("fghz"),kk)==4);
277 test(aFix.FindIsq(*(const TArr<TText,4>*)_S("fghz"),kk,aPos)==0);
279 test(aFix.FindIsq(*(const TArr<TText,4>*)_S("hhxy"),kk,aPos)==0);
283 LOCAL_C void test3(CArrayFix<TInt>& aFix)
286 test.Next(_L("InsertIsqL"));
287 TKeyArrayFix kk(0,ECmpTInt);
298 TRAP(ret,aFix.InsertIsqL(i,kk))
301 TRAP(ret,pos=aFix.InsertIsqL(i,kk))
305 if (ret==KErrAlreadyExists)
312 test(aFix.FindIsq(i,kk,pos)==0);
314 TRAPD(r,aFix.InsertIsqL(i,kk))
315 test(r==KErrAlreadyExists);
319 GLDEF_C TInt E32Main()
321 // Test the Array classes.
327 test.Start(_L("class CArrayFixFlat"));
328 CArrayFixFlat<TBuf<0x10> >* pFixFlat=new CArrayFixFlat<TBuf<0x10> >(KTestGranularity);
330 test.Panic(_L("Allocating array"));
334 CArrayFixFlat<TText>* pFixFlatChar=new CArrayFixFlat<TText>(KTestGranularity);
335 test1(*pFixFlatChar);
338 CArrayFixFlat<TArr<TText,4> >* pFixFlatArr=new CArrayFixFlat<TArr<TText,4> >(KTestGranularity);
342 CArrayFixFlat<TInt>* pFixFlatInt=new CArrayFixFlat<TInt>(KTestGranularity);
346 test.Next(_L("class CArrayPtrFlat of CBase"));
348 CArrayPtrFlat<MyCBase>* pPtrFlatCBase=new CArrayPtrFlat<MyCBase>(KTestGranularity);
349 if (pPtrFlatCBase==NULL)
350 test.Panic(_L("Allocating array of CBase*"));
351 MyCBase* c1 = new MyCBase();
352 pPtrFlatCBase->InsertL(0,&c1,1);
353 pPtrFlatCBase->ResetAndDestroy();
354 // test(pFixFlatCBase->iBase==0);
355 pPtrFlatCBase->ResetAndDestroy();
356 delete pPtrFlatCBase;
358 test.Next(_L("class CArrayFixFlat of CBase"));
360 CArrayFixFlat<MyCBase>* pFixFlatCBase=new CArrayFixFlat<MyCBase>(KTestGranularity);
361 if (pFixFlatCBase==NULL)
362 test.Panic(_L("Allocating array of CBase"));
363 delete pFixFlatCBase;
365 test.Next(_L("class CArrayFixSeg"));
366 CArrayFixSeg<TBuf<0x10> >* pFixSeg=new CArrayFixSeg<TBuf<0x10> >(KTestGranularity);
368 test.Panic(_L("Allocating array"));
372 CArrayFixSeg<TText>* pFixSegChar=new CArrayFixSeg<TText>(KTestGranularity);
376 CArrayFixSeg<TArr<TText,4> >* pFixSegArr=new CArrayFixSeg<TArr<TText,4> >(KTestGranularity);
380 CArrayFixSeg<TInt>* pFixSegInt=new CArrayFixSeg<TInt>(KTestGranularity);