os/kernelhwsrv/kerneltest/e32test/buffer/bin_srch.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\buffer\bin_srch.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32test.h>
sl@0
    19
#include <e32math.h>
sl@0
    20
sl@0
    21
GLREF_D RTest test;
sl@0
    22
sl@0
    23
#define KEEP_RUNNING	100
sl@0
    24
sl@0
    25
struct TestMe
sl@0
    26
	{
sl@0
    27
	TBuf<0x10>	name;
sl@0
    28
	TInt32		key1;
sl@0
    29
	TUint32		key2;
sl@0
    30
	};
sl@0
    31
sl@0
    32
LOCAL_C void fillArray(RArray<TestMe>& arr, TInt size)
sl@0
    33
	{
sl@0
    34
	TInt32 seed = 1 + Math::Random() % size;
sl@0
    35
	TestMe testMe;
sl@0
    36
	for(TInt i=0;i<size;i++)
sl@0
    37
		{
sl@0
    38
		testMe.key1 = seed;
sl@0
    39
		arr.Append(testMe);
sl@0
    40
		seed += 2 + Math::Random() % (2 + size%5);
sl@0
    41
		}
sl@0
    42
sl@0
    43
	}
sl@0
    44
sl@0
    45
LOCAL_C void fillArray(RArray<TInt32>& arr, TInt size)
sl@0
    46
	{
sl@0
    47
	TInt32 seed = 1 + Math::Random() % size;
sl@0
    48
	for(TInt i=0;i<size;i++)
sl@0
    49
		{
sl@0
    50
		arr.Append(seed);
sl@0
    51
		seed += 2 + Math::Random() % (2 + size%5);
sl@0
    52
		}
sl@0
    53
	}
sl@0
    54
sl@0
    55
LOCAL_C void fillArray(RArray<TInt32>& arr, RPointerArray<TUint32>& parr, TInt size)
sl@0
    56
	{
sl@0
    57
	TInt32 seed = 1 + Math::Random() % size;
sl@0
    58
	TInt i;
sl@0
    59
	for(i=0;i<size;i++)
sl@0
    60
		{
sl@0
    61
		arr.Append(seed);
sl@0
    62
		seed += 2 + Math::Random() % (2 + size%5);
sl@0
    63
		}
sl@0
    64
	for(i=0;i<size;i++)
sl@0
    65
		{
sl@0
    66
		parr.Append((const TUint32*)&arr[i]);
sl@0
    67
		}
sl@0
    68
	}
sl@0
    69
sl@0
    70
LOCAL_C void fillArray(RPointerArray<TUint32>& arr, TInt size)
sl@0
    71
	{
sl@0
    72
	TUint32 seed = 1 + Math::Random() % size;
sl@0
    73
	TUint32 dummy;
sl@0
    74
	for(TInt i=0;i<size;i++)
sl@0
    75
		{
sl@0
    76
		arr.Append((&dummy) + seed);
sl@0
    77
		seed += 2 + Math::Random() % (2 + size%5);
sl@0
    78
		}
sl@0
    79
	}
sl@0
    80
sl@0
    81
LOCAL_C TInt simpleOrder(const TInt32& a1, const TInt32& a2)
sl@0
    82
	{
sl@0
    83
	return a1 - a2;
sl@0
    84
	}
sl@0
    85
sl@0
    86
LOCAL_C TInt simpleOrder2(const TUint32& a1, const TUint32& a2)
sl@0
    87
	{
sl@0
    88
	return (a1==a2)?0:(a1>a2?1:-1);
sl@0
    89
	}
sl@0
    90
sl@0
    91
GLDEF_C void DoRArrayTests()
sl@0
    92
	{
sl@0
    93
	{
sl@0
    94
	RArray<TInt32>* rArr1 = new RArray<TInt32>(0x10);
sl@0
    95
	test(rArr1!=NULL);
sl@0
    96
	RPointerArray<TUint32>* rpArr1 = new RPointerArray<TUint32>(0x10);
sl@0
    97
	test(rpArr1!=NULL);
sl@0
    98
	TInt i;
sl@0
    99
	TInt size = 25;
sl@0
   100
	test.Next(_L("Testing RArray::FindInOrder, RPointerArray::FindInOrder, RArrayBase::BinarySearch and RPointerArrayBase::BinarySearch with arrays of different sizes\r\n"));
sl@0
   101
	for(i=0;i<KEEP_RUNNING;i++)
sl@0
   102
		{
sl@0
   103
		test.Printf(_L("Testing with a random array of size %d \r\n"), size);
sl@0
   104
		fillArray(*rArr1,*rpArr1,size);
sl@0
   105
		test(rArr1->Count()==rpArr1->Count());
sl@0
   106
		TInt index;
sl@0
   107
		//test(KErrNotFound==rArr1->BinarySearch((TAny*)(rArr1->operator[](0)-1),index,(TGeneralLinearOrder)simpleOrder));
sl@0
   108
		test(KErrNotFound==rArr1->FindInOrder(rArr1->operator[](0)-1,index,TLinearOrder<TInt32>(simpleOrder)));
sl@0
   109
		test(index==0);
sl@0
   110
		TUint32 t = *rpArr1->operator[](0)-1;
sl@0
   111
		test(KErrNotFound==rpArr1->FindInOrder(&t,index,TLinearOrder<TUint32>(simpleOrder2)));
sl@0
   112
		test(index==0);
sl@0
   113
		for(TInt k=0;k<rArr1->Count();k++)
sl@0
   114
			{
sl@0
   115
			test(KErrNone==rArr1->FindInOrder(rArr1->operator[](k),index,TLinearOrder<TInt32>(simpleOrder)));
sl@0
   116
			test(index==k);
sl@0
   117
			test(KErrNone==rpArr1->FindInOrder(rpArr1->operator[](k),index,TLinearOrder<TUint32>(simpleOrder2)));
sl@0
   118
			test(index==k);
sl@0
   119
			if(k<rArr1->Count()-1)
sl@0
   120
				{
sl@0
   121
				test(KErrNotFound==rArr1->FindInOrder((rArr1->operator[](k)+rArr1->operator[](k+1))>>1,index,TLinearOrder<TInt32>(simpleOrder)));
sl@0
   122
				test(index==k+1);
sl@0
   123
				t = (*rpArr1->operator[](k)+*rpArr1->operator[](k+1))>>1;
sl@0
   124
				test(KErrNotFound==rpArr1->FindInOrder(&t,index,TLinearOrder<TUint32>(simpleOrder2)));
sl@0
   125
				test(index==k+1);
sl@0
   126
				}
sl@0
   127
			}
sl@0
   128
		test(KErrNotFound==rArr1->FindInOrder(rArr1->operator[](rArr1->Count()-1)+5,index,TLinearOrder<TInt32>(simpleOrder)));
sl@0
   129
		test(index==rArr1->Count());
sl@0
   130
		t = *rpArr1->operator[](rpArr1->Count()-1) + 5;
sl@0
   131
		test(KErrNotFound==rpArr1->FindInOrder(&t,index,TLinearOrder<TUint32>(simpleOrder2)));
sl@0
   132
		test(index==rpArr1->Count());
sl@0
   133
		size += 2 + Math::Random() % (2 + size%5);
sl@0
   134
		rArr1->Reset();
sl@0
   135
		rpArr1->Reset();
sl@0
   136
		}
sl@0
   137
	delete rpArr1;
sl@0
   138
sl@0
   139
	test.Next(_L("Testing RArray::FindInSignedKeyOrder and RArrayBase::BinarySignedSearch with arrays of different sizes\r\n"));	
sl@0
   140
	for(i=0;i<KEEP_RUNNING;i++)
sl@0
   141
		{
sl@0
   142
		test.Printf(_L("Testing with a random array of size %d \r\n"), size);
sl@0
   143
		fillArray(*rArr1,size);
sl@0
   144
		TInt index;
sl@0
   145
		//test(KErrNotFound==rArr1->BinarySearch((TAny*)(rArr1->operator[](0)-1),index,(TGeneralLinearOrder)simpleOrder));
sl@0
   146
		test(KErrNotFound==rArr1->FindInSignedKeyOrder(rArr1->operator[](0)-1,index));
sl@0
   147
		test(index==0);
sl@0
   148
		for(TInt k=0;k<rArr1->Count();k++)
sl@0
   149
			{
sl@0
   150
			test(KErrNone==rArr1->FindInSignedKeyOrder(rArr1->operator[](k),index));
sl@0
   151
			test(index==k);
sl@0
   152
			if(k<rArr1->Count()-1)
sl@0
   153
				{
sl@0
   154
				test(KErrNotFound==rArr1->FindInSignedKeyOrder((rArr1->operator[](k)+rArr1->operator[](k+1))>>1,index));
sl@0
   155
				test(index==k+1);
sl@0
   156
				}
sl@0
   157
			}
sl@0
   158
		test(KErrNotFound==rArr1->FindInSignedKeyOrder(rArr1->operator[](rArr1->Count()-1)+5,index));
sl@0
   159
		test(index==rArr1->Count());
sl@0
   160
		size += 2 + Math::Random() % (2 + size%5);
sl@0
   161
		rArr1->Reset();
sl@0
   162
		}
sl@0
   163
sl@0
   164
	size=25;
sl@0
   165
	test.Next(_L("Testing RArray::FindInUnsignedKeyOrder and RArrayBase::BinaryUnsignedSearch with arrays of different sizes\r\n"));	
sl@0
   166
	for(i=0;i<KEEP_RUNNING;i++)
sl@0
   167
		{
sl@0
   168
		test.Printf(_L("Testing with a random array of size %d \r\n"), size);
sl@0
   169
		fillArray(*rArr1,size);
sl@0
   170
		TInt index;
sl@0
   171
		//test(KErrNotFound==rArr1->BinarySearch((TAny*)(rArr1->operator[](0)-1),index,(TGeneralLinearOrder)simpleOrder));
sl@0
   172
		test(KErrNotFound==rArr1->FindInUnsignedKeyOrder(rArr1->operator[](0)-1,index));
sl@0
   173
		test(index==0);
sl@0
   174
		for(TInt k=0;k<rArr1->Count();k++)
sl@0
   175
			{
sl@0
   176
			test(KErrNone==rArr1->FindInUnsignedKeyOrder(rArr1->operator[](k),index));
sl@0
   177
			test(index==k);
sl@0
   178
			if(k<rArr1->Count()-1)
sl@0
   179
				{
sl@0
   180
				test(KErrNotFound==rArr1->FindInUnsignedKeyOrder((rArr1->operator[](k)+rArr1->operator[](k+1))>>1,index));
sl@0
   181
				test(index==k+1);
sl@0
   182
				}
sl@0
   183
			}
sl@0
   184
		test(KErrNotFound==rArr1->FindInUnsignedKeyOrder(rArr1->operator[](rArr1->Count()-1)+5,index));
sl@0
   185
		test(index==rArr1->Count());
sl@0
   186
		size += 2 + Math::Random() % (2 + size%5);
sl@0
   187
		rArr1->Reset();
sl@0
   188
		}
sl@0
   189
	delete rArr1;
sl@0
   190
	}
sl@0
   191
sl@0
   192
	{
sl@0
   193
	RArray<TestMe>* rArr1 = new RArray<TestMe>(0x10,_FOFF(TestMe,key1));
sl@0
   194
	test(rArr1!=NULL);
sl@0
   195
	TInt i;
sl@0
   196
	TInt size = 25;
sl@0
   197
	test.Next(_L("Testing RArray::FindInSignedOrder and RArrayBase::BinarySignedSearch with a structure + key\r\n"));
sl@0
   198
	TestMe testMe;
sl@0
   199
	for(i=0;i<KEEP_RUNNING;i++)
sl@0
   200
		{
sl@0
   201
		test.Printf(_L("Testing with a random array of size %d \r\n"), size);
sl@0
   202
		fillArray(*rArr1,size);
sl@0
   203
		TInt index;
sl@0
   204
		//test(KErrNotFound==rArr1->BinarySearch((TAny*)(rArr1->operator[](0)-1),index,(TGeneralLinearOrder)simpleOrder));
sl@0
   205
		testMe=rArr1->operator[](0);
sl@0
   206
		testMe.key1 = rArr1->operator[](0).key1-1;
sl@0
   207
		test(KErrNotFound==rArr1->FindInSignedKeyOrder(testMe,index));
sl@0
   208
		test(index==0);
sl@0
   209
		for(TInt k=0;k<rArr1->Count();k++)
sl@0
   210
			{
sl@0
   211
			testMe.key1 = rArr1->operator[](k).key1;
sl@0
   212
			test(KErrNone==rArr1->FindInSignedKeyOrder(testMe,index));
sl@0
   213
			test(index==k);
sl@0
   214
			if(k<rArr1->Count()-1)
sl@0
   215
				{
sl@0
   216
				testMe.key1 = (rArr1->operator[](k).key1+rArr1->operator[](k+1).key1)>>1;
sl@0
   217
				test(KErrNotFound==rArr1->FindInSignedKeyOrder(testMe,index));
sl@0
   218
				test(index==k+1);
sl@0
   219
				}
sl@0
   220
			}
sl@0
   221
		testMe.key1 = rArr1->operator[](rArr1->Count()-1).key1+5;
sl@0
   222
		test(KErrNotFound==rArr1->FindInSignedKeyOrder(testMe,index));
sl@0
   223
		test(index==rArr1->Count());
sl@0
   224
		size += 2 + Math::Random() % (2 + size%5);
sl@0
   225
		rArr1->Reset();
sl@0
   226
		}
sl@0
   227
sl@0
   228
	size=25;
sl@0
   229
	test.Next(_L("Testing RArray::FindInUnsignedKeyOrder and RArrayBase::BinaryUnsignedSearch with arrays of different sizes\r\n"));	
sl@0
   230
	for(i=0;i<KEEP_RUNNING;i++)
sl@0
   231
		{
sl@0
   232
		test.Printf(_L("Testing with a random array of size %d \r\n"), size);
sl@0
   233
		fillArray(*rArr1,size);
sl@0
   234
		TInt index;
sl@0
   235
		//test(KErrNotFound==rArr1->BinarySearch((TAny*)(rArr1->operator[](0)-1),index,(TGeneralLinearOrder)simpleOrder));
sl@0
   236
		testMe.key1 = rArr1->operator[](0).key1-1;
sl@0
   237
		test(KErrNotFound==rArr1->FindInUnsignedKeyOrder(testMe,index));
sl@0
   238
		test(index==0);
sl@0
   239
		for(TInt k=0;k<rArr1->Count();k++)
sl@0
   240
			{
sl@0
   241
			testMe.key1 = rArr1->operator[](k).key1;
sl@0
   242
			test(KErrNone==rArr1->FindInUnsignedKeyOrder(testMe,index));
sl@0
   243
			test(index==k);
sl@0
   244
			if(k<rArr1->Count()-1)
sl@0
   245
				{
sl@0
   246
				testMe.key1 = (rArr1->operator[](k).key1+rArr1->operator[](k+1).key1)>>1;
sl@0
   247
				test(KErrNotFound==rArr1->FindInUnsignedKeyOrder(testMe,index));
sl@0
   248
				test(index==k+1);
sl@0
   249
				}
sl@0
   250
			}
sl@0
   251
		testMe.key1 = rArr1->operator[](rArr1->Count()-1).key1+5;
sl@0
   252
		test(KErrNotFound==rArr1->FindInUnsignedKeyOrder(testMe,index));
sl@0
   253
		test(index==rArr1->Count());
sl@0
   254
		size += 2 + Math::Random() % (2 + size%5);
sl@0
   255
		rArr1->Reset();
sl@0
   256
		}
sl@0
   257
	delete rArr1;
sl@0
   258
	}
sl@0
   259
sl@0
   260
	{
sl@0
   261
	RPointerArray<TUint32>* rArr1 = new RPointerArray<TUint32>(0x10);
sl@0
   262
	test(rArr1!=NULL);
sl@0
   263
	TInt i;
sl@0
   264
	TInt size = 25;
sl@0
   265
	test.Next(_L("Testing RPointerArray::FindInAddressOrder and RPointerArrayBase::BinaryUnsignedSearch with arrays of different sizes\r\n"));
sl@0
   266
	for(i=0;i<KEEP_RUNNING;i++)
sl@0
   267
		{
sl@0
   268
		test.Printf(_L("Testing with a random array of size %d \r\n"), size);
sl@0
   269
		fillArray(*rArr1,size);
sl@0
   270
		TInt index;
sl@0
   271
		//test(KErrNotFound==rArr1->BinarySearch((TAny*)(rArr1->operator[](0)-1),index,(TGeneralLinearOrder)simpleOrder));
sl@0
   272
		test(KErrNotFound==rArr1->FindInAddressOrder(rArr1->operator[](0)-1,index));
sl@0
   273
		test(index==0);
sl@0
   274
		for(TInt k=0;k<rArr1->Count();k++)
sl@0
   275
			{
sl@0
   276
			test(KErrNone==rArr1->FindInAddressOrder(rArr1->operator[](k),index));
sl@0
   277
			test(index==k);
sl@0
   278
			if(k<rArr1->Count()-1)
sl@0
   279
				{
sl@0
   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));
sl@0
   281
				test(index==k+1);
sl@0
   282
				}
sl@0
   283
			}
sl@0
   284
		test(KErrNotFound==rArr1->FindInAddressOrder(rArr1->operator[](rArr1->Count()-1)+5,index));
sl@0
   285
		test(index==rArr1->Count());
sl@0
   286
		size += 2 + Math::Random() % (2 + size%5);
sl@0
   287
		rArr1->Reset();
sl@0
   288
		}
sl@0
   289
sl@0
   290
	delete rArr1;
sl@0
   291
	}
sl@0
   292
sl@0
   293
	}