xref: /haiku/src/tests/kits/interface/bpolygon/CreatePolygonTest.cpp (revision f7081347c543247e2c28d08eb3b036ad49b42e22)
1*f7081347Sjrand /*
2*f7081347Sjrand 	$Id: CreatePolygonTest.cpp,v 1.1 2002/11/23 04:42:57 jrand Exp $
3*f7081347Sjrand 
4*f7081347Sjrand 	This file contains the implementation of the tests which show
5*f7081347Sjrand 	that BPolygons can be created successfully a number of
6*f7081347Sjrand 	different ways.  The following use cases are tested:
7*f7081347Sjrand 		- Construction 1
8*f7081347Sjrand 		- Construction 2
9*f7081347Sjrand 		- Construction 3
10*f7081347Sjrand 		- Destruction
11*f7081347Sjrand 		- Add Points
12*f7081347Sjrand 		- Count Points
13*f7081347Sjrand 		- Frame
14*f7081347Sjrand 		- Assignment Operator
15*f7081347Sjrand 
16*f7081347Sjrand 	*/
17*f7081347Sjrand 
18*f7081347Sjrand 
19*f7081347Sjrand #include "CreatePolygonTest.h"
20*f7081347Sjrand #include "DummyPolygon.h"
21*f7081347Sjrand #include <Point.h>
22*f7081347Sjrand #include <Rect.h>
23*f7081347Sjrand #include <Polygon.h>
24*f7081347Sjrand 
25*f7081347Sjrand 
26*f7081347Sjrand /*
27*f7081347Sjrand  *  Method:  CreatePolygonTest::CreatePolygonTest()
28*f7081347Sjrand  *   Descr:  This is the constructor for this class.
29*f7081347Sjrand  */
30*f7081347Sjrand 
31*f7081347Sjrand 
32*f7081347Sjrand 	CreatePolygonTest::CreatePolygonTest(std::string name) :
33*f7081347Sjrand 		TestCase(name)
34*f7081347Sjrand {
35*f7081347Sjrand 	}
36*f7081347Sjrand 
37*f7081347Sjrand 
38*f7081347Sjrand /*
39*f7081347Sjrand  *  Method:  CreatePolygonTest::~CreatePolygonTest()
40*f7081347Sjrand  *   Descr:  This is the destructor for this class.
41*f7081347Sjrand  */
42*f7081347Sjrand 
43*f7081347Sjrand 
44*f7081347Sjrand 	CreatePolygonTest::~CreatePolygonTest()
45*f7081347Sjrand {
46*f7081347Sjrand 	}
47*f7081347Sjrand 
48*f7081347Sjrand 
49*f7081347Sjrand /*
50*f7081347Sjrand  *  Method:  CreatePolygonTest::polygonMatches()
51*f7081347Sjrand  *   Descr:  This member function compares the passed in polygon to the
52*f7081347Sjrand  *           set of points and the expected frame rectangle.
53*f7081347Sjrand  */
54*f7081347Sjrand 
55*f7081347Sjrand void CreatePolygonTest::polygonMatches(BPolygon *srcPoly, const BPoint *pointArray,
56*f7081347Sjrand                                        int numPoints, const BRect expectedFrame)
57*f7081347Sjrand {
58*f7081347Sjrand 	assert(numPoints == srcPoly->CountPoints());
59*f7081347Sjrand 	assert(expectedFrame == srcPoly->Frame());
60*f7081347Sjrand 
61*f7081347Sjrand 	const BPoint *srcPointArray = ((DummyPolygon *)srcPoly)->GetPoints();
62*f7081347Sjrand 	int i;
63*f7081347Sjrand 	for(i = 0; i < numPoints; i++) {
64*f7081347Sjrand 		assert(srcPointArray[i] == pointArray[i]);
65*f7081347Sjrand 	}
66*f7081347Sjrand }
67*f7081347Sjrand 
68*f7081347Sjrand 
69*f7081347Sjrand /*
70*f7081347Sjrand  *  Method:  CreatePolygonTest::PerformTest()
71*f7081347Sjrand  *   Descr:  This member function tests the creation of BPolygon's.
72*f7081347Sjrand  *
73*f7081347Sjrand  *           The code does the following:
74*f7081347Sjrand  *             - creates an array of points to test with
75*f7081347Sjrand  *             - creates five polygons which should all be the same but in
76*f7081347Sjrand  *               different ways:
77*f7081347Sjrand  *                  1. By passing the point array in the constructor
78*f7081347Sjrand  *                  2. By using the copy constructor from the first polygon
79*f7081347Sjrand  *                  3. By using the AddPoints() member to add points to an
80*f7081347Sjrand  *                     empty polygon
81*f7081347Sjrand  *                  4. By using the assignment operator to replace an existing
82*f7081347Sjrand  *                     polygon with a copy of the one from 1
83*f7081347Sjrand  *                  5. By adding three points from the array on the constructor
84*f7081347Sjrand  *                     and then adding th remaining points using the AddPoints()
85*f7081347Sjrand  *                     member.
86*f7081347Sjrand  *              - In each case, the polygonMatches() member is called to make
87*f7081347Sjrand  *                sure the polygon is what is expected.
88*f7081347Sjrand  */
89*f7081347Sjrand 
90*f7081347Sjrand 
91*f7081347Sjrand 	void CreatePolygonTest::PerformTest(void)
92*f7081347Sjrand {
93*f7081347Sjrand 	const int numPoints = 7;
94*f7081347Sjrand 	BPoint pointArray[numPoints];
95*f7081347Sjrand 	pointArray[0].x =  0.0;  pointArray[0].y = 10.0;
96*f7081347Sjrand 	pointArray[1].x = 10.0;  pointArray[1].y =  0.0;
97*f7081347Sjrand 	pointArray[2].x = 10.0;  pointArray[2].y =  5.0;
98*f7081347Sjrand 	pointArray[3].x = 30.0;  pointArray[3].y =  5.0;
99*f7081347Sjrand 	pointArray[4].x = 30.0;  pointArray[4].y = 15.0;
100*f7081347Sjrand 	pointArray[5].x = 10.0;  pointArray[5].y = 15.0;
101*f7081347Sjrand 	pointArray[6].x = 10.0;  pointArray[6].y = 20.0;
102*f7081347Sjrand 	BRect pointArrayFrame(0.0, 0.0, 30.0, 20.0);
103*f7081347Sjrand 
104*f7081347Sjrand 	BPolygon testPoly1(pointArray, numPoints);
105*f7081347Sjrand 	polygonMatches(&testPoly1, pointArray, numPoints, pointArrayFrame);
106*f7081347Sjrand 
107*f7081347Sjrand 	BPolygon testPoly2(&testPoly1);
108*f7081347Sjrand 	polygonMatches(&testPoly2, pointArray, numPoints, pointArrayFrame);
109*f7081347Sjrand 
110*f7081347Sjrand 	BPolygon testPoly3;
111*f7081347Sjrand 	testPoly3.AddPoints(pointArray, numPoints);
112*f7081347Sjrand 	polygonMatches(&testPoly3, pointArray, numPoints, pointArrayFrame);
113*f7081347Sjrand 
114*f7081347Sjrand 	BPolygon testPoly4;
115*f7081347Sjrand 	testPoly4.AddPoints(&pointArray[2], 2);
116*f7081347Sjrand 	testPoly4 = testPoly1;
117*f7081347Sjrand 	polygonMatches(&testPoly4, pointArray, numPoints, pointArrayFrame);
118*f7081347Sjrand 
119*f7081347Sjrand 	BPolygon testPoly5(pointArray, 3);
120*f7081347Sjrand 	testPoly5.AddPoints(&pointArray[3], numPoints - 3);
121*f7081347Sjrand 	polygonMatches(&testPoly5, pointArray, numPoints, pointArrayFrame);
122*f7081347Sjrand }
123*f7081347Sjrand 
124*f7081347Sjrand 
125*f7081347Sjrand /*
126*f7081347Sjrand  *  Method:  PropertyConstructionTest::suite()
127*f7081347Sjrand  *   Descr:  This static member function returns a test caller for performing
128*f7081347Sjrand  *           all combinations of "CreatePolygonTest".
129*f7081347Sjrand  */
130*f7081347Sjrand 
131*f7081347Sjrand  Test *CreatePolygonTest::suite(void)
132*f7081347Sjrand {
133*f7081347Sjrand 	typedef CppUnit::TestCaller<CreatePolygonTest>
134*f7081347Sjrand 		CreatePolygonTestCaller;
135*f7081347Sjrand 
136*f7081347Sjrand 	return(new CreatePolygonTestCaller("BPolygon::Create Polygon Test", &CreatePolygonTest::PerformTest));
137*f7081347Sjrand 	}
138