more operators more tests

master
Marc Wäckerlin 8 years ago
parent 092d194b95
commit b9ccd5dee5
  1. 2
      COPYING
  2. 2
      INSTALL
  3. 3
      ax_cxx_compile_stdcxx_11.m4
  4. 19
      ax_init_standard_project.m4
  5. 2
      bootstrap.sh
  6. 5
      makefile_test.inc.am
  7. 170
      src/matrix.hxx
  8. 203
      test/basic.cxx

@ -1 +1 @@
/usr/share/automake-1.15/COPYING /usr/share/automake-1.14/COPYING

@ -1 +1 @@
/usr/share/automake-1.15/INSTALL /usr/share/automake-1.14/INSTALL

@ -201,15 +201,16 @@ AC_DEFUN([AX_CXX_COMPILE_STDCXX_14], [dnl
AC_MSG_ERROR([*** A compiler with support for C++14 language features is required.]) AC_MSG_ERROR([*** A compiler with support for C++14 language features is required.])
fi fi
else else
HAVE_CXX11=${HAVE_CXX14}
if test x$ac_success = xno; then if test x$ac_success = xno; then
HAVE_CXX14=0 HAVE_CXX14=0
AC_MSG_NOTICE([No compiler with C++14 support was found]) AC_MSG_NOTICE([No compiler with C++14 support was found])
AX_CXX_COMPILE_STDCXX_11([$1], [optional])
else else
HAVE_CXX14=1 HAVE_CXX14=1
AC_DEFINE(HAVE_CXX14,1, AC_DEFINE(HAVE_CXX14,1,
[define if the compiler supports basic C++14 syntax]) [define if the compiler supports basic C++14 syntax])
fi fi
HAVE_CXX11=${HAVE_CXX14}
AC_SUBST(HAVE_CXX11) AC_SUBST(HAVE_CXX11)
AC_SUBST(HAVE_CXX14) AC_SUBST(HAVE_CXX14)
fi fi

@ -129,7 +129,7 @@ AC_DEFUN([AX_SUBST], [
# m4_define(x_minor, MINOR_NUMBER) # project's minor version # m4_define(x_minor, MINOR_NUMBER) # project's minor version
# m4_include(ax_init_standard_project.m4) # m4_include(ax_init_standard_project.m4)
# AC_INIT(x_package_name, x_version, x_bugreport, x_package_name) # AC_INIT(x_package_name, x_version, x_bugreport, x_package_name)
# AM_INIT_AUTOMAKE([1.9 tar-pax]) # AM_INIT_AUTOMAKE([1.9 tar-pax parallel-tests color-tests])
# AX_INIT_STANDARD_PROJECT # AX_INIT_STANDARD_PROJECT
# #
# you change nothing but: YOUR_PACKAGE_NAME, MAJOR_NUMBER, MINOR_NUMBER # you change nothing but: YOUR_PACKAGE_NAME, MAJOR_NUMBER, MINOR_NUMBER
@ -243,6 +243,21 @@ AC_DEFUN([AX_INIT_STANDARD_PROJECT], [
AM_CPPFLAGS="${AM_CPPFLAGS} -DQT_NO_DEBUG_OUTPUT -DQT_NO_DEBUG" AM_CPPFLAGS="${AM_CPPFLAGS} -DQT_NO_DEBUG_OUTPUT -DQT_NO_DEBUG"
fi fi
AC_ARG_WITH(gcov,
[AS_HELP_STRING([--with-gcov=FILE],
[enable gcov, set gcov file (defaults to gcov)])],
[GCOV="$enableval"], [GCOV="no"])
AM_CONDITIONAL(COVERAGE, test "$GCOV" != "no")
if test "$GCOV" != "no"; then
if test "$GCOV" == "yes"; then
GCOV=gcov
fi
AC_MSG_NOTICE([Coverage tests enabled, using ${GCOV}]);
AM_CXXFLAGS="${AM_CXXFLAGS:-} -O0 --coverage -fprofile-arcs -ftest-coverage"
AM_LDFLAGS="${AM_LDFLAGS} -O0 --coverage -fprofile-arcs"
AX_SUBST(GCOV)
fi
if test -f ${PACKAGE_NAME}.desktop.in; then if test -f ${PACKAGE_NAME}.desktop.in; then
AC_CONFIG_FILES([${PACKAGE_NAME}.desktop]) AC_CONFIG_FILES([${PACKAGE_NAME}.desktop])
fi fi
@ -273,7 +288,7 @@ EOF
AC_DEFUN([AX_USE_CXX], [ AC_DEFUN([AX_USE_CXX], [
m4_include(ax_cxx_compile_stdcxx_11.m4) m4_include(ax_cxx_compile_stdcxx_11.m4)
AC_LANG(C++) AC_LANG(C++)
AX_CXX_COMPILE_STDCXX_11(noext, optional) AX_CXX_COMPILE_STDCXX_14(noext, optional)
AC_PROG_CXX AC_PROG_CXX
AC_PROG_CPP AC_PROG_CPP

@ -129,6 +129,7 @@ GENERATED FILES
* ax_init_standard_project.m4 - auxiliary macro definition file * ax_init_standard_project.m4 - auxiliary macro definition file
* ax_cxx_compile_stdcxx_11.m4 - auxiliary macro definition file * ax_cxx_compile_stdcxx_11.m4 - auxiliary macro definition file
* ax_check_qt.m4 - auxiliary macro definition file * ax_check_qt.m4 - auxiliary macro definition file
* makefile_test.inc.am - makefile to be included in tests
* resolve-debbuilddeps.sh - script to install debian package dependencies * resolve-debbuilddeps.sh - script to install debian package dependencies
* resolve-rpmbuilddeps.sh - script to install RPM package dependencies * resolve-rpmbuilddeps.sh - script to install RPM package dependencies
* build-in-docker.sh - script to build the project encapsulated in a docker container * build-in-docker.sh - script to build the project encapsulated in a docker container
@ -500,6 +501,7 @@ copy ${MY_NAME}
copy ax_init_standard_project.m4 copy ax_init_standard_project.m4
copy ax_cxx_compile_stdcxx_11.m4 copy ax_cxx_compile_stdcxx_11.m4
copy ax_check_qt.m4 copy ax_check_qt.m4
copy makefile_test.inc.am
copy resolve-debbuilddeps.sh copy resolve-debbuilddeps.sh
copy resolve-rpmbuilddeps.sh copy resolve-rpmbuilddeps.sh
copy build-in-docker.sh copy build-in-docker.sh

@ -0,0 +1,5 @@
## @id $Id$
## 1 2 3 4 5 6 7 8
## 45678901234567890123456789012345678901234567890123456789012345678901234567890

@ -84,6 +84,41 @@ template<typename TYPE, typename ARRAY=TYPE*> class MatrixBase {
///} ///}
/// @name element access
///{
TYPE& at(size_t row, size_t column) {
assert(row<ROWS);
assert(column<COLUMNS);
return *((TYPE*)_c+row*COLUMNS+column);
}
const TYPE& at(size_t row, size_t column) const {
assert(row<ROWS);
assert(column<COLUMNS);
return *((TYPE*)_c+row*COLUMNS+column);
}
TYPE& operator()(size_t row, size_t column) {
return at(row, column);
}
const TYPE& operator()(size_t row, size_t column) const {
return at(row, column);
}
RowVector operator[](size_t row) {
assert(row<ROWS);
return RowVector(*this, (TYPE*)_c+row*COLUMNS);
}
const ConstRowVector operator[](size_t row) const {
assert(row<ROWS);
return ConstRowVector(*this, (TYPE*)_c+row*COLUMNS);
}
///}
/// @name operators /// @name operators
///{ ///{
@ -138,29 +173,28 @@ template<typename TYPE, typename ARRAY=TYPE*> class MatrixBase {
///} ///}
/// @name element access /// @name special operations
///{ ///{
TYPE& operator()(size_t row, size_t column) { TYPE det() {
assert(row<ROWS); // rule of sarrus
assert(column<COLUMNS); TYPE res(0);
return *((TYPE*)_c+row*COLUMNS+column); assert(ROWS==COLUMNS); // not really necessary
} // 1) terms to add
for (size_t i(0); i<COLUMNS; ++i) {
const TYPE& operator()(size_t row, size_t column) const { TYPE term(1);
assert(row<ROWS); for (size_t j(0); j<ROWS; ++j)
assert(column<COLUMNS); term *= at((i+j)%COLUMNS, j);
return *((TYPE*)_c+row*COLUMNS+column); res += term;
} }
// 2) terms to subtract
RowVector operator[](size_t row) { for (size_t i(0); i<COLUMNS; ++i) {
assert(row<ROWS); TYPE term(1);
return RowVector(*this, (TYPE*)_c+row*COLUMNS); for (size_t j(0); j<ROWS; ++j)
term *= at((i+j)%COLUMNS, ROWS-j-1);
res -= term;
} }
return res;
const ConstRowVector operator[](size_t row) const {
assert(row<ROWS);
return ConstRowVector(*this, (TYPE*)_c+row*COLUMNS);
} }
///} ///}
@ -196,18 +230,19 @@ template<typename TYPE, size_t TROWS=0, size_t TCOLUMNS=0> class Matrix:
/// @name construction /// @name construction
///{ ///{
Matrix(): Parent(TROWS, TCOLUMNS) {} Matrix(): Parent(TROWS, TCOLUMNS) {
memset(Parent::_c, 0, Parent::MEM_SIZE);
template<typename ...ARGS>
Matrix(ARGS...t): Parent(TROWS, TCOLUMNS, t...) {
static_assert(sizeof...(t)==TROWS*TCOLUMNS,
"wrong array size");
} }
Matrix(const Matrix& o): Matrix() { Matrix(const Matrix& o): Matrix() {
memcpy(Parent::_c, o._c, Parent::MEM_SIZE); memcpy(Parent::_c, o._c, Parent::MEM_SIZE);
} }
template<typename ...ARGS>
Matrix(ARGS...t): Parent(TROWS, TCOLUMNS, t...) {
static_assert(sizeof...(t)==TROWS*TCOLUMNS, "wrong array size");
}
///} ///}
/// @name operators /// @name operators
@ -218,14 +253,6 @@ template<typename TYPE, size_t TROWS=0, size_t TCOLUMNS=0> class Matrix:
return *this; return *this;
} }
Matrix<TYPE, TCOLUMNS, TROWS> T() const {
Matrix<TYPE, TCOLUMNS, TROWS> res;
for (size_t row(0); row<TROWS; ++row)
for (size_t column(0); column<TCOLUMNS; ++column)
res(column, row) = Parent::operator()(row, column);
return res;
}
Matrix operator-() const { Matrix operator-() const {
Matrix res; Matrix res;
for (TYPE *to((TYPE*)(res._c)+Parent::SIZE); to>(TYPE*)(Parent::_c); --to) for (TYPE *to((TYPE*)(res._c)+Parent::SIZE); to>(TYPE*)(Parent::_c); --to)
@ -234,15 +261,30 @@ template<typename TYPE, size_t TROWS=0, size_t TCOLUMNS=0> class Matrix:
} }
template<size_t NEWCOLUMNS> template<size_t NEWCOLUMNS>
Matrix operator*(const Matrix<TYPE, TCOLUMNS, NEWCOLUMNS>& o) { Matrix<TYPE, TROWS, NEWCOLUMNS>
operator*(const Matrix<TYPE, TCOLUMNS, NEWCOLUMNS>& o) const {
Matrix<TYPE, TROWS, NEWCOLUMNS> res; Matrix<TYPE, TROWS, NEWCOLUMNS> res;
for (size_t i(0); i<TROWS; ++i) for (size_t i(0); i<TROWS; ++i)
for (size_t k(0); k<NEWCOLUMNS; ++k) for (size_t k(0); k<NEWCOLUMNS; ++k)
for (size_t j(0); j<TCOLUMNS; ++j) for (size_t j(0); j<TCOLUMNS; ++j)
res(i, k) += (*this)(i, j) * o(j, k); res(i, k) += this->at(i, j) * o(j, k);
return res; return res;
} }
///}
/// @name special operations
///{
Matrix<TYPE, TCOLUMNS, TROWS> t() const {
Matrix<TYPE, TCOLUMNS, TROWS> res;
for (size_t row(0); row<TROWS; ++row)
for (size_t column(0); column<TCOLUMNS; ++column)
res(column, row) = this->at(row, column);
return res;
}
///}
}; };
//============================================================================== //==============================================================================
@ -260,12 +302,18 @@ template<typename TYPE> class Matrix<TYPE, 0, 0>: public MatrixBase<TYPE> {
/// @name construction /// @name construction
///{ ///{
Matrix() = delete;
Matrix(size_t rows, size_t columns): Matrix(size_t rows, size_t columns):
Parent(rows, columns) { Parent(rows, columns) {
Parent::_c = new TYPE[rows*columns]; Parent::_c = new TYPE[rows*columns];
memset(Parent::_c, 0, Parent::MEM_SIZE); memset(Parent::_c, 0, Parent::MEM_SIZE);
} }
Matrix(const Matrix& o): Matrix(o.ROWS, o.Parent::COLUMNS) {
memcpy(Parent::_c, o.Parent::_c, Parent::MEM_SIZE);
}
template<typename ...ARGS> template<typename ...ARGS>
Matrix(size_t rows, size_t columns, ARGS...t): Matrix(size_t rows, size_t columns, ARGS...t):
Matrix(rows, columns) { Matrix(rows, columns) {
@ -273,10 +321,6 @@ template<typename TYPE> class Matrix<TYPE, 0, 0>: public MatrixBase<TYPE> {
copy_args(Parent::_c, t...); copy_args(Parent::_c, t...);
} }
Matrix(const Matrix& o): Matrix(o.ROWS, o.Parent::COLUMNS) {
memcpy(Parent::_c, o.Parent::_c, Parent::MEM_SIZE);
}
///} ///}
/// @name destruction /// @name destruction
@ -303,11 +347,11 @@ template<typename TYPE> class Matrix<TYPE, 0, 0>: public MatrixBase<TYPE> {
return Parent::operator=(o); return Parent::operator=(o);
} }
Matrix T() const { Matrix t() const {
Matrix res(Parent::COLUMNS, Parent::ROWS); Matrix res(Parent::COLUMNS, Parent::ROWS);
for (size_t row(0); row<Parent::ROWS; ++row) for (size_t row(0); row<Parent::ROWS; ++row)
for (size_t column(0); column<Parent::COLUMNS; ++column) for (size_t column(0); column<Parent::COLUMNS; ++column)
res(column, row) = Parent::operator()(row, column); res(column, row) = this->at(row, column);
return res; return res;
} }
@ -318,6 +362,16 @@ template<typename TYPE> class Matrix<TYPE, 0, 0>: public MatrixBase<TYPE> {
return res; return res;
} }
Matrix operator*(const Matrix& o) const {
Matrix<TYPE> res(this->ROWS, o.COLUMNS);
assert(this->COLUMNS==o.ROWS);
for (size_t i(0); i<this->ROWS; ++i)
for (size_t k(0); k<o.COLUMNS; ++k)
for (size_t j(0); j<this->COLUMNS; ++j)
res(i, k) += this->at(i, j) * o(j, k);
return res;
}
///} ///}
//................................................................methods //................................................................methods
@ -343,6 +397,38 @@ template<typename TYPE> class Matrix<TYPE, 0, 0>: public MatrixBase<TYPE> {
//============================================================================== //==============================================================================
template<typename TYPE, size_t ROWS, size_t COLUMNS>
Matrix<TYPE, ROWS, COLUMNS> operator+(const Matrix<TYPE, ROWS, COLUMNS>& a,
const Matrix<TYPE, ROWS, COLUMNS>& b) {
Matrix<TYPE, ROWS, COLUMNS> res(a);
res += b;
return res;
}
template<typename TYPE, size_t ROWS, size_t COLUMNS>
Matrix<TYPE, ROWS, COLUMNS> operator-(const Matrix<TYPE, ROWS, COLUMNS>& a,
const Matrix<TYPE, ROWS, COLUMNS>& b) {
Matrix<TYPE, ROWS, COLUMNS> res(a);
res -= b;
return res;
}
template<typename TYPE, size_t ROWS, size_t COLUMNS>
Matrix<TYPE, ROWS, COLUMNS> operator*(const TYPE& v,
const Matrix<TYPE, ROWS, COLUMNS>& m) {
Matrix<TYPE, ROWS, COLUMNS> res(m);
res *= v;
return res;
}
template<typename TYPE, size_t ROWS, size_t COLUMNS>
Matrix<TYPE, ROWS, COLUMNS> operator*(const Matrix<TYPE, ROWS, COLUMNS>& m,
const TYPE& v) {
Matrix<TYPE, ROWS, COLUMNS> res(m);
res *= v;
return res;
}
template<typename TYPE, size_t ROWS, size_t COLUMNS> template<typename TYPE, size_t ROWS, size_t COLUMNS>
std::ostream& operator<<(std::ostream& s, const Matrix<TYPE, ROWS, COLUMNS>& m) { std::ostream& operator<<(std::ostream& s, const Matrix<TYPE, ROWS, COLUMNS>& m) {
for (size_t w = 0; w < m.ROWS; ++w) { for (size_t w = 0; w < m.ROWS; ++w) {

@ -17,7 +17,7 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
public: public:
template<typename T> template<typename T>
void initFromArray1() { void initFromArray1() {
Matrix<T,2,4> m {1, 2, 3, 4, const Matrix<T,2,4> m {1, 2, 3, 4,
5, 6, 7, 8}; 5, 6, 7, 8};
CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]); CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]);
CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]); CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]);
@ -30,7 +30,7 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
} }
template<typename T> template<typename T>
void initFromArray2() { void initFromArray2() {
Matrix<T,2,4> m(1, 2, 3, 4, const Matrix<T,2,4> m(1, 2, 3, 4,
5, 6, 7, 8); 5, 6, 7, 8);
CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]); CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]);
CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]); CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]);
@ -43,7 +43,7 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
} }
template<typename T> template<typename T>
void initFromArray3() { void initFromArray3() {
Matrix<T,2,4> m({1, 2, 3, 4, const Matrix<T,2,4> m({1, 2, 3, 4,
5, 6, 7, 8}); 5, 6, 7, 8});
CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]); CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]);
CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]); CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]);
@ -81,7 +81,7 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
} }
template<typename T> template<typename T>
void initFromDefault() { void initFromDefault() {
Matrix<T,2,2> m; const Matrix<T,2,2> m;
CPPUNIT_ASSERT_EQUAL((T)0, m[0][0]); CPPUNIT_ASSERT_EQUAL((T)0, m[0][0]);
CPPUNIT_ASSERT_EQUAL((T)0, m[0][1]); CPPUNIT_ASSERT_EQUAL((T)0, m[0][1]);
CPPUNIT_ASSERT_EQUAL((T)0, m[1][0]); CPPUNIT_ASSERT_EQUAL((T)0, m[1][0]);
@ -105,9 +105,69 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
5, 6, 7, 9); 5, 6, 7, 9);
Matrix<T,2,4> m4(9, 2, 3, 4, Matrix<T,2,4> m4(9, 2, 3, 4,
5, 6, 7, 8); 5, 6, 7, 8);
Matrix<T,2,4> m5(1, 2, 0, 4,
5, 6, 7, 8);
CPPUNIT_ASSERT(m1==m2); CPPUNIT_ASSERT(m1==m2);
CPPUNIT_ASSERT(m1!=m3); CPPUNIT_ASSERT(m1!=m3);
CPPUNIT_ASSERT(m1!=m4); CPPUNIT_ASSERT(m1!=m4);
CPPUNIT_ASSERT(m1!=m5);
}
template<typename T>
void operator_plus() {
const Matrix<T,2,4> m1(1, 2, 3, 4,
5, 6, 7, 8);
const Matrix<T,2,4> m2(2, 4, 6, 8,
1, 3, 5, 7);
const Matrix<T,2,4> m(m1+m2);
const Matrix<T,2,4> res(3, 6, 9, 12,
6, 9, 12, 15);
CPPUNIT_ASSERT_EQUAL(res, m);
}
template<typename T>
void operator_minus() {
const Matrix<T,2,4> m1(1, 2, 3, 4,
5, 6, 7, 8);
const Matrix<T,2,4> m2(2, 4, 6, 8,
1, 3, 5, 7);
const Matrix<T,2,4> m(m1-m2);
const Matrix<T,2,4> res(-1, -2, -3, -4,
4, 3, 2, 1);
CPPUNIT_ASSERT_EQUAL(res, m);
}
template<typename T>
void scalar_mult() {
const Matrix<T,2,4> m1(1, 2, 3, 4,
5, 6, 7, 8);
T two(2), three(3), four(4), big(32);
CPPUNIT_ASSERT_EQUAL(m1*two, m1+m1);
CPPUNIT_ASSERT_EQUAL(m1*three, m1+m1+m1);
CPPUNIT_ASSERT_EQUAL(m1*four, m1+m1+m1+m1);
CPPUNIT_ASSERT_EQUAL(two*m1, m1*two);
CPPUNIT_ASSERT_EQUAL(three*m1, m1*three);
CPPUNIT_ASSERT_EQUAL(four*m1, m1*four);
CPPUNIT_ASSERT_EQUAL(two*m1*two, m1*four);
CPPUNIT_ASSERT_EQUAL(big*m1*four, m1*four*big);
}
template<typename T>
void matrix_mult() {
const Matrix<T,2,3> m1(1, 2, 3,
4, 5, 6);
const Matrix<T,3,4> m2(1, 4, 7, 10,
2, 5, 8, 11,
3, 6, 9, 12);
const Matrix<T,2,4> m(m1*m2);
const Matrix<T,2,4> res(14, 32, 50, 68,
32, 77, 122, 167);
CPPUNIT_ASSERT_EQUAL(res, m);
}
template<typename T>
void transpose() {
const Matrix<T,2,3> m(1, 2, 3,
4, 5, 6);
const Matrix<T,3,2> res(1, 4,
2, 5,
3, 6);
CPPUNIT_ASSERT_EQUAL(res, m.t());
} }
CPPUNIT_TEST_SUITE(TemplateMatrixTest); CPPUNIT_TEST_SUITE(TemplateMatrixTest);
CPPUNIT_TEST(initFromArray1<int>); CPPUNIT_TEST(initFromArray1<int>);
@ -146,6 +206,36 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
CPPUNIT_TEST(equality<unsigned long>); CPPUNIT_TEST(equality<unsigned long>);
CPPUNIT_TEST(equality<float>); CPPUNIT_TEST(equality<float>);
CPPUNIT_TEST(equality<double>); CPPUNIT_TEST(equality<double>);
CPPUNIT_TEST(operator_plus<int>);
CPPUNIT_TEST(operator_plus<long>);
CPPUNIT_TEST(operator_plus<unsigned>);
CPPUNIT_TEST(operator_plus<unsigned long>);
CPPUNIT_TEST(operator_plus<float>);
CPPUNIT_TEST(operator_plus<double>);
CPPUNIT_TEST(operator_minus<int>);
CPPUNIT_TEST(operator_minus<long>);
CPPUNIT_TEST(operator_minus<unsigned>);
CPPUNIT_TEST(operator_minus<unsigned long>);
CPPUNIT_TEST(operator_minus<float>);
CPPUNIT_TEST(operator_minus<double>);
CPPUNIT_TEST(scalar_mult<int>);
CPPUNIT_TEST(scalar_mult<long>);
CPPUNIT_TEST(scalar_mult<unsigned>);
CPPUNIT_TEST(scalar_mult<unsigned long>);
CPPUNIT_TEST(scalar_mult<float>);
CPPUNIT_TEST(scalar_mult<double>);
CPPUNIT_TEST(matrix_mult<int>);
CPPUNIT_TEST(matrix_mult<long>);
CPPUNIT_TEST(matrix_mult<unsigned>);
CPPUNIT_TEST(matrix_mult<unsigned long>);
CPPUNIT_TEST(matrix_mult<float>);
CPPUNIT_TEST(matrix_mult<double>);
CPPUNIT_TEST(transpose<int>);
CPPUNIT_TEST(transpose<long>);
CPPUNIT_TEST(transpose<unsigned>);
CPPUNIT_TEST(transpose<unsigned long>);
CPPUNIT_TEST(transpose<float>);
CPPUNIT_TEST(transpose<double>);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
}; };
CPPUNIT_TEST_SUITE_REGISTRATION(TemplateMatrixTest); CPPUNIT_TEST_SUITE_REGISTRATION(TemplateMatrixTest);
@ -241,6 +331,75 @@ class VariableMatrixTest: public CppUnit::TestFixture {
CPPUNIT_ASSERT(m1!=m3); CPPUNIT_ASSERT(m1!=m3);
CPPUNIT_ASSERT(m1!=m4); CPPUNIT_ASSERT(m1!=m4);
} }
template<typename T>
void operator_plus() {
const Matrix<T> m1(2, 4,
1, 2, 3, 4,
5, 6, 7, 8);
const Matrix<T> m2(2, 4,
2, 4, 6, 8,
1, 3, 5, 7);
const Matrix<T> m(m1+m2);
const Matrix<T> res(2, 4,
3, 6, 9, 12,
6, 9, 12, 15);
CPPUNIT_ASSERT_EQUAL(res, m);
}
template<typename T>
void operator_minus() {
const Matrix<T> m1(2, 4,
1, 2, 3, 4,
5, 6, 7, 8);
const Matrix<T> m2(2, 4,
2, 4, 6, 8,
1, 3, 5, 7);
const Matrix<T> m(m1-m2);
const Matrix<T> res(2, 4,
-1, -2, -3, -4,
4, 3, 2, 1);
CPPUNIT_ASSERT_EQUAL(res, m);
}
template<typename T>
void scalar_mult() {
const Matrix<T> m1(2, 4,
1, 2, 3, 4,
5, 6, 7, 8);
T two(2), three(3), four(4), big(32);
CPPUNIT_ASSERT_EQUAL(m1*two, m1+m1);
CPPUNIT_ASSERT_EQUAL(m1*three, m1+m1+m1);
CPPUNIT_ASSERT_EQUAL(m1*four, m1+m1+m1+m1);
CPPUNIT_ASSERT_EQUAL(two*m1, m1*two);
CPPUNIT_ASSERT_EQUAL(three*m1, m1*three);
CPPUNIT_ASSERT_EQUAL(four*m1, m1*four);
CPPUNIT_ASSERT_EQUAL(two*m1*two, m1*four);
CPPUNIT_ASSERT_EQUAL(big*m1*four, m1*four*big);
}
template<typename T>
void matrix_mult() {
const Matrix<T> m1(2, 3,
1, 2, 3,
4, 5, 6);
const Matrix<T> m2(3, 4,
1, 4, 7, 10,
2, 5, 8, 11,
3, 6, 9, 12);
const Matrix<T> m(m1*m2);
const Matrix<T> res(2, 4,
14, 32, 50, 68,
32, 77, 122, 167);
CPPUNIT_ASSERT_EQUAL(res, m);
}
template<typename T>
void transpose() {
const Matrix<T> m(2, 3,
1, 2, 3,
4, 5, 6);
const Matrix<T> res(3, 2,
1, 4,
2, 5,
3, 6);
CPPUNIT_ASSERT_EQUAL(res, m.t());
}
CPPUNIT_TEST_SUITE(VariableMatrixTest); CPPUNIT_TEST_SUITE(VariableMatrixTest);
CPPUNIT_TEST(initFromArray1<int>); CPPUNIT_TEST(initFromArray1<int>);
CPPUNIT_TEST(initFromArray1<long>); CPPUNIT_TEST(initFromArray1<long>);
@ -272,6 +431,42 @@ class VariableMatrixTest: public CppUnit::TestFixture {
CPPUNIT_TEST(equality<unsigned long>); CPPUNIT_TEST(equality<unsigned long>);
CPPUNIT_TEST(equality<float>); CPPUNIT_TEST(equality<float>);
CPPUNIT_TEST(equality<double>); CPPUNIT_TEST(equality<double>);
CPPUNIT_TEST(operator_plus<int>);
CPPUNIT_TEST(operator_plus<long>);
CPPUNIT_TEST(operator_plus<unsigned>);
CPPUNIT_TEST(operator_plus<unsigned long>);
CPPUNIT_TEST(operator_plus<float>);
CPPUNIT_TEST(operator_plus<double>);
CPPUNIT_TEST(operator_minus<int>);
CPPUNIT_TEST(operator_minus<long>);
CPPUNIT_TEST(operator_minus<unsigned>);
CPPUNIT_TEST(operator_minus<unsigned long>);
CPPUNIT_TEST(operator_minus<float>);
CPPUNIT_TEST(operator_minus<double>);
CPPUNIT_TEST(scalar_mult<int>);
CPPUNIT_TEST(scalar_mult<long>);
CPPUNIT_TEST(scalar_mult<unsigned>);
CPPUNIT_TEST(scalar_mult<unsigned long>);
CPPUNIT_TEST(scalar_mult<float>);
CPPUNIT_TEST(scalar_mult<double>);
CPPUNIT_TEST(scalar_mult<int>);
CPPUNIT_TEST(scalar_mult<long>);
CPPUNIT_TEST(scalar_mult<unsigned>);
CPPUNIT_TEST(scalar_mult<unsigned long>);
CPPUNIT_TEST(scalar_mult<float>);
CPPUNIT_TEST(scalar_mult<double>);
CPPUNIT_TEST(matrix_mult<int>);
CPPUNIT_TEST(matrix_mult<long>);
CPPUNIT_TEST(matrix_mult<unsigned>);
CPPUNIT_TEST(matrix_mult<unsigned long>);
CPPUNIT_TEST(matrix_mult<float>);
CPPUNIT_TEST(matrix_mult<double>);
CPPUNIT_TEST(transpose<int>);
CPPUNIT_TEST(transpose<long>);
CPPUNIT_TEST(transpose<unsigned>);
CPPUNIT_TEST(transpose<unsigned long>);
CPPUNIT_TEST(transpose<float>);
CPPUNIT_TEST(transpose<double>);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
}; };
CPPUNIT_TEST_SUITE_REGISTRATION(VariableMatrixTest); CPPUNIT_TEST_SUITE_REGISTRATION(VariableMatrixTest);

Loading…
Cancel
Save