1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/buffer/t_farray.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,388 @@
1.4 +// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\buffer\t_farray.cpp
1.18 +// Overview:
1.19 +// Test the functionality of CArrayFixFlat, CArrayPtrFlat, CArrayFixSeg classes.
1.20 +// API Information:
1.21 +// CArrayFixFlat, CArrayPtrFlat, CArrayFixSeg.
1.22 +// Details:
1.23 +// - Create an array of fixed length buffer descriptor objects contained within a flat
1.24 +// dynamic and segmented buffer and verify that
1.25 +// - number of elements held in the array is 0.
1.26 +// - length of an element is as specified.
1.27 +// - array is compressed and reset as expected.
1.28 +// - the elements of the array are sorted as expected.
1.29 +// - insertion of a text into the array at specified position and filling a blank space
1.30 +// at the beginning is as expected.
1.31 +// - return value is 0 when available element is searched within the array.
1.32 +// - removal of first element from the array is successful.
1.33 +// - number of elements held in the array is 1 after appending a single element onto
1.34 +// the end of the array.
1.35 +// - the position of specified element is found successfully
1.36 +// - resetting the array is as expected
1.37 +// - End and Back methods are as expected.
1.38 +// - Create an array of fixed length text character objects contained within a flat dynamic
1.39 +// and segmented buffer.
1.40 +// - append a text onto the end of the array, check the contents and number of elements
1.41 +// held in the array are as expected.
1.42 +// - insert a text and verify that the change in the content of array and number of
1.43 +// elements held in the array are as expected.
1.44 +// - remove a single character and multiple characters from the array and verify that
1.45 +// the Delete method is as expected. Compress the array.
1.46 +// - Create an array of fixed length text character objects contained within a flat dynamic
1.47 +// and segmented buffer.
1.48 +// - append strings of specified length onto the end of the array and verify that the
1.49 +// number of elements held in the array is as expected.
1.50 +// - insert strings at specified location and check that the contents are as expected.
1.51 +// - reset the array, append a string, compress the array and verify that content is as
1.52 +// expected.
1.53 +// - sort the array and verify that content is as expected.
1.54 +// - verify the correct position of the element and return value is zero when an element
1.55 +// is found using binary and sequential search technique and nonzero if not present in
1.56 +// the array.
1.57 +// - Create an array of fixed length text character objects contained within a flat dynamic
1.58 +// and segmented buffer.
1.59 +// - Insert some elements into the array at specified positions determined by key of
1.60 +// type TInt and verify that KErrAlreadyExists is returned if an element with the
1.61 +// same key already exists within the array.
1.62 +// - Create an array of pointers to objects implemented using a flat dynamic buffer, insert one
1.63 +// element into the array at the specified position and destroy the object whose pointer form
1.64 +// the element of the array, before resetting the array.
1.65 +// - Create and delete an array of CBase objects contained within a flat dynamic buffer.
1.66 +// - Test whether the heap has been corrupted by all the tests.
1.67 +// Platforms/Drives/Compatibility:
1.68 +// All
1.69 +// Assumptions/Requirement/Pre-requisites:
1.70 +// Failures and causes:
1.71 +// Base Port information:
1.72 +//
1.73 +//
1.74 +
1.75 +#include <e32test.h>
1.76 +
1.77 +class MyCBase : public CBase
1.78 + {
1.79 + };
1.80 +
1.81 +const TInt KTestGranularity=0x02;
1.82 +
1.83 +LOCAL_D RTest test(_L("T_FARRAY"));
1.84 +
1.85 +template <class T,TInt S>
1.86 +class TArr
1.87 + {
1.88 +public:
1.89 + TArr() {}
1.90 + TInt Count() const {return S;}
1.91 + T& operator[](TInt anIndex) {return iArr[anIndex];}
1.92 + const T& operator[](TInt anIndex) const {return iArr[anIndex];}
1.93 +private:
1.94 + T iArr[S];
1.95 + };
1.96 +
1.97 +LOCAL_C void testFix(CArrayFix<TBuf<0x10> >& aFix)
1.98 +//
1.99 +// Test all methods
1.100 +//
1.101 + {
1.102 + test.Next(_L("Test all methods"));
1.103 + test(aFix.Count()==0);
1.104 + test(aFix.Length()==sizeof(TBuf<0x10>));
1.105 + aFix.Compress();
1.106 + test(TRUE);
1.107 + aFix.Reset();
1.108 + test(TRUE);
1.109 + TKeyArrayFix kk(0,ECmpNormal,0x10);
1.110 + test(TRUE);
1.111 + aFix.Sort(kk);
1.112 + test(TRUE);
1.113 + TBuf<0x10> aa(_L("aaaaa"));
1.114 + aFix.InsertL(0,aa);
1.115 + test(TRUE);
1.116 + aFix[0].Fill(' ');
1.117 + test(TRUE);
1.118 + TBuf<0x10> z(aFix[0]);
1.119 + z.Length();
1.120 + test(TRUE);
1.121 + aFix[0].Fill('a');
1.122 + test(TRUE);
1.123 + TInt pp;
1.124 + test(aFix.Find(aa,kk,pp)==0);
1.125 + test(pp==0);
1.126 + aFix.Delete(0);
1.127 + TBuf<0x10> bb(_L("bbbbb"));
1.128 + aFix.AppendL(bb);
1.129 + test(aFix.Count()==1);
1.130 + test(aFix.InsertIsqAllowDuplicatesL(aa,kk)==0);
1.131 + test(aFix.InsertIsqAllowDuplicatesL(bb,kk)==2);
1.132 + test(aFix.FindIsq(aa,kk,pp)==0);
1.133 + test(pp==0);
1.134 + aFix.Reset();
1.135 + for(TInt index=0;index<KTestGranularity*7/2;index++)
1.136 + aFix.AppendL(aa);
1.137 + const TBuf<0x10> *end=NULL;
1.138 + const TBuf<0x10> *ptr=NULL;
1.139 + for(TInt index2=0;index2<KTestGranularity*7/2;index2++)
1.140 + {
1.141 + if (end==ptr)
1.142 + {
1.143 + end=aFix.End(index2);
1.144 + ptr=&aFix[index2];
1.145 + TInt seglen=end-ptr;
1.146 + test(seglen==KTestGranularity || seglen==(aFix.Count()-index2));
1.147 + }
1.148 + test(&aFix[index2]==ptr++);
1.149 + }
1.150 + const TBuf<0x10> *bak=NULL;
1.151 + ptr=NULL;
1.152 + for(TInt index3=KTestGranularity*7/2;index3>0;index3--)
1.153 + {
1.154 + if (bak==ptr)
1.155 + {
1.156 + bak=aFix.Back(index3);
1.157 + ptr=&aFix[index3-1]+1;
1.158 + TInt seglen=ptr-bak;
1.159 + test(seglen==KTestGranularity || seglen==index3 || seglen==index3%KTestGranularity);
1.160 + }
1.161 + test(&aFix[index3-1]==--ptr);
1.162 + }
1.163 + }
1.164 +
1.165 +LOCAL_C void test1(CArrayFix<TText>& aFix)
1.166 +//
1.167 + {
1.168 + test.Next(_L("AppendL and InsertL single chars"));
1.169 + aFix.AppendL(_S("abcd"),4);
1.170 + test(aFix[0]=='a');
1.171 + test(aFix[1]=='b');
1.172 + test(aFix[3]=='d');
1.173 + test(aFix.Count()==4);
1.174 + aFix.InsertL(2,_S("ef"),2);
1.175 + test(aFix[1]=='b');
1.176 + test(aFix[2]=='e');
1.177 + test(aFix[4]=='c');
1.178 + test(aFix.Count()==6);
1.179 + aFix.AppendL(TText('z'));
1.180 + test(aFix[6]=='z');
1.181 + aFix.InsertL(0,TText('y'));
1.182 + test(aFix[0]=='y');
1.183 + test(aFix[1]=='a');
1.184 + test(aFix.Count()==8);
1.185 + test.Next(_L("Delete single chars"));
1.186 + aFix.Delete(3);
1.187 + test(aFix[2]=='b');
1.188 + test(aFix[3]=='f');
1.189 + test(aFix[4]=='c');
1.190 + aFix.Delete(1,2);
1.191 + test(aFix[0]=='y');
1.192 + test(aFix[1]=='f');
1.193 + test(aFix[2]=='c');
1.194 + test(aFix.Count()==5);
1.195 + aFix.Compress();
1.196 + }
1.197 +
1.198 +LOCAL_C void test2(CArrayFix<TArr<TText,4> >& aFix)
1.199 +//
1.200 + {
1.201 + test(aFix.Length()==sizeof(TArr<TText,4>));
1.202 + test.Next(_L("AppendL and insert strings of length 4"));
1.203 + TPtrC des1=_L("abcd");
1.204 + TPtrC des2=_L("efgh");
1.205 + aFix.AppendL(*(const TArr<TText,4>*)des1.Ptr());
1.206 + aFix.AppendL(*(const TArr<TText,4>*)des2.Ptr());
1.207 + test(aFix.Count()==2);
1.208 + TPtrC des3(&aFix[0][0],4);
1.209 + TPtrC des4(&aFix[1][0],4);
1.210 + test(des3==_L("abcd"));
1.211 + test(des4==_L("efgh"));
1.212 + aFix.InsertL(1,*(const TArr<TText,4>*)_S("ijkl"));
1.213 + test(aFix.Count()==3);
1.214 + TPtrC des5(&aFix[2][0],4);
1.215 + test(des3==_L("abcd"));
1.216 + test(des4==_L("ijkl"));
1.217 + test(des5==_L("efgh"));
1.218 +
1.219 + test.Next(_L("Reset and Compress"));
1.220 + aFix.Reset();
1.221 + TBuf<0x10> buf1=_L("abcdefgh");
1.222 + aFix.AppendL((const TArr<TText,4>*)buf1.Ptr(),2);
1.223 + aFix.Compress();
1.224 + TPtrC des6(&aFix[0][0],4);
1.225 + test(des6==_L("abcd"));
1.226 + TPtrC des7(&aFix[1][0],4);
1.227 + test(des7==_L("efgh"));
1.228 + buf1=_L("ghighhxy");
1.229 + aFix.InsertL(1,(const TArr<TText,4>*)buf1.Ptr(),2);
1.230 + aFix.Compress();
1.231 + TPtrC des8(&aFix[0][0],4);
1.232 + test(des8==_L("abcd"));
1.233 + TPtrC des9(&aFix[1][0],4);
1.234 + test(des9==_L("ghig"));
1.235 + TPtrC des10(&aFix[2][0],4);
1.236 + test(des10==_L("hhxy"));
1.237 + TPtrC des11(&aFix[3][0],4);
1.238 + test(des11==_L("efgh"));
1.239 +
1.240 + test.Next(_L("Sort strings"));
1.241 + TKeyArrayFix kk(0,ECmpNormal,0x04);
1.242 + aFix.Sort(kk);
1.243 + TPtrC des12(&aFix[0][0],4);
1.244 + test(des12==_L("abcd"));
1.245 + TPtrC des13(&aFix[1][0],4);
1.246 + test(des13==_L("efgh"));
1.247 + TPtrC des14(&aFix[2][0],4);
1.248 + test(des14==_L("ghig"));
1.249 + TPtrC des15(&aFix[3][0],4);
1.250 + test(des15==_L("hhxy"));
1.251 +
1.252 + test.Next(_L("Find and FindIsq"));
1.253 + aFix.Compress();
1.254 + test(aFix.InsertIsqL(*(const TArr<TText,4>*)_S("ffff"),kk)==2);
1.255 + aFix.Compress();
1.256 + test(aFix.InsertIsqAllowDuplicatesL(*(const TArr<TText,4>*)_S("ffff"),kk)==3);
1.257 + aFix.Compress();
1.258 + TRAPD(r,aFix.InsertIsqL(*(const TArr<TText,4>*)_S("ffff"),kk))
1.259 + test(r==KErrAlreadyExists);
1.260 + TInt aPos=0;
1.261 + test(aFix.Find(*(const TArr<TText,4>*)_S("xxxx"),kk,aPos)==1);
1.262 + test(aPos==6);
1.263 + test(aFix.Find(*(const TArr<TText,4>*)_S("abcd"),kk,aPos)==0);
1.264 + test(aPos==0);
1.265 + test(aFix.Find(*(const TArr<TText,4>*)_S("ghig"),kk,aPos)==0);
1.266 + test(aPos==4);
1.267 + test(aFix.Find(*(const TArr<TText,4>*)_S("ffff"),kk,aPos)==0);
1.268 + test(aPos==2);
1.269 + test(aFix.Find(*(const TArr<TText,4>*)_S("hhxy"),kk,aPos)==0);
1.270 + test(aPos==5);
1.271 + test(aFix.FindIsq(*(const TArr<TText,4>*)_S("bbbb"),kk,aPos)!=0);
1.272 + test(aPos==1);
1.273 + test(aFix.FindIsq(*(const TArr<TText,4>*)_S("abcd"),kk,aPos)==0);
1.274 + test(aPos==0);
1.275 + test(aFix.FindIsq(*(const TArr<TText,4>*)_S("ghig"),kk,aPos)==0);
1.276 + test(aPos==4);
1.277 + test(aFix.FindIsq(*(const TArr<TText,4>*)_S("ffff"),kk,aPos)==0);
1.278 + test(aPos==2);
1.279 + test(aFix.InsertIsqL(*(const TArr<TText,4>*)_S("fghz"),kk)==4);
1.280 + test(aFix.FindIsq(*(const TArr<TText,4>*)_S("fghz"),kk,aPos)==0);
1.281 + test(aPos==4);
1.282 + test(aFix.FindIsq(*(const TArr<TText,4>*)_S("hhxy"),kk,aPos)==0);
1.283 + test(aPos==6);
1.284 + }
1.285 +
1.286 +LOCAL_C void test3(CArrayFix<TInt>& aFix)
1.287 + {
1.288 +
1.289 + test.Next(_L("InsertIsqL"));
1.290 + TKeyArrayFix kk(0,ECmpTInt);
1.291 +
1.292 + TInt pos=0;
1.293 + TInt mod=47;
1.294 + TInt inc=23;
1.295 + TInt i=0;
1.296 +
1.297 + FOREVER
1.298 + {
1.299 + TInt ret;
1.300 + if (i&1)
1.301 + TRAP(ret,aFix.InsertIsqL(i,kk))
1.302 + else
1.303 + {
1.304 + TRAP(ret,pos=aFix.InsertIsqL(i,kk))
1.305 + if (ret==KErrNone)
1.306 + test(aFix[pos]==i);
1.307 + }
1.308 + if (ret==KErrAlreadyExists)
1.309 + break;
1.310 + i=(i+inc)%mod;
1.311 + }
1.312 +
1.313 + for(i=0;i<mod;i++)
1.314 + {
1.315 + test(aFix.FindIsq(i,kk,pos)==0);
1.316 + test(pos==i);
1.317 + TRAPD(r,aFix.InsertIsqL(i,kk))
1.318 + test(r==KErrAlreadyExists);
1.319 + }
1.320 + }
1.321 +
1.322 +GLDEF_C TInt E32Main()
1.323 +//
1.324 +// Test the Array classes.
1.325 +//
1.326 + {
1.327 +
1.328 + test.Title();
1.329 + __UHEAP_MARK;
1.330 + test.Start(_L("class CArrayFixFlat"));
1.331 + CArrayFixFlat<TBuf<0x10> >* pFixFlat=new CArrayFixFlat<TBuf<0x10> >(KTestGranularity);
1.332 + if (pFixFlat==NULL)
1.333 + test.Panic(_L("Allocating array"));
1.334 + testFix(*pFixFlat);
1.335 + delete pFixFlat;
1.336 +
1.337 + CArrayFixFlat<TText>* pFixFlatChar=new CArrayFixFlat<TText>(KTestGranularity);
1.338 + test1(*pFixFlatChar);
1.339 + delete pFixFlatChar;
1.340 +
1.341 + CArrayFixFlat<TArr<TText,4> >* pFixFlatArr=new CArrayFixFlat<TArr<TText,4> >(KTestGranularity);
1.342 + test2(*pFixFlatArr);
1.343 + delete pFixFlatArr;
1.344 +
1.345 + CArrayFixFlat<TInt>* pFixFlatInt=new CArrayFixFlat<TInt>(KTestGranularity);
1.346 + test3(*pFixFlatInt);
1.347 + delete pFixFlatInt;
1.348 +
1.349 + test.Next(_L("class CArrayPtrFlat of CBase"));
1.350 +
1.351 + CArrayPtrFlat<MyCBase>* pPtrFlatCBase=new CArrayPtrFlat<MyCBase>(KTestGranularity);
1.352 + if (pPtrFlatCBase==NULL)
1.353 + test.Panic(_L("Allocating array of CBase*"));
1.354 + MyCBase* c1 = new MyCBase();
1.355 + pPtrFlatCBase->InsertL(0,&c1,1);
1.356 + pPtrFlatCBase->ResetAndDestroy();
1.357 +// test(pFixFlatCBase->iBase==0);
1.358 + pPtrFlatCBase->ResetAndDestroy();
1.359 + delete pPtrFlatCBase;
1.360 +
1.361 + test.Next(_L("class CArrayFixFlat of CBase"));
1.362 +
1.363 + CArrayFixFlat<MyCBase>* pFixFlatCBase=new CArrayFixFlat<MyCBase>(KTestGranularity);
1.364 + if (pFixFlatCBase==NULL)
1.365 + test.Panic(_L("Allocating array of CBase"));
1.366 + delete pFixFlatCBase;
1.367 +
1.368 + test.Next(_L("class CArrayFixSeg"));
1.369 + CArrayFixSeg<TBuf<0x10> >* pFixSeg=new CArrayFixSeg<TBuf<0x10> >(KTestGranularity);
1.370 + if (pFixSeg==NULL)
1.371 + test.Panic(_L("Allocating array"));
1.372 + testFix(*pFixSeg);
1.373 + delete pFixSeg;
1.374 +
1.375 + CArrayFixSeg<TText>* pFixSegChar=new CArrayFixSeg<TText>(KTestGranularity);
1.376 + test1(*pFixSegChar);
1.377 + delete pFixSegChar;
1.378 +
1.379 + CArrayFixSeg<TArr<TText,4> >* pFixSegArr=new CArrayFixSeg<TArr<TText,4> >(KTestGranularity);
1.380 + test2(*pFixSegArr);
1.381 + delete pFixSegArr;
1.382 +
1.383 + CArrayFixSeg<TInt>* pFixSegInt=new CArrayFixSeg<TInt>(KTestGranularity);
1.384 + test3(*pFixSegInt);
1.385 + delete pFixSegInt;
1.386 +
1.387 + test.End();
1.388 + __UHEAP_MARKEND;
1.389 + return(0);
1.390 + }
1.391 +