kerneltest/e32test/buffer/ANY_PTR_ARR.CPP
changeset 9 96e5fb8b040d
equal deleted inserted replaced
-1:000000000000 9:96e5fb8b040d
       
     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\any_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 SPointerArray
       
    30 	{
       
    31 	TInt iCount;
       
    32 	TAny** iEntries;
       
    33 	TInt iAllocated;
       
    34 	TInt iGranularity;
       
    35 	};
       
    36 
       
    37 LOCAL_D TInt64* Int64s;
       
    38 
       
    39 LOCAL_C TInt64 Random64(TInt64& aMask)
       
    40 	{
       
    41 	TInt64 x = MAKE_TINT64(Random()&I64HIGH(aMask), Random()&I64LOW(aMask));
       
    42 	return x;
       
    43 	}
       
    44 
       
    45 LOCAL_C TInt AnyAppendAndAccessTest(TInt aCount, TInt aNumTests, TInt64 aMask)
       
    46 	{
       
    47 	TInt n;
       
    48 	for (n=0; n<aNumTests; n++)
       
    49 		{
       
    50 		RPointerArray<TAny> a;
       
    51 		TInt64 *pA=new TInt64[aCount];
       
    52 		if (!pA)
       
    53 			{
       
    54 			a.Close();
       
    55 			return -65535;
       
    56 			}
       
    57 		TInt i;
       
    58 		for (i=0; i<aCount; i++)
       
    59 			{
       
    60 			Int64s[i]=Random64(aMask);
       
    61 			pA[i]=Int64s[i];
       
    62 			a.Append(&Int64s[i]);
       
    63 			}
       
    64 		if (a.Count()!=aCount)
       
    65 			{
       
    66 			a.Close();
       
    67 			return -1;
       
    68 			}
       
    69 		for (i=0; i<aCount; i++)
       
    70 			{
       
    71 			if (a[i]!=&Int64s[i])
       
    72 				{
       
    73 				a.Close();
       
    74 				return -2;
       
    75 				}
       
    76 			if (*(TInt64*)a[i]!=pA[i])
       
    77 				{
       
    78 				a.Close();
       
    79 				return -3;
       
    80 				}
       
    81 			a[i]=&pA[i];
       
    82 			}
       
    83 		if (a.Count()!=aCount)
       
    84 			{
       
    85 			a.Close();
       
    86 			return -4;
       
    87 			}
       
    88 		for (i=0; i<aCount; i++)
       
    89 			{
       
    90 			if (a[i]!=&pA[i])
       
    91 				{
       
    92 				a.Close();
       
    93 				return -5;
       
    94 				}
       
    95 			if (*(TInt64*)a[i]!=Int64s[i])
       
    96 				{
       
    97 				a.Close();
       
    98 				return -6;
       
    99 				}
       
   100 			}
       
   101 		delete[] pA;
       
   102 		a.Close();
       
   103 		}
       
   104 	return KErrNone;
       
   105 	}
       
   106 
       
   107 LOCAL_C TInt AnyFindTest(TInt aCount, TInt aNumTests, TInt64 aMask)
       
   108 	{
       
   109 	TInt n;
       
   110 	for (n=0; n<aNumTests; n++)
       
   111 		{
       
   112 		RPointerArray<TAny> a;
       
   113 		TInt64 *pA=new TInt64[aCount];
       
   114 		if (!pA)
       
   115 			{
       
   116 			a.Close();
       
   117 			return -65535;
       
   118 			}
       
   119 		TInt i;
       
   120 		for (i=0; i<aCount; i++)
       
   121 			{
       
   122 			pA[i]=Random64(aMask);
       
   123 			Int64s[i]=pA[i];
       
   124 			a.Append(&Int64s[i]);
       
   125 			}
       
   126 		if (a.Count()!=aCount)
       
   127 			{
       
   128 			a.Close();
       
   129 			return -1;
       
   130 			}
       
   131 		for (i=0; i<aCount; i++)
       
   132 			{
       
   133 			TInt r=a.Find(&Int64s[i]);
       
   134 			if (r!=i)
       
   135 				{
       
   136 				a.Close();
       
   137 				return -2;
       
   138 				}
       
   139 			r=a.Find(&pA[i]);
       
   140 			if (r>=0)
       
   141 				{
       
   142 				a.Close();
       
   143 				return -3;
       
   144 				}
       
   145 			r=a.FindInAddressOrder(&Int64s[i]);
       
   146 			if (r!=i)
       
   147 				{
       
   148 				a.Close();
       
   149 				return -4;
       
   150 				}
       
   151 			TInt j;
       
   152 			r=a.FindInAddressOrder(&Int64s[i], j);
       
   153 			if (r!=KErrNone || j!=i)
       
   154 				{
       
   155 				a.Close();
       
   156 				return -5;
       
   157 				}
       
   158 			/*
       
   159 			r=a.FindInAddressOrder(((TInt*)&Int64s[i])-1, j);
       
   160 			test.Printf(_L("n == %d, i == %d, r == %d, j == %d"), n, i, r, j);
       
   161 			if (r!=KErrNotFound || j!=i)
       
   162 				{
       
   163 				a.Close();
       
   164 				return -6;
       
   165 				}
       
   166 			*/
       
   167 			}
       
   168 		delete[] pA;
       
   169 		a.Close();
       
   170 		}
       
   171 	return KErrNone;
       
   172 	}
       
   173 
       
   174 LOCAL_C TInt AnyInsertInAddressOrderTest(TInt aCount, TInt aNumTests, TUint aMask)
       
   175 	{
       
   176 	TInt n;
       
   177 	for (n=0; n<aNumTests; n++)
       
   178 		{
       
   179 		RPointerArray<TAny> a;
       
   180 		RPointerArray<TAny> b;
       
   181 		RPointerArray<TAny> c;
       
   182 		TInt i;
       
   183 		TInt cc=0;
       
   184 		for (i=0; i<aCount; i++)
       
   185 			{
       
   186 			TAny* x=(TAny*)(((TUint)Random())&aMask);  // don't dereference this!
       
   187 			a.Append(x);
       
   188 			b.InsertInAddressOrderAllowRepeats(x);
       
   189 			TInt r=c.InsertInAddressOrder(x);
       
   190 			if (r==KErrNone)
       
   191 				cc++;
       
   192 			}
       
   193 		if (a.Count()!=aCount)
       
   194 			{
       
   195 			a.Close();
       
   196 			b.Close();
       
   197 			c.Close();
       
   198 			return -1;
       
   199 			}
       
   200 		if (b.Count()!=aCount)
       
   201 			{
       
   202 			a.Close();
       
   203 			b.Close();
       
   204 			c.Close();
       
   205 			return -2;
       
   206 			}
       
   207 		for (i=0; i<aCount-1; i++)
       
   208 			{
       
   209 			if ((TUint)b[i]>(TUint)b[i+1])
       
   210 				{
       
   211 				a.Close();
       
   212 				b.Close();
       
   213 				c.Close();
       
   214 				return -3;
       
   215 				}
       
   216 			}
       
   217 		for (i=0; i<aCount; i++)
       
   218 			{
       
   219 			if (a.Find(b[i])<0)
       
   220 				{
       
   221 				a.Close();
       
   222 				b.Close();
       
   223 				c.Close();
       
   224 				return -4;
       
   225 				}
       
   226 			if (b.Find(a[i])<0)
       
   227 				{
       
   228 				a.Close();
       
   229 				b.Close();
       
   230 				c.Close();
       
   231 				return -5;
       
   232 				}
       
   233 			if (c.Find(a[i])<0)
       
   234 				{
       
   235 				a.Close();
       
   236 				b.Close();
       
   237 				c.Close();
       
   238 				return -6;
       
   239 				}
       
   240 			}
       
   241 		if (c.Count()!=cc)
       
   242 			{
       
   243 			a.Close();
       
   244 			b.Close();
       
   245 			c.Close();
       
   246 			return -7;
       
   247 			}
       
   248 		for (i=0; i<c.Count()-1; i++)
       
   249 			{
       
   250 			if ((TUint)c[i]>=(TUint)c[i+1])
       
   251 				{
       
   252 				a.Close();
       
   253 				b.Close();
       
   254 				c.Close();
       
   255 				return -8;
       
   256 				}
       
   257 			if (a.Find(c[i])<0)
       
   258 				{
       
   259 				a.Close();
       
   260 				b.Close();
       
   261 				c.Close();
       
   262 				return -9;
       
   263 				}
       
   264 			}
       
   265 		a.Close();
       
   266 		b.Close();
       
   267 		c.Close();
       
   268 		}
       
   269 	return KErrNone;
       
   270 	}
       
   271 
       
   272 LOCAL_C TInt AnySortIntoAddressOrderTest(TInt aCount, TInt aNumTests, TUint aMask)
       
   273 	{
       
   274 	TInt n;
       
   275 	for (n=0; n<aNumTests; n++)
       
   276 		{
       
   277 		RPointerArray<TAny> a;
       
   278 		RPointerArray<TAny> b;
       
   279 		TInt i;
       
   280 		for (i=0; i<aCount; i++)
       
   281 			{
       
   282 			TAny* x=(TAny*)(((TUint)Random())&aMask);  // don't dereference this!
       
   283 			a.Append(x);
       
   284 			b.InsertInAddressOrderAllowRepeats(x);
       
   285 			}
       
   286 		a.SortIntoAddressOrder();
       
   287 		if (a.Count()!=aCount)
       
   288 			{
       
   289 			a.Close();
       
   290 			b.Close();
       
   291 			return -1;
       
   292 			}
       
   293 		if (b.Count()!=aCount)
       
   294 			{
       
   295 			a.Close();
       
   296 			b.Close();
       
   297 			return -2;
       
   298 			}
       
   299 		for (i=0; i<aCount; i++)
       
   300 			{
       
   301 			if (a[i]!=b[i])
       
   302 				{
       
   303 				a.Close();
       
   304 				b.Close();
       
   305 				return -3;
       
   306 				}
       
   307 			}
       
   308 		a.Close();
       
   309 		b.Close();
       
   310 		}
       
   311 	return KErrNone;
       
   312 	}
       
   313 
       
   314 LOCAL_C void TestGrowCompress(RPointerArray<TAny>* a, ...)
       
   315 	{
       
   316 	SPointerArray& pa = *(SPointerArray*)a;
       
   317 	VA_LIST list;
       
   318 	VA_START(list, a);
       
   319 	TInt64 x;
       
   320 	FOREVER
       
   321 		{
       
   322 		TInt r = KErrNone;
       
   323 		TInt action = VA_ARG(list, TInt);
       
   324 		if (action == -99)
       
   325 			break;
       
   326 		TInt result = VA_ARG(list, TInt);
       
   327 		TInt orig = pa.iAllocated;
       
   328 		if (action == -1)
       
   329 			a->Compress();
       
   330 		else if (action == -2)
       
   331 			a->GranularCompress();
       
   332 		else if (action == -3)
       
   333 			a->Remove(pa.iCount - 1);
       
   334 		else if (action > 0)
       
   335 			{
       
   336 			TInt i;
       
   337 			for (i=0; i<action && r==KErrNone; ++i)
       
   338 				r = a->Append(&x);
       
   339 			}
       
   340 		if ( (r<0 && (result!=r || pa.iAllocated!=orig)) || (r==0 && pa.iAllocated!=result) )
       
   341 			{
       
   342 			test.Printf(_L("Action %d Orig %d Expected %d r=%d newalloc=%d\n"), action, orig, result, r, pa.iAllocated);
       
   343 			test(0);
       
   344 			}
       
   345 		}
       
   346 	a->Reset();
       
   347 	}
       
   348 
       
   349 LOCAL_C void TestGrowCompress()
       
   350 	{
       
   351 	RPointerArray<TAny> a;
       
   352 	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);
       
   353 	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);
       
   354 
       
   355 	RPointerArray<TAny> b(100);
       
   356 	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);
       
   357 	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);
       
   358 
       
   359 	RPointerArray<TAny> c(8, 512);
       
   360 	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);
       
   361 
       
   362 	RPointerArray<TAny> d(20, 640);
       
   363 	TestGrowCompress(&d, 1, 20, 19, 20, 1, 50, 29, 50, 1, 125, -2, 60, -1, 51, -99);
       
   364 
       
   365 	RPointerArray<TAny> e(8, 320);
       
   366 	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);
       
   367 
       
   368 	RPointerArray<TAny> f(2, 257);
       
   369 	TestGrowCompress(&f, 1, 2, 255, 256, 256, 512, 128, 640, 1, 643, 2, 643, 1, 646, -99);
       
   370 	}
       
   371 
       
   372 GLDEF_C void DoPointerArrayAnyTests()
       
   373 	{
       
   374 	test.Start(_L("TAny Pointer Arrays..."));
       
   375 	test.Next(_L("Allocate memory"));
       
   376 	Int64s=new TInt64[KArraySize];
       
   377 	test(Int64s != NULL);
       
   378 
       
   379 	test.Next(_L("AppendAndAccess tests..."));
       
   380 	test.Next(_L("Count 10 Mask 0x0000000300000003"));
       
   381 	test(AnyAppendAndAccessTest(10,NUM_TESTS,MAKE_TINT64(0x3,0x3))==KErrNone);
       
   382 	test.Next(_L("Count 100 Range all"));
       
   383 	test(AnyAppendAndAccessTest(100,NUM_TESTS,MAKE_TINT64(0xffffffff,0xffffffff))==KErrNone);
       
   384 
       
   385 	test.Next(_L("Find tests..."));
       
   386 	test.Next(_L("Count 10 Mask 0x0000000300000003"));
       
   387 	test(AnyFindTest(10,NUM_TESTS,MAKE_TINT64(3,3))==KErrNone);
       
   388 	test.Next(_L("Count 100 Range all"));
       
   389 	test(AnyFindTest(100,NUM_TESTS,MAKE_TINT64(0xffffffff,0xffffffff))==KErrNone);
       
   390 
       
   391 	test.Next(_L("InsertInAddressOrder tests..."));
       
   392 	test.Next(_L("Count 50 Mask 0x3C000000"));
       
   393 	test(AnyInsertInAddressOrderTest(50,NUM_TESTS,0x3C000000)==KErrNone);
       
   394 	test.Next(_L("Count 100 all"));
       
   395 	test(AnyInsertInAddressOrderTest(100,NUM_TESTS,0xffffffff)==KErrNone);
       
   396 
       
   397 	test.Next(_L("Sort tests..."));
       
   398 	test.Next(_L("Count 30 Mask 0x3C000000"));
       
   399 	test(AnySortIntoAddressOrderTest(30,NUM_TESTS,0x3C000000)==KErrNone);
       
   400 	test.Next(_L("Count 100 all"));
       
   401 	test(AnySortIntoAddressOrderTest(100,NUM_TESTS,0xffffffff)==KErrNone);
       
   402 	
       
   403 	test.Next(_L("Test Grow/Compress"));
       
   404 	TestGrowCompress();
       
   405 
       
   406 	test.Next(_L("Test RPointerArray::Array..."));
       
   407 	TInt a;
       
   408 	TInt b;
       
   409 	TInt c;
       
   410 	RPointerArray<TAny> ptrArr;
       
   411 	ptrArr.Append(&a);
       
   412 	ptrArr.Append(&b);
       
   413 	ptrArr.Append(&c);
       
   414 
       
   415 	TArray<TAny*> arr=ptrArr.Array();
       
   416 	test(arr.Count()==3);
       
   417 	test(arr[0]==&a);
       
   418 	test(arr[1]==&b);
       
   419 	test(arr[2]==&c);
       
   420 
       
   421 	ptrArr.Reset();
       
   422 
       
   423 	delete[] Int64s;
       
   424 	test.End();
       
   425 	}
       
   426 
       
   427 GLDEF_C void DoPointerArrayAnyLeavingInterfaceTest()
       
   428 	{
       
   429 	TInt trap, ret(0);
       
   430 	TInt64 Int64s[3];
       
   431 	for (TInt i=0;i<3;i++) Int64s[i] = i;
       
   432 
       
   433 	RPointerArray<TAny> pArray;
       
   434 	CleanupClosePushL(pArray);
       
   435 
       
   436 	test.Start(_L("Checking Leaving TAny Pointer Arrays Interface..."));
       
   437 
       
   438 	test.Next(_L("AppendL test..."));
       
   439 	TRAP(trap, pArray.AppendL(&Int64s[0]));
       
   440 	test(trap==KErrNone);
       
   441 
       
   442 	test.Next(_L("InsertL test..."));
       
   443 	TRAP(trap, pArray.InsertL(&Int64s[1],1));
       
   444 	test(trap==KErrNone);
       
   445 
       
   446 	test.Next(_L("Test FindL(const T* anEntry) const..."));
       
   447 	TRAP(trap, ret = pArray.FindL(&Int64s[0]));
       
   448 	test(trap==0);
       
   449 	test(ret==0);
       
   450 	TRAP(trap, ret = pArray.FindL(&Int64s[2]));
       
   451 	test(trap==KErrNotFound);
       
   452 
       
   453 	test.Next(_L("Test FindInAddressOrderL(const T* anEntry) const..."));
       
   454 	TRAP(trap, ret = pArray.FindInAddressOrderL(&Int64s[0]));
       
   455 	test(trap==0);
       
   456 	test(ret==0);
       
   457 	TRAP(trap, ret = pArray.FindInAddressOrderL(&Int64s[2]));
       
   458 	test(trap==KErrNotFound);
       
   459 
       
   460 	test.Next(_L("Test FindInAddressOrderL(const T* anEntry, TInt& anIndex) const..."));
       
   461 	TRAP(trap, pArray.FindInAddressOrderL(&Int64s[0], ret));
       
   462 	test(trap==0);
       
   463 	test(ret==0);
       
   464 	TRAP(trap, pArray.FindInAddressOrderL(&Int64s[2], ret));
       
   465 	test(trap==KErrNotFound);
       
   466 
       
   467 	test.Next(_L("Test SpecificFindInAddressOrderL(const T* anEntry, TInt aMode) const..."));
       
   468 	TRAP(trap, ret = pArray.SpecificFindInAddressOrderL(&Int64s[0], EArrayFindMode_First));
       
   469 	test(trap==0);
       
   470 	test(ret==0);
       
   471 	TRAP(trap, ret = pArray.SpecificFindInAddressOrderL(&Int64s[2], EArrayFindMode_First));
       
   472 	test(trap==KErrNotFound);
       
   473 
       
   474 	test.Next(_L("Test SpecificFindInAddressOrderL(const T* anEntry, TInt& anIndex, TInt aMode) const..."));
       
   475 	TRAP(trap, pArray.SpecificFindInAddressOrderL(&Int64s[0], ret, EArrayFindMode_First));
       
   476 	test(trap==0);
       
   477 	test(ret==0);
       
   478 	TRAP(trap, pArray.SpecificFindInAddressOrderL(&Int64s[2], ret, EArrayFindMode_First));
       
   479 	test(trap==KErrNotFound);
       
   480 
       
   481 	test.Next(_L("Test InsertInAddressOrderL(const T* anEntry)..."));
       
   482 	TRAP(trap, pArray.InsertInAddressOrderL(&Int64s[0]));
       
   483 	test(trap==KErrAlreadyExists);
       
   484 	TRAP(trap, pArray.InsertInAddressOrderL(&Int64s[2]));
       
   485 	test(trap==KErrNone);
       
   486 	pArray.Remove(2);
       
   487 	
       
   488 	test.Next(_L("Test InsertInAddressOrderAllowRepeatsL(const T* anEntry)..."));
       
   489 	TRAP(trap, pArray.InsertInAddressOrderAllowRepeatsL(&Int64s[2]));
       
   490 	test(trap==KErrNone);
       
   491 	pArray.Remove(2);
       
   492 	
       
   493 	CleanupStack::PopAndDestroy(&pArray);
       
   494 	test.End();
       
   495 	}