os/kernelhwsrv/kerneltest/e32test/buffer/ptr_arr.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32test\buffer\ptr_arr.cpp
    15 // 
    16 //
    17 
    18 #include <e32test.h>
    19 #include <e32math.h>
    20 #include <e32std.h>
    21 #include <e32std_private.h>
    22 
    23 #define NUM_TESTS 200
    24 const TInt KArraySize=1024;
    25 
    26 GLREF_D RTest test;
    27 GLREF_C TInt Random();
    28 
    29 struct SEntry
    30 	{
    31 	TInt iKey;
    32 	TInt iValue;
    33 	inline TBool operator!=(const SEntry& anEntry) const
    34 		{return (iValue!=anEntry.iValue || iKey!=anEntry.iKey);}
    35 	inline TBool operator==(const SEntry& anEntry) const
    36 		{return (iValue==anEntry.iValue && iKey==anEntry.iKey);}
    37 	};
    38 
    39 struct SPointerArray
    40 	{
    41 	TInt iCount;
    42 	TAny** iEntries;
    43 	TInt iAllocated;
    44 	TInt iGranularity;
    45 	};
    46 
    47 LOCAL_D TInt64* Int64s;
    48 LOCAL_D SEntry* Entries;
    49 
    50 LOCAL_C TInt Order64Bit(const TInt64& aLeft, const TInt64& aRight)
    51 	{
    52 	if (aLeft<aRight)
    53 		return -1;
    54 	if (aLeft==aRight)
    55 		return 0;
    56 	return 1;
    57 	}
    58 
    59 LOCAL_C TBool Compare64Bit(const TInt64& aLeft, const TInt64& aRight)
    60 	{
    61 	return (aLeft==aRight);
    62 	}
    63 
    64 TInt OrderSEntry(const SEntry& aLeft, const SEntry& aRight)
    65 	{
    66 	if (aLeft.iKey<aRight.iKey)
    67 		return -1;
    68 	else if (aLeft.iKey>aRight.iKey)
    69 		return 1;
    70 	else
    71 		return 0;
    72 	}
    73 
    74 TInt OrderSEntryU(const SEntry& aLeft, const SEntry& aRight)
    75 	{
    76 	TUint l = (TUint)aLeft.iKey;
    77 	TUint r = (TUint)aRight.iKey;
    78 	if (l < r)
    79 		return -1;
    80 	else if (l > r)
    81 		return 1;
    82 	else
    83 		return 0;
    84 	}
    85 
    86 TInt OrderSEntry2(const SEntry& aLeft, const SEntry& aRight)
    87 	{
    88 	if (aLeft.iKey<aRight.iKey)
    89 		return -1;
    90 	else if (aLeft.iKey>aRight.iKey)
    91 		return 1;
    92 	else
    93 		return aLeft.iValue - aRight.iValue;
    94 	}
    95 
    96 TInt OrderSEntryU2(const SEntry& aLeft, const SEntry& aRight)
    97 	{
    98 	TUint l = (TUint)aLeft.iKey;
    99 	TUint r = (TUint)aRight.iKey;
   100 	if (l < r)
   101 		return -1;
   102 	else if (l > r)
   103 		return 1;
   104 	else
   105 		return aLeft.iValue - aRight.iValue;
   106 	}
   107 
   108 TInt OrderSEntryByKey(const TInt* aKey, const SEntry& aObject)
   109 	{
   110 	if (*aKey<aObject.iKey)
   111 		return -1;
   112 	else if (*aKey>aObject.iKey)
   113 		return 1;
   114 	else
   115 		return 0;
   116 	}
   117 
   118 LOCAL_C TBool CompareSEntryKey(const SEntry& aLeft, const SEntry& aRight)
   119 	{
   120 	return (aLeft.iKey==aRight.iKey);
   121 	}
   122 
   123 LOCAL_C TBool CompareSEntry(const SEntry& aLeft, const SEntry& aRight)
   124 	{
   125 	return (aLeft.iKey==aRight.iKey && aLeft.iValue==aRight.iValue);
   126 	}
   127 
   128 TBool CompareSEntryByKeyKey(const TInt* aKey, const SEntry& aRight)
   129 	{
   130 	return *aKey==aRight.iKey;
   131 	}
   132 
   133 TBool CompareSEntryByKeyValue(const TInt* aValue, const SEntry& aRight)
   134 	{
   135 	return *aValue==aRight.iValue;
   136 	}
   137 
   138 GLDEF_D TLinearOrder<TInt64> Int64Order(Order64Bit);
   139 GLDEF_D TIdentityRelation<TInt64> Int64Identity(Compare64Bit);
   140 GLDEF_D TLinearOrder<SEntry> SEntryOrder(OrderSEntry);
   141 GLDEF_D TIdentityRelation<SEntry> SEntryKeyIdentity(CompareSEntryKey);
   142 GLDEF_D TIdentityRelation<SEntry> SEntryIdentity(CompareSEntry);
   143 
   144 LOCAL_C TInt64 Random64(TInt64& aMask)
   145 	{
   146 	TInt64 x = MAKE_TINT64(Random()&I64HIGH(aMask), Random()&I64LOW(aMask));
   147 	return x;
   148 	}
   149 
   150 LOCAL_C TInt IntAppendAndAccessTest(TInt aCount, TInt aNumTests, TInt64 aMask)
   151 	{
   152 	TInt n;
   153 	for (n=0; n<aNumTests; n++)
   154 		{
   155 		RPointerArray<TInt64> a;
   156 		TInt64 *pA=new TInt64[aCount];
   157 		if (!pA)
   158 			{
   159 			a.Close();
   160 			return -65535;
   161 			}
   162 		TInt i;
   163 		for (i=0; i<aCount; i++)
   164 			{
   165 			Int64s[i]=Random64(aMask);
   166 			pA[i]=Int64s[i];
   167 			a.Append(&Int64s[i]);
   168 			}
   169 		if (a.Count()!=aCount)
   170 			{
   171 			a.Close();
   172 			return -1;
   173 			}
   174 		for (i=0; i<aCount; i++)
   175 			{
   176 			if (a[i]!=&Int64s[i])
   177 				{
   178 				a.Close();
   179 				return -2;
   180 				}
   181 			if (*a[i]!=pA[i])
   182 				{
   183 				a.Close();
   184 				return -3;
   185 				}
   186 			a[i]=&pA[i];
   187 			}
   188 		if (a.Count()!=aCount)
   189 			{
   190 			a.Close();
   191 			return -4;
   192 			}
   193 		for (i=0; i<aCount; i++)
   194 			{
   195 			if (a[i]!=&pA[i])
   196 				{
   197 				a.Close();
   198 				return -5;
   199 				}
   200 			if (*a[i]!=Int64s[i])
   201 				{
   202 				a.Close();
   203 				return -6;
   204 				}
   205 			}
   206 		delete[] pA;
   207 		a.Close();
   208 		}
   209 	return KErrNone;
   210 	}
   211 
   212 LOCAL_C TInt IntFindTest(TInt aCount, TInt aNumTests, TInt64 aMask)
   213 	{
   214 	TInt n;
   215 	for (n=0; n<aNumTests; n++)
   216 		{
   217 		RPointerArray<TInt64> a;
   218 		TInt64 *pA=new TInt64[aCount];
   219 		if (!pA)
   220 			{
   221 			a.Close();
   222 			return -65535;
   223 			}
   224 		TInt i;
   225 		for (i=0; i<aCount; i++)
   226 			{
   227 			pA[i]=Random64(aMask);
   228 			Int64s[i]=pA[i];
   229 			a.Append(&Int64s[i]);
   230 			}
   231 		if (a.Count()!=aCount)
   232 			{
   233 			a.Close();
   234 			return -1;
   235 			}
   236 		for (i=0; i<aCount; i++)
   237 			{
   238 			TInt r=a.Find(&Int64s[i]);
   239 			if (r!=i)
   240 				{
   241 				a.Close();
   242 				return -2;
   243 				}
   244 			r=a.Find(&pA[i]);
   245 			if (r>=0)
   246 				{
   247 				a.Close();
   248 				return -3;
   249 				}
   250 			r=a.Find(&pA[i],Int64Identity);
   251 			if (r<0 || pA[i]!=Int64s[i] || r>i)
   252 				{
   253 				a.Close();
   254 				return -4;
   255 				}
   256 			}
   257 		delete[] pA;
   258 		a.Close();
   259 		}
   260 	return KErrNone;
   261 	}
   262 
   263 LOCAL_C TInt EntryFindTest(TInt aCount, TInt aNumTests)
   264 	{
   265 	TInt n;
   266 	for (n=0; n<aNumTests; n++)
   267 		{
   268 		RPointerArray<SEntry> a;
   269 		SEntry *pE=new SEntry[aCount];
   270 		if (!pE)
   271 			{
   272 			a.Close();
   273 			return -65535;
   274 			}
   275 		TInt i;
   276 		for (i=0; i<aCount; i++)
   277 			{
   278 			pE[i].iValue=Random();
   279 			pE[i].iKey=i&7;
   280 			Entries[i]=pE[i];
   281 			a.Append(&Entries[i]);
   282 			}
   283 		if (a.Count()!=aCount)
   284 			{
   285 			a.Close();
   286 			return -1;
   287 			}
   288 		for (i=0; i<aCount; i++)
   289 			{
   290 			TInt r=a.Find(&Entries[i]);
   291 			if (r!=i)
   292 				{
   293 				a.Close();
   294 				return -2;
   295 				}
   296 			r=a.Find(&pE[i]);
   297 			if (r>=0)
   298 				{
   299 				a.Close();
   300 				return -3;
   301 				}
   302 			r=a.Find(&pE[i],SEntryIdentity);
   303 			if (r<0 || pE[i]!=Entries[i] || r>i)
   304 				{
   305 				a.Close();
   306 				return -4;
   307 				}
   308 			r=a.Find(&pE[i],SEntryKeyIdentity);
   309 			if (r!=(i&7))
   310 				{
   311 				a.Close();
   312 				return -5;
   313 				}
   314 			r=a.Find(pE[i].iValue,CompareSEntryByKeyValue);
   315 			if (r<0 || pE[i].iValue!=Entries[i].iValue || r>i)
   316 				{
   317 				a.Close();
   318 				return -6;
   319 				}
   320 			r=a.Find(pE[i].iKey,CompareSEntryByKeyKey);
   321 			if (r!=(i&7))
   322 				{
   323 				a.Close();
   324 				return -7;
   325 				}	
   326 			}
   327 		delete[] pE;
   328 		a.Close();
   329 		}
   330 	return KErrNone;
   331 	}
   332 	
   333 
   334 LOCAL_C TInt IntFindReverseTest(TInt aCount, TInt aNumTests, TInt64 aMask)
   335 	{
   336 	TInt n;
   337 	TInt i;
   338 
   339 	for (n=0; n<aNumTests; n++)
   340 		{
   341 		RPointerArray<TInt64> a;
   342 		TInt64 *pA=new TInt64[aCount];
   343 		if (!pA)
   344 			{
   345 			a.Close();
   346 			return -65535;
   347 			}
   348 		for (i=0; i<aCount; i++)
   349 			{
   350 			pA[i]=Random64(aMask);
   351 			Int64s[i]=pA[i];
   352 			a.Append(&Int64s[i]);
   353 			}
   354 		if (a.Count()!=aCount)
   355 			{
   356 			a.Close();
   357 			return -1;
   358 			}
   359 		
   360 		for (i=0; i<aCount; i++)
   361 			{
   362 			TInt r=a.FindReverse(&Int64s[i]);
   363 			if (r!=i)
   364 				{
   365 				a.Close();
   366 				return -2;
   367 				}
   368 			r=a.FindReverse(&pA[i]);
   369 			if (r>=0)
   370 				{
   371 				a.Close();
   372 				return -3;
   373 				}
   374 			r=a.FindReverse(&pA[i],Int64Identity);
   375 			if (pA[i]!=Int64s[i] || r<i)
   376 				{
   377 				a.Close();
   378 				return -4;
   379 				}
   380 			}
   381 		delete[] pA;
   382 		a.Close();
   383 		}
   384 
   385 	return KErrNone;
   386 	}
   387 	
   388 
   389 LOCAL_C TInt EntryFindReverseTest(TInt aCount, TInt aNumTests)
   390 	{
   391 	TInt n;
   392 	TInt revOffs = aCount-(((aCount-1)&7)+1);
   393 
   394 	for (n=0; n<aNumTests; n++)
   395 		{
   396 		RPointerArray<SEntry> a;
   397 		SEntry *pE=new SEntry[aCount];
   398 		if (!pE)
   399 			{
   400 			a.Close();
   401 			return -65535;
   402 			}
   403 		TInt i;
   404 		for (i=0; i<aCount; i++)
   405 			{
   406 			pE[i].iValue=Random();
   407 			pE[i].iKey=((aCount-1)-i)&7;
   408 			Entries[i]=pE[i];
   409 			a.Append(&Entries[i]);
   410 			}
   411 		if (a.Count()!=aCount)
   412 			{
   413 			a.Close();
   414 			return -1;
   415 			}
   416 		for (i=0; i<aCount; i++)
   417 			{
   418 			TInt r=a.FindReverse(&Entries[i]);
   419 			if (r!=i)
   420 				{
   421 				a.Close();
   422 				return -2;
   423 				}
   424 			r=a.FindReverse(&pE[i]);
   425 			if (r>=0)
   426 				{
   427 				a.Close();
   428 				return -3;
   429 				}
   430 			r=a.FindReverse(&pE[i],SEntryIdentity);
   431 			if (pE[i]!=Entries[i] || r<i)
   432 				{
   433 				a.Close();
   434 				return -4;
   435 				}
   436 			r=a.FindReverse(&pE[i],SEntryKeyIdentity);
   437 			if (r!=revOffs+(i&7))
   438 				{
   439 				a.Close();
   440 				return -5;
   441 				}
   442 			r=a.FindReverse(pE[i].iValue,CompareSEntryByKeyValue);
   443 			if (pE[i].iValue!=Entries[i].iValue || r<i)
   444 				{
   445 				a.Close();
   446 				return -6;
   447 				}
   448 			r=a.FindReverse(pE[i].iKey,CompareSEntryByKeyKey);
   449 			if (r!=revOffs+(i&7))
   450 				{
   451 				a.Close();
   452 				return -7;
   453 				}	
   454 			}
   455 		delete[] pE;
   456 		a.Close();
   457 		}
   458 	return KErrNone;
   459 	}
   460 
   461 LOCAL_C TInt IntFindInOrderTest(TInt aCount, TInt aNumTests, TInt64 aMask)
   462 // require aRange*aCount<2^32
   463 	{
   464 	TInt n;
   465 	for (n=0; n<aNumTests; n++)
   466 		{
   467 		RPointerArray<TInt64> a;
   468 		TInt64 *pA=new TInt64[aCount];
   469 		if (!pA)
   470 			{
   471 			a.Close();
   472 			return -65535;
   473 			}
   474 		TInt i=0;
   475 		TInt64 y=-256;
   476 		for(i=0; i<aCount; i++)
   477 			{
   478 			TInt64 x=Random64(aMask);	// this is always >=0
   479 			pA[i]=y;
   480 			Int64s[i]=y;
   481 			y+=x;
   482 			a.Append(&Int64s[i]);
   483 			}
   484 		if (a.Count()!=aCount)
   485 			{
   486 			a.Close();
   487 			return -1;
   488 			}
   489 		for (i=0; i<aCount; i++)
   490 			{
   491 			TInt r=a.FindInOrder(&pA[i],Int64Order);
   492 			if (r<0 || Int64s[r]!=pA[i])
   493 				{
   494 				a.Close();
   495 				return -2;
   496 				}
   497 			TInt64 x=Random64(aMask);
   498 			r=a.FindInOrder(&x,Int64Order);
   499 			if (r<0)
   500 				{
   501 				TInt j;
   502 				for (j=0; j<aCount; j++)
   503 					{
   504 					if (pA[j]==x)
   505 						{
   506 						a.Close();
   507 						return -3;
   508 						}
   509 					}
   510 				}
   511 			else if (Int64s[r]!=x)
   512 				{
   513 				a.Close();
   514 				return -4;
   515 				}
   516 			}
   517 		delete[] pA;
   518 		a.Close();
   519 		}
   520 	return KErrNone;
   521 	}
   522 
   523 LOCAL_C TInt EntryFindInOrderTest(TInt aCount, TInt aNumTests)
   524 // require aRange*aCount<2^32
   525 	{
   526 	TInt n;
   527 	for (n=0; n<aNumTests; n++)
   528 		{
   529 		RPointerArray<SEntry> a;
   530 		SEntry *pE=new SEntry[aCount];
   531 		if (!pE)
   532 			{
   533 			a.Close();
   534 			return -65535;
   535 			}
   536 		TInt i=0;
   537 		for(i=0; i<aCount; i++)
   538 			{
   539 			pE[i].iKey=i*19;
   540 			pE[i].iValue=Random();
   541 			Entries[i]=pE[i];
   542 			a.Append(&Entries[i]);
   543 			}
   544 		if (a.Count()!=aCount)
   545 			{
   546 			a.Close();
   547 			return -1;
   548 			}
   549 		for (i=0; i<aCount; i++)
   550 			{
   551 			TInt r=a.FindInOrder(&pE[i],SEntryOrder);
   552 			if (r!=i)
   553 				{
   554 				a.Close();
   555 				return -2;
   556 				}
   557 			TInt x=Random()&1023;
   558 			SEntry e;
   559 			e.iKey=x;
   560 			r=a.FindInOrder(&e,SEntryOrder);
   561 			if (r<0)
   562 				{
   563 				if (x%19==0)
   564 					{
   565 					a.Close();
   566 					return -3;
   567 					}
   568 				}
   569 			else if (x!=r*19)
   570 				{
   571 				a.Close();
   572 				return -4;
   573 				}
   574 			r=a.FindInOrder(x,OrderSEntryByKey);
   575 			if (r<0)
   576 				{
   577 				if (x%19==0)
   578 					{
   579 					a.Close();
   580 					return -5;
   581 					}
   582 				}
   583 			else if (x!=r*19)
   584 				{
   585 				a.Close();
   586 				return -6;
   587 				}
   588 			}
   589 		delete[] pE;
   590 		a.Close();
   591 		}
   592 	return KErrNone;
   593 	}
   594 
   595 LOCAL_C TInt IntInsertInOrderTest(TInt aCount, TInt aNumTests, TInt64 aMask)
   596 	{
   597 	TInt n;
   598 	for (n=0; n<aNumTests; n++)
   599 		{
   600 		RPointerArray<TInt64> a;
   601 		RPointerArray<TInt64> b;
   602 		RPointerArray<TInt64> c;
   603 		TInt i;
   604 		TInt cc=0;
   605 		for (i=0; i<aCount; i++)
   606 			{
   607 			Int64s[i]=Random64(aMask);
   608 			a.Append(&Int64s[i]);
   609 			b.InsertInOrderAllowRepeats(&Int64s[i],Int64Order);
   610 			TInt r=c.InsertInOrder(&Int64s[i],Int64Order);
   611 			if (r==KErrNone)
   612 				cc++;
   613 			}
   614 		if (a.Count()!=aCount)
   615 			{
   616 			a.Close();
   617 			b.Close();
   618 			c.Close();
   619 			return -1;
   620 			}
   621 		if (b.Count()!=aCount)
   622 			{
   623 			a.Close();
   624 			b.Close();
   625 			c.Close();
   626 			return -2;
   627 			}
   628 		for (i=0; i<aCount-1; i++)
   629 			{
   630 			if (*b[i]>*b[i+1])
   631 				{
   632 				a.Close();
   633 				b.Close();
   634 				c.Close();
   635 				return -3;
   636 				}
   637 			}
   638 		for (i=0; i<aCount; i++)
   639 			{
   640 			if (a.Find(b[i],Int64Identity)<0)
   641 				{
   642 				a.Close();
   643 				b.Close();
   644 				c.Close();
   645 				return -4;
   646 				}
   647 			if (b.Find(a[i],Int64Identity)<0)
   648 				{
   649 				a.Close();
   650 				b.Close();
   651 				c.Close();
   652 				return -5;
   653 				}
   654 			if (c.Find(a[i],Int64Identity)<0)
   655 				{
   656 				a.Close();
   657 				b.Close();
   658 				c.Close();
   659 				return -6;
   660 				}
   661 			}
   662 		if (c.Count()!=cc)
   663 			{
   664 			a.Close();
   665 			b.Close();
   666 			c.Close();
   667 			return -7;
   668 			}
   669 		for (i=0; i<c.Count()-1; i++)
   670 			{
   671 			if (*c[i]>=*c[i+1])
   672 				{
   673 				a.Close();
   674 				b.Close();
   675 				c.Close();
   676 				return -8;
   677 				}
   678 			if (a.Find(c[i],Int64Identity)<0)
   679 				{
   680 				a.Close();
   681 				b.Close();
   682 				c.Close();
   683 				return -9;
   684 				}
   685 			}
   686 		a.Close();
   687 		b.Close();
   688 		c.Close();
   689 		}
   690 	return KErrNone;
   691 	}
   692 
   693 LOCAL_C TInt SEntrySpecificFindTests(TInt aCount, TInt aNumTests, TInt aRange)
   694 	{
   695 	TInt n;
   696 	TInt nmiss = 0;
   697 	TInt nrpt = 0;
   698 	TInt ntot = 0;
   699 	for (n=0; n<aNumTests; n++)
   700 		{
   701 		RPointerArray<SEntry> a;
   702 		RPointerArray<SEntry> b;
   703 		RPointerArray<SEntry> c;
   704 		RPointerArray<SEntry> d;
   705 		TInt i;
   706 		for (i=0; i<aCount; i++)
   707 			{
   708 			TInt x=Random()&aRange;
   709 			x-=(aRange>>1);
   710 			SEntry& e = Entries[i];
   711 			e.iKey = x;
   712 			e.iValue = i;
   713 			a.Append(&e);
   714 			c.Append(&e);
   715 			b.InsertInOrderAllowRepeats(&e, &OrderSEntry);
   716 			d.InsertInOrderAllowRepeats(&e, &OrderSEntryU);
   717 			}
   718 		a.Sort(&OrderSEntry2);
   719 		c.Sort(&OrderSEntryU2);
   720 		test(a.Count()==aCount);
   721 		test(b.Count()==aCount);
   722 		test(c.Count()==aCount);
   723 		test(d.Count()==aCount);
   724 		for (i=0; i<aCount; i++)
   725 			{
   726 			test(a[i]==b[i]);
   727 			test(c[i]==d[i]);
   728 			}
   729 		for (i=-(aRange>>1); i<=(aRange>>1); ++i)
   730 			{
   731 			SEntry es;
   732 			es.iKey = i;
   733 			TInt first = a.SpecificFindInOrder(&es, &OrderSEntry, EArrayFindMode_First);
   734 			TInt last = a.SpecificFindInOrder(&es, &OrderSEntry, EArrayFindMode_Last);
   735 			TInt any = a.SpecificFindInOrder(&es, &OrderSEntry, EArrayFindMode_Any);
   736 			TInt fi, li, ai;
   737 			TInt first2 = a.SpecificFindInOrder(&es, fi, &OrderSEntry, EArrayFindMode_First);
   738 			TInt last2 = a.SpecificFindInOrder(&es, li, &OrderSEntry, EArrayFindMode_Last);
   739 			TInt any2 = a.SpecificFindInOrder(&es, ai, &OrderSEntry, EArrayFindMode_Any);
   740 			++ntot;
   741 			if (first < 0)
   742 				{
   743 				test(first == KErrNotFound);
   744 				test(first == last);
   745 				test(first == any);
   746 				test(first == first2);
   747 				test(first == last2);
   748 				test(first == any2);
   749 				test(fi == li);
   750 				test(fi == ai);
   751 				test(li==aCount || a[li]->iKey>i);
   752 				test(li==0 || a[li-1]->iKey<i);
   753 				++nmiss;
   754 				}
   755 			else
   756 				{
   757 				test(first2 == KErrNone);
   758 				test(last2 == KErrNone);
   759 				test(any2 == KErrNone);
   760 				test(first == fi);
   761 				test(last == li);
   762 				test(any == ai);
   763 				test(a[fi]->iKey == i);
   764 				test(a[li-1]->iKey == i);
   765 				test(li==aCount || a[li]->iKey>i);
   766 				test(ai>=fi && ai<li);
   767 				test(a[ai]->iKey == i);
   768 				if (li-fi > 1)
   769 					{
   770 					++nrpt;
   771 					TInt j;
   772 					for (j=fi+1; j<li; ++j)
   773 						test(a[j]->iValue > a[j-1]->iValue);
   774 					}
   775 				}
   776 			}
   777 		for (i=-(aRange>>1); i<=(aRange>>1); ++i)
   778 			{
   779 			TUint u = (TUint)i;
   780 			SEntry eu;
   781 			eu.iKey = i;
   782 			TInt first = c.SpecificFindInOrder(&eu, &OrderSEntryU, EArrayFindMode_First);
   783 			TInt last = c.SpecificFindInOrder(&eu, &OrderSEntryU, EArrayFindMode_Last);
   784 			TInt any = c.SpecificFindInOrder(&eu, &OrderSEntryU, EArrayFindMode_Any);
   785 			TInt fi, li, ai;
   786 			TInt first2 = c.SpecificFindInOrder(&eu, fi, &OrderSEntryU, EArrayFindMode_First);
   787 			TInt last2 = c.SpecificFindInOrder(&eu, li, &OrderSEntryU, EArrayFindMode_Last);
   788 			TInt any2 = c.SpecificFindInOrder(&eu, ai, &OrderSEntryU, EArrayFindMode_Any);
   789 			++ntot;
   790 			if (first < 0)
   791 				{
   792 				test(first == KErrNotFound);
   793 				test(first == last);
   794 				test(first == any);
   795 				test(first == first2);
   796 				test(first == last2);
   797 				test(first == any2);
   798 				test(fi == li);
   799 				test(fi == ai);
   800 				test(li==aCount || TUint(c[li]->iKey)>u);
   801 				test(li==0 || TUint(c[li-1]->iKey)<u);
   802 				++nmiss;
   803 				}
   804 			else
   805 				{
   806 				test(first2 == KErrNone);
   807 				test(last2 == KErrNone);
   808 				test(any2 == KErrNone);
   809 				test(first == fi);
   810 				test(last == li);
   811 				test(any == ai);
   812 				test(c[fi]->iKey == i);
   813 				test(c[li-1]->iKey == i);
   814 				test(li==aCount || TUint(c[li]->iKey)>u);
   815 				test(ai>=fi && ai<li);
   816 				test(c[ai]->iKey == i);
   817 				if (li-fi > 1)
   818 					{
   819 					++nrpt;
   820 					TInt j;
   821 					for (j=fi+1; j<li; ++j)
   822 						test(c[j]->iValue > c[j-1]->iValue);
   823 					}
   824 				}
   825 			}
   826 		a.Close();
   827 		b.Close();
   828 		c.Close();
   829 		d.Close();
   830 		}
   831 	test.Printf(_L("ntot=%d nmiss=%d nrpt=%d\n"), ntot, nmiss, nrpt);
   832 	return KErrNone;
   833 	}
   834 
   835 LOCAL_C TInt EntryInsertInOrderTest()
   836 	{
   837 	RPointerArray<SEntry> b;
   838 	RPointerArray<SEntry> c;
   839 	TInt i;
   840 	for (i=0; i<1024; i++)
   841 		{
   842 		SEntry e;
   843 		e.iValue=i;
   844 		e.iKey=i&31;
   845 		Entries[i]=e;
   846 		b.InsertInOrderAllowRepeats(&Entries[i],SEntryOrder);
   847 		c.InsertInOrder(&Entries[i],SEntryOrder);
   848 		}
   849 	if (b.Count()!=1024)
   850 		{
   851 		b.Close();
   852 		c.Close();
   853 		return -1;
   854 		}
   855 	for (i=0; i<1024; i++)
   856 		{
   857 		SEntry e=*b[i];
   858 		if (e.iKey!=i>>5)
   859 			{
   860 			b.Close();
   861 			c.Close();
   862 			return -2;
   863 			}
   864 		if (e.iValue!=((i&31)<<5 | (i>>5)))
   865 			{
   866 			b.Close();
   867 			c.Close();
   868 			return -3;
   869 			}
   870 		}
   871 	if (c.Count()!=32)
   872 		{
   873 		b.Close();
   874 		c.Close();
   875 		return -4;
   876 		}
   877 	for (i=0; i<31; i++)
   878 		{
   879 		SEntry e=*c[i];
   880 		if (e.iKey!=i)
   881 			{
   882 			b.Close();
   883 			c.Close();
   884 			return -5;
   885 			}
   886 		if (e.iValue!=i)
   887 			{
   888 			b.Close();
   889 			c.Close();
   890 			return -6;
   891 			}
   892 		}
   893 	b.Close();
   894 	c.Close();
   895 	return KErrNone;
   896 	}
   897 
   898 LOCAL_C TInt IntSortTest(TInt aCount, TInt aNumTests, TInt64 aMask)
   899 	{
   900 	TInt n;
   901 	for (n=0; n<aNumTests; n++)
   902 		{
   903 		RPointerArray<TInt64> a;
   904 		RPointerArray<TInt64> b;
   905 		TInt i;
   906 		for (i=0; i<aCount; i++)
   907 			{
   908 			Int64s[i]=Random64(aMask);
   909 			a.Append(&Int64s[i]);
   910 			b.InsertInOrderAllowRepeats(&Int64s[i],Int64Order);
   911 			}
   912 		a.Sort(Int64Order);
   913 		if (a.Count()!=aCount)
   914 			{
   915 			a.Close();
   916 			b.Close();
   917 			return -1;
   918 			}
   919 		if (b.Count()!=aCount)
   920 			{
   921 			a.Close();
   922 			b.Close();
   923 			return -2;
   924 			}
   925 		for (i=0; i<aCount; i++)
   926 			{
   927 			if (*a[i]!=*b[i])
   928 				{
   929 				a.Close();
   930 				b.Close();
   931 				return -3;
   932 				}
   933 			}
   934 		a.Close();
   935 		b.Close();
   936 		}
   937 	return KErrNone;
   938 	}
   939 
   940 LOCAL_C void TestGrowCompress(RPointerArray<TInt64>* a, ...)
   941 	{
   942 	SPointerArray& pa = *(SPointerArray*)a;
   943 	VA_LIST list;
   944 	VA_START(list, a);
   945 	TInt64 x;
   946 	FOREVER
   947 		{
   948 		TInt r = KErrNone;
   949 		TInt action = VA_ARG(list, TInt);
   950 		if (action == -99)
   951 			break;
   952 		TInt result = VA_ARG(list, TInt);
   953 		TInt orig = pa.iAllocated;
   954 		if (action == -1)
   955 			a->Compress();
   956 		else if (action == -2)
   957 			a->GranularCompress();
   958 		else if (action == -3)
   959 			a->Remove(pa.iCount - 1);
   960 		else if (action > 0)
   961 			{
   962 			TInt i;
   963 			for (i=0; i<action && r==KErrNone; ++i)
   964 				r = a->Append(&x);
   965 			}
   966 		if ( (r<0 && (result!=r || pa.iAllocated!=orig)) || (r==0 && pa.iAllocated!=result) )
   967 			{
   968 			test.Printf(_L("Action %d Orig %d Expected %d r=%d newalloc=%d\n"), action, orig, result, r, pa.iAllocated);
   969 			test(0);
   970 			}
   971 		}
   972 	a->Reset();
   973 	}
   974 
   975 LOCAL_C void TestGrowCompress()
   976 	{
   977 	RPointerArray<TInt64> a;
   978 	TestGrowCompress(&a, 1, 8, 7, 8, 1, 16, 7, 16, 1, 24, -2, 24, -1, 17, 1, 25, -2, 25, -3, 25, -2, 24, -99);
   979 	TestGrowCompress(&a, 1, 8, 7, 8, 1, 16, 7, 16, 1, 24, -2, 24, -1, 17, 1, 25, -2, 25, -3, 25, -3, 25, -2, 16, -99);
   980 
   981 	RPointerArray<TInt64> b(100);
   982 	TestGrowCompress(&b, 1, 100, 99, 100, 1, 200, 99, 200, 1, 300, -2, 300, -1, 201, 1, 301, -2, 301, -3, 301, -2, 300, -99);
   983 	TestGrowCompress(&b, 1, 100, 99, 100, 1, 200, 99, 200, 1, 300, -2, 300, -1, 201, 1, 301, -2, 301, -3, 301, -3, 301, -2, 200, -99);
   984 
   985 	RPointerArray<TInt64> c(8, 512);
   986 	TestGrowCompress(&c, 1, 8, 7, 8, 1, 16, 7, 16, 1, 32, 15, 32, 1, 64, -2, 40, 7, 40, 1, 80, -1, 41, -99);
   987 
   988 	RPointerArray<TInt64> d(20, 640);
   989 	TestGrowCompress(&d, 1, 20, 19, 20, 1, 50, 29, 50, 1, 125, -2, 60, -1, 51, -99);
   990 
   991 	RPointerArray<TInt64> e(8, 320);
   992 	TestGrowCompress(&e, 1, 8, 7, 8, 1, 16, 7, 16, 1, 24, 7, 24, 1, 32, 7, 32, 1, 40, 7, 40, 1, 50, 9, 50, 1, 63, -99);
   993 
   994 	RPointerArray<TInt64> f(2, 257);
   995 	TestGrowCompress(&f, 1, 2, 255, 256, 256, 512, 128, 640, 1, 643, 2, 643, 1, 646, -99);
   996 	}
   997 
   998 GLDEF_C void DoPointerArrayTests()
   999 	{
  1000 	test.Start(_L("Pointer Arrays..."));
  1001 	test.Next(_L("Allocate memory"));
  1002 	Int64s=new TInt64[KArraySize];
  1003 	Entries=new SEntry[KArraySize];
  1004 	test(Int64s && Entries);
  1005 
  1006 	test.Next(_L("AppendAndAccess tests..."));
  1007 	test.Next(_L("Count 10 Mask 0x0000000300000003"));
  1008 	test(IntAppendAndAccessTest(10,NUM_TESTS,MAKE_TINT64(0x3,0x3))==KErrNone);
  1009 	test.Next(_L("Count 100 Range all"));
  1010 	test(IntAppendAndAccessTest(100,NUM_TESTS,MAKE_TINT64(0xffffffff,0xffffffff))==KErrNone);
  1011 
  1012 	test.Next(_L("Find tests..."));
  1013 	test.Next(_L("Count 10 Mask 0x0000000300000003"));
  1014 	test(IntFindTest(10,NUM_TESTS,MAKE_TINT64(3,3))==KErrNone);
  1015 	test.Next(_L("Count 100 Range all"));
  1016 	test(IntFindTest(100,NUM_TESTS,MAKE_TINT64(0xffffffff,0xffffffff))==KErrNone);
  1017 	test.Next(_L("SEntry find tests"));
  1018 	test(EntryFindTest(128,NUM_TESTS)==KErrNone);
  1019 	
  1020 	test.Next(_L("FindReverse tests..."));
  1021 	test.Next(_L("Count 10 Mask 0x0000000300000003"));
  1022 	test(IntFindReverseTest(10,NUM_TESTS,MAKE_TINT64(3,3))==KErrNone);
  1023 	test.Next(_L("Count 100 Range all"));
  1024 	test(IntFindReverseTest(100,NUM_TESTS,MAKE_TINT64(0xffffffff,0xffffffff))==KErrNone);
  1025 	test.Next(_L("SEntry find tests"));
  1026 
  1027 	test(EntryFindReverseTest(128,NUM_TESTS)==KErrNone);
  1028 
  1029 	test.Next(_L("FindInOrder tests..."));
  1030 	test.Next(_L("Count 20 Mask 0x00000003C0000000"));
  1031 	test(IntFindInOrderTest(20,NUM_TESTS,MAKE_TINT64(0x3,0xc0000000))==KErrNone);
  1032 	test.Next(_L("Count 100 Mask 0x0000000FF0000000"));
  1033 	test(IntFindInOrderTest(100,NUM_TESTS,MAKE_TINT64(0xf,0xf0000000))==KErrNone);
  1034 	test.Next(_L("SEntry FindInOrder test"));
  1035 	test(EntryFindInOrderTest(128,NUM_TESTS)==KErrNone);
  1036 
  1037 	test.Next(_L("InsertInOrder tests..."));
  1038 	test.Next(_L("Count 50 Mask 0x00000003C0000000"));
  1039 	test(IntInsertInOrderTest(50,NUM_TESTS,MAKE_TINT64(0x3,0xc0000000))==KErrNone);
  1040 	test.Next(_L("Count 100 all"));
  1041 	test(IntInsertInOrderTest(100,NUM_TESTS,MAKE_TINT64(0xffffffff,0xffffffff))==KErrNone);
  1042 	test.Next(_L("SEntry InsertInOrder test"));
  1043 	test(EntryInsertInOrderTest()==KErrNone);
  1044 
  1045 	test.Next(_L("Sort tests..."));
  1046 	test.Next(_L("Count 30 Mask 0x00000003C0000000"));
  1047 	test(IntSortTest(30,NUM_TESTS,MAKE_TINT64(0x3,0xc0000000))==KErrNone);
  1048 	test.Next(_L("Count 100 all"));
  1049 	test(IntSortTest(100,NUM_TESTS,MAKE_TINT64(0xffffffff,0xffffffff))==KErrNone);
  1050 
  1051 	test.Next(_L("SEntrySpecificFindTests..."));
  1052 	test(SEntrySpecificFindTests(100, 10, 15)==KErrNone);
  1053 	test(SEntrySpecificFindTests(100, 10, 127)==KErrNone);
  1054 
  1055 	test.Next(_L("Test Grow/Compress"));
  1056 	TestGrowCompress();
  1057 
  1058 	test.Next(_L("Test RPointerArray::Array..."));
  1059 	TInt a;
  1060 	TInt b;
  1061 	TInt c;
  1062 	RPointerArray<TInt> ptrArr;
  1063 	ptrArr.Append(&a);
  1064 	ptrArr.Append(&b);
  1065 	ptrArr.Append(&c);
  1066 
  1067 	TArray<TInt*> arr=ptrArr.Array();
  1068 	test(arr.Count()==3);
  1069 	test(arr[0]==&a);
  1070 	test(arr[1]==&b);
  1071 	test(arr[2]==&c);
  1072 
  1073 	ptrArr.Reset();
  1074 
  1075 	delete[] Int64s;
  1076 	delete[] Entries;
  1077 	test.End();
  1078 	}
  1079 
  1080 GLDEF_C void DoPointerArrayLeavingInterfaceTest()
  1081 	{
  1082 	TInt trap, ret(0);
  1083 	TInt64 Int64s[3];
  1084 	for (TInt i=0;i<3;i++) Int64s[i] = i;
  1085 
  1086 	RPointerArray<TInt64> pArray;
  1087 	CleanupClosePushL(pArray);
  1088 
  1089 	test.Start(_L("Checking Leaving Pointer Arrays Interface..."));
  1090 
  1091 	test.Next(_L("AppendL test..."));
  1092 	TRAP(trap, pArray.AppendL(&Int64s[0]));
  1093 	test(trap==KErrNone);
  1094 
  1095 	test.Next(_L("InsertL test..."));
  1096 	TRAP(trap, pArray.InsertL(&Int64s[1],1));
  1097 	test(trap==KErrNone);
  1098 
  1099 	test.Next(_L("Test FindL(const T* anEntry) const..."));
  1100 	TRAP(trap, ret = pArray.FindL(&Int64s[0]));
  1101 	test(trap==0);
  1102 	test(ret==0);
  1103 	TRAP(trap, ret = pArray.FindL(&Int64s[2]));
  1104 	test(trap==KErrNotFound);
  1105 	
  1106 	test.Next(_L("Test FindL(const T* anEntry, TIdentityRelation<T> anIdentity) const..."));
  1107 	TRAP(trap, ret = pArray.FindL(&Int64s[0],Int64Identity));
  1108 	test(trap==0);
  1109 	test(ret==0);
  1110 	TRAP(trap, ret = pArray.FindL(&Int64s[2],Int64Identity));
  1111 	test(trap==KErrNotFound);
  1112 
  1113 	test.Next(_L("Test FindInAddressOrderL(const T* anEntry) const..."));
  1114 	TRAP(trap, ret = pArray.FindInAddressOrderL(&Int64s[0]));
  1115 	test(trap==0);
  1116 	test(ret==0);
  1117 	TRAP(trap, ret = pArray.FindInAddressOrderL(&Int64s[2]));
  1118 	test(trap==KErrNotFound);
  1119 
  1120 	test.Next(_L("Test FindInOrderL(const T* anEntry, TLinearOrder<T> anOrder) const..."));
  1121 	TRAP(trap, ret = pArray.FindInOrderL(&Int64s[0], Int64Order));
  1122 	test(trap==0);
  1123 	test(ret==0);
  1124 	TRAP(trap, ret = pArray.FindInOrderL(&Int64s[2], Int64Order));
  1125 	test(trap==KErrNotFound);
  1126 
  1127 	test.Next(_L("Test FindInAddressOrderL(const T* anEntry, TInt& anIndex) const..."));
  1128 	TRAP(trap, pArray.FindInAddressOrderL(&Int64s[0], ret));
  1129 	test(trap==0);
  1130 	test(ret==0);
  1131 	TRAP(trap, pArray.FindInAddressOrderL(&Int64s[2], ret));
  1132 	test(trap==KErrNotFound);
  1133 
  1134 	test.Next(_L("Test FindInOrderL(const T* anEntry, TInt& anIndex, TLinearOrder<T> anOrder) const..."));
  1135 	TRAP(trap, pArray.FindInOrderL(&Int64s[0], ret, Int64Order));
  1136 	test(trap==0);
  1137 	test(ret==0);
  1138 	TRAP(trap, pArray.FindInOrderL(&Int64s[2], ret, Int64Order));
  1139 	test(trap==KErrNotFound);
  1140 
  1141 	test.Next(_L("Test SpecificFindInAddressOrderL(const T* anEntry, TInt aMode) const..."));
  1142 	TRAP(trap, ret = pArray.SpecificFindInAddressOrderL(&Int64s[0], EArrayFindMode_First));
  1143 	test(trap==0);
  1144 	test(ret==0);
  1145 	TRAP(trap, ret = pArray.SpecificFindInAddressOrderL(&Int64s[2], EArrayFindMode_First));
  1146 	test(trap==KErrNotFound);
  1147 
  1148 	test.Next(_L("Test SpecificFindInOrderL(const T* anEntry, TLinearOrder<T> anOrder, TInt aMode) const..."));
  1149 	TRAP(trap, ret = pArray.SpecificFindInOrderL(&Int64s[0], Int64Order, EArrayFindMode_First));
  1150 	test(trap==0);
  1151 	test(ret==0);
  1152 	TRAP(trap, ret = pArray.SpecificFindInOrderL(&Int64s[2], Int64Order, EArrayFindMode_First));
  1153 	test(trap==KErrNotFound);
  1154 
  1155 	test.Next(_L("Test SpecificFindInAddressOrderL(const T* anEntry, TInt& anIndex, TInt aMode) const..."));
  1156 	TRAP(trap, pArray.SpecificFindInAddressOrderL(&Int64s[0], ret, EArrayFindMode_First));
  1157 	test(trap==0);
  1158 	test(ret==0);
  1159 	TRAP(trap, pArray.SpecificFindInAddressOrderL(&Int64s[2], ret, EArrayFindMode_First));
  1160 	test(trap==KErrNotFound);
  1161 
  1162 	test.Next(_L("Test SpecificFindInOrderL(const T* anEntry, TInt& anIndex, TLinearOrder<T> anOrder, TInt aMode) const..."));
  1163 	TRAP(trap, pArray.SpecificFindInOrderL(&Int64s[0], ret, Int64Order, EArrayFindMode_First));
  1164 	test(trap==0);
  1165 	test(ret==0);
  1166 	TRAP(trap, pArray.SpecificFindInOrderL(&Int64s[2], ret, Int64Order, EArrayFindMode_First));
  1167 	test(trap==KErrNotFound);
  1168 
  1169 	test.Next(_L("Test InsertInAddressOrderL(const T* anEntry)..."));
  1170 	TRAP(trap, pArray.InsertInAddressOrderL(&Int64s[0]));
  1171 	test(trap==KErrAlreadyExists);
  1172 	TRAP(trap, pArray.InsertInAddressOrderL(&Int64s[2]));
  1173 	test(trap==KErrNone);
  1174 	pArray.Remove(2);
  1175 
  1176 	test.Next(_L("Test InsertInOrderL(const T* anEntry, TLinearOrder<T> anOrder)..."));
  1177 	TRAP(trap, pArray.InsertInOrderL(&Int64s[0], Int64Order));
  1178 	test(trap==KErrAlreadyExists);
  1179 	TRAP(trap, pArray.InsertInOrderL(&Int64s[2], Int64Order));
  1180 	test(trap==KErrNone);
  1181 	pArray.Remove(2);
  1182 	
  1183 	test.Next(_L("Test InsertInAddressOrderAllowRepeatsL(const T* anEntry)..."));
  1184 	TRAP(trap, pArray.InsertInAddressOrderAllowRepeatsL(&Int64s[2]));
  1185 	test(trap==KErrNone);
  1186 	pArray.Remove(2);
  1187 
  1188 	test.Next(_L("Test InsertInOrderAllowRepeatsL(const T* anEntry, TLinearOrder<T> anOrder)..."));
  1189 	TRAP(trap, pArray.InsertInOrderAllowRepeatsL(&Int64s[2], Int64Order));
  1190 	test(trap==KErrNone);
  1191 	pArray.Remove(2);
  1192 	
  1193 	CleanupStack::PopAndDestroy(&pArray);
  1194 	test.End();
  1195 	}