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