xref: /haiku/src/tests/kits/interface/bregion/RegionIntersect.cpp (revision 93aeb8c3bc3f13cb1f282e3e749258a23790d947)
1 /*
2 	$Id: RegionIntersect.cpp,v 1.1 2003/08/06 06:46:06 jackburton Exp $
3 
4 	This file implements the intersect test for the OpenBeOS BRegion
5 	code.
6 
7 	*/
8 
9 
10 #include "RegionIntersect.h"
11 #include <Region.h>
12 #include <Rect.h>
13 
14 
15 /*
16  *  Method:  RegionIntersect::RegionIntersect()
17  *   Descr:  This is the constructor for this class.
18  */
19 
20 RegionIntersect::RegionIntersect(std::string name) :
21 	RegionTestcase(name)
22 {
23 	}
24 
25 
26 /*
27  *  Method:  RegionIntersect::~RegionIntersect()
28  *   Descr:  This is the destructor for this class.
29  */
30 
31 RegionIntersect::~RegionIntersect()
32 {
33 	}
34 
35 
36 /*
37  *  Method:  RegionExclude::CheckIntersect()
38  *   Descr:  This member function checks that the result region is in fact
39  *           region A with region B intersected.
40  */
41 
42 void RegionIntersect::CheckIntersect(BRegion *resultRegion, BRegion *testRegionA,
43                                      BRegion *testRegionB)
44 {
45 	bool resultRegionEmpty = RegionIsEmpty(resultRegion);
46 	bool testRegionAEmpty = RegionIsEmpty(testRegionA);
47 	bool testRegionBEmpty = RegionIsEmpty(testRegionB);
48 
49 	if (RegionsAreEqual(testRegionA, testRegionB)) {
50 		assert(RegionsAreEqual(resultRegion, testRegionA));
51 	}
52 
53 	if (RegionsAreEqual(resultRegion, testRegionA)) {
54 		BRegion tempRegion(*testRegionA);
55 		tempRegion.Include(testRegionB);
56 		assert(RegionsAreEqual(&tempRegion, testRegionB));
57 	}
58 
59 	if (RegionsAreEqual(resultRegion, testRegionB)) {
60 		BRegion tempRegion(*testRegionA);
61 		tempRegion.Include(testRegionB);
62 		assert(RegionsAreEqual(&tempRegion, testRegionA));
63 	}
64 
65 	if ((!resultRegionEmpty) && (!testRegionAEmpty) && (!testRegionBEmpty)) {
66 		BRect resultRegionFrame(resultRegion->Frame());
67 		BRect testRegionAFrame(testRegionA->Frame());
68 		BRect testRegionBFrame(testRegionB->Frame());
69 		BRect tempRect = (testRegionAFrame & testRegionBFrame);
70 
71 		assert(tempRect.Contains(resultRegionFrame));
72 	}
73 
74 	if (testRegionBEmpty) {
75 		assert(resultRegionEmpty);
76 	} else {
77 		for(int i = 0; i < testRegionB->CountRects(); i++) {
78 			BRect tempRect = testRegionB->RectAt(i);
79 
80 			assert(testRegionB->Intersects(tempRect));
81 			if (testRegionA->Intersects(tempRect)) {
82 				assert(resultRegion->Intersects(tempRect));
83 			} else {
84 				assert(!resultRegion->Intersects(tempRect));
85 			}
86 
87 			BPoint *pointArray;
88 			int numPoints = GetPointsInRect(tempRect, &pointArray);
89 			for (int j = 0; j < numPoints; j++) {
90 				assert(testRegionB->Contains(pointArray[j]));
91 				if (testRegionA->Contains(pointArray[j])) {
92 					assert(resultRegion->Contains(pointArray[j]));
93 				} else {
94 					assert(!resultRegion->Contains(pointArray[j]));
95 				}
96 			}
97 		}
98 	}
99 
100 	if (testRegionAEmpty) {
101 		assert(resultRegionEmpty);
102 	} else {
103 		for(int i = 0; i < testRegionA->CountRects(); i++) {
104 			BRect tempRect = testRegionA->RectAt(i);
105 
106 			assert(testRegionA->Intersects(tempRect));
107 			if (testRegionB->Intersects(tempRect)) {
108 				assert(resultRegion->Intersects(tempRect));
109 			} else {
110 				assert(!resultRegion->Intersects(tempRect));
111 			}
112 
113 			BPoint *pointArray;
114 			int numPoints = GetPointsInRect(tempRect, &pointArray);
115 			for (int j = 0; j < numPoints; j++) {
116 				assert(testRegionA->Contains(pointArray[j]));
117 				if (testRegionB->Contains(pointArray[j])) {
118 					assert(resultRegion->Contains(pointArray[j]));
119 				} else {
120 					assert(!resultRegion->Contains(pointArray[j]));
121 				}
122 			}
123 		}
124 	}
125 
126 	if (resultRegionEmpty) {
127 		BRegion tempRegion(*testRegionA);
128 		tempRegion.Exclude(testRegionB);
129 		assert(RegionsAreEqual(&tempRegion, testRegionA));
130 
131 		tempRegion = *testRegionB;
132 		tempRegion.Exclude(testRegionA);
133 		assert(RegionsAreEqual(&tempRegion, testRegionB));
134 	} else {
135 		for(int i = 0; i < resultRegion->CountRects(); i++) {
136 			BRect tempRect = resultRegion->RectAt(i);
137 
138 			assert(resultRegion->Intersects(tempRect));
139 			assert(testRegionA->Intersects(tempRect));
140 			assert(testRegionB->Intersects(tempRect));
141 
142 			BPoint *pointArray;
143 			int numPoints = GetPointsInRect(tempRect, &pointArray);
144 			for (int j = 0; j < numPoints; j++) {
145 				assert(resultRegion->Contains(pointArray[j]));
146 				assert(testRegionA->Contains(pointArray[j]));
147 				assert(testRegionB->Contains(pointArray[j]));
148 			}
149 		}
150 	}
151 }
152 
153 
154 /*
155  *  Method:  RegionIntersect::testOneRegion()
156  *   Descr:  This member function performs a test on a single passed in
157  *           region.
158  */
159 
160 void RegionIntersect::testOneRegion(BRegion *testRegion)
161 {
162 
163 }
164 
165 
166 /*
167  *  Method:  RegionIntersect::testTwoRegions()
168  *   Descr:  This member function performs a test on two regions passed in.
169  */
170 
171 void RegionIntersect::testTwoRegions(BRegion *testRegionA, BRegion *testRegionB)
172 {
173 	BRegion tempRegion1(*testRegionA);
174 	CheckFrame(&tempRegion1);
175 	assert(RegionsAreEqual(&tempRegion1, testRegionA));
176 
177 	tempRegion1.IntersectWith(testRegionB);
178 	CheckFrame(&tempRegion1);
179 	CheckIntersect(&tempRegion1, testRegionA, testRegionB);
180 }
181 
182 
183 /*
184  *  Method:  RegionIntersect::suite()
185  *   Descr:  This static member function returns a test caller for performing
186  *           all combinations of "RegionIntersect".
187  */
188 
189  Test *RegionIntersect::suite(void)
190 {
191 	typedef CppUnit::TestCaller<RegionIntersect>
192 		RegionIntersectCaller;
193 
194 	return(new RegionIntersectCaller("BRegion::Intersect Test", &RegionIntersect::PerformTest));
195 	}
196