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