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