Blame tests/test-matrix.cc

Packit 908522
#include <boost/test/unit_test.hpp>
Packit 908522
#include <boost/test/test_tools.hpp>
Packit 908522
#include <boost/test/floating_point_comparison.hpp>
Packit 908522
using namespace boost::unit_test;
Packit 908522
Packit 908522
#include <cairomm/matrix.h>
Packit 908522
Packit 908522
// this is necessary for BOOST_CHECK_EQUAL, but there's no equivalent in the C
Packit 908522
// API, so I'm reluctant to include it in cairomm right now
Packit 908522
bool operator==(const Cairo::Matrix& A, const Cairo::Matrix& B)
Packit 908522
{
Packit 908522
  return
Packit 908522
    A.xx == B.xx &&
Packit 908522
    A.yx == B.yx &&
Packit 908522
    A.xy == B.xy &&
Packit 908522
    A.yy == B.yy &&
Packit 908522
    A.x0 == B.x0 &&
Packit 908522
    A.y0 == B.y0;
Packit 908522
}
Packit 908522
Packit 908522
// this is necessary for BOOST_CHECK_EQUAL to work but doesn't seem useful
Packit 908522
// enough to put in the actual implementation
Packit 908522
std::ostream& operator<<(std::ostream& out, const Cairo::Matrix& matrix)
Packit 908522
{
Packit 908522
  return out << "[ "
Packit 908522
    << matrix.xx << ", "
Packit 908522
    << matrix.yx << ", "
Packit 908522
    << matrix.xy << ", "
Packit 908522
    << matrix.yy << ", "
Packit 908522
    << matrix.x0 << ", "
Packit 908522
    << matrix.y0 << " ]";
Packit 908522
}
Packit 908522
Packit 908522
void test_constructors()
Packit 908522
{
Packit 908522
  cairo_matrix_t c_identity;
Packit 908522
  cairo_matrix_init_identity(&c_identity);
Packit 908522
  BOOST_CHECK_EQUAL(c_identity.xx, Cairo::identity_matrix().xx);
Packit 908522
  BOOST_CHECK_EQUAL(c_identity.xy, Cairo::identity_matrix().xy);
Packit 908522
  BOOST_CHECK_EQUAL(c_identity.yx, Cairo::identity_matrix().yx);
Packit 908522
  BOOST_CHECK_EQUAL(c_identity.yy, Cairo::identity_matrix().yy);
Packit 908522
  BOOST_CHECK_EQUAL(c_identity.x0, Cairo::identity_matrix().x0);
Packit 908522
  BOOST_CHECK_EQUAL(c_identity.y0, Cairo::identity_matrix().y0);
Packit 908522
Packit 908522
  // nonsense values, just for testing
Packit 908522
  const double xx=1, yx=2, xy=3, yy=5, x0=6, y0=7;
Packit 908522
  cairo_matrix_t c_matrix;
Packit 908522
  cairo_matrix_init(&c_matrix, xx, yx, xy, yy, x0, y0);
Packit 908522
  Cairo::Matrix cpp_matrix(xx, yx, xy, yy, x0, y0);
Packit 908522
Packit 908522
  BOOST_CHECK_EQUAL(c_matrix.xx, cpp_matrix.xx);
Packit 908522
  BOOST_CHECK_EQUAL(c_matrix.xy, cpp_matrix.xy);
Packit 908522
  BOOST_CHECK_EQUAL(c_matrix.yx, cpp_matrix.yx);
Packit 908522
  BOOST_CHECK_EQUAL(c_matrix.yy, cpp_matrix.yy);
Packit 908522
  BOOST_CHECK_EQUAL(c_matrix.x0, cpp_matrix.x0);
Packit 908522
  BOOST_CHECK_EQUAL(c_matrix.y0, cpp_matrix.y0);
Packit 908522
}
Packit 908522
Packit 908522
void test_invert()
Packit 908522
{
Packit 908522
  // test a valid matrix
Packit 908522
  BOOST_CHECK_NO_THROW(Cairo::identity_matrix().invert());
Packit 908522
  // check a degenerate matrix
Packit 908522
  Cairo::Matrix degenerate(0,0,0,0,0,0);
Packit 908522
  BOOST_CHECK_THROW(degenerate.invert(), std::logic_error);
Packit 908522
}
Packit 908522
Packit 908522
cairo_matrix_t* test_matrix = nullptr;
Packit 908522
static void foo(cairo_matrix_t* matrix)
Packit 908522
{
Packit 908522
  test_matrix = matrix;
Packit 908522
}
Packit 908522
Packit 908522
void test_cast()
Packit 908522
{
Packit 908522
  // make sure that we can cast between C++ and C types without ill effect
Packit 908522
  auto matrix = Cairo::identity_matrix();
Packit 908522
  auto casted = (cairo_matrix_t) Cairo::identity_matrix();
Packit 908522
  // check that it's equal to the identity matrix
Packit 908522
  cairo_matrix_t identity;
Packit 908522
  cairo_matrix_init_identity(&identity);
Packit 908522
Packit 908522
  BOOST_CHECK_EQUAL(casted.xx, identity.xx);
Packit 908522
  BOOST_CHECK_EQUAL(casted.xy, identity.xy);
Packit 908522
  BOOST_CHECK_EQUAL(casted.yx, identity.yx);
Packit 908522
  BOOST_CHECK_EQUAL(casted.yy, identity.yy);
Packit 908522
  BOOST_CHECK_EQUAL(casted.x0, identity.x0);
Packit 908522
  BOOST_CHECK_EQUAL(casted.y0, identity.y0);
Packit 908522
Packit 908522
  // pass C++ type as an argument to C
Packit 908522
  foo(&matrix);
Packit 908522
  BOOST_CHECK_EQUAL(matrix.xx, test_matrix->xx);
Packit 908522
  BOOST_CHECK_EQUAL(matrix.xy, test_matrix->xy);
Packit 908522
  BOOST_CHECK_EQUAL(matrix.yx, test_matrix->yx);
Packit 908522
  BOOST_CHECK_EQUAL(matrix.yy, test_matrix->yy);
Packit 908522
  BOOST_CHECK_EQUAL(matrix.x0, test_matrix->x0);
Packit 908522
  BOOST_CHECK_EQUAL(matrix.y0, test_matrix->y0);
Packit 908522
}
Packit 908522
Packit 908522
void test_multiply()
Packit 908522
{
Packit 908522
  auto A = Cairo::scaling_matrix(2, 4);
Packit 908522
  auto B = Cairo::translation_matrix(5.3, 1.2);
Packit 908522
  auto C = A * B;
Packit 908522
  Cairo::Matrix D;
Packit 908522
  D.multiply(A, B);
Packit 908522
  BOOST_CHECK_EQUAL(C, D);
Packit 908522
}
Packit 908522
Packit 908522
test_suite*
Packit 908522
init_unit_test_suite(int /*argc*/, char** /*argv*/)
Packit 908522
{
Packit 908522
  test_suite* test= BOOST_TEST_SUITE( "Cairo::Matrix Tests" );
Packit 908522
Packit 908522
  test->add (BOOST_TEST_CASE (&test_constructors));
Packit 908522
  test->add (BOOST_TEST_CASE (&test_invert));
Packit 908522
  test->add (BOOST_TEST_CASE (&test_cast));
Packit 908522
  test->add (BOOST_TEST_CASE (&test_multiply));
Packit 908522
Packit 908522
  return test;
Packit 908522
}