2009-09-03 23:27:51 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2012-07-15 22:33:40 +08:00
|
|
|
// for linear algebra.
|
2009-09-03 23:27:51 +08:00
|
|
|
//
|
|
|
|
// Copyright (C) 2009 Hauke Heibel <hauke.heibel@gmail.com>
|
|
|
|
//
|
2012-07-14 02:42:47 +08:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2009-09-03 23:27:51 +08:00
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#include <Eigen/Core>
|
2018-07-19 05:33:07 +08:00
|
|
|
#include "AnnoyingScalar.h"
|
2009-09-03 23:27:51 +08:00
|
|
|
|
|
|
|
using namespace Eigen;
|
|
|
|
|
|
|
|
template <typename Scalar, int Storage>
|
|
|
|
void run_matrix_tests()
|
|
|
|
{
|
|
|
|
typedef Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Storage> MatrixType;
|
|
|
|
|
|
|
|
MatrixType m, n;
|
|
|
|
|
|
|
|
// boundary cases ...
|
|
|
|
m = n = MatrixType::Random(50,50);
|
|
|
|
m.conservativeResize(1,50);
|
|
|
|
VERIFY_IS_APPROX(m, n.block(0,0,1,50));
|
|
|
|
|
|
|
|
m = n = MatrixType::Random(50,50);
|
|
|
|
m.conservativeResize(50,1);
|
|
|
|
VERIFY_IS_APPROX(m, n.block(0,0,50,1));
|
|
|
|
|
|
|
|
m = n = MatrixType::Random(50,50);
|
|
|
|
m.conservativeResize(50,50);
|
|
|
|
VERIFY_IS_APPROX(m, n.block(0,0,50,50));
|
|
|
|
|
|
|
|
// random shrinking ...
|
|
|
|
for (int i=0; i<25; ++i)
|
|
|
|
{
|
2010-10-25 22:15:22 +08:00
|
|
|
const Index rows = internal::random<Index>(1,50);
|
|
|
|
const Index cols = internal::random<Index>(1,50);
|
2009-09-03 23:27:51 +08:00
|
|
|
m = n = MatrixType::Random(50,50);
|
|
|
|
m.conservativeResize(rows,cols);
|
|
|
|
VERIFY_IS_APPROX(m, n.block(0,0,rows,cols));
|
|
|
|
}
|
|
|
|
|
|
|
|
// random growing with zeroing ...
|
|
|
|
for (int i=0; i<25; ++i)
|
|
|
|
{
|
2010-10-25 22:15:22 +08:00
|
|
|
const Index rows = internal::random<Index>(50,75);
|
|
|
|
const Index cols = internal::random<Index>(50,75);
|
2009-09-03 23:27:51 +08:00
|
|
|
m = n = MatrixType::Random(50,50);
|
2009-09-07 23:48:42 +08:00
|
|
|
m.conservativeResizeLike(MatrixType::Zero(rows,cols));
|
2009-09-03 23:27:51 +08:00
|
|
|
VERIFY_IS_APPROX(m.block(0,0,n.rows(),n.cols()), n);
|
|
|
|
VERIFY( rows<=50 || m.block(50,0,rows-50,cols).sum() == Scalar(0) );
|
|
|
|
VERIFY( cols<=50 || m.block(0,50,rows,cols-50).sum() == Scalar(0) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Scalar>
|
|
|
|
void run_vector_tests()
|
|
|
|
{
|
2013-10-16 18:07:33 +08:00
|
|
|
typedef Matrix<Scalar, 1, Eigen::Dynamic> VectorType;
|
2009-09-03 23:27:51 +08:00
|
|
|
|
2013-10-16 18:07:33 +08:00
|
|
|
VectorType m, n;
|
2009-09-03 23:27:51 +08:00
|
|
|
|
|
|
|
// boundary cases ...
|
2013-10-16 18:07:33 +08:00
|
|
|
m = n = VectorType::Random(50);
|
2009-09-03 23:27:51 +08:00
|
|
|
m.conservativeResize(1);
|
|
|
|
VERIFY_IS_APPROX(m, n.segment(0,1));
|
|
|
|
|
2013-10-16 18:07:33 +08:00
|
|
|
m = n = VectorType::Random(50);
|
2009-09-03 23:27:51 +08:00
|
|
|
m.conservativeResize(50);
|
|
|
|
VERIFY_IS_APPROX(m, n.segment(0,50));
|
2013-10-16 18:07:33 +08:00
|
|
|
|
|
|
|
m = n = VectorType::Random(50);
|
|
|
|
m.conservativeResize(m.rows(),1);
|
|
|
|
VERIFY_IS_APPROX(m, n.segment(0,1));
|
|
|
|
|
|
|
|
m = n = VectorType::Random(50);
|
|
|
|
m.conservativeResize(m.rows(),50);
|
|
|
|
VERIFY_IS_APPROX(m, n.segment(0,50));
|
2009-09-03 23:27:51 +08:00
|
|
|
|
|
|
|
// random shrinking ...
|
|
|
|
for (int i=0; i<50; ++i)
|
|
|
|
{
|
2010-10-25 22:15:22 +08:00
|
|
|
const int size = internal::random<int>(1,50);
|
2013-10-16 18:07:33 +08:00
|
|
|
m = n = VectorType::Random(50);
|
2009-09-03 23:27:51 +08:00
|
|
|
m.conservativeResize(size);
|
|
|
|
VERIFY_IS_APPROX(m, n.segment(0,size));
|
2013-10-16 18:07:33 +08:00
|
|
|
|
|
|
|
m = n = VectorType::Random(50);
|
|
|
|
m.conservativeResize(m.rows(), size);
|
|
|
|
VERIFY_IS_APPROX(m, n.segment(0,size));
|
2009-09-03 23:27:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// random growing with zeroing ...
|
|
|
|
for (int i=0; i<50; ++i)
|
|
|
|
{
|
2010-10-25 22:15:22 +08:00
|
|
|
const int size = internal::random<int>(50,100);
|
2013-10-16 18:07:33 +08:00
|
|
|
m = n = VectorType::Random(50);
|
|
|
|
m.conservativeResizeLike(VectorType::Zero(size));
|
|
|
|
VERIFY_IS_APPROX(m.segment(0,50), n);
|
|
|
|
VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) );
|
|
|
|
|
|
|
|
m = n = VectorType::Random(50);
|
|
|
|
m.conservativeResizeLike(Matrix<Scalar,Dynamic,Dynamic>::Zero(1,size));
|
2009-09-03 23:27:51 +08:00
|
|
|
VERIFY_IS_APPROX(m.segment(0,50), n);
|
|
|
|
VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 05:33:07 +08:00
|
|
|
// Basic memory leak check with a non-copyable scalar type
|
|
|
|
template<int> void noncopyable()
|
|
|
|
{
|
|
|
|
typedef Eigen::Matrix<AnnoyingScalar,Dynamic,1> VectorType;
|
|
|
|
typedef Eigen::Matrix<AnnoyingScalar,Dynamic,Dynamic> MatrixType;
|
2021-08-05 20:15:41 +08:00
|
|
|
|
2018-07-19 05:33:07 +08:00
|
|
|
{
|
2021-08-05 20:15:41 +08:00
|
|
|
#ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
|
2018-07-19 05:33:07 +08:00
|
|
|
AnnoyingScalar::dont_throw = true;
|
2021-08-05 20:15:41 +08:00
|
|
|
#endif
|
2018-07-19 05:33:07 +08:00
|
|
|
int n = 50;
|
|
|
|
VectorType v0(n), v1(n);
|
|
|
|
MatrixType m0(n,n), m1(n,n), m2(n,n);
|
|
|
|
v0.setOnes(); v1.setOnes();
|
|
|
|
m0.setOnes(); m1.setOnes(); m2.setOnes();
|
|
|
|
VERIFY(m0==m1);
|
|
|
|
m0.conservativeResize(2*n,2*n);
|
|
|
|
VERIFY(m0.topLeftCorner(n,n) == m1);
|
|
|
|
|
|
|
|
VERIFY(v0.head(n) == v1);
|
|
|
|
v0.conservativeResize(2*n);
|
|
|
|
VERIFY(v0.head(n) == v1);
|
|
|
|
}
|
|
|
|
VERIFY(AnnoyingScalar::instances==0 && "global memory leak detected in noncopyable");
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:46:15 +08:00
|
|
|
EIGEN_DECLARE_TEST(conservative_resize)
|
2009-09-03 23:27:51 +08:00
|
|
|
{
|
2013-02-25 08:50:58 +08:00
|
|
|
for(int i=0; i<g_repeat; ++i)
|
|
|
|
{
|
|
|
|
CALL_SUBTEST_1((run_matrix_tests<int, Eigen::RowMajor>()));
|
|
|
|
CALL_SUBTEST_1((run_matrix_tests<int, Eigen::ColMajor>()));
|
|
|
|
CALL_SUBTEST_2((run_matrix_tests<float, Eigen::RowMajor>()));
|
|
|
|
CALL_SUBTEST_2((run_matrix_tests<float, Eigen::ColMajor>()));
|
|
|
|
CALL_SUBTEST_3((run_matrix_tests<double, Eigen::RowMajor>()));
|
|
|
|
CALL_SUBTEST_3((run_matrix_tests<double, Eigen::ColMajor>()));
|
|
|
|
CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::RowMajor>()));
|
|
|
|
CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::ColMajor>()));
|
|
|
|
CALL_SUBTEST_5((run_matrix_tests<std::complex<double>, Eigen::RowMajor>()));
|
2018-07-19 05:33:07 +08:00
|
|
|
CALL_SUBTEST_5((run_matrix_tests<std::complex<double>, Eigen::ColMajor>()));
|
2021-05-10 18:43:49 +08:00
|
|
|
CALL_SUBTEST_1((run_matrix_tests<int, Eigen::RowMajor | Eigen::DontAlign>()));
|
2013-02-25 08:50:58 +08:00
|
|
|
|
|
|
|
CALL_SUBTEST_1((run_vector_tests<int>()));
|
|
|
|
CALL_SUBTEST_2((run_vector_tests<float>()));
|
|
|
|
CALL_SUBTEST_3((run_vector_tests<double>()));
|
|
|
|
CALL_SUBTEST_4((run_vector_tests<std::complex<float> >()));
|
|
|
|
CALL_SUBTEST_5((run_vector_tests<std::complex<double> >()));
|
2018-07-19 05:33:07 +08:00
|
|
|
|
2021-08-05 20:15:41 +08:00
|
|
|
#ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
|
2018-07-19 05:33:07 +08:00
|
|
|
AnnoyingScalar::dont_throw = true;
|
2021-08-05 20:15:41 +08:00
|
|
|
#endif
|
2018-07-19 05:33:07 +08:00
|
|
|
CALL_SUBTEST_6(( run_vector_tests<AnnoyingScalar>() ));
|
|
|
|
CALL_SUBTEST_6(( noncopyable<0>() ));
|
2013-02-25 08:50:58 +08:00
|
|
|
}
|
2009-09-03 23:27:51 +08:00
|
|
|
}
|