From 2b20814ced28e465dfa275606d4d20656539f2e5 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Tue, 11 Dec 2007 10:07:18 +0000 Subject: Expand and improve unit-tests --- test/CMakeLists.txt | 1 + test/adjoint.cpp | 2 +- test/basicstuff.cpp | 2 +- test/main.h | 2 +- test/submatrices.cpp | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 test/submatrices.cpp (limited to 'test') diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5680fef9b..42aae4012 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,6 +8,7 @@ SET(test_SRCS main.cpp basicstuff.cpp adjoint.cpp + submatrices.cpp ) QT4_AUTOMOC(${test_SRCS}) diff --git a/test/adjoint.cpp b/test/adjoint.cpp index 4b4b42f20..1d98d6a9b 100644 --- a/test/adjoint.cpp +++ b/test/adjoint.cpp @@ -91,7 +91,7 @@ template void adjoint(const MatrixType& m) void EigenTest::testAdjoint() { - REPEAT { + for(int i = 0; i < m_repeat; i++) { adjoint(Matrix()); adjoint(Matrix4d()); adjoint(MatrixXcf(3, 3)); diff --git a/test/basicstuff.cpp b/test/basicstuff.cpp index 4fb4b991a..f4bea3e46 100644 --- a/test/basicstuff.cpp +++ b/test/basicstuff.cpp @@ -138,7 +138,7 @@ template void basicStuff(const MatrixType& m) void EigenTest::testBasicStuff() { - REPEAT { + for(int i = 0; i < m_repeat; i++) { basicStuff(Matrix()); basicStuff(Matrix4d()); basicStuff(MatrixXcf(3, 3)); diff --git a/test/main.h b/test/main.h index bf0b263c5..837074f0a 100644 --- a/test/main.h +++ b/test/main.h @@ -35,7 +35,6 @@ #include #define DEFAULT_REPEAT 50 -#define REPEAT for(int repeat_iteration = 0; repeat_iteration < m_repeat; repeat_iteration++) #define VERIFY(a) QVERIFY(a) #define VERIFY_IS_APPROX(a, b) QVERIFY(test_isApprox(a, b)) @@ -116,6 +115,7 @@ class EigenTest : public QObject private slots: void testBasicStuff(); void testAdjoint(); + void testSubmatrices(); protected: int m_repeat; diff --git a/test/submatrices.cpp b/test/submatrices.cpp new file mode 100644 index 000000000..33d135646 --- /dev/null +++ b/test/submatrices.cpp @@ -0,0 +1,120 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2006-2007 Benoit Jacob +// +// Eigen is free software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along +// with Eigen; if not, write to the Free Software Foundation, Inc., 51 +// Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +// +// As a special exception, if other files instantiate templates or use macros +// or functions from this file, or you compile this file and link it +// with other works to produce a work based on this file, this file does not +// by itself cause the resulting work to be covered by the GNU General Public +// License. This exception does not invalidate any other reasons why a work +// based on this file might be covered by the GNU General Public License. + +#include "main.h" + +namespace Eigen { + +template void submatrices(const MatrixType& m) +{ + /* this test covers the following files: + Transpose.h Conjugate.h Dot.h + */ + + typedef typename MatrixType::Scalar Scalar; + typedef Matrix VectorType; + typedef Matrix RowVectorType; + int rows = m.rows(); + int cols = m.cols(); + + MatrixType m1 = MatrixType::random(rows, cols), + m2 = MatrixType::random(rows, cols), + m3(rows, cols), + mzero = MatrixType::zero(rows, cols), + identity = Matrix + ::identity(rows), + square = Matrix + ::random(rows, rows); + VectorType v1 = VectorType::random(rows), + v2 = VectorType::random(rows), + v3 = VectorType::random(rows), + vzero = VectorType::zero(rows); + + Scalar s1 = random(); + + /* this test covers the following files: + Row.h Column.h Block.h DynBlock.h Minor.h + */ + + int r1 = random(0,rows-1); + int r2 = random(r1,rows-1); + int c1 = random(0,cols-1); + int c2 = random(c1,cols-1); + + //check row() and col() + VERIFY_IS_APPROX(m1.col(c1).transpose(), m1.transpose().row(c1)); + VERIFY_IS_APPROX(square.row(r1).dot(m1.col(c1)), square.lazyProduct(m1.conjugate())(r1,c1)); + //check operator(), both constant and non-constant, on row() and col() + m1.row(r1) += s1 * m1.row(r2); + m1.col(c1) += s1 * m1.col(c2); + + //check dynBlock() + Matrix b1(1,1); b1(0,0) = m1(r1,c1); + RowVectorType br1(m1.dynBlock(r1,0,1,cols)); + VectorType bc1(m1.dynBlock(0,c1,rows,1)); + VERIFY_IS_APPROX(b1, m1.dynBlock(r1,c1,1,1)); + VERIFY_IS_APPROX(m1.row(r1), br1); + VERIFY_IS_APPROX(m1.col(c1), bc1); + //check operator(), both constant and non-constant, on dynBlock() + m1.dynBlock(r1,c1,r2-r1+1,c2-c1+1) = s1 * m2.dynBlock(0, 0, r2-r1+1,c2-c1+1); + m1.dynBlock(r1,c1,r2-r1+1,c2-c1+1)(r2-r1,c2-c1) = m2.dynBlock(0, 0, r2-r1+1,c2-c1+1)(0,0); + + //check minor() + if(rows > 1 && cols > 1) + { + Matrix mi = m1.minor(0,0).eval(); + VERIFY_IS_APPROX(mi, m1.dynBlock(1,1,rows-1,cols-1)); + mi = m1.minor(r1,c1); + VERIFY_IS_APPROX(mi.transpose(), m1.transpose().minor(c1,r1)); + //check operator(), both constant and non-constant, on minor() + m1.minor(r1,c1)(0,0) = m1.minor(0,0)(0,0); + } +} + +void EigenTest::testSubmatrices() +{ + for(int i = 0; i < m_repeat; i++) { + submatrices(Matrix()); + submatrices(Matrix4d()); + submatrices(MatrixXcf(3, 3)); + submatrices(MatrixXi(8, 12)); + submatrices(MatrixXcd(20, 20)); + + // test block() separately as it is a template method so doesn't support + // being called as a member of a class that is itself a template parameter + // (at least as of g++ 4.2) + Matrix m = Matrix::random(); + float s = random(); + // test block() as lvalue + m.block<2,5>(1,1) *= s; + // test operator() on block() both as constant and non-constant + m.block<2,5>(1,1)(0, 3) = m.block<2,5>(1,1)(1,2); + // check that block() and dynBlock() agree + MatrixXf b = m.block<3,2>(3,3); + VERIFY_IS_APPROX(b, m.dynBlock(3,3,3,2)); + } +} + +} // namespace Eigen -- cgit v1.2.3