1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/buffer/t_regn.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,862 @@
1.4 +// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\buffer\t_regn.cpp
1.18 +// Overview:
1.19 +// Test fixed and variable clipping regions.
1.20 +// API Information:
1.21 +// TRegionFix, RRegion .
1.22 +// Details:
1.23 +// - Construct some expandable clipping regions, add some rectangles, check the region
1.24 +// matches the rectangles, clear the region, add some rectangles to the region and
1.25 +// check the region matches the rectangles.
1.26 +// - Copy one region to another, using the Copy method and the copy constructor,
1.27 +// and check the region matches the rectangles.
1.28 +// - Create a some fixed clipping regions, add some rectangles, check the region
1.29 +// matches the rectangles, clear the region, add some rectangles, and check the
1.30 +// region matches the rectangles.
1.31 +// - Copy one fixed region to another, using the Copy method and the copy constructor,
1.32 +// and check the region matches the rectangles.
1.33 +// - Test TRegionFix creation and error handling using Clear, Count, AddRect, CheckError
1.34 +// and Tidy methods
1.35 +// - Test adding rectangles, via AddRect, to an RRegion object. Verify the results
1.36 +// via the Count, BoundingRect and IsEmpty methods.
1.37 +// - Test subtracting rectangles, via SubRect, from an RRegion object. Verify the
1.38 +// results via the Count, BoundingRect and SubRegion methods.
1.39 +// - Test subtracting regions, via AddRect and SubRegion, from an RRegion object.
1.40 +// Verify the results via the Count, BoundingRect, Clear, Copy, and SubRect methods.
1.41 +// - Test the RRegion Tidy method. Verify the results via the Count, AddRect,
1.42 +// BoundingRect and Clear methods.
1.43 +// - Test the RRegion CheckSpare method. Verify the results via the AddRect, Tidy,
1.44 +// Clear and SubRect methods.
1.45 +// - Test the RRegion Offset method. Verify the results via the AddRect, Move,
1.46 +// Clear, IsEmpty and RectangleList methods.
1.47 +// - Test the RRegion Intersection and Intersect methods. Verify the results via
1.48 +// the AddRect, Count, IsEmpty and RectangleList methods.
1.49 +// - Test the RRegion Union method. Verify the results via the AddRect, Count,
1.50 +// Copy, Offset and BoundingRect methods.
1.51 +// - Test the RRegion ClipRect method. Verify the results via the AddRect, Count,
1.52 +// and BoundingRect methods.
1.53 +// - Test the RRegion and TRgionFix Contains methods. Verify the results via the
1.54 +// AddRect method.
1.55 +// - Test the RRegion ForceError and CheckError methods. Verify the results via the
1.56 +// AddRect, Copy, Count, SubRect, Clear and BoundingRect methods.
1.57 +// - Test the RRegion and RRegionBuf sort method.
1.58 +// - Construct some regions with pre-allocated buffer (RRegionBuf), add some rectangles,
1.59 +// get a pointer to the array of rectangles defining this region and check the
1.60 +// rectangles are as expected.
1.61 +// Platforms/Drives/Compatibility:
1.62 +// All
1.63 +// Assumptions/Requirement/Pre-requisites:
1.64 +// Failures and causes:
1.65 +// Base Port information:
1.66 +//
1.67 +//
1.68 +
1.69 +#include <e32test.h>
1.70 +
1.71 +LOCAL_D RTest test(_L("T_REGN"));
1.72 +
1.73 +class TestRRegion
1.74 + {
1.75 +public:
1.76 + TestRRegion(TInt tlx, TInt tly, TInt brx, TInt bry);
1.77 + void TestSet();
1.78 + void TestRegionFix();
1.79 + void TestAddRect();
1.80 + void TestSubRect();
1.81 + void TestSubRegion();
1.82 + void TestTidy();
1.83 + void TestSpare();
1.84 + void TestOffset();
1.85 + void TestIntersection();
1.86 + void TestUnion();
1.87 + void TestClipRect();
1.88 + void TestContains();
1.89 + void TestIntersects();
1.90 + void TestErrors();
1.91 + void doTestSort(RRegion &aRegion);
1.92 + void TestSort();
1.93 + void doTestRegionBuf(RRegion &aRegion);
1.94 + void TestRegionBuf();
1.95 +private:
1.96 + void DoTestSet(TRegion** rgn,TInt rgnArraySize);
1.97 + void CheckRectRegion(const TRegion& region,const TRect& rect);
1.98 +private:
1.99 + TRect rect[4];
1.100 + TRect bounds;
1.101 + TRect xrect;
1.102 + };
1.103 +
1.104 +// Region test code
1.105 +TestRRegion::TestRRegion(TInt tlx, TInt tly, TInt brx, TInt bry)
1.106 + {
1.107 + rect[0]=TRect( tlx, tly, brx, bry);
1.108 + rect[1]=TRect(-brx,-bry,-tlx,-tly);
1.109 + rect[2]=TRect( tlx,-bry, brx,-tly);
1.110 + rect[3]=TRect(-brx, tly,-tlx, bry);
1.111 + bounds=TRect(-brx,-bry,brx,bry);
1.112 + xrect=TRect(-(tlx/2+brx/2),-(tly/2+bry/2),tlx/2+brx/2,tly/2+bry/2);
1.113 + }
1.114 +
1.115 +void TestRRegion::CheckRectRegion(const TRegion& region,const TRect& rect)
1.116 +// Check the region matches the rectangle
1.117 + {
1.118 + const TRect* rlist;
1.119 +
1.120 + if (rect.IsEmpty())
1.121 + test(region.Count()==0);
1.122 + else
1.123 + {
1.124 + test(region.Count()==1);
1.125 + rlist=region.RectangleList();
1.126 + test(rlist[0]==rect);
1.127 + test(region[0]==rect);
1.128 + }
1.129 + }
1.130 +
1.131 +void TestRRegion::DoTestSet(TRegion** rgn,TInt rgnArraySize)
1.132 + {
1.133 + TInt index;
1.134 + for(index=0;index<rgnArraySize;index++)
1.135 + rgn[index]->AddRect(rect[index]);
1.136 + for(index=0;index<rgnArraySize;index++)
1.137 + CheckRectRegion(*rgn[index],rect[index]);
1.138 + for(index=0;index<rgnArraySize;index++)
1.139 + {
1.140 + rgn[index]->Clear();
1.141 + rgn[index]->AddRect(rect[index]);
1.142 + }
1.143 + for(index=0;index<rgnArraySize;index++)
1.144 + CheckRectRegion(*rgn[index],rect[index]);
1.145 + }
1.146 +
1.147 +void TestRRegion::TestSet()
1.148 + {
1.149 + TUint index;
1.150 +
1.151 + RRegion xrgn(rect[0]);
1.152 + CheckRectRegion(xrgn,rect[0]);
1.153 + xrgn.Close();
1.154 +//
1.155 + RRegion rgn[5];
1.156 + TRegion* prgn[5]={&rgn[0],&rgn[1],&rgn[2],&rgn[3],&rgn[4]};
1.157 + DoTestSet(&prgn[0],(sizeof(rgn)/sizeof(rgn[0])));
1.158 + for(index=0;index<(sizeof(rgn)/sizeof(rgn[0]));index++)
1.159 + {
1.160 + RRegion rgn1;
1.161 + rgn1.Copy(rgn[index]);
1.162 + CheckRectRegion(rgn1,rect[index]);
1.163 + RRegion rgn2(rgn[index]);
1.164 + CheckRectRegion(rgn2,rect[index]);
1.165 + rgn[index].Close();
1.166 + rgn1.Close();
1.167 + }
1.168 +//
1.169 + TRegionFix<5> rgnf[5];
1.170 + TRegion* prgnf[5]={&rgnf[0],&rgnf[1],&rgnf[2],&rgnf[3],&rgnf[4]};
1.171 + DoTestSet(&prgnf[0],(sizeof(rgnf)/sizeof(rgnf[0])));
1.172 + for(index=0;index<(sizeof(rgn)/sizeof(rgn[0]));index++)
1.173 + {
1.174 + TRegionFix<5> rgn1;
1.175 + rgn1.Copy(rgnf[index]);
1.176 + CheckRectRegion(rgn1,rect[index]);
1.177 + TRegionFix<5> rgn2(rgnf[index]);
1.178 + CheckRectRegion(rgn2,rect[index]);
1.179 + }
1.180 + }
1.181 +
1.182 +void TestRRegion::TestRegionFix()
1.183 +//
1.184 +// Test TRegionFix creation and error handling
1.185 +//
1.186 + {
1.187 + TRegionFix<1> rgnf(rect[0]);
1.188 + CheckRectRegion(rgnf,rect[0]);
1.189 + rgnf.Clear();
1.190 + test(rgnf.Count()==0);
1.191 + rgnf.AddRect(TRect(0,0,2,2));
1.192 + test(rgnf.CheckError()==FALSE);
1.193 + rgnf.AddRect(TRect(2,2,4,4)); // Should cause error, rgnf can only hold 1 rectangle
1.194 + test(rgnf.CheckError()==TRUE && rgnf.Count()==0);
1.195 + rgnf.Clear();
1.196 + test(rgnf.CheckError()==FALSE && rgnf.Count()==0);
1.197 +//
1.198 + TRegionFix<10> rgnf2;
1.199 + TInt index;
1.200 + for(index=0;index<10;index++)
1.201 + {
1.202 + rgnf2.AddRect(TRect(index*4,0,index*4+2,10));
1.203 + test(rgnf2.Count()==(index+1));
1.204 + TRegionFix<10> rgnf4(rgnf2); // Test copy constructor
1.205 + TRegionFix<10> rgnf5;
1.206 + rgnf5=rgnf2; // Test assignment
1.207 + test(rgnf4.Count()==(index+1));
1.208 + for(TInt index2=0;index2<=index;index2++)
1.209 + test(rgnf2[index2]==rgnf4[index2] && rgnf2[index2]==rgnf5[index2]);
1.210 + }
1.211 + rgnf2.AddRect(TRect(-10,-10,0,0)); // Should push it over the edge
1.212 + test(rgnf2.CheckError()==TRUE && rgnf2.Count()==0);
1.213 +//
1.214 + TRegionFix<5> rgnf3;
1.215 + for(index=0;index<4;index++)
1.216 + {
1.217 + rgnf3.AddRect(TRect(index*4,index*4,index*4+8,index*4+8));
1.218 + if (index==3)
1.219 + test(rgnf3.CheckError()==TRUE);
1.220 + else
1.221 + {
1.222 + rgnf3.Tidy();
1.223 + if (index>0)
1.224 + test(rgnf3.Count()==(index+2));
1.225 + }
1.226 + }
1.227 + }
1.228 +
1.229 +void TestRRegion::TestAddRect()
1.230 + {
1.231 + RRegion rgn;
1.232 + TInt index,i;
1.233 +
1.234 + if (!rect[0].IsEmpty())
1.235 + {
1.236 + for(index=0;index<4;index++)
1.237 + {
1.238 + for(i=0;i<=index;i++)
1.239 + rgn.AddRect(rect[index]);
1.240 + test(rgn.Count()==(index+1));
1.241 + }
1.242 + test(rgn.BoundingRect()==bounds);
1.243 + if (!xrect.IsEmpty())
1.244 + {
1.245 + rgn.AddRect(xrect);
1.246 + TInt count = rgn.Count();
1.247 + test( (count > 4) && (count <= 9) );
1.248 + }
1.249 + }
1.250 + rgn.AddRect(bounds);
1.251 + test(rgn.Count()==1);
1.252 + rgn.Close();
1.253 + }
1.254 +
1.255 +void TestRRegion::TestSubRect()
1.256 + {
1.257 + TRect rect1(-rect[0].iBr.iX,-rect[0].iBr.iY,rect[0].iBr.iX,rect[0].iBr.iY);
1.258 + RRegion rgn;
1.259 + RRegion subRgn;
1.260 + RRegion rgn2;
1.261 + TInt index;
1.262 +
1.263 + if (!rect[0].IsEmpty())
1.264 + {
1.265 + rgn.AddRect(rect1);
1.266 + for(index=0;index<4;index++)
1.267 + rgn.SubRect(rect[index],&subRgn);
1.268 + if (rect[0].iTl.iX==0) // Special case region, all rects join in the middle
1.269 + {
1.270 + test(rgn.Count()==0);
1.271 + test(subRgn.Count()==4);
1.272 + }
1.273 + else
1.274 + {
1.275 + test(rgn.Count()==3);
1.276 + test(subRgn.Count()==4);
1.277 + test(rgn.BoundingRect()==subRgn.BoundingRect());
1.278 + rgn.SubRect(xrect);
1.279 + test(rgn.Count()==4);
1.280 + rgn2.Copy(rgn);
1.281 + subRgn.Clear();
1.282 + rgn.SubRect(rgn.BoundingRect(),&subRgn);
1.283 + test(rgn.Count()==0);
1.284 + rgn2.SubRegion(subRgn,&rgn);
1.285 + test(rgn2.Count()==0);
1.286 + subRgn.SubRegion(rgn);
1.287 + test(subRgn.Count()==0);
1.288 + }
1.289 + }
1.290 + rgn.Close();
1.291 + rgn2.Close();
1.292 + subRgn.Close();
1.293 + }
1.294 +
1.295 +void TestRRegion::TestSubRegion()
1.296 + {
1.297 + TRect rect1(-rect[0].iBr.iX,-rect[0].iBr.iY,rect[0].iBr.iX,rect[0].iBr.iY);
1.298 + RRegion rgn,subRgn;
1.299 + RRegion rgn2;
1.300 + TInt index;
1.301 +
1.302 + if (!rect[0].IsEmpty())
1.303 + {
1.304 + rgn.AddRect(rect1);
1.305 + for(index=0;index<4;index++)
1.306 + rgn2.AddRect(rect[index]);
1.307 + rgn.SubRegion(rgn2,&subRgn);
1.308 + if (rect[0].iTl.iX==0) // Special case region, all rects join in the middle
1.309 + {
1.310 + test(rgn.Count()==0);
1.311 + test(subRgn.Count()==4);
1.312 + }
1.313 + else
1.314 + {
1.315 + test(rgn.Count()==3);
1.316 + test(subRgn.Count()==4);
1.317 + test(rgn.BoundingRect()==subRgn.BoundingRect());
1.318 + rgn2.Clear();
1.319 + rgn2.AddRect(xrect);
1.320 + rgn.SubRegion(rgn2);
1.321 + test(rgn.Count()==4);
1.322 + rgn2.Copy(rgn);
1.323 + subRgn.Clear();
1.324 + rgn.SubRect(rgn.BoundingRect(),&subRgn);
1.325 + test(rgn.Count()==0);
1.326 + rgn2.SubRegion(subRgn,&rgn);
1.327 + test(rgn2.Count()==0);
1.328 + subRgn.SubRegion(rgn);
1.329 + test(subRgn.Count()==0);
1.330 + }
1.331 + }
1.332 + rgn.Close();
1.333 + rgn2.Close();
1.334 + subRgn.Close();
1.335 + }
1.336 +
1.337 +void TestRRegion::TestTidy()
1.338 + {
1.339 + RRegion rgn;
1.340 + TInt loop;
1.341 + TRect const rlist[8]={ // 8 Rectangles that form a square with the centre rectangle missing
1.342 + TRect(10,10,20,20),
1.343 + TRect(20,10,30,20),
1.344 + TRect(30,10,40,20),
1.345 +
1.346 + TRect(10,20,20,30),
1.347 + TRect(30,20,40,30),
1.348 +
1.349 + TRect(10,30,20,40),
1.350 + TRect(20,30,30,40),
1.351 + TRect(30,30,40,40)};
1.352 + TRect const rect1(rlist[0].iTl.iX,rlist[0].iTl.iY,rlist[2].iBr.iX,rlist[2].iBr.iY);
1.353 + TRect const rect2(rlist[0].iTl.iX,rlist[0].iTl.iY,rlist[7].iBr.iX,rlist[7].iBr.iY);
1.354 +
1.355 +// Add 3 adjoining rectangles and tidy them
1.356 + for(loop=0;loop<3;loop++)
1.357 + rgn.AddRect(rlist[loop]);
1.358 + test(rgn.Count()==3);
1.359 + rgn.Tidy();
1.360 + test(rgn.Count()==1);
1.361 + test(rgn[0]==rect1);
1.362 +// Same again but add the adjoining rectangles in reverse order
1.363 + rgn.Clear();
1.364 + for(loop=2;loop>=0;loop--)
1.365 + rgn.AddRect(rlist[loop]);
1.366 + test(rgn.Count()==3);
1.367 + rgn.Tidy();
1.368 + test(rgn.Count()==1);
1.369 + test(rgn[0]==rect1);
1.370 +// Now add 8 rectangles that should tidy down to 4
1.371 + rgn.Clear();
1.372 + for(loop=0;loop<8;loop++)
1.373 + rgn.AddRect(rlist[loop]);
1.374 + test(rgn.Count()==8);
1.375 + rgn.Tidy();
1.376 + test(rgn.Count()==4);
1.377 + test(rgn.BoundingRect()==rect2);
1.378 +// ...and in reverse
1.379 + rgn.Clear();
1.380 + for(loop=7;loop>=0;loop--)
1.381 + rgn.AddRect(rlist[loop]);
1.382 + test(rgn.Count()==8);
1.383 + rgn.Tidy();
1.384 + test(rgn.Count()==4);
1.385 + test(rgn.BoundingRect()==rect2);
1.386 + rgn.Close();
1.387 + }
1.388 +
1.389 +void TestRRegion::TestSpare()
1.390 + {
1.391 + TInt gran[5]={1,2,4,20,100};
1.392 + RRegion rgn[5]={gran[0],gran[1],gran[2],gran[3],gran[4]};
1.393 + TUint index;
1.394 + TInt loop,spare;
1.395 + TRect rect1(0,0,1,1);
1.396 + TRect rect;
1.397 +
1.398 + for(index=0;index<(sizeof(rgn)/sizeof(rgn[0]));index++)
1.399 + {
1.400 + test(rgn[index].CheckSpare()==0);
1.401 + rgn[index].AddRect(rect1);
1.402 + test(rgn[index].CheckSpare()==(gran[index]-1));
1.403 + rgn[index].Tidy();
1.404 + test(rgn[index].CheckSpare()<gran[index]);
1.405 + rgn[index].Clear();
1.406 + test(rgn[index].CheckSpare()==0);
1.407 + }
1.408 + for(index=0;index<(sizeof(rgn)/sizeof(rgn[0]));index++)
1.409 + {
1.410 + spare=0;
1.411 + for(loop=0;loop<30;loop++)
1.412 + {
1.413 + rect.iTl.iX=rect.iTl.iY=loop;
1.414 + rect.iBr.iX=rect.iBr.iY=loop+1;
1.415 + rgn[index].AddRect(rect);
1.416 + if (spare==0)
1.417 + spare=gran[index];
1.418 + spare--;
1.419 + test(rgn[index].CheckSpare()==spare);
1.420 + }
1.421 + do
1.422 + {
1.423 + loop--;
1.424 + rect.iTl.iX=rect.iTl.iY=loop;
1.425 + rect.iBr.iX=rect.iBr.iY=loop+1;
1.426 + rgn[index].SubRect(rect);
1.427 + spare++;
1.428 + test(rgn[index].CheckSpare()==spare);
1.429 + } while(loop>0);
1.430 + }
1.431 + for(index=0;index<(sizeof(rgn)/sizeof(rgn[0]));index++)
1.432 + rgn[index].Close();
1.433 + }
1.434 +
1.435 +void TestRRegion::TestOffset()
1.436 + {
1.437 + RRegion rgn;
1.438 + const TRect* rlist;
1.439 + TRect r;
1.440 + TUint index;
1.441 +
1.442 + for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
1.443 + {
1.444 + rgn.Clear();
1.445 + rgn.AddRect(rect[index]);
1.446 + r=rect[index];
1.447 + r.Move(1,1);
1.448 + rgn.Offset(1,1);
1.449 + if (rect[index].IsEmpty())
1.450 + test(rgn.Count()==0);
1.451 + else
1.452 + {
1.453 + test(rgn.Count()==1);
1.454 + rlist=rgn.RectangleList();
1.455 + test(rlist[0]==r);
1.456 + }
1.457 + }
1.458 + rgn.Close();
1.459 + }
1.460 +
1.461 +void TestRRegion::TestIntersection()
1.462 + {
1.463 + RRegion rgn1,rgn2,tmp_rgn;
1.464 + const TRect* rlist;
1.465 + TUint index;
1.466 +
1.467 + rgn1.AddRect(xrect);
1.468 + rgn2.Copy(rgn1);
1.469 + if (!rgn1.IsEmpty())
1.470 + {
1.471 + for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
1.472 + tmp_rgn.AddRect(rect[index]);
1.473 + rgn2.Intersection(rgn1,tmp_rgn);
1.474 + test(rgn2.Count()==4);
1.475 + rlist=rgn2.RectangleList();
1.476 + for(index=0;index<(TUint)rgn2.Count();index++)
1.477 + {
1.478 + test(rlist[index].iTl.iX==xrect.iTl.iX || rlist[index].iTl.iX==rect[0].iTl.iX);
1.479 + test(rlist[index].iTl.iY==xrect.iTl.iY || rlist[index].iTl.iY==rect[0].iTl.iY);
1.480 + test(rlist[index].iBr.iX==xrect.iBr.iX || rlist[index].iBr.iX==(-rect[0].iTl.iX));
1.481 + test(rlist[index].iBr.iY==xrect.iBr.iY || rlist[index].iBr.iY==(-rect[0].iTl.iY));
1.482 + }
1.483 + rgn1.Intersect(tmp_rgn);
1.484 + test(rgn1.Count()==4);
1.485 + rlist=rgn1.RectangleList();
1.486 + for(index=0;index<(TUint)rgn1.Count();index++)
1.487 + {
1.488 + test(rlist[index].iTl.iX==xrect.iTl.iX || rlist[index].iTl.iX==rect[0].iTl.iX);
1.489 + test(rlist[index].iTl.iY==xrect.iTl.iY || rlist[index].iTl.iY==rect[0].iTl.iY);
1.490 + test(rlist[index].iBr.iX==xrect.iBr.iX || rlist[index].iBr.iX==(-rect[0].iTl.iX));
1.491 + test(rlist[index].iBr.iY==xrect.iBr.iY || rlist[index].iBr.iY==(-rect[0].iTl.iY));
1.492 + }
1.493 + }
1.494 + rgn1.Close();
1.495 + rgn2.Close();
1.496 + tmp_rgn.Close();
1.497 + }
1.498 +
1.499 +void TestRRegion::TestUnion()
1.500 + {
1.501 + RRegion rgn1,rgn2,rgn3,tmp_rgn;
1.502 + TRect bounds, rgn1Bounds;
1.503 + TUint index;
1.504 +
1.505 + // Test RRegion (always has a dynamic buffer).
1.506 + rgn1.AddRect(xrect);
1.507 + if (!rgn1.IsEmpty())
1.508 + {
1.509 + for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
1.510 + tmp_rgn.AddRect(rect[index]);
1.511 + test(tmp_rgn.Count()==4);
1.512 + rgn1.Union(tmp_rgn);
1.513 + test(rgn1.Count()==7);
1.514 + rgn1Bounds = rgn1.BoundingRect();
1.515 + rgn2.Copy(rgn1);
1.516 + rgn2.Offset(3,5);
1.517 + rgn3.Copy(rgn1);
1.518 + rgn3.Offset(5,7);
1.519 + rgn3.Union(rgn2);
1.520 + test(rgn3.Count()==17);
1.521 + bounds=rgn1.BoundingRect();
1.522 + rgn1.Union(rgn2);
1.523 + bounds.Resize(3,5);
1.524 + test(rgn1.BoundingRect()==bounds);
1.525 + rgn1Bounds.Shrink(3,5);
1.526 + rgn1Bounds.Resize(8,12);
1.527 + test(rgn3.BoundingRect()==rgn1Bounds);
1.528 + }
1.529 + rgn1.Close();
1.530 + rgn2.Close();
1.531 + rgn3.Close();
1.532 + tmp_rgn.Close();
1.533 +
1.534 + RRegionBuf<8> rgnBuf1,rgnBuf2,rgnBuf3,tmp_rgnBuf;
1.535 +
1.536 + // Test RRegionBuf (can transform from using a static to using a dynamic buffer).
1.537 + rgnBuf1.AddRect(xrect);
1.538 + if (!rgnBuf1.IsEmpty())
1.539 + {
1.540 + for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
1.541 + tmp_rgnBuf.AddRect(rect[index]);
1.542 + test(tmp_rgnBuf.Count()==4);
1.543 + rgnBuf1.Union(tmp_rgnBuf);
1.544 + test(rgnBuf1.Count()==7);
1.545 + rgn1Bounds = rgnBuf1.BoundingRect();
1.546 + rgnBuf2.Copy(rgnBuf1);
1.547 + rgnBuf2.Offset(3,5);
1.548 + rgnBuf3.Copy(rgnBuf1);
1.549 + rgnBuf3.Offset(5,7);
1.550 + rgnBuf3.Union(rgnBuf2);
1.551 + test(rgnBuf3.Count()==17);
1.552 + bounds=rgnBuf1.BoundingRect();
1.553 + rgnBuf1.Union(rgnBuf2);
1.554 + bounds.Resize(3,5);
1.555 + test(rgnBuf1.BoundingRect()==bounds);
1.556 + rgn1Bounds.Shrink(3,5);
1.557 + rgn1Bounds.Resize(8,12);
1.558 + test(rgnBuf3.BoundingRect()==rgn1Bounds);
1.559 + }
1.560 + rgnBuf1.Close();
1.561 + rgnBuf2.Close();
1.562 + rgnBuf3.Close();
1.563 + tmp_rgnBuf.Close();
1.564 + }
1.565 +
1.566 +void TestRRegion::TestClipRect()
1.567 + {
1.568 + RRegion rgn1(xrect);
1.569 + if (!rgn1.IsEmpty())
1.570 + {
1.571 + TUint index;
1.572 + for(index=0;index<(sizeof(rect)/sizeof(rect[0]));index++)
1.573 + rgn1.AddRect(rect[index]);
1.574 + TRect clip=rgn1.BoundingRect();
1.575 + rgn1.ClipRect(clip);
1.576 + test(clip==rgn1.BoundingRect());
1.577 + clip.iTl.iX>>=1;
1.578 + clip.iTl.iY>>=1;
1.579 + clip.iBr.iX>>=1;
1.580 + clip.iBr.iY>>=1;
1.581 + rgn1.ClipRect(clip);
1.582 + test(clip==rgn1.BoundingRect());
1.583 + clip.iTl.iX=clip.iBr.iX;
1.584 + rgn1.ClipRect(clip);
1.585 + test(rgn1.Count()==0);
1.586 + }
1.587 + rgn1.Close();
1.588 + }
1.589 +
1.590 +void TestRRegion::TestContains()
1.591 + {
1.592 + RRegion rgn;
1.593 + rgn.AddRect(TRect(10,10,20,20));
1.594 + rgn.AddRect(TRect(10,20,50,30));
1.595 + test(rgn.Contains(TPoint(10,10)));
1.596 + test(rgn.Contains(TPoint(49,29)));
1.597 + test(rgn.Contains(TPoint(15,15)));
1.598 + test(rgn.Contains(TPoint(31,22)));
1.599 + test(rgn.Contains(TPoint(50,30))==EFalse);
1.600 + test(rgn.Contains(TPoint(-100,-30))==EFalse);
1.601 + test(rgn.Contains(TPoint(200,20))==EFalse);
1.602 + test(rgn.Contains(TPoint(15,30000))==EFalse);
1.603 + rgn.Close();
1.604 + TRegionFix<1> rgn2(TRect(20,20,25,25));
1.605 + test(rgn2.Contains(TPoint(20,20)));
1.606 + test(rgn2.Contains(TPoint(22,23)));
1.607 + test(rgn2.Contains(TPoint(0,0))==EFalse);
1.608 + test(rgn2.Contains(TPoint(25,25))==EFalse);
1.609 + test(rgn2.Contains(TPoint(30,30))==EFalse);
1.610 + }
1.611 +
1.612 +void TestRRegion::TestIntersects()
1.613 + {
1.614 + RRegion rgn;
1.615 + rgn.AddRect(TRect(10,10,20,20));
1.616 + rgn.AddRect(TRect(10,20,50,30));
1.617 + test(rgn.Intersects(TRect(10,10,20,20)));
1.618 + test(rgn.Intersects(TRect(5,5,15,15)));
1.619 + test(rgn.Intersects(TRect(10,20,50,30)));
1.620 + test(rgn.Intersects(TRect(10,10,20,20)));
1.621 + test(rgn.Intersects(TRect(40,10,60,30)));
1.622 + test(rgn.Intersects(TRect(0,0,10,10))==EFalse);
1.623 + test(rgn.Intersects(TRect(30,10,40,20))==EFalse);
1.624 + rgn.Close();
1.625 + TRegionFix<1> rgn2(TRect(20,20,30,30));
1.626 + test(rgn2.Intersects(TRect(20,20,30,30)));
1.627 + test(rgn2.Intersects(TRect(15,25,25,35)));
1.628 + test(rgn2.Intersects(TRect(25,15,35,25)));
1.629 + test(rgn2.Intersects(TRect(15,15,25,25)));
1.630 + test(rgn2.Intersects(TRect(25,25,35,35)));
1.631 + test(rgn2.Intersects(TRect(10,20,20,30))==EFalse);
1.632 + test(rgn2.Intersects(TRect(30,20,40,30))==EFalse);
1.633 + test(rgn2.Intersects(TRect(20,10,30,20))==EFalse);
1.634 + test(rgn2.Intersects(TRect(20,30,30,40))==EFalse);
1.635 + // empty rectangles
1.636 + test(rgn2.Intersects(TRect(30,30,20,20))==EFalse);
1.637 + TRegionFix<1> rgn3(TRect(30,30,20,20));
1.638 + test(rgn3.Intersects(TRect(30,30,20,20))==EFalse);
1.639 + test(rgn3.Intersects(TRect(20,20,30,30))==EFalse);
1.640 + }
1.641 +
1.642 +void TestRRegion::TestErrors()
1.643 + {
1.644 + RRegion rgnErr,rgnErr2;
1.645 + RRegion rgn;
1.646 + TRect rect(1,2,3,4),rect2;
1.647 + const TRect* rList;
1.648 + TPoint pnt;
1.649 +
1.650 + rgnErr.ForceError();
1.651 + rgn.AddRect(rect);
1.652 + rgnErr.Copy(rgn);
1.653 + test(rgnErr.CheckError()==EFalse);
1.654 + test(rgnErr.Count()==1);
1.655 +
1.656 + rgnErr.ForceError();
1.657 + test(rgnErr.CheckError()==TRUE);
1.658 + rgnErr.AddRect(rect);
1.659 + rgnErr.AddRect(TRect(2,3,4,5));
1.660 + test(rgnErr.CheckError()==TRUE);
1.661 + rgnErr.SubRect(rect);
1.662 + test(rgnErr.CheckError()==TRUE);
1.663 + rgn.Copy(rgnErr);
1.664 + test(rgn.CheckError()==TRUE);
1.665 + rgnErr.Offset(1,2);
1.666 + rgnErr.Offset(pnt);
1.667 + test(rgnErr.CheckError()==TRUE);
1.668 +
1.669 + rgn.Union(rgnErr);
1.670 + test(rgn.CheckError()==TRUE);
1.671 + rgnErr.AddRect(rect);
1.672 + test(rgnErr.CheckError()==TRUE);
1.673 + rgn.Clear();
1.674 + rgn.AddRect(rect);
1.675 + rgnErr.Union(rgn);
1.676 + test(rgnErr.CheckError()==TRUE);
1.677 + rgn.Clear();
1.678 + test(rgn.CheckError()==FALSE);
1.679 +
1.680 + rgnErr2.Clear();
1.681 + rgnErr2.AddRect(rect);
1.682 + rgn.Intersection(rgnErr,rgnErr2);
1.683 + test(rgn.CheckError()==TRUE);
1.684 + rgn.Clear();
1.685 + rgn.Intersection(rgnErr2,rgnErr);
1.686 + test(rgn.CheckError()==TRUE);
1.687 + rgnErr2.ForceError();
1.688 + rgn.Clear();
1.689 + rgn.Intersection(rgnErr2,rgnErr);
1.690 + test(rgn.CheckError()==TRUE);
1.691 + rgn.Clear();
1.692 + rgn.AddRect(rect);
1.693 + rgn.Intersect(rgnErr);
1.694 + test(rgn.CheckError()==TRUE);
1.695 + rgn.Clear();
1.696 + rgn.AddRect(rect);
1.697 + rgnErr.Intersect(rgn);
1.698 + test(rgnErr.CheckError()==TRUE);
1.699 + test(rgn.CheckError()==FALSE);
1.700 +
1.701 + test(rgnErr.IsEmpty()==FALSE);
1.702 +
1.703 + rgn.Clear();
1.704 + rgn.AddRect(rect);
1.705 + rgn.SubRegion(rgnErr);
1.706 + test(rgn.CheckError()==TRUE);
1.707 + test(rgnErr.CheckError()==TRUE);
1.708 +
1.709 + rgnErr.ClipRect(rect);
1.710 + test(rgnErr.CheckError()==TRUE);
1.711 + rgnErr.Tidy();
1.712 + test(rgnErr.CheckError()==TRUE);
1.713 + rect2=rgnErr.BoundingRect();
1.714 + test(rect2.iTl.iX==0 && rect2.iBr.iY==0);
1.715 +
1.716 + test(rgnErr.Count()==0);
1.717 + rList=rgnErr.RectangleList();
1.718 + rgn.Close();
1.719 + rgnErr.Close();
1.720 + rgnErr2.Close();
1.721 + }
1.722 +
1.723 +void TestRRegion::doTestSort(RRegion &rgn)
1.724 + {
1.725 + TInt loop,loop2;
1.726 + TRect const rlist[8]={ // 8 Rectangles that form a square with the centre rectangle missing
1.727 + TRect(20,30,30,40),
1.728 + TRect(20,10,30,20),
1.729 + TRect(10,20,20,30),
1.730 + TRect(30,20,40,30),
1.731 + TRect(10,30,20,40),
1.732 + TRect(30,30,40,40),
1.733 + TRect(10,10,20,20),
1.734 + TRect(30,10,40,20)};
1.735 + TRect sorted[8];
1.736 + TRect const* plist;
1.737 + TRect tmp;
1.738 +
1.739 +// Work out wot the sorted list should be
1.740 + for(loop=0;loop<8;loop++)
1.741 + sorted[loop]=rlist[loop];
1.742 + for(loop=0;loop<8;loop++)
1.743 + {
1.744 +restart:
1.745 + for(loop2=loop+1;loop2<8;loop2++)
1.746 + {
1.747 + if (sorted[loop2].iTl.iY>sorted[loop].iTl.iY)
1.748 + continue;
1.749 + if (sorted[loop2].iTl.iY==sorted[loop].iTl.iY && sorted[loop2].iTl.iX>sorted[loop].iTl.iX)
1.750 + continue;
1.751 + tmp=sorted[loop];
1.752 + sorted[loop]=sorted[loop2];
1.753 + sorted[loop2]=tmp;
1.754 + goto restart;
1.755 + }
1.756 + }
1.757 + for(loop=0;loop<8;loop++)
1.758 + rgn.AddRect(rlist[loop]);
1.759 + rgn.Sort();
1.760 + plist=rgn.RectangleList();
1.761 + for(loop=0;loop<8;loop++)
1.762 + test(plist[loop]==sorted[loop]);
1.763 + rgn.Close();
1.764 + }
1.765 +
1.766 +void TestRRegion::TestSort()
1.767 + {
1.768 + RRegion rgn;
1.769 + doTestSort(rgn);
1.770 + RRegionBuf<1> rgnBuf;
1.771 + doTestSort(rgnBuf);
1.772 + }
1.773 +
1.774 +void TestRRegion::doTestRegionBuf(RRegion &aRegion)
1.775 + {
1.776 + for(TInt index=0;index<8;index++)
1.777 + {
1.778 + aRegion.AddRect(TRect(index*2,index*2,index*2+2,index*2+2));
1.779 + test(aRegion.Count()==(index+1));
1.780 + const TRect *pr=aRegion.RectangleList();
1.781 + for(TInt index2=0;index2<=index;index2++)
1.782 + test(pr[index2]==TRect(index2*2,index2*2,index2*2+2,index2*2+2));
1.783 + }
1.784 + aRegion.Close();
1.785 + }
1.786 +
1.787 +void TestRRegion::TestRegionBuf()
1.788 + {
1.789 + RRegionBuf<1> rgn(TRect(1,2,3,4));
1.790 + test(rgn[0]==TRect(1,2,3,4));
1.791 + RRegionBuf<1> rgn1;
1.792 + doTestRegionBuf(rgn1);
1.793 + RRegionBuf<2> rgn2;
1.794 + doTestRegionBuf(rgn2);
1.795 + RRegionBuf<5> rgn5;
1.796 + doTestRegionBuf(rgn5);
1.797 + RRegionBuf<10> rgn10;
1.798 + doTestRegionBuf(rgn10);
1.799 + }
1.800 +
1.801 +// Top level test code
1.802 +LOCAL_C void test_region(TestRRegion t)
1.803 + {
1.804 + test.Start(_L("Setting values"));
1.805 + t.TestSet();
1.806 + test.Next(_L("TRegionFix"));
1.807 + t.TestRegionFix();
1.808 + test.Next(_L("AddRect"));
1.809 + t.TestAddRect();
1.810 + test.Next(_L("SubRect"));
1.811 + t.TestSubRect();
1.812 + test.Next(_L("SubRegion"));
1.813 + t.TestSubRegion();
1.814 + test.Next(_L("Tidy"));
1.815 + t.TestTidy();
1.816 + test.Next(_L("Spare"));
1.817 + t.TestSpare();
1.818 + test.Next(_L("Offset"));
1.819 + t.TestOffset();
1.820 + test.Next(_L("Intersection"));
1.821 + t.TestIntersection();
1.822 + test.Next(_L("Union"));
1.823 + t.TestUnion();
1.824 + test.Next(_L("Clip rect"));
1.825 + t.TestClipRect();
1.826 + test.Next(_L("Contains"));
1.827 + t.TestContains();
1.828 + test.Next(_L("Intersects"));
1.829 + t.TestIntersects();
1.830 + test.Next(_L("Errors"));
1.831 + t.TestErrors();
1.832 + test.Next(_L("Sort"));
1.833 + t.TestSort();
1.834 + test.Next(_L("RegionBuf"));
1.835 + t.TestRegionBuf();
1.836 + test.End();
1.837 + }
1.838 +
1.839 +GLDEF_C TInt E32Main()
1.840 +//
1.841 +// Main
1.842 +//
1.843 + {
1.844 +
1.845 + test.Title();
1.846 + __UHEAP_MARK;
1.847 + TestRRegion t1(10,20,30,40);
1.848 + TestRRegion t2(0,0,1,1);
1.849 + TestRRegion t3(1,1,1,1);
1.850 + TestRRegion t4(1000000,2000000,3000000,4000000);
1.851 +
1.852 + test.Start(_L("class RRegion 1"));
1.853 + test_region(t1);
1.854 + test.Next(_L("class RRegion 2"));
1.855 + test_region(t2);
1.856 + test.Next(_L("class RRegion 3"));
1.857 + test_region(t3);
1.858 + test.Next(_L("class RRegion 4"));
1.859 + test_region(t4);
1.860 + test.End();
1.861 + __UHEAP_MARKEND;
1.862 + return(0);
1.863 + }
1.864 +
1.865 +