mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
implement the first _real_ unit-tests, testing the results for correctness instead
of just checking compilation. Fix the many issues discovered by these unit-tests, by the way fixing a performance bug.
This commit is contained in:
@@ -6,9 +6,7 @@ INCLUDE_DIRECTORIES( ${QT_INCLUDE_DIR} )
|
||||
|
||||
SET(test_SRCS
|
||||
main.cpp
|
||||
vectorops.cpp
|
||||
matrixops.cpp
|
||||
matrixmanip.cpp
|
||||
basicstuff.cpp
|
||||
)
|
||||
QT4_AUTOMOC(${test_SRCS})
|
||||
|
||||
|
||||
83
test/basicstuff.cpp
Normal file
83
test/basicstuff.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
// 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 <jacob@math.jussieu.fr>
|
||||
//
|
||||
// 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"
|
||||
|
||||
template<typename MatrixType> void basicStuff(const MatrixType& m)
|
||||
{
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
|
||||
int rows = m.rows();
|
||||
int cols = m.cols();
|
||||
|
||||
MatrixType m1 = MatrixType::random(rows, cols),
|
||||
m2 = MatrixType::random(rows, cols),
|
||||
m3;
|
||||
VectorType v1 = VectorType::random(rows, 1),
|
||||
v2 = VectorType::random(rows, 1);
|
||||
|
||||
Scalar s1 = NumTraits<Scalar>::random(),
|
||||
s2 = NumTraits<Scalar>::random();
|
||||
|
||||
QVERIFY(v1.isApprox(v1));
|
||||
QVERIFY((v1-v1).isMuchSmallerThan(v1));
|
||||
QVERIFY((v1-v1).isMuchSmallerThan(v1.norm()));
|
||||
QVERIFY(m1.isApprox(m1));
|
||||
QVERIFY((m1-m1).isMuchSmallerThan(m1));
|
||||
|
||||
QVERIFY((m1+m1).isApprox(2 * m1));
|
||||
QVERIFY((m1 * s1).isApprox(s1 * m1));
|
||||
QVERIFY(((m1 + m2) * s1).isApprox(s1 * m1 + s1 * m2));
|
||||
QVERIFY(((s1 + s2) * m1).isApprox(m1 * s1 + m1 * s2));
|
||||
|
||||
|
||||
|
||||
m3 = m2;
|
||||
QVERIFY((m3 += m1).isApprox(m1 + m2));
|
||||
m3 = m2;
|
||||
QVERIFY((m3 -= m1).isApprox(-m1 + m2));
|
||||
m3 = m2;
|
||||
QVERIFY((m3 *= s1).isApprox(s1 * m2));
|
||||
m3 = m2;
|
||||
if(NumTraits<Scalar>::HasFloatingPoint
|
||||
&& s1 != static_cast<Scalar>(0))
|
||||
QVERIFY((m3 /= s1).isApprox(m2 / s1));
|
||||
|
||||
QVERIFY(((m1 * m1.transpose()) * m2).isApprox(m1 * (m1.transpose() * m2)));
|
||||
m3 = m1;
|
||||
m3 *= (m1.transpose() * m2);
|
||||
QVERIFY(m3.isApprox(m1 * (m1.transpose() * m2)));
|
||||
QVERIFY(m3.isApprox(m1.lazyProduct(m1.transpose() * m2)));
|
||||
}
|
||||
|
||||
void EigenTest::testBasicStuff()
|
||||
{
|
||||
basicStuff(Matrix<float, 1, 1>());
|
||||
basicStuff(Matrix<complex<int>, 2, 5>());
|
||||
basicStuff(Matrix<complex<double>, 4, 4>());
|
||||
basicStuff(MatrixXcf(3, 3));
|
||||
basicStuff(MatrixXi(8, 12));
|
||||
basicStuff(MatrixXd(20, 20));
|
||||
}
|
||||
55
test/main.h
55
test/main.h
@@ -44,60 +44,7 @@ class EigenTest : public QObject
|
||||
EigenTest();
|
||||
|
||||
private slots:
|
||||
void testVectorOps();
|
||||
void testMatrixOps();
|
||||
void testMatrixManip();
|
||||
void testBasicStuff();
|
||||
};
|
||||
|
||||
template<typename T> inline typename Eigen::NumTraits<T>::Real TestEpsilon();
|
||||
template<> inline int TestEpsilon<int>() { return 0; }
|
||||
template<> inline float TestEpsilon<float>() { return 1e-2f; }
|
||||
template<> inline double TestEpsilon<double>() { return 1e-4; }
|
||||
template<> inline int TestEpsilon<std::complex<int> >() { return TestEpsilon<int>(); }
|
||||
template<> inline float TestEpsilon<std::complex<float> >() { return TestEpsilon<float>(); }
|
||||
template<> inline double TestEpsilon<std::complex<double> >() { return TestEpsilon<double>(); }
|
||||
|
||||
template<typename T> bool TestMuchSmallerThan(const T& a, const T& b)
|
||||
{
|
||||
return NumTraits<T>::isMuchSmallerThan(a, b, TestEpsilon<T>());
|
||||
}
|
||||
|
||||
template<typename Scalar, typename Derived, typename OtherDerived>
|
||||
bool TestMuchSmallerThan(
|
||||
const Object<Scalar, Derived>& a,
|
||||
const Object<Scalar, OtherDerived>& b)
|
||||
{
|
||||
return a.isMuchSmallerThan(b, TestEpsilon<Scalar>());
|
||||
}
|
||||
|
||||
template<typename T> bool TestApprox(const T& a, const T& b)
|
||||
{
|
||||
return NumTraits<T>::isApprox(a, b, TestEpsilon<T>());
|
||||
}
|
||||
|
||||
template<typename Scalar, typename Derived, typename OtherDerived>
|
||||
bool TestApprox(
|
||||
const Object<Scalar, Derived>& a,
|
||||
const Object<Scalar, OtherDerived>& b)
|
||||
{
|
||||
return a.isApprox(b, TestEpsilon<Scalar>());
|
||||
}
|
||||
|
||||
template<typename T> bool TestApproxOrLessThan(const T& a, const T& b)
|
||||
{
|
||||
return NumTraits<T>::isApproxOrLessThan(a, b, TestEpsilon<T>());
|
||||
}
|
||||
|
||||
template<typename Scalar, typename Derived, typename OtherDerived>
|
||||
bool TestApproxOrLessThan(
|
||||
const Object<Scalar, Derived>& a,
|
||||
const Object<Scalar, OtherDerived>& b)
|
||||
{
|
||||
return a.isApproxOrLessThan(b, TestEpsilon<Scalar>());
|
||||
}
|
||||
|
||||
#define QVERIFY_MUCH_SMALLER_THAN(a, b) QVERIFY(TestMuchSmallerThan(a, b))
|
||||
#define QVERIFY_APPROX(a, b) QVERIFY(TestApprox(a, b))
|
||||
#define QVERIFY_APPROX_OR_LESS_THAN(a, b) QVERIFY(TestApproxOrLessThan(a, b))
|
||||
|
||||
#endif // EI_TEST_MAIN_H
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
// 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 <jacob@math.jussieu.fr>
|
||||
//
|
||||
// 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"
|
||||
|
||||
template<typename MatrixType> void matrixManip(const MatrixType& m)
|
||||
{
|
||||
int rows = m.rows(), cols = m.cols();
|
||||
int i = rand()%rows, j = rand()%cols;
|
||||
|
||||
MatrixType a(rows, cols), b(rows, cols);
|
||||
a.row(i);
|
||||
a.col(j);
|
||||
a.minor(i, j);
|
||||
a.block(1, rows-1, 1, cols-1);
|
||||
a.row(i) = b.row(i);
|
||||
a.row(i) += b.row(i);
|
||||
a.col(j) *= 2;
|
||||
a.minor(i, j) = b.block(1, rows-1, 1, cols-1);
|
||||
a.minor(i, j) -= a.block(1, rows-1, 1, cols-1).eval();
|
||||
}
|
||||
|
||||
void EigenTest::testMatrixManip()
|
||||
{
|
||||
matrixManip(Matrix<int, 2, 3>());
|
||||
matrixManip(Matrix<double, 3, 3>());
|
||||
matrixManip(Matrix<complex<float>, 4,3>());
|
||||
matrixManip(MatrixXi(2, 2));
|
||||
matrixManip(MatrixXd(3, 5));
|
||||
matrixManip(MatrixXcf(4, 4));
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
// 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 <jacob@math.jussieu.fr>
|
||||
//
|
||||
// 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"
|
||||
|
||||
template<typename MatrixType1,
|
||||
typename MatrixType2> void matrixOps(const MatrixType1& m1, const MatrixType2& m2)
|
||||
{
|
||||
typedef typename MatrixType1::Scalar Scalar;
|
||||
int rows1 = m1.rows(), cols1 = m1.cols();
|
||||
int rows2 = m2.rows(), cols2 = m2.cols();
|
||||
|
||||
MatrixType1 a(rows1, cols1), b(rows1, cols1), c(b);
|
||||
Scalar s;
|
||||
a * s;
|
||||
s * a;
|
||||
a + b;
|
||||
a - b;
|
||||
(a + b) * s;
|
||||
s * (a + b);
|
||||
a + b + c;
|
||||
a = b;
|
||||
a = b + c;
|
||||
a = s * (b - c);
|
||||
a = (a + b).eval();
|
||||
a += b;
|
||||
a -= b + b;
|
||||
a *= s;
|
||||
if(rows1 == cols1)
|
||||
{
|
||||
a *= b;
|
||||
a.lazyProduct(b);
|
||||
}
|
||||
|
||||
MatrixType1 d(rows1, cols1);
|
||||
MatrixType2 e(rows2, cols2);
|
||||
QVERIFY( (d * e).rows() == rows1 && (d * e).cols() == cols2 );
|
||||
}
|
||||
|
||||
void EigenTest::testMatrixOps()
|
||||
{
|
||||
matrixOps(Matrix<float, 1, 1>(), Matrix<float, 1, 1>());
|
||||
matrixOps(Matrix<int, 2, 3>(), Matrix<int, 3, 1>());
|
||||
matrixOps(Matrix<double, 3, 3>(), Matrix<double, 3, 3>());
|
||||
matrixOps(Matrix<complex<float>, 4,3>(), Matrix<complex<float>, 3,4>());
|
||||
matrixOps(MatrixXf(1, 1), MatrixXf(1, 3));
|
||||
matrixOps(MatrixXi(2, 2), MatrixXi(2, 2));
|
||||
matrixOps(MatrixXd(3, 5), MatrixXd(5, 1));
|
||||
matrixOps(MatrixXcf(4, 4), MatrixXcf(4, 4));
|
||||
matrixOps(MatrixXd(3, 5), Matrix<double, 5, 1>());
|
||||
matrixOps(Matrix4cf(), MatrixXcf(4, 4));
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
// 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 <jacob@math.jussieu.fr>
|
||||
//
|
||||
// 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"
|
||||
|
||||
template<typename VectorType> void vectorOps(const VectorType& v)
|
||||
{
|
||||
typedef typename VectorType::Scalar Scalar;
|
||||
int size = v.size();
|
||||
|
||||
VectorType a(size), b(size), c(b);
|
||||
Scalar s;
|
||||
a * s;
|
||||
s * a;
|
||||
a + b;
|
||||
a - b;
|
||||
(a + b) * s;
|
||||
s * (a + b);
|
||||
a + b + c;
|
||||
a = b;
|
||||
a = b + c;
|
||||
a = s * (b - c);
|
||||
a = (s * (b - c)).eval();
|
||||
|
||||
a += b;
|
||||
a -= b + b;
|
||||
a *= s;
|
||||
a += (a + a).eval();
|
||||
}
|
||||
|
||||
void EigenTest::testVectorOps()
|
||||
{
|
||||
vectorOps(Vector2i());
|
||||
vectorOps(Vector3d());
|
||||
vectorOps(Vector4cf());
|
||||
vectorOps(VectorXf(1));
|
||||
vectorOps(VectorXi(2));
|
||||
vectorOps(VectorXd(3));
|
||||
vectorOps(VectorXcf(4));
|
||||
}
|
||||
Reference in New Issue
Block a user