Add final test for BPolygon, update Marc's BPolygon implementation a bit

and integrate it into the build.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@2136 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
jrand 2002-12-03 04:07:21 +00:00
parent 1a72f541b5
commit eae4530961
6 changed files with 207 additions and 26 deletions

View File

@ -38,13 +38,30 @@
// Globals ---------------------------------------------------------------------
//------------------------------------------------------------------------------
BPolygon::BPolygon(const BPoint *ptArray, int32 numPoints)
BPolygon::BPolygon(const BPoint *ptArray, int32 numPoints) :
fBounds(0.0, 0.0, 0.0, 0.0), fCount(numPoints), fPts(NULL)
{
fCount = numPoints;
fPts = new BPoint[numPoints];
memcpy(fPts, ptArray, numPoints * sizeof(BPoint));
compute_bounds();
if (fCount > 0) {
fPts = new BPoint[numPoints];
// Note the use of memcpy here. The assumption is that an array of BPoints can
// be copied bit by bit and not use a copy constructor or an assignment
// operator. This breaks the containment of BPoint but will result in better
// performance. An example where the memcpy will fail would be if BPoint begins
// to do lazy copying through reference counting. By copying the bits, we will
// copy reference counting state which will not be relevant at the destination.
// Luckily, BPoint is a very simple class which isn't likely to change much.
// However, it is a risk of this implementation.
//
// If necessary, this code can be changed to iterate over the input array of
// BPoints and use the assignment operator to copy from the source to the
// destination array, one element at a time.
//
// Similar use of memcpy appears later in this implementation also.
//
memcpy(fPts, ptArray, numPoints * sizeof(BPoint));
compute_bounds();
}
}
//------------------------------------------------------------------------------
BPolygon::BPolygon(const BPolygon *poly)
@ -53,7 +70,8 @@ BPolygon::BPolygon(const BPolygon *poly)
}
//------------------------------------------------------------------------------
BPolygon::BPolygon ()
: fCount(0),
: fBounds(0.0, 0.0, 0.0, 0.0),
fCount(0),
fPts(NULL)
{
}
@ -66,11 +84,15 @@ BPolygon::~BPolygon ()
//------------------------------------------------------------------------------
BPolygon &BPolygon::operator=(const BPolygon &from)
{
fBounds = from.fBounds;
fCount = from.fCount;
fPts = new BPoint[fCount];
memcpy(fPts, from.fPts, fCount * sizeof(BPoint));
// Make sure we aren't trying to perform a "self assignment".
if (this != &from) {
fBounds = from.fBounds;
fCount = from.fCount;
if (fCount > 0) {
fPts = new BPoint[fCount];
memcpy(fPts, from.fPts, fCount * sizeof(BPoint));
}
}
return *this;
}
//------------------------------------------------------------------------------
@ -81,20 +103,17 @@ BRect BPolygon::Frame() const
//------------------------------------------------------------------------------
void BPolygon::AddPoints(const BPoint *ptArray, int32 numPoints)
{
BPoint *newPts = new BPoint[fCount + numPoints];
if (fPts)
{
memcpy(newPts, fPts, fCount);
delete fPts;
if (numPoints > 0) {
BPoint *newPts = new BPoint[fCount + numPoints];
if (fPts) {
memcpy(newPts, fPts, fCount * sizeof(BPoint));
delete fPts;
}
memcpy(newPts + fCount, ptArray, numPoints * sizeof(BPoint));
fPts = newPts;
fCount += numPoints;
compute_bounds();
}
memcpy(newPts + fCount, ptArray, numPoints);
fPts = newPts;
fCount += numPoints;
compute_bounds();
}
//------------------------------------------------------------------------------
int32 BPolygon::CountPoints() const
@ -117,8 +136,10 @@ void BPolygon::PrintToStream () const
//------------------------------------------------------------------------------
void BPolygon::compute_bounds()
{
if (fCount == 0)
if (fCount == 0) {
fBounds = BRect(0.0, 0.0, 0.0, 0.0);
return;
}
fBounds = BRect(fPts[0], fPts[0]);

View File

@ -9,6 +9,7 @@ INTERFACE_KIT_SOURCE =
Font.cpp
PictureButton.cpp
Point.cpp
Polygon.cpp
RadioButton.cpp
Rect.cpp
StatusBar.cpp

View File

@ -27,6 +27,7 @@ CommonTestLib libinterfacetest.so
PolygonTest.cpp
DummyPolygon.cpp
CreatePolygonTest.cpp
MapPolygonTest.cpp
: <boot!home!config!lib>libopenbeos.so
be stdc++.r4

View File

@ -0,0 +1,115 @@
/*
$Id: MapPolygonTest.cpp,v 1.1 2002/12/03 04:07:20 jrand Exp $
This file contains the implementation of the tests which show
that BPolygons can be created successfully a number of
different ways. The following use cases are tested:
- Map To
*/
#include "MapPolygonTest.h"
#include "DummyPolygon.h"
#include <Point.h>
#include <Rect.h>
#include <Polygon.h>
/*
* Method: MapPolygonTest::MapPolygonTest()
* Descr: This is the constructor for this class.
*/
MapPolygonTest::MapPolygonTest(std::string name) :
TestCase(name)
{
}
/*
* Method: MapPolygonTest::~MapPolygonTest()
* Descr: This is the destructor for this class.
*/
MapPolygonTest::~MapPolygonTest()
{
}
/*
* Method: MapPolygonTest::polygonMatches()
* Descr: This member function compares the passed in polygon to the
* set of points and the expected frame rectangle.
*/
void MapPolygonTest::polygonMatches(BPolygon *srcPoly, const BPoint *pointArray,
int numPoints, const BRect expectedFrame)
{
assert(numPoints == srcPoly->CountPoints());
assert(expectedFrame == srcPoly->Frame());
const BPoint *srcPointArray = ((DummyPolygon *)srcPoly)->GetPoints();
int i;
for(i = 0; i < numPoints; i++) {
assert(srcPointArray[i] == pointArray[i]);
}
}
/*
* Method: MapPolygonTest::PerformTest()
* Descr: This member function tests the creation of BPolygon's.
*
* The code does the following:
*/
void MapPolygonTest::PerformTest(void)
{
const int numPoints = 7;
BPoint pointArray[numPoints];
pointArray[0].x = 0.0; pointArray[0].y = 10.0;
pointArray[1].x = 10.0; pointArray[1].y = 0.0;
pointArray[2].x = 10.0; pointArray[2].y = 5.0;
pointArray[3].x = 30.0; pointArray[3].y = 5.0;
pointArray[4].x = 30.0; pointArray[4].y = 15.0;
pointArray[5].x = 10.0; pointArray[5].y = 15.0;
pointArray[6].x = 10.0; pointArray[6].y = 20.0;
BRect pointArrayFrame(0.0, 0.0, 30.0, 20.0);
BRect fromRect(1.0, 1.0, 3.0, 3.0);
BRect toRect(1.0, 4.0, 5.0, 10.0);
BPoint mapPointArray[numPoints];
mapPointArray[0].x = -1.0; mapPointArray[0].y = 31.0;
mapPointArray[1].x = 19.0; mapPointArray[1].y = 1.0;
mapPointArray[2].x = 19.0; mapPointArray[2].y = 16.0;
mapPointArray[3].x = 59.0; mapPointArray[3].y = 16.0;
mapPointArray[4].x = 59.0; mapPointArray[4].y = 46.0;
mapPointArray[5].x = 19.0; mapPointArray[5].y = 46.0;
mapPointArray[6].x = 19.0; mapPointArray[6].y = 61.0;
BRect mapPointArrayFrame(-1.0, 1.0, 59.0, 61.0);
BPolygon testPoly1(pointArray, numPoints);
polygonMatches(&testPoly1, pointArray, numPoints, pointArrayFrame);
testPoly1.MapTo(fromRect, toRect);
polygonMatches(&testPoly1, mapPointArray, numPoints, mapPointArrayFrame);
}
/*
* Method: PropertyConstructionTest::suite()
* Descr: This static member function returns a test caller for performing
* all combinations of "MapPolygonTest".
*/
Test *MapPolygonTest::suite(void)
{
typedef CppUnit::TestCaller<MapPolygonTest>
MapPolygonTestCaller;
return(new MapPolygonTestCaller("BPolygon::Map Polygon Test", &MapPolygonTest::PerformTest));
}

View File

@ -0,0 +1,41 @@
/*
$Id: MapPolygonTest.h,v 1.1 2002/12/03 04:07:21 jrand Exp $
This file contains the definition of a class for testing the
mapping of BPolygon objects.
*/
#ifndef MapPolygonTest_H
#define MapPolygonTest_H
#include "../common.h"
class BPolygon;
class BPoint;
class BRect;
class MapPolygonTest :
public TestCase {
private:
void polygonMatches(BPolygon *, const BPoint *, int numPoints, BRect);
protected:
public:
static Test *suite(void);
void PerformTest(void);
MapPolygonTest(std::string name = "");
virtual ~MapPolygonTest();
};
#endif

View File

@ -1,11 +1,13 @@
#include "../common.h"
#include "CreatePolygonTest.h"
#include "MapPolygonTest.h"
Test *PolygonTestSuite()
{
TestSuite *testSuite = new TestSuite();
testSuite->addTest(CreatePolygonTest::suite());
testSuite->addTest(MapPolygonTest::suite());
return(testSuite);
}