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_bseg.cpp
16 // Test all aspects of the CBufSeg class.
20 // - Create a segmented dynamic buffer of specified size.
21 // - Test all operations of CBufSeg class and see if methods are implemented --
22 // including NewL, Reset, Size, InsertL, Delete, Ptr, BackPtr, Read, Write and Compress.
23 // - Test CBufSeg constructor is as expected.
24 // - Insert data into the segmented dynamic buffer and verify that InsertL method
25 // is as expected using the Read and Write methods.
26 // - Delete all data using Reset method and check size is zero.
27 // - Test InsertL, Read, Write and Length methods.
28 // - Test Ptr, Free, Size, Backptr and SetReserveL methods are as expected.
29 // - Check self consistancy of segment lengths, and check contents of segmented
30 // buffer using Ptr() and BackPtr().
31 // - Verify the size of the of the test buffers are correct.
32 // - Insert data into the buffer, delete some data from the beginning, middle and end
33 // then check for results as expected.
34 // - Verify the data in the buffer before and after Compress method is as expected.
35 // Platforms/Drives/Compatibility:
37 // Assumptions/Requirement/Pre-requisites:
38 // Failures and causes:
39 // Base Port information:
45 class TBufSegLink : public TDblQueLink
48 inline TBufSegLink() : iLen(0) {}
49 inline TBufSegLink* Next() const {return((TBufSegLink*)iNext);}
50 inline TBufSegLink* Prev() const {return((TBufSegLink*)iPrev);}
58 TestCBufSeg() : iDummy(0) {};
59 void Test1(); // Test all operations of the class.
60 void Test2(); // Inherited Methods
61 void Test3(); // Insert
62 void Test4(); // Delete
63 void Test5(); // Compress
64 void Test6L(); // borderlines
67 static const TInt SegLen;
68 void CheckSeg(const CBufSeg*);
69 void CheckContents1(CBufSeg* const);
70 void CheckContents2(CBufSeg* const);
73 const TInt TestCBufSeg::SegLen=64;
75 LOCAL_D RTest test(_L("T_BSEG"));
77 class CSpy : public CBufBase
82 CSpy(TInt anExpandSize);
84 TDblQue<TBufSegLink> iQue;
90 GLDEF_C void TestCBufSeg::CheckSeg(const CBufSeg* bf)
92 // Check Self consistancy of segment lengths
99 TBufSegLink* p1=((CSpy*)bf)->iQue.First();
100 while (!((CSpy*)bf)->iQue.IsHead(p1))
102 test(p1->iLen<=bf->Size());
106 test(sum==bf->Size());
108 p1=((CSpy*)bf)->iQue.Last();
109 while (!((CSpy*)bf)->iQue.IsHead(p1))
111 test(p1->iLen<=bf->Size());
115 test(sum==bf->Size());
118 GLDEF_C void TestCBufSeg::CheckContents1(CBufSeg* const bf)
120 // Check contents of segmented buffer using Ptr()
125 TInt nbytes=bf->Size();
126 for (TInt pos=0;pos<nbytes;)
128 TPtr8 p=bf->Ptr(pos);
130 TInt8* pT=(TInt8*)p.Ptr();
131 for (TInt i=0;i<len;i++)
138 GLDEF_C void TestCBufSeg::CheckContents2(CBufSeg* const bf)
140 // Check contents of segmented buffer using BackPtr()
145 TInt nbytes=bf->Size();
146 for(TInt pos=nbytes;pos>0;)
148 TPtr8 p=bf->BackPtr(pos);
150 TInt8* pT=(TInt8*)p.Ptr();
151 for (TInt i=0;i<len;i++)
158 GLDEF_C void TestCBufSeg::Test1()
160 // Test all operations of BufSeg class
164 test.Start(_L("Test all operations of CBufSeg"));
165 CBufSeg* bf=(CBufSeg*)CBufSeg::NewL(100);
168 bf->InsertL(0,TPtrC8((TText8*)"Hello World"));
170 TText8* tp=(TText8*)bf->Ptr(3).Ptr();
171 tp=(TText8*)bf->BackPtr(3).Ptr();
180 GLDEF_C void TestCBufSeg::Test2()
182 // Test all inherited methods
186 TBuf8<0x40> tb1=(TText8*)"Hello World";
187 TBuf8<0x40> tb2=(TText8*)"String number two";
189 test.Start(_L("Free,Size,Read,Write,Reset"));
190 CBufSeg* bf=(CBufSeg*)CBufSeg::NewL(SegLen);
193 test(bf->Size()==tb1.Length());
195 test(tb3==tb1.Right(5));
197 bf->Read(0,tb3,bf->Size());
198 test(tb3==TPtrC8((TText8*)"HStringorld"));
201 while (bf->Size()<400)
203 bf->InsertL(bf->Size(),tb1);
204 bf->InsertL(bf->Size(),tb2);
209 bf->Read(i,tb3,tb1.Size());
212 bf->Read(i,tb3,tb2.Size());
227 bf->Read(i,tb3,tb2.Size());
230 bf->Read(i,tb3,tb1.Size());
238 GLDEF_C void TestCBufSeg::Test3()
240 // Test input methods
246 test.Start(_L("InsertL"));
247 CBufSeg* bf1=(CBufSeg*)CBufSeg::NewL(SegLen);
248 CBufSeg* bf2=(CBufSeg*)CBufSeg::NewL(SegLen);
249 CBufSeg* bf3=(CBufSeg*)CBufSeg::NewL(SegLen);
252 for(TInt j=0;j<20;j++)
254 for(TInt i=0;i<10*j;i+=2)
260 bf1->InsertL(bf1->Size()/3*2,&bb[0],10*j);
264 bf2->InsertL(bf2->Size(),&bb[0],10*j);
268 bf3->InsertL(0,&bb[0],10*j);
274 test(nbytes==bf1->Size());
275 test(nbytes==bf2->Size());
276 test(nbytes==bf3->Size());
283 GLDEF_C void TestCBufSeg::Test4()
290 test.Start(_L("Delete"));
291 CBufSeg* bf1=(CBufSeg*)CBufSeg::NewL(SegLen);
292 CBufSeg* bf2=(CBufSeg*)CBufSeg::NewL(SegLen);
293 CBufSeg* bf3=(CBufSeg*)CBufSeg::NewL(SegLen);
296 for(TInt j=0;j<20;j++)
298 for(TInt i=0;i<10*j;i+=2)
304 bf1->InsertL(bf1->Size()/3*2,&bb[0],10*j);
305 bf2->InsertL(bf2->Size(),&bb[0],10*j);
306 bf3->InsertL(0,&bb[0],10*j);
313 for (TInt pos=0;pos<nbytes;pos+=44)
318 bf1->Delete(pos,len);
322 aLength=bf2->Ptr(0).Length();
323 aLength=((aLength>len) ? aLength : len);
324 bf2->Delete(aLength-len,len);
333 test(nbytes==bf1->Size());
334 test(nbytes==bf2->Size());
335 test(nbytes==bf3->Size());
344 GLDEF_C void TestCBufSeg::Test5()
351 test.Start(_L("Compress"));
352 CBufSeg* bf1=(CBufSeg*)CBufSeg::NewL(SegLen);
353 CBufSeg* bf2=(CBufSeg*)CBufSeg::NewL(SegLen);
354 CBufSeg* bf3=(CBufSeg*)CBufSeg::NewL(SegLen);
357 for(TInt j=0;j<20;j++)
359 for(TInt i=0;i<10*j;i+=2)
365 bf1->InsertL(bf1->Size()/3*2,&bb[0],10*j);
366 bf2->InsertL(bf2->Size(),&bb[0],10*j);
367 bf3->InsertL(0,&bb[0],10*j);
374 for (TInt pos=0;pos<nbytes;pos+=44)
378 bf1->Delete(pos,len);
383 aLength=bf2->Ptr(0).Length();
384 aLength=((aLength>len)? aLength : len);
385 bf2->Delete(aLength-len,len);
396 test(nbytes==bf1->Size());
397 test(nbytes==bf2->Size());
398 test(nbytes==bf3->Size());
407 void TestCBufSeg::Test6L()
409 test.Start(_L("Test compress frees empty cells"));
411 TUint8 alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
412 CBufSeg* buf = CBufSeg::NewL(10);
413 CleanupStack::PushL(buf);
414 buf->InsertL(0, alphabet, 16); // "abcdefghij" ++ "klmnop"
415 buf->Delete(5, 5); // "abcde" ++ "klmnop"
416 buf->Delete(10, 1); // "abcde" ++ "klmno"
417 buf->Compress(); // "abcdefklmno". i.e. empty cell should be freed.
418 CleanupStack::PopAndDestroy(buf);
423 LOCAL_C void test_CBufSeg()
425 // Test the BufSeg class
430 test.Start(_L("All operations"));
432 test.Next(_L("Inherited Methods"));
434 test.Next(_L("Insert"));
436 test.Next(_L("Delete"));
438 test.Next(_L("Compress"));
440 test.Next(_L("Bordeline cases"));
447 GLDEF_C TInt E32Main()
449 // Test the ADT segmented varray.
456 // Install a trap handler
458 CTrapCleanup* trapHandler=CTrapCleanup::New();
459 test(trapHandler!=NULL);
460 // CleanupStack::NextLevel();
461 test.Start(_L("class CBufSeg"));