First public contribution.
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_varray.cpp
16 // Test variable record length array classes.
18 // CArrayVarFlat, CArrayVarSeg.
20 // - Create an array of variable length text using a flat dynamic & segmented
21 // buffer and verify that:
22 // - number of elements held in the array is 0.
23 // - array is compressed and reset as expected.
24 // - the elements of the array are sorted as expected.
25 // - insertion of a text character into the array at specified position
26 // and length of the array is as expected.
27 // - return value is 0 when available element is searched using sequential
28 // search technique 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
31 // element at the end of empty array.
32 // - insertion of a single element with specified key is successful.
33 // - the element is found when searched using binary search technique
34 // - KErrAlreadyExists is returned if an element is inserted with the same
35 // key already exists within the array.
36 // - Create an array of variable length text character implemented using a flat
37 // dynamic & segmented buffer.
38 // - append some strings onto the end of the array, check the contents and
39 // number of elements held in the array are as expected.
40 // - insert some strings and verify that the change in the content of array
41 // and number of elements held in the array are as expected.
42 // - remove a single, multiple elements from the array and verify that the
43 // Delete method is as expected.
44 // - Create an array of variable length text character contained within a flat
45 // dynamic & segmented buffer.
46 // - append some strings of specified length onto the end of the array, compress
47 // the array and verify that the number of elements held in the array is as specified.
48 // - insert a string at specified location, check the contents and reset the array.
49 // - append some strings at the end, insert some strings at specified position,
50 // compress the array and verify that content, number of strings in the array
52 // - test that the number of elements and contents of the array are sorted as expected.
53 // - verify the correct position of the element and return value is zero when
54 // an element is found using binary, sequential search technique and nonzero
55 // if not present in the array.
56 // - insert some strings at the specified location and check that correct position
57 // is returned when searched using binary search technique.
58 // - Create an array of variable length integer contained within a flat dynamic &
60 // - insert some elements with same key which is already present within the array
61 // and check that KErrAlreadyExists is returned.
62 // - Test whether the heap has been corrupted by all the tests.
63 // Platforms/Drives/Compatibility:
65 // Assumptions/Requirement/Pre-requisites:
66 // Failures and causes:
67 // Base Port information:
72 #include <e32std_private.h>
74 #include <e32base_private.h>
79 const TInt KTestGranularity=0x02;
81 LOCAL_D RTest test(_L("T_VARRAY"));
83 LOCAL_C void testAllMethods(CArrayVar<TText>& aVar)
85 test.Next(_L("Test all methods"));
86 test(aVar.Count()==0);
91 TKeyArrayVar kk(sizeof(TText),ECmpNormal,0);
95 const TText* aa=_S("a");
96 aVar.InsertL(0,*aa,sizeof(TText));
97 test(aVar.Length(0)==sizeof(TText));
100 test(aVar.Find(*aa,kk,pp)==0);
104 test(aVar.Count()==1);
105 aVar.InsertIsqAllowDuplicatesL(*aa,0,kk);
107 test(aVar.FindIsq(*aa,kk,pp)==0);
109 TRAPD(r,aVar.InsertIsqL(*aa,0,kk));
110 test(r==KErrAlreadyExists);
113 LOCAL_C void test1(CArrayVar<TText>& aVar)
116 test.Next(_L("AppendL and InsertL chars"));
117 aVar.AppendL(*_S("abcd"),5*sizeof(TText)); // abcd
118 TBuf<0x10> des1(&aVar[0]);
119 test(des1==_L("abcd"));
120 test(aVar.Count()==1);
121 aVar.AppendL(*_S("wxyz"),5*sizeof(TText)); // abcd wxyz
123 test(des1==_L("wxyz"));
124 test(aVar.Count()==2);
125 aVar.InsertL(1,*_S("ef"),3*sizeof(TText)); // abcd ef wxyz
127 test(des1==_L("ef"));
128 test(aVar.Count()==3);
129 aVar.AppendL(*_S("z"),2*sizeof(TText)); // abcd ef wxyz z
132 aVar.InsertL(0,*_S("y"),2*sizeof(TText)); // y abcd ef wxyz z
135 test(aVar.Length(0)==2*sizeof(TText));
136 test(aVar.Length(1)==5*sizeof(TText));
137 test(aVar.Length(2)==3*sizeof(TText));
138 test(aVar.Length(3)==5*sizeof(TText));
139 test(aVar.Length(4)==2*sizeof(TText));
141 test(des1==_L("abcd"));
142 test(aVar.Count()==5);
144 test.Next(_L("Delete chars"));
145 aVar.Delete(3,1); // y abcd ef z
147 test(des1==_L("ef"));
149 test(des1==_L("abcd"));
152 aVar.Delete(1,2); // y z
157 test(aVar.Count()==2);
160 LOCAL_C void test2(CArrayVar<TText>& aVar)
163 test.Next(_L("Reset and Compress"));
164 TBuf<0x10> des1(_L("abcde"));
165 TBuf<0x10> des2(_L("fgh"));
166 TBuf<0x10> des3(_L("wxyz"));
167 aVar.AppendL(*(TText*)des1.Ptr(),des1.Size());
168 aVar.AppendL(*(TText*)des2.Ptr(),des2.Size());
170 test(aVar.Count()==2);
171 TPtrC des4((TText*)&aVar[0],des1.Length());
173 TPtrC des5((TText*)&aVar[1],des2.Length());
175 aVar.InsertL(1,*(TText*)des3.Ptr(),des3.Size());
176 test(aVar.Count()==3);
177 TPtrC des6((TText*)&aVar[0],des1.Length());
179 TPtrC des7((TText*)&aVar[2],des2.Length());
181 TPtrC des8((TText*)&aVar[1],des3.Length());
185 TBuf<0x10> buf1=_L("abcdef");
186 TBuf<0x10> buf2=_L("wxyz");
187 TBuf<0x10> buf3=_L("lmnop");
188 TBuf<0x10> buf4=_L("aa");
189 aVar.AppendL(*buf1.Ptr(),buf1.Size());
190 aVar.InsertL(0,*buf2.Ptr(),buf2.Size());
191 aVar.AppendL(*buf3.Ptr(),buf3.Size());
192 aVar.InsertL(1,*buf4.Ptr(),buf4.Size());
194 TPtrC rd1((TText*)&aVar[2],buf1.Length());
196 TPtrC rd2((TText*)&aVar[0],buf2.Length());
198 TPtrC rd3((TText*)&aVar[3],buf3.Length());
200 TPtrC rd4((TText*)&aVar[1],buf4.Length());
202 test(aVar.Count()==4);
204 TKeyArrayVar kk(0,ECmpNormal,3); // Compare 3 characters
205 TKeyArrayVar kk1(0,ECmpNormal,2); // Compare 2 characters
206 test.Next(_L("Sort"));
208 TPtrC rd5((TText*)&aVar[1],buf1.Length());
210 TPtrC rd6((TText*)&aVar[3],buf2.Length());
212 TPtrC rd7((TText*)&aVar[2],buf3.Length());
214 TPtrC rd8((TText*)&aVar[0],buf4.Length());
216 test(aVar.Count()==4);
218 test.Next(_L("Find and FindIsq"));
219 TBuf<0x10> buf5=_L("ffff");
220 test(aVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==2);
221 TRAPD(r,aVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk))
222 test(r==KErrAlreadyExists);
223 test(aVar.InsertIsqAllowDuplicatesL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==3);
225 test(aVar.Find(*_S("abc"),kk,aPos)==0); // Second parameter 'aLength' is unused.
227 test(aVar.Find(*_S("aa"),kk1,aPos)==0);
229 test(aVar.Find(*_S("wxyz"),kk,aPos)==0);
231 test(aVar.Find(*_S("fgh"),kk,aPos)!=0); // Returns !=0 if string not found.
232 test(aPos==6); // Not present in list, aPos set to last position
233 test(aVar.Find(*_S("ffff"),kk,aPos)==0);
235 test(aVar.Find(*_S("lmn"),kk,aPos)==0);
237 test(aVar.FindIsq(*_S("abc"),kk,aPos)==0);
239 test(aVar.FindIsq(*_S("aa"),kk1,aPos)==0);
241 test(aVar.FindIsq(*_S("wxyz"),kk,aPos)==0);
243 test(aVar.FindIsq(*_S("fgh"),kk,aPos)!=0); // Returns result of last test
244 test(aPos==4); // Not present in list, aPos set to last position tested
245 TBuf<0x10> buf7=_L("fgh");
246 test(aVar.InsertIsqL(*(TText*)buf7.Ptr(),buf7.Size(),kk)==4);
247 test(aVar.FindIsq(*_S("fgh"),kk,aPos)==0); // Returns result of last test
250 test(aVar.FindIsq(*_S("ffff"),kk,aPos)==0);
252 test(aVar.FindIsq(*_S("lmn"),kk,aPos)==0);
256 LOCAL_C void test3(CArrayVar<TInt>& aVar)
259 test.Next(_L("InsertIsqL"));
260 TKeyArrayVar kk(0,ECmpTInt);
271 TRAP(ret,aVar.InsertIsqL(i,sizeof(TInt),kk))
274 TRAP(ret,pos=aVar.InsertIsqL(i,sizeof(TInt),kk))
278 if (ret==KErrAlreadyExists)
285 test(aVar.FindIsq(i,kk,pos)==0);
287 TRAPD(r,aVar.InsertIsqL(i,sizeof(TInt),kk))
288 test(r==KErrAlreadyExists);
292 GLDEF_C TInt E32Main()
294 // Test the variable record length array classes.
300 test.Start(_L("class CArrayFixFlat"));
302 CArrayVarFlat<TText>* pVarFlat=new CArrayVarFlat<TText>(KTestGranularity);
304 test.Panic(_L("Allocating array"));
305 testAllMethods(*pVarFlat);
308 CArrayVarFlat<TText>* pVarFlatChar=new CArrayVarFlat<TText>(KTestGranularity);
309 test1(*pVarFlatChar);
312 CArrayVarFlat<TText>* pVarFlatArr=new CArrayVarFlat<TText>(KTestGranularity);
316 CArrayVarFlat<TInt>* pVarFlatInt=new CArrayVarFlat<TInt>(KTestGranularity);
320 test.Next(_L("class CArrayVarSeg"));
321 CArrayVarSeg<TText>* pVarSeg=new CArrayVarSeg<TText>(KTestGranularity);
323 test.Panic(_L("Allocating array"));
324 testAllMethods(*pVarSeg);
327 CArrayVarSeg<TText>* pVarSegChar=new CArrayVarSeg<TText>(KTestGranularity);
331 CArrayVarSeg<TText>* pVarSegArr=new CArrayVarSeg<TText>(KTestGranularity);
335 CArrayVarSeg<TInt>* pVarSegInt=new CArrayVarSeg<TInt>(KTestGranularity);