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