aboutsummaryrefslogtreecommitdiff
path: root/test/nomalloc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/nomalloc.cpp')
-rw-r--r--test/nomalloc.cpp59
1 files changed, 38 insertions, 21 deletions
diff --git a/test/nomalloc.cpp b/test/nomalloc.cpp
index 8e0402358..50756c2fb 100644
--- a/test/nomalloc.cpp
+++ b/test/nomalloc.cpp
@@ -8,20 +8,10 @@
// 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/.
-// this hack is needed to make this file compiles with -pedantic (gcc)
-#ifdef __GNUC__
-#define throw(X)
-#endif
-
-#ifdef __INTEL_COMPILER
- // disable "warning #76: argument to macro is empty" produced by the above hack
- #pragma warning disable 76
-#endif
-
// discard stack allocation as that too bypasses malloc
#define EIGEN_STACK_ALLOCATION_LIMIT 0
-// any heap allocation will raise an assert
-#define EIGEN_NO_MALLOC
+// heap allocation will raise an assert if enabled at runtime
+#define EIGEN_RUNTIME_NO_MALLOC
#include "main.h"
#include <Eigen/Cholesky>
@@ -88,14 +78,15 @@ template<typename MatrixType> void nomalloc(const MatrixType& m)
VERIFY_IS_APPROX(m2,m2);
m2.template selfadjointView<Lower>().rankUpdate(m1.col(0),-1);
- m2.template selfadjointView<Lower>().rankUpdate(m1.row(0),-1);
+ m2.template selfadjointView<Upper>().rankUpdate(m1.row(0),-1);
+ m2.template selfadjointView<Lower>().rankUpdate(m1.col(0), m1.col(0)); // rank-2
// The following fancy matrix-matrix products are not safe yet regarding static allocation
-// m1 += m1.template triangularView<Upper>() * m2.col(;
-// m1.template selfadjointView<Lower>().rankUpdate(m2);
-// m1 += m1.template triangularView<Upper>() * m2;
-// m1 += m1.template selfadjointView<Lower>() * m2;
-// VERIFY_IS_APPROX(m1,m1);
+ m2.template selfadjointView<Lower>().rankUpdate(m1);
+ m2 += m2.template triangularView<Upper>() * m1;
+ m2.template triangularView<Upper>() = m2 * m2;
+ m1 += m1.template selfadjointView<Lower>() * m2;
+ VERIFY_IS_APPROX(m2,m2);
}
template<typename Scalar>
@@ -171,7 +162,7 @@ void test_zerosized() {
Eigen::VectorXd v;
// explicit zero-sized:
Eigen::ArrayXXd A0(0,0);
- Eigen::ArrayXd v0(std::ptrdiff_t(0)); // FIXME ArrayXd(0) is ambiguous
+ Eigen::ArrayXd v0(0);
// assigning empty objects to each other:
A=A0;
@@ -183,9 +174,11 @@ template<typename MatrixType> void test_reference(const MatrixType& m) {
enum { Flag = MatrixType::IsRowMajor ? Eigen::RowMajor : Eigen::ColMajor};
enum { TransposeFlag = !MatrixType::IsRowMajor ? Eigen::RowMajor : Eigen::ColMajor};
typename MatrixType::Index rows = m.rows(), cols=m.cols();
+ typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Flag > MatrixX;
+ typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, TransposeFlag> MatrixXT;
// Dynamic reference:
- typedef Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Flag > > Ref;
- typedef Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, TransposeFlag> > RefT;
+ typedef Eigen::Ref<const MatrixX > Ref;
+ typedef Eigen::Ref<const MatrixXT > RefT;
Ref r1(m);
Ref r2(m.block(rows/3, cols/4, rows/2, cols/2));
@@ -195,10 +188,30 @@ template<typename MatrixType> void test_reference(const MatrixType& m) {
VERIFY_RAISES_ASSERT(RefT r5(m));
VERIFY_RAISES_ASSERT(Ref r6(m.transpose()));
VERIFY_RAISES_ASSERT(Ref r7(Scalar(2) * m));
+
+ // Copy constructors shall also never malloc
+ Ref r8 = r1;
+ RefT r9 = r3;
+
+ // Initializing from a compatible Ref shall also never malloc
+ Eigen::Ref<const MatrixX, Unaligned, Stride<Dynamic, Dynamic> > r10=r8, r11=m;
+
+ // Initializing from an incompatible Ref will malloc:
+ typedef Eigen::Ref<const MatrixX, Aligned> RefAligned;
+ VERIFY_RAISES_ASSERT(RefAligned r12=r10);
+ VERIFY_RAISES_ASSERT(Ref r13=r10); // r10 has more dynamic strides
+
}
void test_nomalloc()
{
+ // create some dynamic objects
+ Eigen::MatrixXd M1 = MatrixXd::Random(3,3);
+ Ref<const MatrixXd> R1 = 2.0*M1; // Ref requires temporary
+
+ // from here on prohibit malloc:
+ Eigen::internal::set_is_malloc_allowed(false);
+
// check that our operator new is indeed called:
VERIFY_RAISES_ASSERT(MatrixXd dummy(MatrixXd::Random(3,3)));
CALL_SUBTEST_1(nomalloc(Matrix<float, 1, 1>()) );
@@ -207,6 +220,10 @@ void test_nomalloc()
// Check decomposition modules with dynamic matrices that have a known compile-time max size (ctms)
CALL_SUBTEST_4(ctms_decompositions<float>());
+
CALL_SUBTEST_5(test_zerosized());
+
CALL_SUBTEST_6(test_reference(Matrix<float,32,32>()));
+ CALL_SUBTEST_7(test_reference(R1));
+ CALL_SUBTEST_8(Ref<MatrixXd> R2 = M1.topRows<2>(); test_reference(R2));
}