os/kernelhwsrv/kerneltest/e32test/buffer/t_regn.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\t_regn.cpp
    15 // Overview:
    16 // Test fixed and variable clipping regions.
    17 // API Information:
    18 // TRegionFix, RRegion .
    19 // Details:
    20 // - Construct some expandable clipping regions, add some rectangles, check the region 
    21 // matches the rectangles, clear the region, add some rectangles to the region and 
    22 // check the region matches the rectangles.
    23 // - Copy one region to another, using the Copy method and the copy constructor,
    24 // and check the region matches the rectangles.
    25 // - Create a some fixed clipping regions, add some rectangles, check the region
    26 // matches the rectangles, clear the region, add some rectangles, and check the
    27 // region matches the rectangles.
    28 // - Copy one fixed region to another, using the Copy method and the copy constructor,
    29 // and check the region matches the rectangles.
    30 // - Test TRegionFix creation and error handling using Clear, Count, AddRect, CheckError
    31 // and Tidy methods
    32 // - Test adding rectangles, via AddRect, to an RRegion object. Verify the results 
    33 // via the Count, BoundingRect and IsEmpty methods.
    34 // - Test subtracting rectangles, via SubRect, from an RRegion object. Verify the 
    35 // results via the Count, BoundingRect and SubRegion methods.
    36 // - Test subtracting regions, via AddRect and SubRegion, from an RRegion object. 
    37 // Verify the results via the Count, BoundingRect, Clear, Copy, and SubRect methods.
    38 // - Test the RRegion Tidy method. Verify the results via the Count, AddRect, 
    39 // BoundingRect and Clear methods.
    40 // - Test the RRegion CheckSpare method. Verify the results via the AddRect, Tidy,
    41 // Clear and SubRect methods.
    42 // - Test the RRegion Offset method. Verify the results via the AddRect, Move,
    43 // Clear, IsEmpty and RectangleList methods.
    44 // - Test the RRegion Intersection and Intersect methods. Verify the results via 
    45 // the AddRect, Count, IsEmpty and RectangleList methods.
    46 // - Test the RRegion Union method. Verify the results via the AddRect, Count,
    47 // Copy, Offset and BoundingRect methods.
    48 // - Test the RRegion ClipRect method. Verify the results via the AddRect, Count,
    49 // and BoundingRect methods.
    50 // - Test the RRegion and TRgionFix Contains methods. Verify the results via the 
    51 // AddRect method.
    52 // - Test the RRegion ForceError and CheckError methods. Verify the results via the 
    53 // AddRect, Copy, Count, SubRect, Clear and BoundingRect methods.
    54 // - Test the RRegion  and RRegionBuf sort method.
    55 // - Construct some regions with pre-allocated buffer (RRegionBuf), add some rectangles, 
    56 // get a pointer to the array of rectangles defining this region and check the 
    57 // rectangles are as expected.
    58 // Platforms/Drives/Compatibility:
    59 // All 
    60 // Assumptions/Requirement/Pre-requisites:
    61 // Failures and causes:
    62 // Base Port information:
    63 // 
    64 //
    65 
    66 #include <e32test.h>
    67 
    68 LOCAL_D RTest test(_L("T_REGN"));
    69 
    70 class TestRRegion
    71 	{
    72 public:
    73 	TestRRegion(TInt tlx, TInt tly, TInt brx, TInt bry);
    74 	void TestSet();
    75 	void TestRegionFix();
    76 	void TestAddRect();
    77 	void TestSubRect();
    78 	void TestSubRegion();
    79 	void TestTidy();
    80 	void TestSpare();
    81 	void TestOffset();
    82 	void TestIntersection();
    83 	void TestUnion();
    84 	void TestClipRect();
    85 	void TestContains();
    86 	void TestIntersects();
    87 	void TestErrors();
    88 	void doTestSort(RRegion &aRegion);
    89 	void TestSort();
    90 	void doTestRegionBuf(RRegion &aRegion);
    91 	void TestRegionBuf();
    92 private:
    93 	void DoTestSet(TRegion** rgn,TInt rgnArraySize);
    94 	void CheckRectRegion(const TRegion& region,const TRect& rect);
    95 private:
    96 	TRect rect[4];
    97 	TRect bounds;
    98 	TRect xrect;
    99 	};
   100 
   101 // Region test code
   102 TestRRegion::TestRRegion(TInt tlx, TInt tly, TInt brx, TInt bry)
   103 	{
   104 	rect[0]=TRect( tlx, tly, brx, bry);
   105 	rect[1]=TRect(-brx,-bry,-tlx,-tly);
   106 	rect[2]=TRect( tlx,-bry, brx,-tly);
   107 	rect[3]=TRect(-brx, tly,-tlx, bry);
   108 	bounds=TRect(-brx,-bry,brx,bry);
   109 	xrect=TRect(-(tlx/2+brx/2),-(tly/2+bry/2),tlx/2+brx/2,tly/2+bry/2);
   110 	}
   111 
   112 void TestRRegion::CheckRectRegion(const TRegion& region,const TRect& rect)
   113 // Check the region matches the rectangle
   114 	{
   115 	const TRect* rlist;
   116 
   117 	if (rect.IsEmpty())
   118 		test(region.Count()==0);
   119 	else
   120 		{
   121 		test(region.Count()==1);
   122 		rlist=region.RectangleList();
   123 		test(rlist[0]==rect);
   124 		test(region[0]==rect);
   125 		}
   126 	}
   127 
   128 void TestRRegion::DoTestSet(TRegion** rgn,TInt rgnArraySize)
   129 	{
   130 	TInt index; 
   131 	for(index=0;index<rgnArraySize;index++)
   132 		rgn[index]->AddRect(rect[index]);
   133 	for(index=0;index<rgnArraySize;index++)
   134 		CheckRectRegion(*rgn[index],rect[index]);
   135 	for(index=0;index<rgnArraySize;index++)
   136 		{
   137 		rgn[index]->Clear();
   138 		rgn[index]->AddRect(rect[index]);
   139 		}
   140 	for(index=0;index<rgnArraySize;index++)
   141 		CheckRectRegion(*rgn[index],rect[index]);
   142 	}
   143 
   144 void TestRRegion::TestSet()
   145 	{
   146 	TUint index; 
   147 
   148 	RRegion xrgn(rect[0]);
   149 	CheckRectRegion(xrgn,rect[0]);
   150 	xrgn.Close();
   151 //
   152 	RRegion rgn[5];
   153 	TRegion* prgn[5]={&rgn[0],&rgn[1],&rgn[2],&rgn[3],&rgn[4]};
   154 	DoTestSet(&prgn[0],(sizeof(rgn)/sizeof(rgn[0])));
   155 	for(index=0;index<(sizeof(rgn)/sizeof(rgn[0]));index++)
   156 		{
   157 		RRegion rgn1;
   158 		rgn1.Copy(rgn[index]);
   159 		CheckRectRegion(rgn1,rect[index]);
   160 		RRegion rgn2(rgn[index]);
   161 		CheckRectRegion(rgn2,rect[index]);
   162 		rgn[index].Close();
   163 		rgn1.Close();
   164 		}
   165 //
   166 	TRegionFix<5> rgnf[5];
   167 	TRegion* prgnf[5]={&rgnf[0],&rgnf[1],&rgnf[2],&rgnf[3],&rgnf[4]};
   168 	DoTestSet(&prgnf[0],(sizeof(rgnf)/sizeof(rgnf[0])));
   169 	for(index=0;index<(sizeof(rgn)/sizeof(rgn[0]));index++)
   170 		{
   171 		TRegionFix<5> rgn1;
   172 		rgn1.Copy(rgnf[index]);
   173 		CheckRectRegion(rgn1,rect[index]);
   174 		TRegionFix<5> rgn2(rgnf[index]);
   175 		CheckRectRegion(rgn2,rect[index]);
   176 		}
   177 	}
   178 
   179 void TestRRegion::TestRegionFix()
   180 //
   181 // Test TRegionFix creation and error handling
   182 //
   183 	{
   184 	TRegionFix<1> rgnf(rect[0]);
   185 	CheckRectRegion(rgnf,rect[0]);
   186 	rgnf.Clear();
   187 	test(rgnf.Count()==0);
   188 	rgnf.AddRect(TRect(0,0,2,2));
   189 	test(rgnf.CheckError()==FALSE);
   190 	rgnf.AddRect(TRect(2,2,4,4));	// Should cause error, rgnf can only hold 1 rectangle
   191 	test(rgnf.CheckError()==TRUE && rgnf.Count()==0);
   192 	rgnf.Clear();
   193 	test(rgnf.CheckError()==FALSE && rgnf.Count()==0);
   194 //
   195 	TRegionFix<10> rgnf2;
   196 	TInt index;
   197 	for(index=0;index<10;index++)
   198 		{
   199 		rgnf2.AddRect(TRect(index*4,0,index*4+2,10));
   200 		test(rgnf2.Count()==(index+1));
   201 		TRegionFix<10> rgnf4(rgnf2);	// Test copy constructor
   202 		TRegionFix<10> rgnf5;
   203 		rgnf5=rgnf2;					// Test assignment
   204 		test(rgnf4.Count()==(index+1));
   205 		for(TInt index2=0;index2<=index;index2++)
   206 			test(rgnf2[index2]==rgnf4[index2] && rgnf2[index2]==rgnf5[index2]);
   207 		}
   208 	rgnf2.AddRect(TRect(-10,-10,0,0));	// Should push it over the edge
   209 	test(rgnf2.CheckError()==TRUE && rgnf2.Count()==0);
   210 //
   211 	TRegionFix<5> rgnf3;
   212 	for(index=0;index<4;index++)
   213 		{
   214 		rgnf3.AddRect(TRect(index*4,index*4,index*4+8,index*4+8));
   215 		if (index==3)
   216 			test(rgnf3.CheckError()==TRUE);
   217 		else
   218 			{			
   219 			rgnf3.Tidy();
   220 			if (index>0)
   221 				test(rgnf3.Count()==(index+2));
   222 			}
   223 		}
   224 	}
   225 
   226 void TestRRegion::TestAddRect()
   227 	{
   228 	RRegion rgn;
   229 	TInt index,i;
   230 
   231 	if (!rect[0].IsEmpty())
   232 		{
   233 		for(index=0;index<4;index++)
   234 			{
   235 			for(i=0;i<=index;i++)
   236 				rgn.AddRect(rect[index]);
   237 			test(rgn.Count()==(index+1));
   238 			}
   239 		test(rgn.BoundingRect()==bounds);
   240 		if (!xrect.IsEmpty())
   241 			{
   242 			rgn.AddRect(xrect);
   243 			TInt count = rgn.Count();
   244 			test( (count > 4) && (count <= 9) );
   245 			}
   246 		}
   247 	rgn.AddRect(bounds);
   248 	test(rgn.Count()==1);
   249 	rgn.Close();
   250 	}
   251 
   252 void TestRRegion::TestSubRect()
   253 	{
   254 	TRect rect1(-rect[0].iBr.iX,-rect[0].iBr.iY,rect[0].iBr.iX,rect[0].iBr.iY);
   255 	RRegion rgn;
   256 	RRegion subRgn;
   257 	RRegion rgn2;
   258 	TInt index;
   259 
   260 	if (!rect[0].IsEmpty())
   261 		{
   262 		rgn.AddRect(rect1);
   263 		for(index=0;index<4;index++)
   264 			rgn.SubRect(rect[index],&subRgn);
   265 		if (rect[0].iTl.iX==0)	// Special case region, all rects join in the middle
   266 			{
   267 			test(rgn.Count()==0);
   268 			test(subRgn.Count()==4);
   269 			}
   270 		else
   271 			{
   272 			test(rgn.Count()==3);
   273 			test(subRgn.Count()==4);
   274 			test(rgn.BoundingRect()==subRgn.BoundingRect());
   275 			rgn.SubRect(xrect);
   276 			test(rgn.Count()==4);
   277 			rgn2.Copy(rgn);
   278 			subRgn.Clear();
   279 			rgn.SubRect(rgn.BoundingRect(),&subRgn);
   280 			test(rgn.Count()==0);
   281 			rgn2.SubRegion(subRgn,&rgn);
   282 			test(rgn2.Count()==0);
   283 			subRgn.SubRegion(rgn);
   284 			test(subRgn.Count()==0);
   285 			}
   286 		}
   287 	rgn.Close();
   288 	rgn2.Close();
   289 	subRgn.Close();
   290 	}
   291 
   292 void TestRRegion::TestSubRegion()
   293 	{
   294 	TRect rect1(-rect[0].iBr.iX,-rect[0].iBr.iY,rect[0].iBr.iX,rect[0].iBr.iY);
   295 	RRegion rgn,subRgn;
   296 	RRegion rgn2;
   297 	TInt index;
   298 
   299 	if (!rect[0].IsEmpty())
   300 		{
   301 		rgn.AddRect(rect1);
   302 		for(index=0;index<4;index++)
   303 			rgn2.AddRect(rect[index]);
   304 		rgn.SubRegion(rgn2,&subRgn);
   305 		if (rect[0].iTl.iX==0)	// Special case region, all rects join in the middle
   306 			{
   307 			test(rgn.Count()==0);
   308 			test(subRgn.Count()==4);
   309 			}
   310 		else
   311 			{
   312 			test(rgn.Count()==3);
   313 			test(subRgn.Count()==4);
   314 			test(rgn.BoundingRect()==subRgn.BoundingRect());
   315 			rgn2.Clear();
   316 			rgn2.AddRect(xrect);
   317 			rgn.SubRegion(rgn2);
   318 			test(rgn.Count()==4);
   319 			rgn2.Copy(rgn);
   320 			subRgn.Clear();
   321 			rgn.SubRect(rgn.BoundingRect(),&subRgn);
   322 			test(rgn.Count()==0);
   323 			rgn2.SubRegion(subRgn,&rgn);
   324 			test(rgn2.Count()==0);
   325 			subRgn.SubRegion(rgn);
   326 			test(subRgn.Count()==0);
   327 			}
   328 		}
   329 	rgn.Close();
   330 	rgn2.Close();
   331 	subRgn.Close();
   332 	}
   333 
   334 void TestRRegion::TestTidy()
   335 	{
   336 	RRegion rgn;
   337 	TInt loop;
   338 	TRect const rlist[8]={	// 8 Rectangles that form a square with the centre rectangle missing
   339 		TRect(10,10,20,20),
   340 		TRect(20,10,30,20),
   341 		TRect(30,10,40,20),
   342 
   343 		TRect(10,20,20,30),
   344 		TRect(30,20,40,30),
   345 
   346 		TRect(10,30,20,40),
   347 		TRect(20,30,30,40),
   348 		TRect(30,30,40,40)};
   349 	TRect const rect1(rlist[0].iTl.iX,rlist[0].iTl.iY,rlist[2].iBr.iX,rlist[2].iBr.iY);
   350 	TRect const rect2(rlist[0].iTl.iX,rlist[0].iTl.iY,rlist[7].iBr.iX,rlist[7].iBr.iY);
   351 
   352 // Add 3 adjoining rectangles and tidy them
   353 	for(loop=0;loop<3;loop++)
   354 		rgn.AddRect(rlist[loop]);
   355 	test(rgn.Count()==3);
   356 	rgn.Tidy();
   357 	test(rgn.Count()==1);
   358 	test(rgn[0]==rect1);
   359 // Same again but add the adjoining rectangles in reverse order
   360 	rgn.Clear();
   361 	for(loop=2;loop>=0;loop--)
   362 		rgn.AddRect(rlist[loop]);
   363 	test(rgn.Count()==3);
   364 	rgn.Tidy();
   365 	test(rgn.Count()==1);
   366 	test(rgn[0]==rect1);
   367 // Now add 8 rectangles that should tidy down to 4
   368 	rgn.Clear();
   369 	for(loop=0;loop<8;loop++)
   370 		rgn.AddRect(rlist[loop]);
   371 	test(rgn.Count()==8);
   372 	rgn.Tidy();
   373 	test(rgn.Count()==4);
   374 	test(rgn.BoundingRect()==rect2);
   375 // ...and in reverse
   376 	rgn.Clear();
   377 	for(loop=7;loop>=0;loop--)
   378 		rgn.AddRect(rlist[loop]);
   379 	test(rgn.Count()==8);
   380 	rgn.Tidy();
   381 	test(rgn.Count()==4);
   382 	test(rgn.BoundingRect()==rect2);
   383 	rgn.Close();
   384 	}
   385 
   386 void TestRRegion::TestSpare()
   387 	{
   388 	TInt gran[5]={1,2,4,20,100};
   389 	RRegion rgn[5]={gran[0],gran[1],gran[2],gran[3],gran[4]};
   390     TUint index;
   391 	TInt loop,spare;
   392 	TRect rect1(0,0,1,1);
   393 	TRect rect;
   394 
   395 	for(index=0;index<(sizeof(rgn)/sizeof(rgn[0]));index++)
   396 		{
   397 		test(rgn[index].CheckSpare()==0);
   398 		rgn[index].AddRect(rect1);
   399 		test(rgn[index].CheckSpare()==(gran[index]-1));
   400 		rgn[index].Tidy();
   401 		test(rgn[index].CheckSpare()<gran[index]);
   402 		rgn[index].Clear();
   403 		test(rgn[index].CheckSpare()==0);
   404 		}
   405 	for(index=0;index<(sizeof(rgn)/sizeof(rgn[0]));index++)
   406 		{
   407 		spare=0;
   408 		for(loop=0;loop<30;loop++)
   409 			{
   410 			rect.iTl.iX=rect.iTl.iY=loop;
   411 			rect.iBr.iX=rect.iBr.iY=loop+1;
   412 			rgn[index].AddRect(rect);
   413 			if (spare==0)
   414 				spare=gran[index];
   415 			spare--;
   416 			test(rgn[index].CheckSpare()==spare);
   417 			}
   418 		do
   419 			{
   420 			loop--;
   421 			rect.iTl.iX=rect.iTl.iY=loop;
   422 			rect.iBr.iX=rect.iBr.iY=loop+1;
   423 			rgn[index].SubRect(rect);
   424 			spare++;
   425 			test(rgn[index].CheckSpare()==spare);
   426 			} while(loop>0);
   427 		}
   428 	for(index=0;index<(sizeof(rgn)/sizeof(rgn[0]));index++)
   429 		rgn[index].Close();
   430 	}
   431 
   432 void TestRRegion::TestOffset()
   433 	{
   434 	RRegion rgn;
   435 	const TRect* rlist;
   436 	TRect r;
   437 	TUint index;
   438 
   439 	for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
   440 		{
   441 		rgn.Clear();
   442 		rgn.AddRect(rect[index]);
   443 		r=rect[index];
   444 		r.Move(1,1);
   445 		rgn.Offset(1,1);
   446 		if (rect[index].IsEmpty())
   447 			test(rgn.Count()==0);
   448 		else
   449 			{
   450 			test(rgn.Count()==1);
   451 			rlist=rgn.RectangleList();
   452 			test(rlist[0]==r);
   453 			}
   454 		}
   455 	rgn.Close();
   456 	}
   457 
   458 void TestRRegion::TestIntersection()
   459 	{
   460 	RRegion rgn1,rgn2,tmp_rgn;
   461 	const TRect* rlist;
   462 	TUint index;
   463 
   464 	rgn1.AddRect(xrect);
   465 	rgn2.Copy(rgn1);
   466 	if (!rgn1.IsEmpty())
   467 		{
   468 		for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
   469 			tmp_rgn.AddRect(rect[index]);
   470 		rgn2.Intersection(rgn1,tmp_rgn);
   471 		test(rgn2.Count()==4);
   472 		rlist=rgn2.RectangleList();
   473 		for(index=0;index<(TUint)rgn2.Count();index++)
   474 			{
   475 			test(rlist[index].iTl.iX==xrect.iTl.iX || rlist[index].iTl.iX==rect[0].iTl.iX);
   476 			test(rlist[index].iTl.iY==xrect.iTl.iY || rlist[index].iTl.iY==rect[0].iTl.iY);
   477 			test(rlist[index].iBr.iX==xrect.iBr.iX || rlist[index].iBr.iX==(-rect[0].iTl.iX));
   478 			test(rlist[index].iBr.iY==xrect.iBr.iY || rlist[index].iBr.iY==(-rect[0].iTl.iY));
   479 			}
   480 		rgn1.Intersect(tmp_rgn);
   481 		test(rgn1.Count()==4);
   482 		rlist=rgn1.RectangleList();
   483 		for(index=0;index<(TUint)rgn1.Count();index++)
   484 			{
   485 			test(rlist[index].iTl.iX==xrect.iTl.iX || rlist[index].iTl.iX==rect[0].iTl.iX);
   486 			test(rlist[index].iTl.iY==xrect.iTl.iY || rlist[index].iTl.iY==rect[0].iTl.iY);
   487 			test(rlist[index].iBr.iX==xrect.iBr.iX || rlist[index].iBr.iX==(-rect[0].iTl.iX));
   488 			test(rlist[index].iBr.iY==xrect.iBr.iY || rlist[index].iBr.iY==(-rect[0].iTl.iY));
   489 			}
   490 		}
   491 	rgn1.Close();
   492 	rgn2.Close();
   493 	tmp_rgn.Close();
   494 	}
   495 
   496 void TestRRegion::TestUnion()
   497 	{
   498 	RRegion rgn1,rgn2,rgn3,tmp_rgn;
   499 	TRect bounds, rgn1Bounds;
   500 	TUint index;
   501 
   502 	// Test RRegion (always has a dynamic buffer).
   503 	rgn1.AddRect(xrect);
   504 	if (!rgn1.IsEmpty())
   505 		{
   506 		for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
   507 			tmp_rgn.AddRect(rect[index]);
   508 		test(tmp_rgn.Count()==4);
   509 		rgn1.Union(tmp_rgn);
   510 		test(rgn1.Count()==7);
   511 		rgn1Bounds = rgn1.BoundingRect();
   512 		rgn2.Copy(rgn1);
   513 		rgn2.Offset(3,5);
   514 		rgn3.Copy(rgn1);
   515 		rgn3.Offset(5,7);
   516 		rgn3.Union(rgn2);
   517 		test(rgn3.Count()==17);
   518 		bounds=rgn1.BoundingRect();
   519 		rgn1.Union(rgn2);
   520 		bounds.Resize(3,5);
   521 		test(rgn1.BoundingRect()==bounds);
   522 		rgn1Bounds.Shrink(3,5);
   523 		rgn1Bounds.Resize(8,12);
   524 		test(rgn3.BoundingRect()==rgn1Bounds);
   525 		}
   526 	rgn1.Close();
   527 	rgn2.Close();
   528 	rgn3.Close();
   529 	tmp_rgn.Close();
   530 
   531 	RRegionBuf<8> rgnBuf1,rgnBuf2,rgnBuf3,tmp_rgnBuf;
   532 
   533 	// Test RRegionBuf (can transform from using a static to using a dynamic buffer).
   534 	rgnBuf1.AddRect(xrect);
   535 	if (!rgnBuf1.IsEmpty())
   536 		{
   537 		for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
   538 			tmp_rgnBuf.AddRect(rect[index]);
   539 		test(tmp_rgnBuf.Count()==4);
   540 		rgnBuf1.Union(tmp_rgnBuf);
   541 		test(rgnBuf1.Count()==7);
   542 		rgn1Bounds = rgnBuf1.BoundingRect();
   543 		rgnBuf2.Copy(rgnBuf1);
   544 		rgnBuf2.Offset(3,5);
   545 		rgnBuf3.Copy(rgnBuf1);
   546 		rgnBuf3.Offset(5,7);
   547 		rgnBuf3.Union(rgnBuf2);
   548 		test(rgnBuf3.Count()==17);
   549 		bounds=rgnBuf1.BoundingRect();
   550 		rgnBuf1.Union(rgnBuf2);
   551 		bounds.Resize(3,5);
   552 		test(rgnBuf1.BoundingRect()==bounds);
   553 		rgn1Bounds.Shrink(3,5);
   554 		rgn1Bounds.Resize(8,12);
   555 		test(rgnBuf3.BoundingRect()==rgn1Bounds);
   556 		}
   557 	rgnBuf1.Close();
   558 	rgnBuf2.Close();
   559 	rgnBuf3.Close();
   560 	tmp_rgnBuf.Close();
   561 	}
   562 
   563 void TestRRegion::TestClipRect()
   564 	{
   565 	RRegion rgn1(xrect);
   566 	if (!rgn1.IsEmpty())
   567 		{
   568 		TUint index;
   569 		for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
   570 			rgn1.AddRect(rect[index]);
   571 		TRect clip=rgn1.BoundingRect();
   572 		rgn1.ClipRect(clip);
   573 		test(clip==rgn1.BoundingRect());
   574 		clip.iTl.iX>>=1;
   575 		clip.iTl.iY>>=1;
   576 		clip.iBr.iX>>=1;
   577 		clip.iBr.iY>>=1;
   578 		rgn1.ClipRect(clip);
   579 		test(clip==rgn1.BoundingRect());
   580 		clip.iTl.iX=clip.iBr.iX;
   581 		rgn1.ClipRect(clip);
   582 		test(rgn1.Count()==0);
   583 		}
   584 	rgn1.Close();
   585 	}
   586 
   587 void TestRRegion::TestContains()
   588 	{
   589 	RRegion rgn;
   590 	rgn.AddRect(TRect(10,10,20,20));
   591 	rgn.AddRect(TRect(10,20,50,30));
   592 	test(rgn.Contains(TPoint(10,10)));
   593 	test(rgn.Contains(TPoint(49,29)));
   594 	test(rgn.Contains(TPoint(15,15)));
   595 	test(rgn.Contains(TPoint(31,22)));
   596 	test(rgn.Contains(TPoint(50,30))==EFalse);
   597 	test(rgn.Contains(TPoint(-100,-30))==EFalse);
   598 	test(rgn.Contains(TPoint(200,20))==EFalse);
   599 	test(rgn.Contains(TPoint(15,30000))==EFalse);
   600 	rgn.Close();
   601 	TRegionFix<1> rgn2(TRect(20,20,25,25));
   602 	test(rgn2.Contains(TPoint(20,20)));
   603 	test(rgn2.Contains(TPoint(22,23)));
   604  	test(rgn2.Contains(TPoint(0,0))==EFalse);
   605 	test(rgn2.Contains(TPoint(25,25))==EFalse);
   606 	test(rgn2.Contains(TPoint(30,30))==EFalse);
   607 	}
   608 
   609 void TestRRegion::TestIntersects()
   610 	{
   611 	RRegion rgn;
   612 	rgn.AddRect(TRect(10,10,20,20));
   613 	rgn.AddRect(TRect(10,20,50,30));
   614 	test(rgn.Intersects(TRect(10,10,20,20)));
   615 	test(rgn.Intersects(TRect(5,5,15,15)));
   616 	test(rgn.Intersects(TRect(10,20,50,30)));
   617 	test(rgn.Intersects(TRect(10,10,20,20)));
   618 	test(rgn.Intersects(TRect(40,10,60,30)));
   619 	test(rgn.Intersects(TRect(0,0,10,10))==EFalse);
   620 	test(rgn.Intersects(TRect(30,10,40,20))==EFalse);
   621 	rgn.Close();
   622 	TRegionFix<1> rgn2(TRect(20,20,30,30));
   623 	test(rgn2.Intersects(TRect(20,20,30,30)));
   624 	test(rgn2.Intersects(TRect(15,25,25,35)));
   625 	test(rgn2.Intersects(TRect(25,15,35,25)));
   626 	test(rgn2.Intersects(TRect(15,15,25,25)));
   627 	test(rgn2.Intersects(TRect(25,25,35,35)));
   628 	test(rgn2.Intersects(TRect(10,20,20,30))==EFalse);
   629 	test(rgn2.Intersects(TRect(30,20,40,30))==EFalse);
   630 	test(rgn2.Intersects(TRect(20,10,30,20))==EFalse);
   631 	test(rgn2.Intersects(TRect(20,30,30,40))==EFalse);
   632 	// empty rectangles
   633 	test(rgn2.Intersects(TRect(30,30,20,20))==EFalse);
   634 	TRegionFix<1> rgn3(TRect(30,30,20,20));
   635 	test(rgn3.Intersects(TRect(30,30,20,20))==EFalse);
   636 	test(rgn3.Intersects(TRect(20,20,30,30))==EFalse);
   637 	}
   638 
   639 void TestRRegion::TestErrors()
   640 	{
   641 	RRegion rgnErr,rgnErr2;
   642 	RRegion rgn;
   643 	TRect rect(1,2,3,4),rect2;
   644 	const TRect* rList;
   645 	TPoint pnt;
   646 
   647 	rgnErr.ForceError();
   648 	rgn.AddRect(rect);
   649 	rgnErr.Copy(rgn);
   650 	test(rgnErr.CheckError()==EFalse);
   651 	test(rgnErr.Count()==1);
   652 
   653 	rgnErr.ForceError();
   654 	test(rgnErr.CheckError()==TRUE);
   655 	rgnErr.AddRect(rect);
   656 	rgnErr.AddRect(TRect(2,3,4,5));
   657 	test(rgnErr.CheckError()==TRUE);
   658 	rgnErr.SubRect(rect);
   659 	test(rgnErr.CheckError()==TRUE);
   660 	rgn.Copy(rgnErr);
   661 	test(rgn.CheckError()==TRUE);
   662 	rgnErr.Offset(1,2);
   663 	rgnErr.Offset(pnt);
   664 	test(rgnErr.CheckError()==TRUE);
   665 
   666 	rgn.Union(rgnErr);
   667 	test(rgn.CheckError()==TRUE);
   668 	rgnErr.AddRect(rect);
   669 	test(rgnErr.CheckError()==TRUE);
   670 	rgn.Clear();
   671 	rgn.AddRect(rect);
   672 	rgnErr.Union(rgn);
   673 	test(rgnErr.CheckError()==TRUE);
   674 	rgn.Clear();
   675 	test(rgn.CheckError()==FALSE);
   676 
   677 	rgnErr2.Clear();
   678 	rgnErr2.AddRect(rect);
   679 	rgn.Intersection(rgnErr,rgnErr2);
   680 	test(rgn.CheckError()==TRUE);
   681 	rgn.Clear();
   682 	rgn.Intersection(rgnErr2,rgnErr);
   683 	test(rgn.CheckError()==TRUE);
   684 	rgnErr2.ForceError();
   685 	rgn.Clear();
   686 	rgn.Intersection(rgnErr2,rgnErr);
   687 	test(rgn.CheckError()==TRUE);
   688 	rgn.Clear();
   689 	rgn.AddRect(rect);
   690 	rgn.Intersect(rgnErr);
   691 	test(rgn.CheckError()==TRUE);
   692 	rgn.Clear();
   693 	rgn.AddRect(rect);
   694 	rgnErr.Intersect(rgn);
   695 	test(rgnErr.CheckError()==TRUE);
   696 	test(rgn.CheckError()==FALSE);
   697 
   698 	test(rgnErr.IsEmpty()==FALSE);
   699 
   700 	rgn.Clear();
   701 	rgn.AddRect(rect);
   702 	rgn.SubRegion(rgnErr);
   703 	test(rgn.CheckError()==TRUE);
   704 	test(rgnErr.CheckError()==TRUE);
   705 
   706 	rgnErr.ClipRect(rect);
   707 	test(rgnErr.CheckError()==TRUE);
   708 	rgnErr.Tidy();
   709 	test(rgnErr.CheckError()==TRUE);
   710 	rect2=rgnErr.BoundingRect();
   711 	test(rect2.iTl.iX==0 && rect2.iBr.iY==0);
   712 
   713 	test(rgnErr.Count()==0);
   714 	rList=rgnErr.RectangleList();
   715 	rgn.Close();
   716 	rgnErr.Close();
   717 	rgnErr2.Close();
   718 	}
   719 
   720 void TestRRegion::doTestSort(RRegion &rgn)
   721 	{
   722 	TInt loop,loop2;
   723 	TRect const rlist[8]={	// 8 Rectangles that form a square with the centre rectangle missing
   724 		TRect(20,30,30,40),
   725 		TRect(20,10,30,20),
   726 		TRect(10,20,20,30),
   727 		TRect(30,20,40,30),
   728 		TRect(10,30,20,40),
   729 		TRect(30,30,40,40),
   730 		TRect(10,10,20,20),
   731 		TRect(30,10,40,20)};
   732 	TRect sorted[8];
   733 	TRect const* plist;
   734 	TRect tmp;
   735 
   736 // Work out wot the sorted list should be
   737 	for(loop=0;loop<8;loop++)
   738 		sorted[loop]=rlist[loop];
   739 	for(loop=0;loop<8;loop++)
   740 		{
   741 restart:
   742 		for(loop2=loop+1;loop2<8;loop2++)
   743 			{
   744 			if (sorted[loop2].iTl.iY>sorted[loop].iTl.iY)
   745 				continue;
   746 			if (sorted[loop2].iTl.iY==sorted[loop].iTl.iY && sorted[loop2].iTl.iX>sorted[loop].iTl.iX)
   747 				continue;
   748 			tmp=sorted[loop];
   749 			sorted[loop]=sorted[loop2];
   750 			sorted[loop2]=tmp;
   751 			goto restart;
   752 			}	
   753 		}
   754 	for(loop=0;loop<8;loop++)
   755 		rgn.AddRect(rlist[loop]);
   756 	rgn.Sort();
   757 	plist=rgn.RectangleList();
   758 	for(loop=0;loop<8;loop++)
   759 		test(plist[loop]==sorted[loop]);
   760 	rgn.Close();
   761 	}
   762 
   763 void TestRRegion::TestSort()
   764 	{
   765 	RRegion rgn;
   766 	doTestSort(rgn);
   767 	RRegionBuf<1> rgnBuf;
   768 	doTestSort(rgnBuf);
   769 	}
   770 
   771 void TestRRegion::doTestRegionBuf(RRegion &aRegion)
   772 	{
   773 	for(TInt index=0;index<8;index++)
   774 		{
   775 		aRegion.AddRect(TRect(index*2,index*2,index*2+2,index*2+2));
   776 		test(aRegion.Count()==(index+1));
   777 		const TRect *pr=aRegion.RectangleList();
   778 		for(TInt index2=0;index2<=index;index2++)
   779 			test(pr[index2]==TRect(index2*2,index2*2,index2*2+2,index2*2+2));
   780 		}
   781 	aRegion.Close();
   782 	}
   783 
   784 void TestRRegion::TestRegionBuf()
   785 	{
   786 	RRegionBuf<1> rgn(TRect(1,2,3,4));
   787 	test(rgn[0]==TRect(1,2,3,4));
   788 	RRegionBuf<1> rgn1;
   789 	doTestRegionBuf(rgn1);
   790 	RRegionBuf<2> rgn2;
   791 	doTestRegionBuf(rgn2);
   792 	RRegionBuf<5> rgn5;
   793 	doTestRegionBuf(rgn5);
   794 	RRegionBuf<10> rgn10;
   795 	doTestRegionBuf(rgn10);
   796 	}
   797 
   798 // Top level test code
   799 LOCAL_C void test_region(TestRRegion t)
   800 	{
   801 	test.Start(_L("Setting values"));
   802 	t.TestSet();
   803 	test.Next(_L("TRegionFix"));
   804 	t.TestRegionFix();
   805 	test.Next(_L("AddRect"));
   806 	t.TestAddRect();
   807 	test.Next(_L("SubRect"));
   808 	t.TestSubRect();
   809 	test.Next(_L("SubRegion"));
   810 	t.TestSubRegion();
   811 	test.Next(_L("Tidy"));
   812 	t.TestTidy();
   813 	test.Next(_L("Spare"));
   814 	t.TestSpare();
   815 	test.Next(_L("Offset"));
   816 	t.TestOffset();
   817 	test.Next(_L("Intersection"));
   818 	t.TestIntersection();
   819 	test.Next(_L("Union"));
   820 	t.TestUnion();
   821 	test.Next(_L("Clip rect"));
   822 	t.TestClipRect();
   823 	test.Next(_L("Contains"));
   824 	t.TestContains();
   825 	test.Next(_L("Intersects"));
   826 	t.TestIntersects();
   827 	test.Next(_L("Errors"));
   828 	t.TestErrors();
   829 	test.Next(_L("Sort"));
   830 	t.TestSort();
   831 	test.Next(_L("RegionBuf"));
   832 	t.TestRegionBuf();
   833 	test.End();
   834 	}
   835 
   836 GLDEF_C TInt E32Main()
   837 //
   838 // Main
   839 //
   840     {
   841 
   842 	test.Title();
   843 	__UHEAP_MARK;
   844 	TestRRegion t1(10,20,30,40);
   845 	TestRRegion t2(0,0,1,1);
   846 	TestRRegion t3(1,1,1,1);
   847 	TestRRegion t4(1000000,2000000,3000000,4000000);
   848 
   849 	test.Start(_L("class RRegion 1"));
   850 	test_region(t1);
   851 	test.Next(_L("class RRegion 2"));
   852 	test_region(t2);
   853 	test.Next(_L("class RRegion 3"));
   854 	test_region(t3);
   855 	test.Next(_L("class RRegion 4"));
   856 	test_region(t4);
   857 	test.End();
   858 	__UHEAP_MARKEND;
   859 	return(0);
   860     }
   861 
   862