kerneltest/e32test/buffer/uint_arr.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     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\uint_arr.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32test.h>
       
    19 #include <e32math.h>
       
    20 
       
    21 #define NUM_TESTS 250
       
    22 
       
    23 GLREF_D RTest test;
       
    24 GLREF_C TInt Random();
       
    25 
       
    26 LOCAL_C TInt IntAppendAndAccessTest(TInt aCount, TInt aNumTests, TInt aRange)
       
    27 	{
       
    28 	TInt n;
       
    29 	for (n=0; n<aNumTests; n++)
       
    30 		{
       
    31 		RArray<TUint> a;
       
    32 		TUint *pA=(TUint*)User::Alloc(aCount*sizeof(TUint));
       
    33 		if (!pA)
       
    34 			{
       
    35 			a.Close();
       
    36 			return -65535;
       
    37 			}
       
    38 		TInt i;
       
    39 		for (i=0; i<aCount; i++)
       
    40 			{
       
    41 			TUint x=Random()&aRange;
       
    42 			a.Append(x);
       
    43 			pA[i]=x;
       
    44 			}
       
    45 		if (a.Count()!=aCount)
       
    46 			{
       
    47 			a.Close();
       
    48 			return -1;
       
    49 			}
       
    50 		for (i=0; i<aCount; i++)
       
    51 			{
       
    52 			if (a[i]!=pA[i])
       
    53 				{
       
    54 				a.Close();
       
    55 				return -2;
       
    56 				}
       
    57 			TUint x=Random()&aRange;
       
    58 			a[i]=x;
       
    59 			pA[i]=x;
       
    60 			}
       
    61 		if (a.Count()!=aCount)
       
    62 			{
       
    63 			a.Close();
       
    64 			return -3;
       
    65 			}
       
    66 		for (i=0; i<aCount; i++)
       
    67 			{
       
    68 			if (a[i]!=pA[i])
       
    69 				{
       
    70 				a.Close();
       
    71 				return -4;
       
    72 				}
       
    73 			}
       
    74 		delete pA;
       
    75 		a.Close();
       
    76 		}
       
    77 	return KErrNone;
       
    78 	}
       
    79 
       
    80 LOCAL_C TInt IntRemoveTest()
       
    81 	{
       
    82 	TInt m=32;
       
    83 	TInt n=m*m+1;
       
    84 	RArray<TUint> a;
       
    85 	TInt i;
       
    86 	for (i=0; i<n; i++)
       
    87 		{
       
    88 		a.Append(TUint(i));
       
    89 		}
       
    90 	TInt p=2;
       
    91 	for (i=2; i<=m; i++)
       
    92 		{
       
    93 		TInt j;
       
    94 		for (j=0; j<(2*i-2); j++)
       
    95 			{
       
    96 			a.Remove(p);
       
    97 			}
       
    98 		p++;
       
    99 		}
       
   100 	if (a.Count()!=m+1)
       
   101 		{
       
   102 		a.Close();
       
   103 		return -1;
       
   104 		}
       
   105 	for (i=0; i<m; i++)
       
   106 		{
       
   107 		if (a[i]!=TUint(i*i))
       
   108 			{
       
   109 			a.Close();
       
   110 			return -2;
       
   111 			}
       
   112 		}
       
   113 	a.Close();
       
   114 	return KErrNone;
       
   115 	}
       
   116 
       
   117 LOCAL_C TInt IntFindTest(TInt aCount, TInt aNumTests, TInt aRange)
       
   118 	{
       
   119 	TInt n;
       
   120 	for (n=0; n<aNumTests; n++)
       
   121 		{
       
   122 		RArray<TUint> a;
       
   123 		TUint *pA=(TUint*)User::Alloc(aCount*sizeof(TUint));
       
   124 		if (!pA)
       
   125 			{
       
   126 			a.Close();
       
   127 			return -65535;
       
   128 			}
       
   129 		TInt i;
       
   130 		for (i=0; i<aCount; i++)
       
   131 			{
       
   132 			TUint x=Random()&aRange;
       
   133 			a.Append(x);
       
   134 			pA[i]=x;
       
   135 			}
       
   136 		if (a.Count()!=aCount)
       
   137 			{
       
   138 			a.Close();
       
   139 			return -1;
       
   140 			}
       
   141 		for (i=0; i<aCount; i++)
       
   142 			{
       
   143 			TInt r=a.Find(pA[i]);
       
   144 			if (r<0 || pA[r]!=pA[i] || r>i)
       
   145 				{
       
   146 				a.Close();
       
   147 				return -2;
       
   148 				}
       
   149 			TUint x=Random()&aRange;
       
   150 			r=a.Find(x);
       
   151 			if (r<0)
       
   152 				{
       
   153 				TInt j;
       
   154 				for (j=0; j<aCount; j++)
       
   155 					{
       
   156 					if (pA[j]==x)
       
   157 						{
       
   158 						a.Close();
       
   159 						return -3;
       
   160 						}
       
   161 					}
       
   162 				}
       
   163 			else if (pA[r]!=x)
       
   164 				{
       
   165 				a.Close();
       
   166 				return -4;
       
   167 				}
       
   168 			else
       
   169 				{
       
   170 				TInt j;
       
   171 				for (j=0; j<r; j++)
       
   172 					{
       
   173 					if (pA[j]==x)
       
   174 						{
       
   175 						a.Close();
       
   176 						return -5;
       
   177 						}
       
   178 					}
       
   179 				}
       
   180 			}
       
   181 		delete pA;
       
   182 		a.Close();
       
   183 		}
       
   184 	return KErrNone;
       
   185 	}
       
   186 
       
   187 LOCAL_C TInt IntFindInOrderTest(TInt aCount, TInt aNumTests, TInt aRange)
       
   188 // require aRange*aCount<2^32
       
   189 	{
       
   190 	TInt n;
       
   191 	for (n=0; n<aNumTests; n++)
       
   192 		{
       
   193 		RArray<TUint> a;
       
   194 		TUint *pA=(TUint*)User::Alloc(aCount*sizeof(TUint));
       
   195 		if (!pA)
       
   196 			{
       
   197 			a.Close();
       
   198 			return -65535;
       
   199 			}
       
   200 		TInt i=0;
       
   201 		TUint y=TUint(Random())>>16;
       
   202 		for(i=0; i<aCount; i++)
       
   203 			{
       
   204 			TUint x=Random()&aRange;	// this is always >=0
       
   205 			a.Append(y);
       
   206 			pA[i]=y;
       
   207 			y+=x;
       
   208 			}
       
   209 		if (a.Count()!=aCount)
       
   210 			{
       
   211 			a.Close();
       
   212 			return -1;
       
   213 			}
       
   214 		for (i=0; i<aCount; i++)
       
   215 			{
       
   216 			TInt r=a.FindInOrder(pA[i]);
       
   217 			if (r<0 || pA[r]!=pA[i])
       
   218 				{
       
   219 				a.Close();
       
   220 				return -2;
       
   221 				}
       
   222 			TUint x=Random()&aRange;
       
   223 			r=a.FindInOrder(x);
       
   224 			if (r<0)
       
   225 				{
       
   226 				TInt j;
       
   227 				for (j=0; j<aCount; j++)
       
   228 					{
       
   229 					if (pA[j]==x)
       
   230 						{
       
   231 						a.Close();
       
   232 						return -3;
       
   233 						}
       
   234 					}
       
   235 				}
       
   236 			else if (pA[r]!=x)
       
   237 				{
       
   238 				a.Close();
       
   239 				return -4;
       
   240 				}
       
   241 			}
       
   242 		delete pA;
       
   243 		a.Close();
       
   244 		}
       
   245 	return KErrNone;
       
   246 	}
       
   247 
       
   248 LOCAL_C TInt IntFindInOrderTest2()
       
   249 	{
       
   250 	TInt i;
       
   251 	RArray<TUint> sq;
       
   252 	for (i=0; i<1024; i++)
       
   253 		{
       
   254 		TInt j=i*i;
       
   255 		sq.Append(j);
       
   256 		}
       
   257 	for (i=0; i<10000; i++)
       
   258 		{
       
   259 		TInt x=Random()&1023;
       
   260 		TInt y=x*x;
       
   261 		TInt r=sq.FindInOrder(y);
       
   262 		if (r!=x)
       
   263 			{
       
   264 			sq.Close();
       
   265 			return -1;
       
   266 			}
       
   267 		}
       
   268 	sq.Close();
       
   269 	return 0;
       
   270 	}
       
   271 
       
   272 LOCAL_C TInt IntInsertInOrderTest(TInt aCount, TInt aNumTests, TInt aRange)
       
   273 	{
       
   274 	TInt n;
       
   275 	for (n=0; n<aNumTests; n++)
       
   276 		{
       
   277 		RArray<TUint> a;
       
   278 		RArray<TUint> b;
       
   279 		RArray<TUint> c;
       
   280 		TInt i;
       
   281 		TInt cc=0;
       
   282 		for (i=0; i<aCount; i++)
       
   283 			{
       
   284 			TUint x=Random()&aRange;
       
   285 			a.Append(x);
       
   286 			b.InsertInOrderAllowRepeats(x);
       
   287 			TInt r=c.InsertInOrder(x);
       
   288 			if (r==KErrNone)
       
   289 				cc++;
       
   290 			}
       
   291 		if (a.Count()!=aCount)
       
   292 			{
       
   293 			a.Close();
       
   294 			b.Close();
       
   295 			c.Close();
       
   296 			return -1;
       
   297 			}
       
   298 		if (b.Count()!=aCount)
       
   299 			{
       
   300 			a.Close();
       
   301 			b.Close();
       
   302 			c.Close();
       
   303 			return -2;
       
   304 			}
       
   305 		for (i=0; i<aCount-1; i++)
       
   306 			{
       
   307 			if (b[i]>b[i+1])
       
   308 				{
       
   309 				a.Close();
       
   310 				b.Close();
       
   311 				c.Close();
       
   312 				return -3;
       
   313 				}
       
   314 			}
       
   315 		for (i=0; i<aCount; i++)
       
   316 			{
       
   317 			if (a.Find(b[i])<0)
       
   318 				{
       
   319 				a.Close();
       
   320 				b.Close();
       
   321 				c.Close();
       
   322 				return -4;
       
   323 				}
       
   324 			if (b.Find(a[i])<0)
       
   325 				{
       
   326 				a.Close();
       
   327 				b.Close();
       
   328 				c.Close();
       
   329 				return -5;
       
   330 				}
       
   331 			if (c.Find(a[i])<0)
       
   332 				{
       
   333 				a.Close();
       
   334 				b.Close();
       
   335 				c.Close();
       
   336 				return -6;
       
   337 				}
       
   338 			}
       
   339 		if (c.Count()!=cc)
       
   340 			{
       
   341 			a.Close();
       
   342 			b.Close();
       
   343 			c.Close();
       
   344 			return -7;
       
   345 			}
       
   346 		for (i=0; i<c.Count()-1; i++)
       
   347 			{
       
   348 			if (c[i]>=c[i+1])
       
   349 				{
       
   350 				a.Close();
       
   351 				b.Close();
       
   352 				c.Close();
       
   353 				return -8;
       
   354 				}
       
   355 			if (a.Find(c[i])<0)
       
   356 				{
       
   357 				a.Close();
       
   358 				b.Close();
       
   359 				c.Close();
       
   360 				return -9;
       
   361 				}
       
   362 			}
       
   363 		a.Close();
       
   364 		b.Close();
       
   365 		c.Close();
       
   366 		}
       
   367 	return KErrNone;
       
   368 	}
       
   369 
       
   370 LOCAL_C TInt IntInsertInOrderTest2()
       
   371 	{
       
   372 	TInt i;
       
   373 	RArray<TUint> sq;
       
   374 	for (i=0; i<1024; i++)
       
   375 		{
       
   376 		TInt j=i*i;
       
   377 		sq.InsertInOrder(j);
       
   378 		sq.InsertInOrder(-j);
       
   379 		}
       
   380 	if (sq.Count()!=2047)
       
   381 		{
       
   382 		sq.Close();
       
   383 		return -1;
       
   384 		}
       
   385 	for (i=0; i<2047; i++)
       
   386 		{
       
   387 		TInt y;
       
   388 		if (i<1024)
       
   389 			y=i*i;
       
   390 		else
       
   391 			y=-(2047-i)*(2047-i);
       
   392 		if (sq[i]!=TUint(y))
       
   393 			{
       
   394 			sq.Close();
       
   395 			return -2;
       
   396 			}
       
   397 		}
       
   398 	sq.Close();
       
   399 	return 0;
       
   400 	}
       
   401 
       
   402 LOCAL_C TInt IntSortTest(TInt aCount, TInt aNumTests, TInt aRange)
       
   403 	{
       
   404 	TInt n;
       
   405 	for (n=0; n<aNumTests; n++)
       
   406 		{
       
   407 		RArray<TUint> a;
       
   408 		RArray<TUint> b;
       
   409 		TInt i;
       
   410 		for (i=0; i<aCount; i++)
       
   411 			{
       
   412 			TUint x=Random()&aRange;
       
   413 			a.Append(x);
       
   414 			b.InsertInOrderAllowRepeats(x);
       
   415 			}
       
   416 		a.Sort();
       
   417 		if (a.Count()!=aCount)
       
   418 			{
       
   419 			a.Close();
       
   420 			b.Close();
       
   421 			return -1;
       
   422 			}
       
   423 		if (b.Count()!=aCount)
       
   424 			{
       
   425 			a.Close();
       
   426 			b.Close();
       
   427 			return -2;
       
   428 			}
       
   429 		for (i=0; i<aCount; i++)
       
   430 			{
       
   431 			if (a[i]!=b[i])
       
   432 				{
       
   433 				a.Close();
       
   434 				b.Close();
       
   435 				return -3;
       
   436 				}
       
   437 			}
       
   438 		a.Close();
       
   439 		b.Close();
       
   440 		}
       
   441 	return KErrNone;
       
   442 	}
       
   443 
       
   444 TInt UIntSpecificFindTests(TInt aCount, TInt aNumTests, TInt aRange)
       
   445 	{
       
   446 	TInt n;
       
   447 	TInt nmiss = 0;
       
   448 	TInt nrpt = 0;
       
   449 	TInt ntot = 0;
       
   450 	for (n=0; n<aNumTests; n++)
       
   451 		{
       
   452 		RArray<TUint> a;
       
   453 		RArray<TUint> b;
       
   454 		TInt i;
       
   455 		for (i=0; i<aCount; i++)
       
   456 			{
       
   457 			TInt x=Random()&aRange;
       
   458 			x-=(aRange>>1);
       
   459 			a.Append(TUint(x));
       
   460 			b.InsertInOrderAllowRepeats(TUint(x));
       
   461 			}
       
   462 		a.Sort();
       
   463 		test(a.Count()==aCount);
       
   464 		test(b.Count()==aCount);
       
   465 		for (i=0; i<aCount; i++)
       
   466 			test(a[i]==b[i]);
       
   467 		for (i=-(aRange>>1); i<=(aRange>>1); ++i)
       
   468 			{
       
   469 			TUint u = (TUint)i;
       
   470 			TInt first = a.SpecificFindInOrder(u, EArrayFindMode_First);
       
   471 			TInt last = a.SpecificFindInOrder(u, EArrayFindMode_Last);
       
   472 			TInt any = a.SpecificFindInOrder(u, EArrayFindMode_Any);
       
   473 			TInt fi, li, ai;
       
   474 			TInt first2 = a.SpecificFindInOrder(u, fi, EArrayFindMode_First);
       
   475 			TInt last2 = a.SpecificFindInOrder(u, li, EArrayFindMode_Last);
       
   476 			TInt any2 = a.SpecificFindInOrder(u, ai, EArrayFindMode_Any);
       
   477 			++ntot;
       
   478 			if (first < 0)
       
   479 				{
       
   480 				test(first == KErrNotFound);
       
   481 				test(first == last);
       
   482 				test(first == any);
       
   483 				test(first == first2);
       
   484 				test(first == last2);
       
   485 				test(first == any2);
       
   486 				test(fi == li);
       
   487 				test(fi == ai);
       
   488 				test(li==aCount || a[li]>u);
       
   489 				test(li==0 || a[li-1]<u);
       
   490 				++nmiss;
       
   491 				}
       
   492 			else
       
   493 				{
       
   494 				test(first2 == KErrNone);
       
   495 				test(last2 == KErrNone);
       
   496 				test(any2 == KErrNone);
       
   497 				test(first == fi);
       
   498 				test(last == li);
       
   499 				test(any == ai);
       
   500 				test(a[fi] == u);
       
   501 				test(a[li-1] == u);
       
   502 				test(li==aCount || a[li]>u);
       
   503 				test(ai>=fi && ai<li);
       
   504 				test(a[ai] == u);
       
   505 				if (li-fi > 1)
       
   506 					++nrpt;
       
   507 				}
       
   508 			}
       
   509 		a.Close();
       
   510 		b.Close();
       
   511 		}
       
   512 	test.Printf(_L("ntot=%d nmiss=%d nrpt=%d\n"), ntot, nmiss, nrpt);
       
   513 	return KErrNone;
       
   514 	}
       
   515 
       
   516 GLDEF_C void DoUintArrayTests()
       
   517 	{
       
   518 	test.Start(_L("Unsigned Integer Arrays..."));
       
   519 
       
   520 	test.Next(_L("AppendAndAccess tests..."));
       
   521 	test.Next(_L("Count 10 Range 15"));
       
   522 	test(IntAppendAndAccessTest(10,NUM_TESTS,15)==KErrNone);
       
   523 	test.Next(_L("Count 20 Range 255"));
       
   524 	test(IntAppendAndAccessTest(20,NUM_TESTS,255)==KErrNone);
       
   525 	test.Next(_L("Count 100 Range all"));
       
   526 	test(IntAppendAndAccessTest(100,NUM_TESTS,-1)==KErrNone);
       
   527 
       
   528 	test.Next(_L("Remove tests..."));
       
   529 	test(IntRemoveTest()==KErrNone);
       
   530 
       
   531 	test.Next(_L("Find tests..."));
       
   532 	test.Next(_L("Count 10 Range 15"));
       
   533 	test(IntFindTest(10,NUM_TESTS,15)==KErrNone);
       
   534 	test.Next(_L("Count 20 Range 255"));
       
   535 	test(IntFindTest(20,NUM_TESTS,255)==KErrNone);
       
   536 	test.Next(_L("Count 100 Range all"));
       
   537 	test(IntFindTest(100,NUM_TESTS,-1)==KErrNone);
       
   538 
       
   539 	test.Next(_L("FindInOrder tests..."));
       
   540 	test.Next(_L("Count 10 Range 15"));
       
   541 	test(IntFindInOrderTest(10,NUM_TESTS,15)==KErrNone);
       
   542 	test.Next(_L("Count 20 Range 255"));
       
   543 	test(IntFindInOrderTest(20,NUM_TESTS,255)==KErrNone);
       
   544 	test.Next(_L("Count 100 Range 4095"));
       
   545 	test(IntFindInOrderTest(100,NUM_TESTS,4095)==KErrNone);
       
   546 	test.Next(_L("Squares"));
       
   547 	test(IntFindInOrderTest2()==KErrNone);
       
   548 
       
   549 	test.Next(_L("InsertInOrder tests..."));
       
   550 	test.Next(_L("Count 10 Range 15"));
       
   551 	test(IntInsertInOrderTest(10,NUM_TESTS,15)==KErrNone);
       
   552 	test.Next(_L("Count 20 Range 255"));
       
   553 	test(IntInsertInOrderTest(20,NUM_TESTS,255)==KErrNone);
       
   554 	test.Next(_L("Count 100 Range all"));
       
   555 	test(IntInsertInOrderTest(100,NUM_TESTS,-1)==KErrNone);
       
   556 	test.Next(_L("Squares"));
       
   557 	test(IntInsertInOrderTest2()==KErrNone);
       
   558 
       
   559 	test.Next(_L("Sort tests..."));
       
   560 	test.Next(_L("Count 10 Range 15"));
       
   561 	test(IntSortTest(10,NUM_TESTS,15)==KErrNone);
       
   562 	test.Next(_L("Count 20 Range 255"));
       
   563 	test(IntSortTest(20,NUM_TESTS,255)==KErrNone);
       
   564 	test.Next(_L("Count 100 Range all"));
       
   565 	test(IntSortTest(100,NUM_TESTS,-1)==KErrNone);
       
   566 
       
   567 	test.Next(_L("UIntSpecificFindTests..."));
       
   568 	test(UIntSpecificFindTests(100, 10, 15)==KErrNone);
       
   569 	test(UIntSpecificFindTests(100, 10, 127)==KErrNone);
       
   570 
       
   571 	test.End();
       
   572 	}