From 32348091ba2536031a1bed4ae37206a1ce1f4743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20S=C3=A1nchez?= Date: Mon, 23 May 2022 14:46:16 +0000 Subject: [PATCH] Avoid signed integer overflow in adjoint test. --- test/adjoint.cpp | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/test/adjoint.cpp b/test/adjoint.cpp index da8f9589c..210a4d927 100644 --- a/test/adjoint.cpp +++ b/test/adjoint.cpp @@ -62,6 +62,17 @@ template<> struct adjoint_specific { } }; +template +MatrixType RandomMatrix(int rows, int cols, Scalar min, Scalar max) { + MatrixType M = MatrixType(rows, cols); + for (int i=0; i(min, max); + } + } + return M; +} + template void adjoint(const MatrixType& m) { /* this test covers the following files: @@ -77,17 +88,21 @@ template void adjoint(const MatrixType& m) Index rows = m.rows(); Index cols = m.cols(); - MatrixType m1 = MatrixType::Random(rows, cols), - m2 = MatrixType::Random(rows, cols), + // Avoid integer overflow by limiting input values. + RealScalar rmin = static_cast(NumTraits::IsInteger ? NumTraits::IsSigned ? -100 : 0 : -1); + RealScalar rmax = static_cast(NumTraits::IsInteger ? 100 : 1); + + MatrixType m1 = RandomMatrix(rows, cols, rmin, rmax), + m2 = RandomMatrix(rows, cols, rmin, rmax), m3(rows, cols), - square = SquareMatrixType::Random(rows, rows); - VectorType v1 = VectorType::Random(rows), - v2 = VectorType::Random(rows), - v3 = VectorType::Random(rows), + square = RandomMatrix(rows, rows, rmin, rmax); + VectorType v1 = RandomMatrix(rows, 1, rmin, rmax), + v2 = RandomMatrix(rows, 1, rmin, rmax), + v3 = RandomMatrix(rows, 1, rmin, rmax), vzero = VectorType::Zero(rows); - Scalar s1 = internal::random(), - s2 = internal::random(); + Scalar s1 = internal::random(rmin, rmax), + s2 = internal::random(rmin, rmax); // check basic compatibility of adjoint, transpose, conjugate VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1); @@ -138,7 +153,8 @@ template void adjoint(const MatrixType& m) // check mixed dot product typedef Matrix RealVectorType; - RealVectorType rv1 = RealVectorType::Random(rows); + RealVectorType rv1 = RandomMatrix(rows, 1, rmin, rmax); + VERIFY_IS_APPROX(v1.dot(rv1.template cast()), v1.dot(rv1)); VERIFY_IS_APPROX(rv1.template cast().dot(v1), rv1.dot(v1));