Update contrib.
1 // Copyright (c) 1995-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_parray.cpp
16 // Test the variable record length array classes.
17 // Test only covers the Flat type implementation.
21 // - Create an array of variable length text character objects packed into a flat buffer.
22 // - check the number of elements held in the array is 0.
23 // - test that Compress and Reset methods are as expected.
24 // - sort the array and check that no error is returned.
25 // - insert an element and check the length is as expected.
26 // - search for the inserted element and check it is found successfully.
27 // - remove all the elements, append an element and verify that number of elements
28 // held in the array is 1.
29 // - insert an element into the array, search for that element using sequential,
30 // binary search technique and verify that it is found at the expected position.
31 // - insert another element with the same key and check that KErrAlreadyExists is
33 // - Create an array of variable length text character objects packed into a flat buffer.
34 // - append some strings at the end, insert some strings at the specified location and
35 // verify that the length of each string and number of strings held in the array are
37 // - delete some strings and check the remaining strings in the array are as expected.
38 // - Create an array of variable length text character objects packed into a flat buffer.
39 // - append some strings at the end and insert some stings at specified location.
40 // - compress the array and verify strings and the number of strings held in the
41 // array are as expected.
42 // - reset the array and verify the number of elements held in the array is 0.
43 // - sort the array and check that array is sorted as expected.
44 // - test that KErrAlreadyExists is returned if another element with the same key
46 // - search for strings using sequential, binary search and verify that 0 is returned
47 // if found else nonzero.
48 // - delete some text from the array and check the results are as expected.
49 // - Create an array of variable length integer objects packed into a flat buffer.
50 // - Insert some elements with same key which already exists within the array and
51 // check that KErrAlreadyExists is returned.
52 // - Test whether the heap has been corrupted by all the tests.
53 // Platforms/Drives/Compatibility:
55 // Assumptions/Requirement/Pre-requisites:
56 // Failures and causes:
57 // Base Port information:
62 #include <e32std_private.h>
64 #include <e32base_private.h>
69 const TInt KTestGranularity=0x10;
71 LOCAL_D RTest test(_L("T_PARRAY"));
73 LOCAL_C void testAllMethods(CArrayPak<TText>& aPakVar)
75 test.Next(_L("Test all methods"));
76 test(aPakVar.Count()==0);
81 TKeyArrayPak kk(sizeof(TText),ECmpNormal,0);
82 TKeyArrayVar vv(sizeof(TText),ECmpNormal,0);
84 TRAPD(res,aPakVar.SortL(vv));
86 const TText* aa=_S("a");
87 aPakVar.InsertL(0,*aa,sizeof(TText));
91 test(aPakVar.Length(0)==sizeof(TText));
94 test(aPakVar.Find(*aa,kk,pp)==0);
97 aPakVar.AppendL(*aa,1);
98 test(aPakVar.Count()==1);
99 aPakVar.InsertIsqAllowDuplicatesL(*aa,0,kk);
101 test(aPakVar.FindIsq(*aa,kk,pp)==0);
103 TRAPD(r,aPakVar.InsertIsqL(*aa,0,kk));
104 test(r==KErrAlreadyExists);
107 LOCAL_C void test1(CArrayPak<TText>& aPakVar)
110 test.Next(_L("AppendL and InsertL chars"));
111 aPakVar.AppendL(*_S("abcd"),5*sizeof(TText)); // abcd
112 TBuf<0x10> des1(&aPakVar[0]);
113 test(des1==_L("abcd"));
114 test(aPakVar.Count()==1);
115 aPakVar.AppendL(*_S("wxyz"),5*sizeof(TText)); // abcd wxyz
117 test(des1==_L("wxyz"));
118 test(aPakVar.Count()==2);
119 aPakVar.InsertL(1,*_S("ef"),3*sizeof(TText)); // abcd ef wxyz
121 test(des1==_L("ef"));
122 test(aPakVar.Count()==3);
123 aPakVar.AppendL(*_S("z"),2*sizeof(TText)); // abcd ef wxyz z
126 aPakVar.InsertL(0,*_S("y"),2*sizeof(TText)); // y abcd ef wxyz z
129 test(aPakVar.Length(0)==2*sizeof(TText));
130 test(aPakVar.Length(1)==5*sizeof(TText));
131 test(aPakVar.Length(2)==3*sizeof(TText));
132 test(aPakVar.Length(3)==5*sizeof(TText));
133 test(aPakVar.Length(4)==2*sizeof(TText));
135 test(des1==_L("abcd"));
136 test(aPakVar.Count()==5);
138 test.Next(_L("Delete chars"));
139 aPakVar.Delete(3,1); // y abcd ef z
141 test(des1==_L("ef"));
143 test(des1==_L("abcd"));
146 aPakVar.Delete(1,2); // y z
151 test(aPakVar.Count()==2);
154 LOCAL_C void test2(CArrayPak<TText>& aPakVar)
157 test.Next(_L("Reset and Compress"));
158 TBuf<0x10> des1(_L("abcde"));
159 TBuf<0x10> des2(_L("fgh"));
160 TBuf<0x10> des3(_L("wxyz"));
161 aPakVar.AppendL(*(TText*)des1.Ptr(),des1.Size());
162 aPakVar.AppendL(*(TText*)des2.Ptr(),des2.Size());
164 test(aPakVar.Count()==2);
165 TPtrC des4((TText*)&aPakVar[0],des1.Length());
167 TPtrC des5((TText*)&aPakVar[1],des2.Length());
169 aPakVar.InsertL(1,*(TText*)des3.Ptr(),des3.Size());
170 test(aPakVar.Count()==3);
171 TPtrC des6((TText*)&aPakVar[0],des1.Length());
173 TPtrC des7((TText*)&aPakVar[2],des2.Length());
175 TPtrC des8((TText*)&aPakVar[1],des3.Length());
178 // So nothing in this array
179 test(aPakVar.Count()==0);
180 TKeyArrayPak kk(0,ECmpNormal,3); // Compare 3 characters
181 TKeyArrayPak kk1(0,ECmpNormal,2); // Compare 2 characters
182 TKeyArrayVar vv(0,ECmpNormal,3); // Compare 3 characters
183 TBuf<0x10> buf1=_L("abcdef");
184 TBuf<0x10> buf2=_L("wxyz");
185 TBuf<0x10> buf3=_L("lmnop");
186 TBuf<0x10> buf4=_L("aa");
187 aPakVar.AppendL(*buf1.Ptr(),buf1.Size());
188 aPakVar.InsertL(0,*buf2.Ptr(),buf2.Size());
189 aPakVar.AppendL(*buf3.Ptr(),buf3.Size());
190 aPakVar.InsertL(1,*buf4.Ptr(),buf4.Size());
192 TPtrC rd1((TText*)&aPakVar[2],buf1.Length());
194 TPtrC rd2((TText*)&aPakVar[0],buf2.Length());
196 TPtrC rd3((TText*)&aPakVar[3],buf3.Length());
198 TPtrC rd4((TText*)&aPakVar[1],buf4.Length());
200 test(aPakVar.Count()==4);
202 test.Next(_L("Sort"));
203 TRAPD(res,aPakVar.SortL(vv));
206 TPtrC rd5((TText*)&aPakVar[1],buf1.Length());
208 TPtrC rd6((TText*)&aPakVar[3],buf2.Length());
210 TPtrC rd7((TText*)&aPakVar[2],buf3.Length());
212 TPtrC rd8((TText*)&aPakVar[0],buf4.Length());
214 test(aPakVar.Count()==4);
216 test.Next(_L("Find and FindIsq"));
217 TBuf<0x10> buf5=_L("ffff");
218 test(aPakVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==2);
219 TRAPD(r,aPakVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk))
220 test(r==KErrAlreadyExists);
221 test(aPakVar.InsertIsqAllowDuplicatesL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==3);
223 test(aPakVar.Find(*_S("abc"),kk,aPos)==0); // Second parameter 'aLength' is unused.
225 test(aPakVar.Find(*_S("aa"),kk1,aPos)==0);
227 test(aPakVar.Find(*_S("wxyz"),kk,aPos)==0);
229 test(aPakVar.Find(*_S("fgh"),kk,aPos)!=0); // Returns non zero if string not found.
230 test(aPos==6); // Not present in list, aPos set to last position
231 test(aPakVar.Find(*_S("ffff"),kk,aPos)==0);
233 test(aPakVar.Find(*_S("lmn"),kk,aPos)==0);
235 test(aPakVar.FindIsq(*_S("abc"),kk,aPos)==0);
237 test(aPakVar.FindIsq(*_S("aa"),kk1,aPos)==0);
239 test(aPakVar.FindIsq(*_S("wxyz"),kk,aPos)==0);
240 test(aPakVar.FindIsq(*_S("fgh"),kk,aPos)!=0); // 22 Returns result of last test
241 test(aPos==4); // Not present in list, aPos set to last position tested
242 //This test shows problem with BinarySearch
243 TBuf<0x10> buf7=_L("fghij");
244 test(aPakVar.InsertIsqL(*(TText*)buf7.Ptr(),buf7.Size(),kk)==4);
245 test(aPakVar.FindIsq(*_S("fgh"),kk,aPos)==0); // Returns result of last test
247 test(aPakVar.FindIsq(*_S("ffff"),kk,aPos)==0);
249 test(aPakVar.FindIsq(*_S("lmn"),kk,aPos)==0);
252 test(aPakVar.Find(*_S("wxyz"),kk,aPos)==0);
256 LOCAL_C void test3(CArrayPak<TInt>& aPak)
259 test.Next(_L("InsertIsqL"));
260 TKeyArrayPak kk(0,ECmpTInt);
271 TRAP(ret,aPak.InsertIsqL(i,sizeof(TInt),kk))
274 TRAP(ret,pos=aPak.InsertIsqL(i,sizeof(TInt),kk))
278 if (ret==KErrAlreadyExists)
285 test(aPak.FindIsq(i,kk,pos)==0);
287 TRAPD(r,aPak.InsertIsqL(i,sizeof(TInt),kk))
288 test(r==KErrAlreadyExists);
292 GLDEF_C TInt E32Main()
294 // Test the variable record length array classes.
295 // Initially test only covers the Flat type implementation.
300 test.Start(_L("class CArrayPakFlat"));
302 CArrayPakFlat<TText>* pPakFlat=new CArrayPakFlat<TText>(KTestGranularity);
303 testAllMethods(*pPakFlat);
306 CArrayPakFlat<TText>* pPakFlatChar=new CArrayPakFlat<TText>(KTestGranularity);
307 test1(*pPakFlatChar);
310 CArrayPakFlat<TText>* pPakFlatArr=new CArrayPakFlat<TText>(KTestGranularity);
314 CArrayPakFlat<TInt>* pPakFlatInt=new CArrayPakFlat<TInt>(KTestGranularity);