wxWidgets/tests/geometry/point.cpp

120 lines
3.2 KiB
C++
Raw Normal View History

///////////////////////////////////////////////////////////////////////////////
// Name: tests/geometry/point.cpp
// Purpose: wxPoint unit test
// Author: Wlodzimierz ABX Skiba
// Created: 2004-12-14
// Copyright: (c) 2004 wxWindows
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/gdicmn.h"
#endif // WX_PRECOMP
#include "wx/math.h"
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
class PointTestCase : public CppUnit::TestCase
{
public:
PointTestCase() { }
private:
CPPUNIT_TEST_SUITE( PointTestCase );
CPPUNIT_TEST( Operators );
CPPUNIT_TEST_SUITE_END();
void Operators();
wxDECLARE_NO_COPY_CLASS(PointTestCase);
};
class RealPointTestCase : public CppUnit::TestCase
{
public:
RealPointTestCase() { }
private:
CPPUNIT_TEST_SUITE( RealPointTestCase );
CPPUNIT_TEST( Operators );
CPPUNIT_TEST_SUITE_END();
void Operators();
wxDECLARE_NO_COPY_CLASS(RealPointTestCase);
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( PointTestCase );
CPPUNIT_TEST_SUITE_REGISTRATION( RealPointTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( PointTestCase, "PointTestCase" );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RealPointTestCase, "RealPointTestCase" );
void PointTestCase::Operators()
{
wxPoint p1(1,2);
wxPoint p2(6,3);
wxPoint p3(7,5);
wxPoint p4(5,1);
wxPoint p5 = p2 + p1;
wxPoint p6 = p2 - p1;
Replace CppUnit with Catch for unit tests Drop the legacy CppUnit testing framework used for the unit tests. Replacing it with Catch has the advantage of not requiring CppUnit libraries to be installed on the system in order to be able to run tests (Catch is header-only and a copy of it is now included in the main repository itself) and, in the future, of being able to write the tests in a much more natural way. For now, however, avoid changing the existing tests code as much as [reasonably] possible to avoid introducing bugs in them and provide the CppUnit compatibility macros in the new wx/catch_cppunit.h header which allow to preserve the 99% of the existing code unchanged. Some of the required changes are: - Decompose asserts using "a && b" conditions into multiple asserts checking "a" and "b" independently. This would have been better even with CppUnit (to know which part of condition exactly failed) and is required with Catch. - Use extra parentheses around such conditions when they can't be easily decomposed in the arrays test, due to the use of macros. This is not ideal from the point of view of messages given when the tests fail but will do for now. - Rewrite asserts using "a || b" as a combination of condition checks and assert macros. Again, this is better anyhow, and is required with Catch. Incidentally, this allowed to fix a bug in the "exec" unit test which didn't leave enough time for the new process to be launched before trying to kill it. - Remove multiple CPPUNIT_TEST_SUITE_NAMED_REGISTRATION() macros, our emulation of this macro can be used only once. - Provide string conversions using Catch-specific StringMaker for a couple of types. - Replace custom wxImage comparison with a Catch-specific matcher class. - Remove most of test running logic from test.cpp, in particular don't parse command line ourselves any longer but use Catch built-in command line parser. This is a source of a minor regression: previously, both "Foo" and "FooTestCase" could be used as the name of the test to run, but now only the latter is accepted.
2017-11-01 14:15:24 -04:00
CPPUNIT_ASSERT( p3.x == p5.x );
CPPUNIT_ASSERT( p3.y == p5.y );
CPPUNIT_ASSERT( p4.x == p6.x );
CPPUNIT_ASSERT( p4.y == p6.y );
CPPUNIT_ASSERT( p3 == p5 );
CPPUNIT_ASSERT( p4 == p6 );
CPPUNIT_ASSERT( p3 != p4 );
p5 = p2; p5 += p1;
p6 = p2; p6 -= p1;
CPPUNIT_ASSERT( p3 == p5 );
CPPUNIT_ASSERT( p4 == p6 );
wxSize s(p1.x,p1.y);
p5 = p2; p5 = p2 + s;
p6 = p2; p6 = p2 - s;
CPPUNIT_ASSERT( p3 == p5 );
CPPUNIT_ASSERT( p4 == p6 );
p5 = p2; p5 = s + p2;
p6 = p2; p6 = s - p2;
CPPUNIT_ASSERT( p3 == p5 );
CPPUNIT_ASSERT( p4 == -p6 );
p5 = p2; p5 += s;
p6 = p2; p6 -= s;
CPPUNIT_ASSERT( p3 == p5 );
CPPUNIT_ASSERT( p4 == p6 );
}
void RealPointTestCase::Operators()
{
const double EPSILON = 0.00001;
wxRealPoint p1(1.2,3.4);
wxRealPoint p2(8.7,5.4);
wxRealPoint p3(9.9,8.8);
wxRealPoint p4(7.5,2.0);
wxRealPoint p5 = p2 + p1;
wxRealPoint p6 = p2 - p1;
/*
CPPUNIT_ASSERT( p3 == p5 );
CPPUNIT_ASSERT( p4 == p6 );
CPPUNIT_ASSERT( p3 != p4 );
*/
Replace CppUnit with Catch for unit tests Drop the legacy CppUnit testing framework used for the unit tests. Replacing it with Catch has the advantage of not requiring CppUnit libraries to be installed on the system in order to be able to run tests (Catch is header-only and a copy of it is now included in the main repository itself) and, in the future, of being able to write the tests in a much more natural way. For now, however, avoid changing the existing tests code as much as [reasonably] possible to avoid introducing bugs in them and provide the CppUnit compatibility macros in the new wx/catch_cppunit.h header which allow to preserve the 99% of the existing code unchanged. Some of the required changes are: - Decompose asserts using "a && b" conditions into multiple asserts checking "a" and "b" independently. This would have been better even with CppUnit (to know which part of condition exactly failed) and is required with Catch. - Use extra parentheses around such conditions when they can't be easily decomposed in the arrays test, due to the use of macros. This is not ideal from the point of view of messages given when the tests fail but will do for now. - Rewrite asserts using "a || b" as a combination of condition checks and assert macros. Again, this is better anyhow, and is required with Catch. Incidentally, this allowed to fix a bug in the "exec" unit test which didn't leave enough time for the new process to be launched before trying to kill it. - Remove multiple CPPUNIT_TEST_SUITE_NAMED_REGISTRATION() macros, our emulation of this macro can be used only once. - Provide string conversions using Catch-specific StringMaker for a couple of types. - Replace custom wxImage comparison with a Catch-specific matcher class. - Remove most of test running logic from test.cpp, in particular don't parse command line ourselves any longer but use Catch built-in command line parser. This is a source of a minor regression: previously, both "Foo" and "FooTestCase" could be used as the name of the test to run, but now only the latter is accepted.
2017-11-01 14:15:24 -04:00
CPPUNIT_ASSERT( fabs( p3.x - p5.x ) < EPSILON );
CPPUNIT_ASSERT( fabs( p3.y - p5.y ) < EPSILON );
CPPUNIT_ASSERT( fabs( p4.x - p6.x ) < EPSILON );
CPPUNIT_ASSERT( fabs( p4.y - p6.y ) < EPSILON );
}