symport/e32test/buffer/t_regn.cpp
changeset 1 0a7b44b10206
child 2 806186ab5e14
equal deleted inserted replaced
0:c55016431358 1:0a7b44b10206
       
     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 "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-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 			test(rgn.Count()==7);
       
   244 			}
       
   245 		}
       
   246 	rgn.AddRect(bounds);
       
   247 	test(rgn.Count()==1);
       
   248 	rgn.Close();
       
   249 	}
       
   250 
       
   251 void TestRRegion::TestSubRect()
       
   252 	{
       
   253 	TRect rect1(-rect[0].iBr.iX,-rect[0].iBr.iY,rect[0].iBr.iX,rect[0].iBr.iY);
       
   254 	RRegion rgn;
       
   255 	RRegion subRgn;
       
   256 	RRegion rgn2;
       
   257 	TInt index;
       
   258 
       
   259 	if (!rect[0].IsEmpty())
       
   260 		{
       
   261 		rgn.AddRect(rect1);
       
   262 		for(index=0;index<4;index++)
       
   263 			rgn.SubRect(rect[index],&subRgn);
       
   264 		if (rect[0].iTl.iX==0)	// Special case region, all rects join in the middle
       
   265 			{
       
   266 			test(rgn.Count()==0);
       
   267 			test(subRgn.Count()==4);
       
   268 			}
       
   269 		else
       
   270 			{
       
   271 			test(rgn.Count()==3);
       
   272 			test(subRgn.Count()==4);
       
   273 			test(rgn.BoundingRect()==subRgn.BoundingRect());
       
   274 			rgn.SubRect(xrect);
       
   275 			test(rgn.Count()==4);
       
   276 			rgn2.Copy(rgn);
       
   277 			subRgn.Clear();
       
   278 			rgn.SubRect(rgn.BoundingRect(),&subRgn);
       
   279 			test(rgn.Count()==0);
       
   280 			rgn2.SubRegion(subRgn,&rgn);
       
   281 			test(rgn2.Count()==0);
       
   282 			subRgn.SubRegion(rgn);
       
   283 			test(subRgn.Count()==0);
       
   284 			}
       
   285 		}
       
   286 	rgn.Close();
       
   287 	rgn2.Close();
       
   288 	subRgn.Close();
       
   289 	}
       
   290 
       
   291 void TestRRegion::TestSubRegion()
       
   292 	{
       
   293 	TRect rect1(-rect[0].iBr.iX,-rect[0].iBr.iY,rect[0].iBr.iX,rect[0].iBr.iY);
       
   294 	RRegion rgn,subRgn;
       
   295 	RRegion rgn2;
       
   296 	TInt index;
       
   297 
       
   298 	if (!rect[0].IsEmpty())
       
   299 		{
       
   300 		rgn.AddRect(rect1);
       
   301 		for(index=0;index<4;index++)
       
   302 			rgn2.AddRect(rect[index]);
       
   303 		rgn.SubRegion(rgn2,&subRgn);
       
   304 		if (rect[0].iTl.iX==0)	// Special case region, all rects join in the middle
       
   305 			{
       
   306 			test(rgn.Count()==0);
       
   307 			test(subRgn.Count()==4);
       
   308 			}
       
   309 		else
       
   310 			{
       
   311 			test(rgn.Count()==3);
       
   312 			test(subRgn.Count()==4);
       
   313 			test(rgn.BoundingRect()==subRgn.BoundingRect());
       
   314 			rgn2.Clear();
       
   315 			rgn2.AddRect(xrect);
       
   316 			rgn.SubRegion(rgn2);
       
   317 			test(rgn.Count()==4);
       
   318 			rgn2.Copy(rgn);
       
   319 			subRgn.Clear();
       
   320 			rgn.SubRect(rgn.BoundingRect(),&subRgn);
       
   321 			test(rgn.Count()==0);
       
   322 			rgn2.SubRegion(subRgn,&rgn);
       
   323 			test(rgn2.Count()==0);
       
   324 			subRgn.SubRegion(rgn);
       
   325 			test(subRgn.Count()==0);
       
   326 			}
       
   327 		}
       
   328 	rgn.Close();
       
   329 	rgn2.Close();
       
   330 	subRgn.Close();
       
   331 	}
       
   332 
       
   333 void TestRRegion::TestTidy()
       
   334 	{
       
   335 	RRegion rgn;
       
   336 	TInt loop;
       
   337 	TRect const rlist[8]={	// 8 Rectangles that form a square with the centre rectangle missing
       
   338 		TRect(10,10,20,20),
       
   339 		TRect(20,10,30,20),
       
   340 		TRect(30,10,40,20),
       
   341 
       
   342 		TRect(10,20,20,30),
       
   343 		TRect(30,20,40,30),
       
   344 
       
   345 		TRect(10,30,20,40),
       
   346 		TRect(20,30,30,40),
       
   347 		TRect(30,30,40,40)};
       
   348 	TRect const rect1(rlist[0].iTl.iX,rlist[0].iTl.iY,rlist[2].iBr.iX,rlist[2].iBr.iY);
       
   349 	TRect const rect2(rlist[0].iTl.iX,rlist[0].iTl.iY,rlist[7].iBr.iX,rlist[7].iBr.iY);
       
   350 
       
   351 // Add 3 adjoining rectangles and tidy them
       
   352 	for(loop=0;loop<3;loop++)
       
   353 		rgn.AddRect(rlist[loop]);
       
   354 	test(rgn.Count()==3);
       
   355 	rgn.Tidy();
       
   356 	test(rgn.Count()==1);
       
   357 	test(rgn[0]==rect1);
       
   358 // Same again but add the adjoining rectangles in reverse order
       
   359 	rgn.Clear();
       
   360 	for(loop=2;loop>=0;loop--)
       
   361 		rgn.AddRect(rlist[loop]);
       
   362 	test(rgn.Count()==3);
       
   363 	rgn.Tidy();
       
   364 	test(rgn.Count()==1);
       
   365 	test(rgn[0]==rect1);
       
   366 // Now add 8 rectangles that should tidy down to 4
       
   367 	rgn.Clear();
       
   368 	for(loop=0;loop<8;loop++)
       
   369 		rgn.AddRect(rlist[loop]);
       
   370 	test(rgn.Count()==8);
       
   371 	rgn.Tidy();
       
   372 	test(rgn.Count()==4);
       
   373 	test(rgn.BoundingRect()==rect2);
       
   374 // ...and in reverse
       
   375 	rgn.Clear();
       
   376 	for(loop=7;loop>=0;loop--)
       
   377 		rgn.AddRect(rlist[loop]);
       
   378 	test(rgn.Count()==8);
       
   379 	rgn.Tidy();
       
   380 	test(rgn.Count()==4);
       
   381 	test(rgn.BoundingRect()==rect2);
       
   382 	rgn.Close();
       
   383 	}
       
   384 
       
   385 void TestRRegion::TestSpare()
       
   386 	{
       
   387 	TInt gran[5]={1,2,4,20,100};
       
   388 	RRegion rgn[5]={gran[0],gran[1],gran[2],gran[3],gran[4]};
       
   389     TUint index;
       
   390 	TInt loop,spare;
       
   391 	TRect rect1(0,0,1,1);
       
   392 	TRect rect;
       
   393 
       
   394 	for(index=0;index<(sizeof(rgn)/sizeof(rgn[0]));index++)
       
   395 		{
       
   396 		test(rgn[index].CheckSpare()==0);
       
   397 		rgn[index].AddRect(rect1);
       
   398 		test(rgn[index].CheckSpare()==(gran[index]-1));
       
   399 		rgn[index].Tidy();
       
   400 		test(rgn[index].CheckSpare()==0);
       
   401 		rgn[index].Clear();
       
   402 		test(rgn[index].CheckSpare()==0);
       
   403 		}
       
   404 	for(index=0;index<(sizeof(rgn)/sizeof(rgn[0]));index++)
       
   405 		{
       
   406 		spare=0;
       
   407 		for(loop=0;loop<30;loop++)
       
   408 			{
       
   409 			rect.iTl.iX=rect.iTl.iY=loop;
       
   410 			rect.iBr.iX=rect.iBr.iY=loop+1;
       
   411 			rgn[index].AddRect(rect);
       
   412 			if (spare==0)
       
   413 				spare=gran[index];
       
   414 			spare--;
       
   415 			test(rgn[index].CheckSpare()==spare);
       
   416 			}
       
   417 		do
       
   418 			{
       
   419 			loop--;
       
   420 			rect.iTl.iX=rect.iTl.iY=loop;
       
   421 			rect.iBr.iX=rect.iBr.iY=loop+1;
       
   422 			rgn[index].SubRect(rect);
       
   423 			spare++;
       
   424 			test(rgn[index].CheckSpare()==spare);
       
   425 			} while(loop>0);
       
   426 		}
       
   427 	for(index=0;index<(sizeof(rgn)/sizeof(rgn[0]));index++)
       
   428 		rgn[index].Close();
       
   429 	}
       
   430 
       
   431 void TestRRegion::TestOffset()
       
   432 	{
       
   433 	RRegion rgn;
       
   434 	const TRect* rlist;
       
   435 	TRect r;
       
   436 	TUint index;
       
   437 
       
   438 	for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
       
   439 		{
       
   440 		rgn.Clear();
       
   441 		rgn.AddRect(rect[index]);
       
   442 		r=rect[index];
       
   443 		r.Move(1,1);
       
   444 		rgn.Offset(1,1);
       
   445 		if (rect[index].IsEmpty())
       
   446 			test(rgn.Count()==0);
       
   447 		else
       
   448 			{
       
   449 			test(rgn.Count()==1);
       
   450 			rlist=rgn.RectangleList();
       
   451 			test(rlist[0]==r);
       
   452 			}
       
   453 		}
       
   454 	rgn.Close();
       
   455 	}
       
   456 
       
   457 void TestRRegion::TestIntersection()
       
   458 	{
       
   459 	RRegion rgn1,rgn2,tmp_rgn;
       
   460 	const TRect* rlist;
       
   461 	TUint index;
       
   462 
       
   463 	rgn1.AddRect(xrect);
       
   464 	rgn2.Copy(rgn1);
       
   465 	if (!rgn1.IsEmpty())
       
   466 		{
       
   467 		for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
       
   468 			tmp_rgn.AddRect(rect[index]);
       
   469 		rgn2.Intersection(rgn1,tmp_rgn);
       
   470 		test(rgn2.Count()==4);
       
   471 		rlist=rgn2.RectangleList();
       
   472 		for(index=0;index<(TUint)rgn2.Count();index++)
       
   473 			{
       
   474 			test(rlist[index].iTl.iX==xrect.iTl.iX || rlist[index].iTl.iX==rect[0].iTl.iX);
       
   475 			test(rlist[index].iTl.iY==xrect.iTl.iY || rlist[index].iTl.iY==rect[0].iTl.iY);
       
   476 			test(rlist[index].iBr.iX==xrect.iBr.iX || rlist[index].iBr.iX==(-rect[0].iTl.iX));
       
   477 			test(rlist[index].iBr.iY==xrect.iBr.iY || rlist[index].iBr.iY==(-rect[0].iTl.iY));
       
   478 			}
       
   479 		rgn1.Intersect(tmp_rgn);
       
   480 		test(rgn1.Count()==4);
       
   481 		rlist=rgn1.RectangleList();
       
   482 		for(index=0;index<(TUint)rgn1.Count();index++)
       
   483 			{
       
   484 			test(rlist[index].iTl.iX==xrect.iTl.iX || rlist[index].iTl.iX==rect[0].iTl.iX);
       
   485 			test(rlist[index].iTl.iY==xrect.iTl.iY || rlist[index].iTl.iY==rect[0].iTl.iY);
       
   486 			test(rlist[index].iBr.iX==xrect.iBr.iX || rlist[index].iBr.iX==(-rect[0].iTl.iX));
       
   487 			test(rlist[index].iBr.iY==xrect.iBr.iY || rlist[index].iBr.iY==(-rect[0].iTl.iY));
       
   488 			}
       
   489 		}
       
   490 	rgn1.Close();
       
   491 	rgn2.Close();
       
   492 	tmp_rgn.Close();
       
   493 	}
       
   494 
       
   495 void TestRRegion::TestUnion()
       
   496 	{
       
   497 	RRegion rgn1,rgn2,tmp_rgn;
       
   498 	TRect bounds;
       
   499 	TUint index;
       
   500 
       
   501 	rgn1.AddRect(xrect);
       
   502 	if (!rgn1.IsEmpty())
       
   503 		{
       
   504 		for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
       
   505 			tmp_rgn.AddRect(rect[index]);
       
   506 		test(tmp_rgn.Count()==4);
       
   507 		rgn1.Union(tmp_rgn);
       
   508 		test(rgn1.Count()==7);
       
   509 		rgn2.Copy(rgn1);
       
   510 		rgn2.Offset(3,5);
       
   511 		bounds=rgn1.BoundingRect();
       
   512 		rgn1.Union(rgn2);
       
   513 		bounds.Resize(3,5);
       
   514 		test(rgn1.BoundingRect()==bounds);
       
   515 		}
       
   516 	rgn1.Close();
       
   517 	rgn2.Close();
       
   518 	tmp_rgn.Close();
       
   519 	}
       
   520 
       
   521 void TestRRegion::TestClipRect()
       
   522 	{
       
   523 	RRegion rgn1(xrect);
       
   524 	if (!rgn1.IsEmpty())
       
   525 		{
       
   526 		TUint index;
       
   527 		for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
       
   528 			rgn1.AddRect(rect[index]);
       
   529 		TRect clip=rgn1.BoundingRect();
       
   530 		rgn1.ClipRect(clip);
       
   531 		test(clip==rgn1.BoundingRect());
       
   532 		clip.iTl.iX>>=1;
       
   533 		clip.iTl.iY>>=1;
       
   534 		clip.iBr.iX>>=1;
       
   535 		clip.iBr.iY>>=1;
       
   536 		rgn1.ClipRect(clip);
       
   537 		test(clip==rgn1.BoundingRect());
       
   538 		clip.iTl.iX=clip.iBr.iX;
       
   539 		rgn1.ClipRect(clip);
       
   540 		test(rgn1.Count()==0);
       
   541 		}
       
   542 	rgn1.Close();
       
   543 	}
       
   544 
       
   545 void TestRRegion::TestContains()
       
   546 	{
       
   547 	RRegion rgn;
       
   548 	rgn.AddRect(TRect(10,10,20,20));
       
   549 	rgn.AddRect(TRect(10,20,50,30));
       
   550 	test(rgn.Contains(TPoint(10,10)));
       
   551 	test(rgn.Contains(TPoint(49,29)));
       
   552 	test(rgn.Contains(TPoint(15,15)));
       
   553 	test(rgn.Contains(TPoint(31,22)));
       
   554 	test(rgn.Contains(TPoint(50,30))==EFalse);
       
   555 	test(rgn.Contains(TPoint(-100,-30))==EFalse);
       
   556 	test(rgn.Contains(TPoint(200,20))==EFalse);
       
   557 	test(rgn.Contains(TPoint(15,30000))==EFalse);
       
   558 	rgn.Close();
       
   559 	TRegionFix<1> rgn2(TRect(20,20,25,25));
       
   560 	test(rgn2.Contains(TPoint(20,20)));
       
   561 	test(rgn2.Contains(TPoint(22,23)));
       
   562  	test(rgn2.Contains(TPoint(0,0))==EFalse);
       
   563 	test(rgn2.Contains(TPoint(25,25))==EFalse);
       
   564 	test(rgn2.Contains(TPoint(30,30))==EFalse);
       
   565 	}
       
   566 
       
   567 void TestRRegion::TestIntersects()
       
   568 	{
       
   569 	RRegion rgn;
       
   570 	rgn.AddRect(TRect(10,10,20,20));
       
   571 	rgn.AddRect(TRect(10,20,50,30));
       
   572 	test(rgn.Intersects(TRect(10,10,20,20)));
       
   573 	test(rgn.Intersects(TRect(5,5,15,15)));
       
   574 	test(rgn.Intersects(TRect(10,20,50,30)));
       
   575 	test(rgn.Intersects(TRect(10,10,20,20)));
       
   576 	test(rgn.Intersects(TRect(40,10,60,30)));
       
   577 	test(rgn.Intersects(TRect(0,0,10,10))==EFalse);
       
   578 	test(rgn.Intersects(TRect(30,10,40,20))==EFalse);
       
   579 	rgn.Close();
       
   580 	TRegionFix<1> rgn2(TRect(20,20,30,30));
       
   581 	test(rgn2.Intersects(TRect(20,20,30,30)));
       
   582 	test(rgn2.Intersects(TRect(15,25,25,35)));
       
   583 	test(rgn2.Intersects(TRect(25,15,35,25)));
       
   584 	test(rgn2.Intersects(TRect(15,15,25,25)));
       
   585 	test(rgn2.Intersects(TRect(25,25,35,35)));
       
   586 	test(rgn2.Intersects(TRect(10,20,20,30))==EFalse);
       
   587 	test(rgn2.Intersects(TRect(30,20,40,30))==EFalse);
       
   588 	test(rgn2.Intersects(TRect(20,10,30,20))==EFalse);
       
   589 	test(rgn2.Intersects(TRect(20,30,30,40))==EFalse);
       
   590 	// empty rectangles
       
   591 	test(rgn2.Intersects(TRect(30,30,20,20))==EFalse);
       
   592 	TRegionFix<1> rgn3(TRect(30,30,20,20));
       
   593 	test(rgn3.Intersects(TRect(30,30,20,20))==EFalse);
       
   594 	test(rgn3.Intersects(TRect(20,20,30,30))==EFalse);
       
   595 	}
       
   596 
       
   597 void TestRRegion::TestErrors()
       
   598 	{
       
   599 	RRegion rgnErr,rgnErr2;
       
   600 	RRegion rgn;
       
   601 	TRect rect(1,2,3,4),rect2;
       
   602 	const TRect* rList;
       
   603 	TPoint pnt;
       
   604 
       
   605 	rgnErr.ForceError();
       
   606 	rgn.AddRect(rect);
       
   607 	rgnErr.Copy(rgn);
       
   608 	test(rgnErr.CheckError()==EFalse);
       
   609 	test(rgnErr.Count()==1);
       
   610 
       
   611 	rgnErr.ForceError();
       
   612 	test(rgnErr.CheckError()==TRUE);
       
   613 	rgnErr.AddRect(rect);
       
   614 	rgnErr.AddRect(TRect(2,3,4,5));
       
   615 	test(rgnErr.CheckError()==TRUE);
       
   616 	rgnErr.SubRect(rect);
       
   617 	test(rgnErr.CheckError()==TRUE);
       
   618 	rgn.Copy(rgnErr);
       
   619 	test(rgn.CheckError()==TRUE);
       
   620 	rgnErr.Offset(1,2);
       
   621 	rgnErr.Offset(pnt);
       
   622 	test(rgnErr.CheckError()==TRUE);
       
   623 
       
   624 	rgn.Union(rgnErr);
       
   625 	test(rgn.CheckError()==TRUE);
       
   626 	rgnErr.AddRect(rect);
       
   627 	test(rgnErr.CheckError()==TRUE);
       
   628 	rgn.Clear();
       
   629 	rgn.AddRect(rect);
       
   630 	rgnErr.Union(rgn);
       
   631 	test(rgnErr.CheckError()==TRUE);
       
   632 	rgn.Clear();
       
   633 	test(rgn.CheckError()==FALSE);
       
   634 
       
   635 	rgnErr2.Clear();
       
   636 	rgnErr2.AddRect(rect);
       
   637 	rgn.Intersection(rgnErr,rgnErr2);
       
   638 	test(rgn.CheckError()==TRUE);
       
   639 	rgn.Clear();
       
   640 	rgn.Intersection(rgnErr2,rgnErr);
       
   641 	test(rgn.CheckError()==TRUE);
       
   642 	rgnErr2.ForceError();
       
   643 	rgn.Clear();
       
   644 	rgn.Intersection(rgnErr2,rgnErr);
       
   645 	test(rgn.CheckError()==TRUE);
       
   646 	rgn.Clear();
       
   647 	rgn.AddRect(rect);
       
   648 	rgn.Intersect(rgnErr);
       
   649 	test(rgn.CheckError()==TRUE);
       
   650 	rgn.Clear();
       
   651 	rgn.AddRect(rect);
       
   652 	rgnErr.Intersect(rgn);
       
   653 	test(rgnErr.CheckError()==TRUE);
       
   654 	test(rgn.CheckError()==FALSE);
       
   655 
       
   656 	test(rgnErr.IsEmpty()==FALSE);
       
   657 
       
   658 	rgn.Clear();
       
   659 	rgn.AddRect(rect);
       
   660 	rgn.SubRegion(rgnErr);
       
   661 	test(rgn.CheckError()==TRUE);
       
   662 	test(rgnErr.CheckError()==TRUE);
       
   663 
       
   664 	rgnErr.ClipRect(rect);
       
   665 	test(rgnErr.CheckError()==TRUE);
       
   666 	rgnErr.Tidy();
       
   667 	test(rgnErr.CheckError()==TRUE);
       
   668 	rect2=rgnErr.BoundingRect();
       
   669 	test(rect2.iTl.iX==0 && rect2.iBr.iY==0);
       
   670 
       
   671 	test(rgnErr.Count()==0);
       
   672 	rList=rgnErr.RectangleList();
       
   673 	rgn.Close();
       
   674 	rgnErr.Close();
       
   675 	rgnErr2.Close();
       
   676 	}
       
   677 
       
   678 void TestRRegion::doTestSort(RRegion &rgn)
       
   679 	{
       
   680 	TInt loop,loop2;
       
   681 	TRect const rlist[8]={	// 8 Rectangles that form a square with the centre rectangle missing
       
   682 		TRect(20,30,30,40),
       
   683 		TRect(20,10,30,20),
       
   684 		TRect(10,20,20,30),
       
   685 		TRect(30,20,40,30),
       
   686 		TRect(10,30,20,40),
       
   687 		TRect(30,30,40,40),
       
   688 		TRect(10,10,20,20),
       
   689 		TRect(30,10,40,20)};
       
   690 	TRect sorted[8];
       
   691 	TRect const* plist;
       
   692 	TRect tmp;
       
   693 
       
   694 // Work out wot the sorted list should be
       
   695 	for(loop=0;loop<8;loop++)
       
   696 		sorted[loop]=rlist[loop];
       
   697 	for(loop=0;loop<8;loop++)
       
   698 		{
       
   699 restart:
       
   700 		for(loop2=loop+1;loop2<8;loop2++)
       
   701 			{
       
   702 			if (sorted[loop2].iTl.iY>sorted[loop].iTl.iY)
       
   703 				continue;
       
   704 			if (sorted[loop2].iTl.iY==sorted[loop].iTl.iY && sorted[loop2].iTl.iX>sorted[loop].iTl.iX)
       
   705 				continue;
       
   706 			tmp=sorted[loop];
       
   707 			sorted[loop]=sorted[loop2];
       
   708 			sorted[loop2]=tmp;
       
   709 			goto restart;
       
   710 			}	
       
   711 		}
       
   712 	for(loop=0;loop<8;loop++)
       
   713 		rgn.AddRect(rlist[loop]);
       
   714 	rgn.Sort();
       
   715 	plist=rgn.RectangleList();
       
   716 	for(loop=0;loop<8;loop++)
       
   717 		test(plist[loop]==sorted[loop]);
       
   718 	rgn.Close();
       
   719 	}
       
   720 
       
   721 void TestRRegion::TestSort()
       
   722 	{
       
   723 	RRegion rgn;
       
   724 	doTestSort(rgn);
       
   725 	RRegionBuf<1> rgnBuf;
       
   726 	doTestSort(rgnBuf);
       
   727 	}
       
   728 
       
   729 void TestRRegion::doTestRegionBuf(RRegion &aRegion)
       
   730 	{
       
   731 	for(TInt index=0;index<8;index++)
       
   732 		{
       
   733 		aRegion.AddRect(TRect(index*2,index*2,index*2+2,index*2+2));
       
   734 		test(aRegion.Count()==(index+1));
       
   735 		const TRect *pr=aRegion.RectangleList();
       
   736 		for(TInt index2=0;index2<=index;index2++)
       
   737 			test(pr[index2]==TRect(index2*2,index2*2,index2*2+2,index2*2+2));
       
   738 		}
       
   739 	aRegion.Close();
       
   740 	}
       
   741 
       
   742 void TestRRegion::TestRegionBuf()
       
   743 	{
       
   744 	RRegionBuf<1> rgn(TRect(1,2,3,4));
       
   745 	test(rgn[0]==TRect(1,2,3,4));
       
   746 	RRegionBuf<1> rgn1;
       
   747 	doTestRegionBuf(rgn1);
       
   748 	RRegionBuf<2> rgn2;
       
   749 	doTestRegionBuf(rgn2);
       
   750 	RRegionBuf<5> rgn5;
       
   751 	doTestRegionBuf(rgn5);
       
   752 	RRegionBuf<10> rgn10;
       
   753 	doTestRegionBuf(rgn10);
       
   754 	}
       
   755 
       
   756 // Top level test code
       
   757 LOCAL_C void test_region(TestRRegion t)
       
   758 	{
       
   759 	test.Start(_L("Setting values"));
       
   760 	t.TestSet();
       
   761 	test.Next(_L("TRegionFix"));
       
   762 	t.TestRegionFix();
       
   763 	test.Next(_L("AddRect"));
       
   764 	t.TestAddRect();
       
   765 	test.Next(_L("SubRect"));
       
   766 	t.TestSubRect();
       
   767 	test.Next(_L("SubRegion"));
       
   768 	t.TestSubRegion();
       
   769 	test.Next(_L("Tidy"));
       
   770 	t.TestTidy();
       
   771 	test.Next(_L("Spare"));
       
   772 	t.TestSpare();
       
   773 	test.Next(_L("Offset"));
       
   774 	t.TestOffset();
       
   775 	test.Next(_L("Intersection"));
       
   776 	t.TestIntersection();
       
   777 	test.Next(_L("Union"));
       
   778 	t.TestUnion();
       
   779 	test.Next(_L("Clip rect"));
       
   780 	t.TestClipRect();
       
   781 	test.Next(_L("Contains"));
       
   782 	t.TestContains();
       
   783 	test.Next(_L("Intersects"));
       
   784 	t.TestIntersects();
       
   785 	test.Next(_L("Errors"));
       
   786 	t.TestErrors();
       
   787 	test.Next(_L("Sort"));
       
   788 	t.TestSort();
       
   789 	test.Next(_L("RegionBuf"));
       
   790 	t.TestRegionBuf();
       
   791 	test.End();
       
   792 	}
       
   793 
       
   794 GLDEF_C TInt E32Main()
       
   795 //
       
   796 // Main
       
   797 //
       
   798     {
       
   799 
       
   800 	test.Title();
       
   801 	__UHEAP_MARK;
       
   802 	TestRRegion t1(10,20,30,40);
       
   803 	TestRRegion t2(0,0,1,1);
       
   804 	TestRRegion t3(1,1,1,1);
       
   805 	TestRRegion t4(1000000,2000000,3000000,4000000);
       
   806 
       
   807 	test.Start(_L("class RRegion 1"));
       
   808 	test_region(t1);
       
   809 	test.Next(_L("class RRegion 2"));
       
   810 	test_region(t2);
       
   811 	test.Next(_L("class RRegion 3"));
       
   812 	test_region(t3);
       
   813 	test.Next(_L("class RRegion 4"));
       
   814 	test_region(t4);
       
   815 	test.End();
       
   816 	__UHEAP_MARKEND;
       
   817 	return(0);
       
   818     }
       
   819 
       
   820