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_array.cpp
16 // Simple array tests.
18 // RArray, RPointerArray.
20 // - Create fixed length array of 32 and 64 bit integer objects, an array
21 // of pointers to objects and verify that they are created successfully.
22 // - Simulate heap allocation failure test for the current thread's heap,
23 // append some 32 & 64 bit integers to the created arrays and verify the
24 // returned errors are as expected.
25 // - Append some 32, 64 bit integers to fixed length arrays of 32 and 64
26 // bit integer objects respectively, check that KErrNoMemory is returned
28 // - Verify heap allocation granularity.
29 // - Simulate heap allocation failure, attempt to insert an object into
30 // the arrays, verify failure as expected and verify that the array
31 // contents were not modified.
32 // - Remove elements from the arrays and verify that the number of
33 // elements held in the arrays are as expected.
34 // - Append and remove an element to each array (uncompressed) and check
35 // that the number of elements held in the arrays are as expected.
36 // - Simulate heap allocation failure, compress the arrays and verify
37 // that KErrNoMemory is returned on appending elements to the arrays.
38 // - Reset the arrays and check the number of elements held in the arrays are 0.
39 // - Append some 64 bit integer objects to the array of pointers to objects and
40 // verify that the number of elements held in the array is as expected.
41 // - Empty the array of pointers, and verify that the heap has not been corrupted by
43 // - Using a variety of random sized arrays, test RArray::FindInOrder and
44 // RPointerArray::FindInOrder, verify that the results are as expected.
45 // - Using a variety of random sized arrays, test RArray::FindInSignedKeyOrder
46 // and RArray::FindInUnsignedKeyOrder, verify that the results are as expected.
47 // - Using a variety of random sized arrays of a struct, test RArray::FindInUnsignedKeyOrder
48 // an dRArray::FindInUnsignedKeyOrder, verify that the results are as expected.
49 // - Using a variety of random sized arrays, test RPointerArray::FindInAddressOrder,
50 // verify that the results are as expected.
51 // - Verify that the heap has not been corrupted by any of the tests.
52 // - Tests for RArray and standard array objects:
53 // - Append random numbers to the arrays and verify that the arrays are as expected.
54 // - Append and remove integers to an RArray, check the values are added and removed
56 // - Append some random numbers, check that the numbers are found in the array using
57 // sequential and binary search techniques.
58 // - Append some random numbers, insert them into the arrays allowing duplicates
59 // entries and without duplicate entries and check the numbers are found as expected.
60 // - Insert some random numbers into the arrays allowing duplicates, and check the
61 // numbers are added as expected.
62 // - Insert a sequence of integers into an array, use the SpecificFindInOrder method
63 // and verify that results are as expected.
64 // - Tests for 4 byte RArrays:
65 // - Append random numbers to the arrays and verify that the arrays are as expected.
66 // - Append and remove integers to an RArray, check the values are added and removed
68 // - Append some random numbers, check that the numbers are found in the array using
69 // sequential and binary search techniques.
70 // - Append some random numbers, insert them into the arrays allowing duplicates
71 // entries and without duplicate entries and check the numbers are found as expected.
72 // - Insert some random numbers into the arrays allowing duplicates, and check the
73 // numbers are added as expected.
74 // - Insert a sequence of integers into an array, use the SpecificFindInOrder method
75 // and verify that results are as expected.
76 // - Verify that the heap has not been corrupted by any of the tests.
77 // - Repeat the above test for arrays of unsigned integers, pointers, 64 bit integer
78 // array objects and array of pointers objects.
79 // - Test and trap a variety of error conditions that cause the array functions to leave.
80 // Test on arrays of integers, pointers, unsigned integers and TInts.
81 // - Verify that the heap has not been corrupted by any of the tests.
82 // - Perform simple array tests by appending, finding, find in order, insert in order,
83 // sorting, growing and compressing arrays. Verify results are as expected.
84 // - Perform a variety of speed tests on array objects.
85 // - Test whether the heap has been corrupted by all the tests.
86 // Platforms/Drives/Compatibility:
88 // Assumptions/Requirement/Pre-requisites:
89 // Failures and causes:
90 // Base Port information:
97 GLREF_C void DoSpeedTests();
98 GLREF_C void DoIntArrayTests();
99 GLREF_C void DoUintArrayTests();
100 GLREF_C void DoPointerArrayTests();
101 GLREF_C void DoPointerArrayLeavingInterfaceTest();
102 GLREF_C void DoPointerArrayAnyTests();
103 GLREF_C void DoPointerArrayAnyLeavingInterfaceTest();
104 GLREF_C void DoArrayLeavingInterfaceTest();
105 GLDEF_C void DoTIntArrayLeavingInterfaceTest();
106 GLDEF_C void DoTUintArrayLeavingInterfaceTest();
107 GLREF_C void DoSimpleArrayTests();
108 GLREF_C void DoRArrayTests();
110 GLDEF_C RTest test(_L("T_ARRAY"));
112 static TInt64 seed = MAKE_TINT64(0xb504f333,0xf9de6484);
113 GLDEF_C TInt Random()
115 // Using this formula ensures repeated numbers wont come up in the tests.
116 seed = ((TUint) (69069*seed + 41));
121 RArray<TInt> *TheIntArray;
122 RPointerArray<TInt64> *ThePtrArray;
123 RArray<TInt64> *TheSimpleArray;
127 test.Next(_L("Testing alloc failure"));
128 TheIntArray = new RArray<TInt>(16);
129 test(TheIntArray!=NULL);
130 ThePtrArray = new RPointerArray<TInt64>;
131 test(ThePtrArray!=NULL);
132 TheSimpleArray = new RArray<TInt64>;
133 test(TheSimpleArray!=NULL);
135 __UHEAP_SETFAIL(RHeap::EDeterministic,1);
136 TInt64 x = MAKE_TINT64(0xb504f333,0xf9de6484);
137 TInt64 y = MAKE_TINT64(0xc90fdaa2,0xc2352168);
139 TInt r=TheIntArray->Append(0);
140 test(r==KErrNoMemory);
141 r=ThePtrArray->Append(&x);
142 test(r==KErrNoMemory);
143 r=TheSimpleArray->Append(x);
144 test(r==KErrNoMemory);
146 r=TheIntArray->Append(0);
148 r=ThePtrArray->Append(&x);
150 r=TheSimpleArray->Append(x);
152 TUint8* p1=new TUint8[1024]; // alloc a big cell to block simple expansion
153 __UHEAP_SETFAIL(RHeap::EDeterministic,1);
154 test.Next(_L("Testing granularity"));
159 r=TheIntArray->Append(0);
161 test(r==KErrNoMemory);
163 test(TheIntArray->Count()==16);
169 r=ThePtrArray->Append(&x);
171 test(r==KErrNoMemory);
173 test(ThePtrArray->Count()==8); // default
179 r=TheSimpleArray->Append(x);
181 test(r==KErrNoMemory);
183 test(TheSimpleArray->Count()==8); // default
185 r=TheIntArray->Insert(1,1);
186 test(r==KErrNoMemory);
187 test(TheIntArray->Count()==16);
188 for (i=0; i<TheIntArray->Count(); i++)
190 test((*TheIntArray)[i]==0);
192 r=ThePtrArray->Insert(&y,1);
193 test(r==KErrNoMemory);
194 test(ThePtrArray->Count()==8);
195 for (i=0; i<ThePtrArray->Count(); i++)
197 test((*ThePtrArray)[i]==&x);
199 r=TheSimpleArray->Insert(y,1);
200 test(r==KErrNoMemory);
201 test(TheSimpleArray->Count()==8);
202 for (i=0; i<TheSimpleArray->Count(); i++)
204 test((*TheSimpleArray)[i]==x);
209 TheIntArray->Remove(1);
213 ThePtrArray->Remove(1);
217 TheSimpleArray->Remove(1);
219 test(TheIntArray->Count()==1);
220 test(ThePtrArray->Count()==1);
221 test(TheSimpleArray->Count()==1);
224 TAny* p2=User::Alloc(48);
225 TAny* p3=User::Alloc(24);
226 TAny* p4=User::Alloc(24);
227 __UHEAP_SETFAIL(RHeap::EDeterministic,1);
228 r=TheIntArray->Append(0);
230 r=ThePtrArray->Append(&x);
232 r=TheSimpleArray->Append(x);
234 test(TheIntArray->Count()==2);
235 test(ThePtrArray->Count()==2);
236 test(TheSimpleArray->Count()==2);
237 TheIntArray->Remove(1);
238 ThePtrArray->Remove(1);
239 TheSimpleArray->Remove(1);
240 test(TheIntArray->Count()==1);
241 test(ThePtrArray->Count()==1);
242 test(TheSimpleArray->Count()==1);
243 TheIntArray->Compress();
244 ThePtrArray->Compress();
245 TheSimpleArray->Compress();
253 __UHEAP_SETFAIL(RHeap::EDeterministic,1);
254 r=TheIntArray->Append(0);
255 test(r==KErrNoMemory);
256 r=ThePtrArray->Append(&x);
257 test(r==KErrNoMemory);
258 r=TheSimpleArray->Append(x);
259 test(r==KErrNoMemory);
260 TheIntArray->Reset();
261 ThePtrArray->Reset();
262 TheSimpleArray->Reset();
263 test(TheIntArray->Count()==0);
264 test(ThePtrArray->Count()==0);
265 test(TheSimpleArray->Count()==0);
271 test.Next(_L("ResetAndDestroy"));
272 TInt64 *i1=new TInt64;
273 TInt64 *i2=new TInt64;
274 TInt64 *i3=new TInt64;
275 TInt64 *i4=new TInt64;
276 ThePtrArray->Append(i1);
277 ThePtrArray->Append(i2);
278 ThePtrArray->Append(i3);
279 ThePtrArray->Append(i4);
280 test(ThePtrArray->Count()==4);
281 ThePtrArray->ResetAndDestroy();
283 TheIntArray->Close();
285 ThePtrArray->Close();
287 TheSimpleArray->Close();
288 delete TheSimpleArray;
292 class RHeapMonitor : public RAllocator
295 static RHeapMonitor& Install();
299 virtual TAny* Alloc(TInt);
300 virtual void Free(TAny*);
301 virtual TAny* ReAlloc(TAny*, TInt, TInt);
302 virtual TInt AllocLen(const TAny*) const;
303 virtual TInt Compress();
304 virtual void Reset();
305 virtual TInt AllocSize(TInt&) const;
306 virtual TInt Available(TInt&) const;
307 virtual TInt DebugFunction(TInt, TAny*, TAny*);
308 virtual TInt Extension_(TUint, TAny*&, TAny*);
315 TInt iFailedReallocs;
318 RHeapMonitor::RHeapMonitor()
320 iOrig = &User::Allocator();
328 RHeapMonitor& RHeapMonitor::Install()
330 RHeapMonitor* m = new RHeapMonitor;
332 RAllocator* orig = User::SwitchAllocator(m);
333 test(orig == m->iOrig);
337 void RHeapMonitor::Uninstall()
339 RAllocator* m = User::SwitchAllocator(iOrig);
344 TAny* RHeapMonitor::Alloc(TInt a)
347 TAny* p = iOrig->Alloc(a);
348 if (!p) ++iFailedAllocs;
352 void RHeapMonitor::Free(TAny* a)
358 TAny* RHeapMonitor::ReAlloc(TAny* aCell, TInt aSize, TInt aMode)
360 if (aCell && aSize>0)
366 TAny* p = iOrig->ReAlloc(aCell, aSize, aMode);
377 TInt RHeapMonitor::AllocLen(const TAny* a) const
379 return iOrig->AllocLen(a);
382 TInt RHeapMonitor::Compress()
384 return iOrig->Compress();
387 void RHeapMonitor::Reset()
392 TInt RHeapMonitor::AllocSize(TInt& a) const
394 return iOrig->AllocSize(a);
397 TInt RHeapMonitor::Available(TInt& a) const
399 return iOrig->Available(a);
402 TInt RHeapMonitor::DebugFunction(TInt aFunc, TAny* a1, TAny* a2)
404 return iOrig->DebugFunction(aFunc, a1, a2);
407 TInt RHeapMonitor::Extension_(TUint, TAny*&, TAny*)
409 return KErrExtensionNotSupported;
415 RHeapMonitor& m = RHeapMonitor::Install();
420 test(a.Append(1)==KErrNone);
422 test(m.iReallocs==0);
423 test(a.Append(2)==KErrNone);
424 test(m.iReallocs==1); // should have realloc'd
428 test(m.iReallocs==1);
430 test(a.Reserve(2)==KErrNone);
432 TRAP(r,a.ReserveL(2));
436 test(m.iReallocs==1);
437 test(a.Append(1)==KErrNone);
440 test(m.iReallocs==1);
441 test(a.Append(2)==KErrNone);
444 test(m.iReallocs==1); // shouldn't have realloc'd
445 test(a.Append(3)==KErrNone);
448 test(m.iReallocs==2); // should have realloc'd
452 test(m.iReallocs==2);
454 test(a.Reserve(2)==KErrNone);
457 test(m.iReallocs==2);
458 test(a.Append(1)==KErrNone);
461 test(m.iReallocs==2);
462 test(a.Append(2)==KErrNone);
465 test(m.iReallocs==2);
466 test(a.Reserve(0x20000000)==KErrNoMemory);
469 test(m.iReallocs==2);
472 test(m.iReallocs==2);
473 test(a.Reserve(8)==KErrNone);
476 test(m.iReallocs==3);
477 test(a.Append(3)==KErrNone);
478 test(a.Append(4)==KErrNone);
479 test(a.Append(5)==KErrNone);
480 test(a.Append(6)==KErrNone);
481 test(a.Append(7)==KErrNone);
482 test(a.Append(8)==KErrNone);
486 test(m.iReallocs==3);
490 test(a.Reserve(i)==KErrNone);
493 test(m.iReallocs==3);
495 test(a.Append(9)==KErrNone);
498 test(m.iReallocs==4);
502 test(m.iReallocs==4);
506 test(a.Reserve(0)==KErrNone);
509 test(m.iReallocs==4);
510 test(m.iFailedAllocs==0);
511 test(a.Reserve(1)==KErrNoMemory);
514 test(m.iReallocs==4);
515 test(m.iFailedAllocs==1);
516 test(a.Reserve(1)==KErrNone);
519 test(m.iReallocs==4);
520 test(m.iFailedAllocs==1);
524 test(m.iReallocs==4);
525 test(m.iFailedAllocs==1);
528 TUint count = 0x80000000u / sizeof(T);
530 // don't do this in the heap monitored section because
531 // throwing a C++ exception allocates and frees memory
532 TRAP(r,a.ReserveL(count));
533 test(r==KErrNoMemory);
538 test.Start(_L("Test Reserve()"));
541 TestReserveT<TInt>();
542 TestReserveT<TInt64>();
548 GLDEF_C TInt E32Main()
551 CTrapCleanup* trapHandler=CTrapCleanup::New();
552 test(trapHandler!=NULL);
555 test.Start(_L("Simple array tests"));
570 DoPointerArrayTests();
573 TRAPD(ret,DoArrayLeavingInterfaceTest());
577 TRAP(ret,DoPointerArrayLeavingInterfaceTest());
581 TRAP(ret,DoTIntArrayLeavingInterfaceTest());
585 TRAP(ret,DoTUintArrayLeavingInterfaceTest());
589 DoSimpleArrayTests();
592 DoPointerArrayAnyTests();
595 TRAP(ret,DoPointerArrayAnyLeavingInterfaceTest());