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