1 /* 2 $Id: RegionTestcase.cpp,v 1.1 2003/08/06 06:46:06 jackburton Exp $ 3 4 This file implements a base class for all tests of the OpenBeOS 5 BRegion code. 6 7 */ 8 9 10 #include "RegionTestcase.h" 11 #include <Region.h> 12 #include <Rect.h> 13 #include <math.h> 14 15 16 /* 17 * Method: RegionTestcase::RegionTestcase() 18 * Descr: This is the constructor for this class. 19 */ 20 21 RegionTestcase::RegionTestcase(std::string name) : 22 TestCase(name) 23 { 24 const int numTestRegions = 5; 25 const int numRectsPerRegion = 3; 26 27 float theRegions[numTestRegions][numRectsPerRegion][4] = 28 { 29 { 30 {10.0, 10.0, 50.0, 50.0}, 31 {25.0, 10.0, 75.0, 40.0}, 32 {70.0, 100.0, 90.0, 120.0} 33 }, 34 { 35 {15.0, 15.0, 45.0, 45.0}, 36 {30.0, 15.0, 70.0, 35.0}, 37 {75.0, 105.0, 85.0, 115.0} 38 }, 39 { 40 {15.0, 15.0, 55.0, 55.0}, 41 {30.0, 15.0, 80.0, 45.0}, 42 {75.0, 105.0, 95.0, 125.0} 43 }, 44 { 45 {210.0, 210.0, 250.0, 250.0}, 46 {225.0, 210.0, 275.0, 240.0}, 47 {270.0, 300.0, 290.0, 320.0} 48 }, 49 { 50 {-50.0, -50.0, -10.0, -10.0}, 51 {-75.0, -40.0, -25.0, -10.0}, 52 {-90.0, -120.0, -70.0, -100.0} 53 } 54 }; 55 56 listOfRegions.AddItem(new BRegion); 57 for(int regionNum = 0; regionNum < numTestRegions; regionNum++) { 58 BRegion *tempRegion = new BRegion; 59 for(int rectNum = 0; rectNum < numRectsPerRegion; rectNum++) { 60 tempRegion->Include(BRect(theRegions[regionNum][rectNum][0], 61 theRegions[regionNum][rectNum][1], 62 theRegions[regionNum][rectNum][2], 63 theRegions[regionNum][rectNum][3])); 64 } 65 listOfRegions.AddItem(tempRegion); 66 } 67 } 68 69 70 /* 71 * Method: RegionTestcase::~RegionTestcase() 72 * Descr: This is the destructor for this class. 73 */ 74 75 RegionTestcase::~RegionTestcase() 76 { 77 while(!listOfRegions.IsEmpty()) { 78 delete static_cast<BRegion *>(listOfRegions.RemoveItem((long int)0)); 79 } 80 } 81 82 83 /* 84 * Method: RegionTestcase::GetPointsInRect() 85 * Descr: This member function returns an array of BPoints on the edge and 86 * inside the passed in BRect. It also returns the number of points 87 * in the array. 88 */ 89 90 int RegionTestcase::GetPointsInRect(BRect theRect, BPoint **pointArrayPtr) 91 { 92 *pointArrayPtr = pointArray; 93 if (!theRect.IsValid()) { 94 return(0); 95 } 96 97 float xIncrement = (theRect.Width() + 1.0) / (numPointsPerSide - 1); 98 float yIncrement = (theRect.Height() + 1.0) / (numPointsPerSide - 1); 99 100 int numPoints = 0; 101 102 for(int i = 0; i < numPointsPerSide; i++) { 103 float xCoord = theRect.left + (i * xIncrement); 104 if (i == numPointsPerSide - 1) { 105 xCoord = theRect.right; 106 } 107 for(int j = 0; j < numPointsPerSide; j++) { 108 float yCoord = theRect.top + (j * yIncrement); 109 if (j == numPointsPerSide - 1) { 110 yCoord = theRect.bottom; 111 } 112 pointArray[numPoints].Set(floor(xCoord), floor(yCoord)); 113 assert(theRect.Contains(pointArray[numPoints])); 114 numPoints++; 115 } 116 } 117 return(numPoints); 118 } 119 120 121 /* 122 * Method: RegionTestcase::CheckFrame() 123 * Descr: This member function checks that the BRegion's frame matches 124 * the regions contents. 125 */ 126 127 void RegionTestcase::CheckFrame(BRegion *theRegion) 128 { 129 BRect theFrame = theRegion->Frame(); 130 if (theFrame.IsValid()) { 131 assert(!RegionIsEmpty(theRegion)); 132 133 BRect testFrame = theRegion->RectAt(0); 134 assert(theFrame.Contains(testFrame)); 135 136 for(int i = 1; i < theRegion->CountRects(); i++) { 137 BRect tempRect = theRegion->RectAt(i); 138 assert(theFrame.Contains(tempRect)); 139 testFrame = testFrame | tempRect; 140 } 141 assert(testFrame == theFrame); 142 } else { 143 assert(RegionIsEmpty(theRegion)); 144 } 145 } 146 147 148 /* 149 * Method: RegionTestcase::RegionsAreEqual() 150 * Descr: This member function returns true if the two BRegion's passed 151 * in are the same, otherwise it returns false. 152 */ 153 154 bool RegionTestcase::RegionsAreEqual(BRegion *regionA, BRegion *regionB) 155 { 156 bool result = false; 157 158 if (regionA->CountRects() == regionB->CountRects()) { 159 bool gotAMatch = true; 160 for(int i = 0; i < regionA->CountRects(); i++) { 161 gotAMatch = false; 162 for(int j = 0; j < regionB->CountRects(); j++) { 163 if (regionA->RectAt(i) == regionB->RectAt(j)) { 164 gotAMatch = true; 165 break; 166 } 167 } 168 if (!gotAMatch) { 169 break; 170 } 171 } 172 if (gotAMatch) { 173 result = true; 174 } 175 } 176 177 if (!result) { 178 BRegion tempRegion(*regionA); 179 180 tempRegion.Exclude(regionB); 181 if (RegionIsEmpty(&tempRegion)) { 182 tempRegion = *regionB; 183 tempRegion.Exclude(regionA); 184 if (RegionIsEmpty(&tempRegion)) { 185 result = true; 186 } 187 } 188 } 189 190 if (result) { 191 assert(regionA->Frame() == regionB->Frame()); 192 if (regionA->CountRects() == 0) { 193 assert(RegionIsEmpty(regionA)); 194 assert(RegionIsEmpty(regionB)); 195 } 196 } 197 return(result); 198 } 199 200 201 /* 202 * Method: RegionTestcase::RegionsIsEmpty() 203 * Descr: This member function returns true if the BRegion passed 204 * in is an empty region, otherwise it returns false. 205 */ 206 207 bool RegionTestcase::RegionIsEmpty(BRegion *theRegion) 208 { 209 if (theRegion->CountRects() == 0) { 210 assert(!theRegion->Frame().IsValid()); 211 return(true); 212 } 213 assert(theRegion->Frame().IsValid()); 214 return(false); 215 } 216 217 218 /* 219 * Method: RegionTestcase::PerformTest() 220 * Descr: This member function iterates over the set of BRegion's for 221 * testing and calls testOneRegion() for each region. Then it 222 * calls testTwoRegions() for each pair of regions (including 223 * when the two regions are the same). 224 */ 225 226 void RegionTestcase::PerformTest(void) 227 { 228 int numItems = listOfRegions.CountItems(); 229 230 for(int i = 0; i < numItems; i++) { 231 BRegion *testRegion = static_cast<BRegion *>(listOfRegions.ItemAt(i)); 232 233 CheckFrame(testRegion); 234 testOneRegion(testRegion); 235 } 236 for(int i = 0; i < numItems; i++) { 237 BRegion *testRegionA = static_cast<BRegion *>(listOfRegions.ItemAt(i)); 238 239 for(int j = 0; j < numItems; j++) { 240 BRegion *testRegionB = static_cast<BRegion *>(listOfRegions.ItemAt(j)); 241 242 testTwoRegions(testRegionA, testRegionB); 243 } 244 } 245 } 246