1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 Hauke Heibel <hauke.heibel@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#include "main.h"
11
12#include <Eigen/Core>
13
14using namespace Eigen;
15
16template <typename Scalar, int Storage>
17void run_matrix_tests()
18{
19  typedef Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Storage> MatrixType;
20  typedef typename MatrixType::Index Index;
21
22  MatrixType m, n;
23
24  // boundary cases ...
25  m = n = MatrixType::Random(50,50);
26  m.conservativeResize(1,50);
27  VERIFY_IS_APPROX(m, n.block(0,0,1,50));
28
29  m = n = MatrixType::Random(50,50);
30  m.conservativeResize(50,1);
31  VERIFY_IS_APPROX(m, n.block(0,0,50,1));
32
33  m = n = MatrixType::Random(50,50);
34  m.conservativeResize(50,50);
35  VERIFY_IS_APPROX(m, n.block(0,0,50,50));
36
37  // random shrinking ...
38  for (int i=0; i<25; ++i)
39  {
40    const Index rows = internal::random<Index>(1,50);
41    const Index cols = internal::random<Index>(1,50);
42    m = n = MatrixType::Random(50,50);
43    m.conservativeResize(rows,cols);
44    VERIFY_IS_APPROX(m, n.block(0,0,rows,cols));
45  }
46
47  // random growing with zeroing ...
48  for (int i=0; i<25; ++i)
49  {
50    const Index rows = internal::random<Index>(50,75);
51    const Index cols = internal::random<Index>(50,75);
52    m = n = MatrixType::Random(50,50);
53    m.conservativeResizeLike(MatrixType::Zero(rows,cols));
54    VERIFY_IS_APPROX(m.block(0,0,n.rows(),n.cols()), n);
55    VERIFY( rows<=50 || m.block(50,0,rows-50,cols).sum() == Scalar(0) );
56    VERIFY( cols<=50 || m.block(0,50,rows,cols-50).sum() == Scalar(0) );
57  }
58}
59
60template <typename Scalar>
61void run_vector_tests()
62{
63  typedef Matrix<Scalar, 1, Eigen::Dynamic> MatrixType;
64
65  MatrixType m, n;
66
67  // boundary cases ...
68  m = n = MatrixType::Random(50);
69  m.conservativeResize(1);
70  VERIFY_IS_APPROX(m, n.segment(0,1));
71
72  m = n = MatrixType::Random(50);
73  m.conservativeResize(50);
74  VERIFY_IS_APPROX(m, n.segment(0,50));
75
76  // random shrinking ...
77  for (int i=0; i<50; ++i)
78  {
79    const int size = internal::random<int>(1,50);
80    m = n = MatrixType::Random(50);
81    m.conservativeResize(size);
82    VERIFY_IS_APPROX(m, n.segment(0,size));
83  }
84
85  // random growing with zeroing ...
86  for (int i=0; i<50; ++i)
87  {
88    const int size = internal::random<int>(50,100);
89    m = n = MatrixType::Random(50);
90    m.conservativeResizeLike(MatrixType::Zero(size));
91    VERIFY_IS_APPROX(m.segment(0,50), n);
92    VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) );
93  }
94}
95
96void test_conservative_resize()
97{
98  CALL_SUBTEST_1((run_matrix_tests<int, Eigen::RowMajor>()));
99  CALL_SUBTEST_1((run_matrix_tests<int, Eigen::ColMajor>()));
100  CALL_SUBTEST_2((run_matrix_tests<float, Eigen::RowMajor>()));
101  CALL_SUBTEST_2((run_matrix_tests<float, Eigen::ColMajor>()));
102  CALL_SUBTEST_3((run_matrix_tests<double, Eigen::RowMajor>()));
103  CALL_SUBTEST_3((run_matrix_tests<double, Eigen::ColMajor>()));
104  CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::RowMajor>()));
105  CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::ColMajor>()));
106  CALL_SUBTEST_5((run_matrix_tests<std::complex<double>, Eigen::RowMajor>()));
107  CALL_SUBTEST_6((run_matrix_tests<std::complex<double>, Eigen::ColMajor>()));
108
109  CALL_SUBTEST_1((run_vector_tests<int>()));
110  CALL_SUBTEST_2((run_vector_tests<float>()));
111  CALL_SUBTEST_3((run_vector_tests<double>()));
112  CALL_SUBTEST_4((run_vector_tests<std::complex<float> >()));
113  CALL_SUBTEST_5((run_vector_tests<std::complex<double> >()));
114}
115