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\bin_srch.cpp
23 #define KEEP_RUNNING 100
32 LOCAL_C void fillArray(RArray<TestMe>& arr, TInt size)
34 TInt32 seed = 1 + Math::Random() % size;
36 for(TInt i=0;i<size;i++)
40 seed += 2 + Math::Random() % (2 + size%5);
45 LOCAL_C void fillArray(RArray<TInt32>& arr, TInt size)
47 TInt32 seed = 1 + Math::Random() % size;
48 for(TInt i=0;i<size;i++)
51 seed += 2 + Math::Random() % (2 + size%5);
55 LOCAL_C void fillArray(RArray<TInt32>& arr, RPointerArray<TUint32>& parr, TInt size)
57 TInt32 seed = 1 + Math::Random() % size;
62 seed += 2 + Math::Random() % (2 + size%5);
66 parr.Append((const TUint32*)&arr[i]);
70 LOCAL_C void fillArray(RPointerArray<TUint32>& arr, TInt size)
72 TUint32 seed = 1 + Math::Random() % size;
74 for(TInt i=0;i<size;i++)
76 arr.Append((&dummy) + seed);
77 seed += 2 + Math::Random() % (2 + size%5);
81 LOCAL_C TInt simpleOrder(const TInt32& a1, const TInt32& a2)
86 LOCAL_C TInt simpleOrder2(const TUint32& a1, const TUint32& a2)
88 return (a1==a2)?0:(a1>a2?1:-1);
91 GLDEF_C void DoRArrayTests()
94 RArray<TInt32>* rArr1 = new RArray<TInt32>(0x10);
96 RPointerArray<TUint32>* rpArr1 = new RPointerArray<TUint32>(0x10);
100 test.Next(_L("Testing RArray::FindInOrder, RPointerArray::FindInOrder, RArrayBase::BinarySearch and RPointerArrayBase::BinarySearch with arrays of different sizes\r\n"));
101 for(i=0;i<KEEP_RUNNING;i++)
103 test.Printf(_L("Testing with a random array of size %d \r\n"), size);
104 fillArray(*rArr1,*rpArr1,size);
105 test(rArr1->Count()==rpArr1->Count());
107 //test(KErrNotFound==rArr1->BinarySearch((TAny*)(rArr1->operator[](0)-1),index,(TGeneralLinearOrder)simpleOrder));
108 test(KErrNotFound==rArr1->FindInOrder(rArr1->operator[](0)-1,index,TLinearOrder<TInt32>(simpleOrder)));
110 TUint32 t = *rpArr1->operator[](0)-1;
111 test(KErrNotFound==rpArr1->FindInOrder(&t,index,TLinearOrder<TUint32>(simpleOrder2)));
113 for(TInt k=0;k<rArr1->Count();k++)
115 test(KErrNone==rArr1->FindInOrder(rArr1->operator[](k),index,TLinearOrder<TInt32>(simpleOrder)));
117 test(KErrNone==rpArr1->FindInOrder(rpArr1->operator[](k),index,TLinearOrder<TUint32>(simpleOrder2)));
119 if(k<rArr1->Count()-1)
121 test(KErrNotFound==rArr1->FindInOrder((rArr1->operator[](k)+rArr1->operator[](k+1))>>1,index,TLinearOrder<TInt32>(simpleOrder)));
123 t = (*rpArr1->operator[](k)+*rpArr1->operator[](k+1))>>1;
124 test(KErrNotFound==rpArr1->FindInOrder(&t,index,TLinearOrder<TUint32>(simpleOrder2)));
128 test(KErrNotFound==rArr1->FindInOrder(rArr1->operator[](rArr1->Count()-1)+5,index,TLinearOrder<TInt32>(simpleOrder)));
129 test(index==rArr1->Count());
130 t = *rpArr1->operator[](rpArr1->Count()-1) + 5;
131 test(KErrNotFound==rpArr1->FindInOrder(&t,index,TLinearOrder<TUint32>(simpleOrder2)));
132 test(index==rpArr1->Count());
133 size += 2 + Math::Random() % (2 + size%5);
139 test.Next(_L("Testing RArray::FindInSignedKeyOrder and RArrayBase::BinarySignedSearch with arrays of different sizes\r\n"));
140 for(i=0;i<KEEP_RUNNING;i++)
142 test.Printf(_L("Testing with a random array of size %d \r\n"), size);
143 fillArray(*rArr1,size);
145 //test(KErrNotFound==rArr1->BinarySearch((TAny*)(rArr1->operator[](0)-1),index,(TGeneralLinearOrder)simpleOrder));
146 test(KErrNotFound==rArr1->FindInSignedKeyOrder(rArr1->operator[](0)-1,index));
148 for(TInt k=0;k<rArr1->Count();k++)
150 test(KErrNone==rArr1->FindInSignedKeyOrder(rArr1->operator[](k),index));
152 if(k<rArr1->Count()-1)
154 test(KErrNotFound==rArr1->FindInSignedKeyOrder((rArr1->operator[](k)+rArr1->operator[](k+1))>>1,index));
158 test(KErrNotFound==rArr1->FindInSignedKeyOrder(rArr1->operator[](rArr1->Count()-1)+5,index));
159 test(index==rArr1->Count());
160 size += 2 + Math::Random() % (2 + size%5);
165 test.Next(_L("Testing RArray::FindInUnsignedKeyOrder and RArrayBase::BinaryUnsignedSearch with arrays of different sizes\r\n"));
166 for(i=0;i<KEEP_RUNNING;i++)
168 test.Printf(_L("Testing with a random array of size %d \r\n"), size);
169 fillArray(*rArr1,size);
171 //test(KErrNotFound==rArr1->BinarySearch((TAny*)(rArr1->operator[](0)-1),index,(TGeneralLinearOrder)simpleOrder));
172 test(KErrNotFound==rArr1->FindInUnsignedKeyOrder(rArr1->operator[](0)-1,index));
174 for(TInt k=0;k<rArr1->Count();k++)
176 test(KErrNone==rArr1->FindInUnsignedKeyOrder(rArr1->operator[](k),index));
178 if(k<rArr1->Count()-1)
180 test(KErrNotFound==rArr1->FindInUnsignedKeyOrder((rArr1->operator[](k)+rArr1->operator[](k+1))>>1,index));
184 test(KErrNotFound==rArr1->FindInUnsignedKeyOrder(rArr1->operator[](rArr1->Count()-1)+5,index));
185 test(index==rArr1->Count());
186 size += 2 + Math::Random() % (2 + size%5);
193 RArray<TestMe>* rArr1 = new RArray<TestMe>(0x10,_FOFF(TestMe,key1));
197 test.Next(_L("Testing RArray::FindInSignedOrder and RArrayBase::BinarySignedSearch with a structure + key\r\n"));
199 for(i=0;i<KEEP_RUNNING;i++)
201 test.Printf(_L("Testing with a random array of size %d \r\n"), size);
202 fillArray(*rArr1,size);
204 //test(KErrNotFound==rArr1->BinarySearch((TAny*)(rArr1->operator[](0)-1),index,(TGeneralLinearOrder)simpleOrder));
205 testMe=rArr1->operator[](0);
206 testMe.key1 = rArr1->operator[](0).key1-1;
207 test(KErrNotFound==rArr1->FindInSignedKeyOrder(testMe,index));
209 for(TInt k=0;k<rArr1->Count();k++)
211 testMe.key1 = rArr1->operator[](k).key1;
212 test(KErrNone==rArr1->FindInSignedKeyOrder(testMe,index));
214 if(k<rArr1->Count()-1)
216 testMe.key1 = (rArr1->operator[](k).key1+rArr1->operator[](k+1).key1)>>1;
217 test(KErrNotFound==rArr1->FindInSignedKeyOrder(testMe,index));
221 testMe.key1 = rArr1->operator[](rArr1->Count()-1).key1+5;
222 test(KErrNotFound==rArr1->FindInSignedKeyOrder(testMe,index));
223 test(index==rArr1->Count());
224 size += 2 + Math::Random() % (2 + size%5);
229 test.Next(_L("Testing RArray::FindInUnsignedKeyOrder and RArrayBase::BinaryUnsignedSearch with arrays of different sizes\r\n"));
230 for(i=0;i<KEEP_RUNNING;i++)
232 test.Printf(_L("Testing with a random array of size %d \r\n"), size);
233 fillArray(*rArr1,size);
235 //test(KErrNotFound==rArr1->BinarySearch((TAny*)(rArr1->operator[](0)-1),index,(TGeneralLinearOrder)simpleOrder));
236 testMe.key1 = rArr1->operator[](0).key1-1;
237 test(KErrNotFound==rArr1->FindInUnsignedKeyOrder(testMe,index));
239 for(TInt k=0;k<rArr1->Count();k++)
241 testMe.key1 = rArr1->operator[](k).key1;
242 test(KErrNone==rArr1->FindInUnsignedKeyOrder(testMe,index));
244 if(k<rArr1->Count()-1)
246 testMe.key1 = (rArr1->operator[](k).key1+rArr1->operator[](k+1).key1)>>1;
247 test(KErrNotFound==rArr1->FindInUnsignedKeyOrder(testMe,index));
251 testMe.key1 = rArr1->operator[](rArr1->Count()-1).key1+5;
252 test(KErrNotFound==rArr1->FindInUnsignedKeyOrder(testMe,index));
253 test(index==rArr1->Count());
254 size += 2 + Math::Random() % (2 + size%5);
261 RPointerArray<TUint32>* rArr1 = new RPointerArray<TUint32>(0x10);
265 test.Next(_L("Testing RPointerArray::FindInAddressOrder and RPointerArrayBase::BinaryUnsignedSearch with arrays of different sizes\r\n"));
266 for(i=0;i<KEEP_RUNNING;i++)
268 test.Printf(_L("Testing with a random array of size %d \r\n"), size);
269 fillArray(*rArr1,size);
271 //test(KErrNotFound==rArr1->BinarySearch((TAny*)(rArr1->operator[](0)-1),index,(TGeneralLinearOrder)simpleOrder));
272 test(KErrNotFound==rArr1->FindInAddressOrder(rArr1->operator[](0)-1,index));
274 for(TInt k=0;k<rArr1->Count();k++)
276 test(KErrNone==rArr1->FindInAddressOrder(rArr1->operator[](k),index));
278 if(k<rArr1->Count()-1)
280 test(KErrNotFound==rArr1->FindInAddressOrder((const TUint32*)(((TUint32)rArr1->operator[](k))/2+((TUint32)rArr1->operator[](k+1))/2 + (((TUint32)rArr1->operator[](k))%2 + ((TUint32)rArr1->operator[](k+1))%2)/2),index));
284 test(KErrNotFound==rArr1->FindInAddressOrder(rArr1->operator[](rArr1->Count()-1)+5,index));
285 test(index==rArr1->Count());
286 size += 2 + Math::Random() % (2 + size%5);