First public contribution.
1 // Copyright (c) 2004-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_rbuf.cpp
16 // Test methods of the RBuf16, RBuf8, RBuf template class.
18 // RBuf16, RBuf8, RBuf.
20 // For RBuf8, RBuf16 and RBuf objects:
21 // - Test the Create and CreateMax methods by verifying the return value of
22 // KErrNone, the initial length and max length. Perform basic write and read
23 // operations and verify the results.
24 // - Test the CreateL and CreateMaxL methods by verifying the return value of
25 // KErrNone. Also force a heap error and verify return value of KErrNoMemory.
26 // - Test the Create(const TDesC_& aDesc) and Create(const TDesCX_ aDesc,
27 // TInt aMaxLength) methods by verifying the return value of KErrNone. Verify
28 // initial length, max length and initialisation.
29 // - Test the CreateL(const TDesC_& aDesc) and CreateMaxL(const TDesCX_ aDesc,
30 // TInt aMaxLength) methods by verifying the return value of KErrNone. Also
31 // force a heap error and verify return value of KErrNoMemory.
32 // - Test the Swap method by creating two initialised objects, calling Swap
33 // and confirming the results as expected.
34 // - Test the Assign method by performing an assign from a variety of sources
35 // and verifying the results are as expected.
36 // - Test the ReAlloc method in a variety of scenarios that decrease memory,
37 // increase memory and zero-length memory. Verify that the results are as
39 // - Test the ReAllocL by verifying the return value of KErrNone. Also force
40 // a heap error and verify return value of KErrNoMemory. Verify that the
41 // object is the same as before the failed ReAllocL call.
42 // - Test the CleanupClosePushL method via CleanupStack::PopAndDestroy().
43 // - Force the CleanupClosePushL to leave to check cleanup of RBuf.
44 // Platforms/Drives/Compatibility:
46 // Assumptions/Requirement/Pre-requisites:
47 // Failures and causes:
48 // Base Port information:
56 LOCAL_D RTest test(_L("T_RBUF"));
59 #define _TS(a) ((const TTEXT*)RTest::String(sizeof(TTEXT),(TText8*)a,(TText16*)L ## a))
62 Tests the following methods.
63 - TInt Create(TInt aMaxLength);
64 - TInt CreateMax(TInt aMaxLength);
67 LOCAL_C void TestCreate(RBUF*)
71 test.Next(_L("Create(TInt aMaxLength) method"));
73 test(rBuf.Create(19)==KErrNone); //Create RBuf as EPtr type
74 test(rBuf.Length()==0);
75 test(rBuf.MaxLength()==19);
77 rBuf[1] = 1; //Try basic write & ...
78 test(rBuf[1] == 1); //... read
81 test(rBuf.Create(0)==KErrNone); //Create zero length RBuf as EPtr type
82 test(rBuf.Length()==0);
83 test(rBuf.MaxLength()==0);
86 test.Next(_L("CreateMax(TInt aMaxLength) method"));
88 test(rBuf.CreateMax(20)==KErrNone); //Create RBuf as EPtr type
89 test(rBuf.Length()==20);
90 test(rBuf.MaxLength()==20);
97 Tests the following methods.
98 - void CreateL(TInt aMaxLength);
99 - void CreateMaxL(TInt aMaxLength);
102 LOCAL_C void TestCreateLeaving(RBUF*)
106 test.Next(_L("CreateL(TInt aMaxLength) method"));
108 TRAPD(ret, rBuf.CreateL(20)); //Create RBuf as EPtr type
109 test(KErrNone == ret);
113 __UHEAP_FAILNEXT(1); //Set the next alloc to fail
114 TRAP(ret, rBuf.CreateL(10));
115 test(KErrNoMemory == ret); // It fails due to __UHEAP_FAILNEXT(1);
118 test.Next(_L("CreateMaxL(TInt aMaxLength) method"));
120 TRAP(ret, rBuf.CreateMaxL(20)); //Create RBuf as EPtr type
121 test(KErrNone == ret);
125 __UHEAP_FAILNEXT(1); //Set the next alloc to fail
126 TRAP(ret, rBuf.CreateMaxL(10));
127 test(KErrNoMemory == ret); // It fails due to __UHEAP_FAILNEXT(1);
132 Tests the following methods.
133 - TInt Create(const TDesC_& aDesc);
134 - TInt Create(const TDesC_& aDesc, TInt aMaxLength));
136 template<class RBUF, class TBUF, class TTEXT>
137 LOCAL_C void TestCreateFromDes(RBUF*)
140 TBUF des (_TS("012345"));
142 test.Next(_L("Create(const TDesC_& aDesc) method"));
144 test(rBuf.Create(des)==KErrNone); //Create RBuf as EPtr type
148 test.Next(_L("Create(const TDesCX_ aDesc, TInt aMaxLength) method"));
150 test(rBuf.Create(des, des.Length())==KErrNone); //Create RBuf as EPtr type
154 test(rBuf.Create(des, des.Length()-2)==KErrNone); //Create RBuf as EPtr type
155 test(rBuf.Length()==4);
156 test(rBuf.MaxLength()==4);
157 test(rBuf[0] == (TTEXT)('0'));
158 test(rBuf[3] == (TTEXT)('3'));
162 test(rBuf.Create(des, des.Length()+2)==KErrNone); //Create RBuf as EPtr type
163 test(rBuf.Length()==6);
164 test(rBuf.MaxLength()==8);
170 Tests the following methods.
171 - void CreateL(const TDesC_& aDesc);
172 - void CreateMaxL(const TDesC_& aDesc, TInt aMaxLength);
174 template<class RBUF, class TBUF, class TTEXT>
175 LOCAL_C void TestCreateFromDesLeaving(RBUF*)
178 TBUF des (_TS("123456"));
180 test.Next(_L("CreateL(const TDesC_& aDesc) method"));
182 TRAPD(ret, rBuf.CreateL(des)); //Create RBuf as EPtr type
183 test(KErrNone == ret);
187 __UHEAP_FAILNEXT(1); //Set the next alloc to fail
188 TRAP(ret, rBuf.CreateL(des));
189 test(KErrNoMemory == ret); // This will fail due to __UHEAP_FAILNEXT(1);
192 test.Next(_L("CreateL(const TDesC_& aDesc, TInt aMaxLength) method"));
194 TRAP(ret, rBuf.CreateL(des, des.Length())); //Create RBuf as EPtr type
195 test(KErrNone == ret);
199 __UHEAP_FAILNEXT(1); //Set the next alloc to fail
200 TRAP(ret, rBuf.CreateL(des, des.Length()));
201 test(KErrNoMemory == ret); // It fails due to __UHEAP_FAILNEXT(1);
206 Tests the following methods:
207 - TInt Assign(const RBuf_& rBuf);
208 - TInt Assign(TUint* aHeapCell, TInt aMaxLength);
209 - TInt Assign(TUint* aHeapCell, TInt aLength, TInt aMaxLength);
210 - TInt Assign(HBufC& aHBuf);
211 - RBuf(HBufC_&) constructor.
213 template<class RBUF, class TBUF, class TTEXT, class HBUF>
214 LOCAL_C void TestAssign(RBUF*)
217 TBUF des (_TS("123456"));
220 test.Next(_L("Assign(const RBuf_& aRBuf) method"));
227 test.Next(_L("Assign(TUint* aHeapCell, TInt aLength, TInt aMaxLength ) method"));
229 TTEXT* heap = (TTEXT*)User::Alloc(24*(TInt)sizeof(TTEXT)); //Allocate 48 bytes for 24 long RBuf16
230 rBuf.Assign(heap, 12,24);
231 test(rBuf.Length() == 12);
232 test(rBuf.MaxLength() == 24);
236 rBuf.Assign(heap, 0,0);
237 test(rBuf.Length() == 0);
238 test(rBuf.MaxLength() == 0);
241 test.Next(_L("Assign(TUint* aHeapCell, TInt aMaxLength ) method"));
243 heap = (TTEXT*)User::Alloc(24*(TInt)sizeof(TTEXT)); //Allocate 48 bytes for 24 long RBuf16
244 rBuf.Assign(heap, 24);
245 test(rBuf.Length() == 0);
246 test(rBuf.MaxLength() == 24);
249 test.Next(_L("Assign(HBufC_* aHBuf) method"));
251 HBUF* hBuf = HBUF::NewMax(11);
252 rBuf.Assign(hBuf); //Create RBuf as EBufCPtr type
253 test(rBuf.Length() == 11);
254 test(rBuf.MaxLength() >= 11); //There could me more allocated memory - see HBufC8::Des()
257 test.Next(_L("RBuf_(HBufC_* aHBuf) constructor"));
259 hBuf = HBUF::NewMax(12); //Create RBuf as EBufCPtr
261 test(rBuf3.Length() == 12);
262 test(rBuf3.MaxLength() >= 12);
265 hBuf = HBUF::NewMax(0);
266 RBUF rBuf4(hBuf); //The length of aHBuf is zero
267 test(rBuf4.Length() == 0);
270 hBuf = NULL; //aHBuf is NULL
272 test(rBuf5.Length() == 0);
273 test(rBuf5.MaxLength() == 0);
278 Tests the following methods.
279 - TInt ReAlloc(TInt aMaxLength);
281 template<class RBUF, class TBUF, class TTEXT, class HBUF>
282 LOCAL_C void TestReAlloc(RBUF*)
286 TBUF des (_TS("0123456"));
289 test.Next(_L("ReAlloc(TInt aMaxLength) method"));
291 //reallocate EPtr type - decrease memory
292 test(rBuf.Create(des)==KErrNone); //Create as EPtr
294 test(rBuf.ReAlloc(3)==KErrNone); //ReAlloc to EPtr
295 test(rBuf.MaxLength()>=3);
296 test(rBuf.Length()==3);
297 test(rBuf[0] == (TTEXT)('0'));
298 test(rBuf[2] == (TTEXT)('2'));
301 //reallocate EPtr type - increase memory
302 test(rBuf.Create(des,des.MaxLength())==KErrNone); //Create as EPtr
303 test(rBuf.ReAlloc(15)==KErrNone); //ReAlloc to EPtr
304 test(rBuf.MaxLength()==15);
305 test(rBuf.Length()==7);
306 test(rBuf[0] == (TTEXT)('0'));
307 test(rBuf[6] == (TTEXT)('6'));
311 //reallocate EBufCPtr type - decrease memory
312 HBUF* hBuf = HBUF::NewMax(9);
313 *hBuf = _TS("012345678");
314 rBuf.Assign(hBuf); //Create as EBufCPtr
316 test(rBuf.ReAlloc(5)==KErrNone); //ReAlloc to EBufCPtr
317 test(rBuf.MaxLength()>=5);//There could be more allocated memory - see HBufC8::Des()
318 test(rBuf.Length()==5);
319 test(rBuf[0] == (TTEXT)('0'));
320 test(rBuf[4] == (TTEXT)('4'));
323 //reallocate EBufCPtr type - increase memory
324 hBuf = HBUF::NewMax(9);
325 *hBuf = _TS("012345678");
326 rBuf.Assign(hBuf); //Create as EBufCPtr
327 test(rBuf.ReAlloc(15)==KErrNone); //ReAlloc to EBufCPtr
328 test(rBuf.MaxLength()>=15);//There could be more allocated memory - see HBufC8::Des()
329 test(rBuf.Length()==9);
330 test(rBuf[0] == (TTEXT)('0'));
331 test(rBuf[8] == (TTEXT)('8'));
334 //reallocate EPtr type - to zero-length
335 test(rBuf.Create(des)==KErrNone); //Create as EPtr
337 test(rBuf.ReAlloc(0)==KErrNone); //ReAlloc to EPtr
338 test(rBuf.MaxLength()==0);
339 test(rBuf.Length()==0);
342 //reallocate EBufCPtr type to zero-length
343 hBuf = HBUF::NewMax(9);
344 *hBuf = _TS("012345678");
345 rBuf.Assign(hBuf); //Create as EBufCPtr
347 test(rBuf.ReAlloc(0)==KErrNone); //ReAlloc to EPtr
348 test(rBuf.MaxLength()==0);
349 test(rBuf.Length()==0);
352 //reallocate from zero-length
353 rBuf.Create(0); //Create as EPtr
354 test(rBuf.ReAlloc(9)==KErrNone); //ReAlloc to EPtr
355 test(rBuf.MaxLength()==9);
356 test(rBuf.Length()==0);
359 //reallocate from zero-length EBufCPtr to EPtr
360 struct dummy // make it look like RBuf16
364 HBUF* iEBufCPtrType; //Pointer to buffer data
367 // reference rBuf as our dummy..
368 dummy &drBuf = (dummy&) rBuf;
369 rBuf.Assign(HBUF::NewL(0)); //Create as EBufCPtr
370 test(EBufCPtr == (drBuf.iLength>>KShiftDesType));
371 rBuf.Close(); // the actual behavior causes memory leaks, so we should close it first.
372 test(rBuf.ReAlloc(13)==KErrNone); // ReAlloc changes it from EBufCPtr to EPtr
373 test(EPtr == (drBuf.iLength>>KShiftDesType));
374 test(rBuf.MaxLength() == 13);
375 test(rBuf.Length() == 0);
378 //reallocate from zero-length to zero-length
379 rBuf.Create(0); //Create as EPtr
380 test(rBuf.ReAlloc(0)==KErrNone); //ReAlloc to EPtr
381 test(rBuf.Length() == 0);
382 test(rBuf.MaxLength() == 0);
388 Tests the following methods.
389 - TInt ReAllocL(TInt aMaxLength);
391 template<class RBUF, class TBUF, class TTEXT>
392 LOCAL_C void TestReAllocLeaving(RBUF*)
398 test.Next(_L("ReAllocL(TInt aMaxLength) method"));
400 test(rBuf.Create(des) ==KErrNone);
401 TRAPD(ret, rBuf.ReAllocL(6)); //ReAlloc buffer
402 test(KErrNone == ret);
406 TRAP(ret, rBuf.ReAllocL(100)); //Realloc buffer. This should fail.
407 test(KErrNoMemory == ret);
410 test(rBuf.MaxLength()==6); //Check RBuf is the same as before ...
411 test(rBuf.Length()==2); //... ReAlloc that failed.
412 test(rBuf[0] == (TTEXT)('0'));
413 test(rBuf[1] == (TTEXT)('1'));
418 Tests the following methods.
419 - void Swap(RBuf_& aBuf);
421 template<class RBUF, class TBUF, class TTEXT>
422 LOCAL_C void TestSwap(RBUF*)
425 TBUF des1(_TS("12"));
426 TBUF des2 (_TS("345678"));
428 test.Next(_L("Swap(RBuf_& aRBuf) method"));
430 test(rBuf1.Create(des1) ==KErrNone);
431 test(rBuf2.Create(des2) ==KErrNone);
443 Test assignemnt operator.
445 template<class RBUF, class TBUF, class TBUFC, class TTEXT>
446 LOCAL_C void TestAssignmentOperator()
448 test.Next(_L("Assignment operator"));
450 TBUF tdes(_TS("Modifiable descriptor"));
451 TBUFC tdesc(_TS("Non-modifiable descriptor"));
456 rbuf2.Copy(_TS("Buffer descriptor"), 17);
458 rbuf = tdesc; test(rbuf == tdesc);
459 rbuf = tdes; test(rbuf == tdes);
460 rbuf = rbuf2; test(rbuf == rbuf2);
467 Tests the following methods.
468 - void CleanupClosePushL();
470 template<class RBUF> LOCAL_C void TestCleanupClosePushL(RBUF*)
474 test.Next(_L("CleanupClosePushL() method"));
475 test(KErrNone == rBuf.Create(10));
476 rBuf.CleanupClosePushL();
477 CleanupStack::PopAndDestroy();
481 This function will intentionally leave to check cleanup of RBuf.
482 To be called in debug build only. Otherwise will panic.
484 template<class RBUF> LOCAL_C void TestRBufCleanupL(RBUF*)
488 test.Next(_L("Test cleanup of RBuf"));
489 test(KErrNone == rBuf.Create(10));
490 rBuf.CleanupClosePushL();
493 TInt* ptr = (TInt*)User::AllocL(20); //This should leave
494 *ptr = 0; //Avoid compiler warning
495 User::Panic(_L("Should not reach this line"),0);
498 GLDEF_C TInt E32Main()
504 CTrapCleanup* trapHandler=CTrapCleanup::New();
505 test(trapHandler!=NULL);
508 test.Start(_L("Testing RBuf8, RBuf16 & RBuf classes"));
512 test.Start(_L("Testing class RBuf8 ..."));
513 TestCreate<RBuf8>(r8);
514 TestCreateLeaving<RBuf8>(r8);
515 TestCreateFromDes<RBuf8,TBuf8<11>,TText8>(r8);
516 TestCreateFromDesLeaving<RBuf8,TBuf8<11>,TText8>(r8);
517 TestSwap<RBuf8,TBuf8<11>,TText8>(r8);
518 TestAssign<RBuf8,TBuf8<11>,TText8,HBufC8>(r8);
519 TestReAlloc<RBuf8,TBuf8<11>,TText8,HBufC8>(r8);
520 TestReAllocLeaving<RBuf8,TBuf8<11>,TText8>(r8);
521 TestAssignmentOperator<RBuf8,TBuf8<32>,TBufC8<32>,TText8>();
522 TRAPD(ret,TestCleanupClosePushL<RBuf8>(r8)); test(ret==KErrNone);
524 TRAP(ret, TestRBufCleanupL<RBuf8>(r8)); test(KErrNoMemory == ret);
528 test.Start(_L("Testing class RBuf16 ..."));
529 TestCreate<RBuf16>(r16);
530 TestCreateLeaving<RBuf16>(r16);
531 TestCreateFromDes<RBuf16,TBuf16<11>,TText16>(r16);
532 TestCreateFromDesLeaving<RBuf16,TBuf16<11>,TText16>(r16);
533 TestSwap<RBuf16,TBuf16<11>,TText16>(r16);
534 TestAssign<RBuf16,TBuf16<11>,TText16,HBufC16>(r16);
535 TestReAlloc<RBuf16,TBuf16<11>,TText16,HBufC16>(r16);
536 TestReAllocLeaving<RBuf16,TBuf16<11>,TText16>(r16);
537 TestAssignmentOperator<RBuf16,TBuf16<32>,TBufC16<32>,TText16>();
538 TRAP(ret,TestCleanupClosePushL<RBuf16>(r16)); test(ret==KErrNone);
540 TRAP(ret, TestRBufCleanupL<RBuf16>(r16)); test(KErrNoMemory == ret);
544 test.Start(_L("Testing class RBuf ..."));
546 TestCreateLeaving<RBuf>(r);
547 TestCreateFromDes<RBuf,TBuf<11>,TText>(r);
548 TestCreateFromDesLeaving<RBuf,TBuf<11>,TText>(r);
549 TestSwap<RBuf,TBuf<11>,TText>(r);
550 TestAssign<RBuf,TBuf<11>,TText,HBufC>(r);
551 TestReAlloc<RBuf,TBuf<11>,TText,HBufC>(r);
552 TestReAllocLeaving<RBuf,TBuf<11>,TText>(r);
553 TestAssignmentOperator<RBuf,TBuf<32>,TBufC<32>,TText>();
554 TRAP(ret,TestCleanupClosePushL<RBuf>(r)); test(ret==KErrNone);
556 TRAP(ret, TestRBufCleanupL<RBuf>(r)); test(KErrNoMemory == ret);