diff --git a/test/adjoint.cpp b/test/adjoint.cpp index 881d63951..8ecbcab4a 100644 --- a/test/adjoint.cpp +++ b/test/adjoint.cpp @@ -70,17 +70,6 @@ struct adjoint_specific { } }; -template -MatrixType RandomMatrix(Index rows, Index cols, Scalar min, Scalar max) { - MatrixType M = MatrixType(rows, cols); - for (Index i = 0; i < rows; ++i) { - for (Index j = 0; j < cols; ++j) { - M(i, j) = Eigen::internal::random(min, max); - } - } - return M; -} - template void adjoint(const MatrixType& m) { /* this test covers the following files: diff --git a/test/cholesky.cpp b/test/cholesky.cpp index a703b93ff..5c8ec85bb 100644 --- a/test/cholesky.cpp +++ b/test/cholesky.cpp @@ -505,7 +505,6 @@ template void cholesky_rowmajor_boundary() { typedef typename NumTraits::Real RealScalar; typedef Matrix RowMatrixType; - typedef Matrix VectorType; const Index sizes[] = {7, 8, 9, 15, 16, 17, 31, 32, 33}; for (Index si = 0; si < Index(sizeof(sizes) / sizeof(sizes[0])); ++si) { diff --git a/test/integer_types.cpp b/test/integer_types.cpp index 142854cf2..bfda96e60 100644 --- a/test/integer_types.cpp +++ b/test/integer_types.cpp @@ -17,6 +17,7 @@ template void signed_integer_type_tests(const MatrixType& m) { typedef typename MatrixType::Scalar Scalar; + constexpr Scalar kMax = (Scalar(1) << ((8 * sizeof(Scalar) - 2) / 2)) - 1; enum { is_signed = (Scalar(-1) > Scalar(0)) ? 0 : 1 }; VERIFY(is_signed == 1); @@ -24,26 +25,12 @@ void signed_integer_type_tests(const MatrixType& m) { Index rows = m.rows(); Index cols = m.cols(); - MatrixType m1(rows, cols), m2 = MatrixType::Random(rows, cols), mzero = MatrixType::Zero(rows, cols); - - { - int guard = 0; - do { - m1 = MatrixType::Random(rows, cols); - } while ((m1 == mzero || m1 == m2) && (++guard) < 100); - VERIFY(guard < 100); - } + MatrixType m1 = RandomMatrix(rows, cols, Scalar(0), kMax); + MatrixType m2 = RandomMatrix(rows, cols, Scalar(0), kMax); // check linear structure - Scalar s1; - { - int guard = 0; - do { - s1 = internal::random(); - } while (s1 == 0 && (++guard) < 100); - VERIFY(guard < 100); - } + Scalar s1 = internal::random(1, kMax); VERIFY_IS_EQUAL(-(-m1), m1); VERIFY_IS_EQUAL(-m2 + m1 + m2, m1); @@ -53,46 +40,31 @@ void signed_integer_type_tests(const MatrixType& m) { template void integer_type_tests(const MatrixType& m) { typedef typename MatrixType::Scalar Scalar; + constexpr Scalar kMax = (Scalar(1) << ((8 * sizeof(Scalar) - 2) / 2)) - 1; VERIFY(NumTraits::IsInteger); enum { is_signed = (Scalar(-1) > Scalar(0)) ? 0 : 1 }; VERIFY(int(NumTraits::IsSigned) == is_signed); typedef Matrix VectorType; + typedef Matrix SquareMatrixType; Index rows = m.rows(); Index cols = m.cols(); - // this test relies a lot on Random.h, and there's not much more that we can do - // to test it, hence I consider that we will have tested Random.h - MatrixType m1(rows, cols), m2 = MatrixType::Random(rows, cols), m3(rows, cols), mzero = MatrixType::Zero(rows, cols); - - typedef Matrix SquareMatrixType; - SquareMatrixType identity = SquareMatrixType::Identity(rows, rows), square = SquareMatrixType::Random(rows, rows); - VectorType v1(rows), v2 = VectorType::Random(rows), vzero = VectorType::Zero(rows); - - { - int guard = 0; - do { - m1 = MatrixType::Random(rows, cols); - } while ((m1 == mzero || m1 == m2) && (++guard) < 100); - VERIFY(guard < 100); - } - - { - int guard = 0; - do { - v1 = VectorType::Random(rows); - } while ((v1 == vzero || v1 == v2) && (++guard) < 100); - VERIFY(guard < 100); - } + MatrixType m1 = RandomMatrix(rows, cols, Scalar(0), kMax); + MatrixType m2 = RandomMatrix(rows, cols, Scalar(0), kMax); + MatrixType m3 = RandomMatrix(rows, cols, Scalar(0), kMax); + SquareMatrixType square = RandomMatrix(rows, rows, Scalar(0), kMax); + VectorType v1 = RandomMatrix(rows, Index(1), Scalar(0), NumTraits::highest() / Scalar(2)); VERIFY_IS_APPROX(v1, v1); VERIFY_IS_NOT_APPROX(v1, 2 * v1); - VERIFY_IS_APPROX(vzero, v1 - v1); + VERIFY_IS_APPROX(VectorType::Zero(rows), v1 - v1); + VERIFY_IS_APPROX(m1, m1); VERIFY_IS_NOT_APPROX(m1, 2 * m1); - VERIFY_IS_APPROX(mzero, m1 - m1); + VERIFY_IS_APPROX(MatrixType::Zero(rows, cols), m1 - m1); VERIFY_IS_APPROX(m3 = m1, m1); MatrixType m4; @@ -113,10 +85,7 @@ void integer_type_tests(const MatrixType& m) { // check linear structure - Scalar s1; - do { - s1 = internal::random(); - } while (s1 == 0); + Scalar s1 = internal::random(1, kMax); VERIFY_IS_EQUAL(m1 + m1, 2 * m1); VERIFY_IS_EQUAL(m1 + m2 - m1, m2); @@ -134,10 +103,12 @@ void integer_type_tests(const MatrixType& m) { // check matrix product. - VERIFY_IS_APPROX(identity * m1, m1); - VERIFY_IS_APPROX(square * (m1 + m2), square * m1 + square * m2); - VERIFY_IS_APPROX((m1 + m2).transpose() * square, m1.transpose() * square + m2.transpose() * square); - VERIFY_IS_APPROX((m1 * m2.transpose()) * m1, m1 * (m2.transpose() * m1)); + if (!NumTraits::IsSigned) { + VERIFY_IS_APPROX(SquareMatrixType::Identity(rows, rows) * m1, m1); + VERIFY_IS_APPROX(square * (m1 + m2), square * m1 + square * m2); + VERIFY_IS_APPROX((m1 + m2).transpose() * square, m1.transpose() * square + m2.transpose() * square); + VERIFY_IS_APPROX((m1 * m2.transpose()) * m1, m1 * (m2.transpose() * m1)); + } } template diff --git a/test/main.h b/test/main.h index 52c27117b..6cedca562 100644 --- a/test/main.h +++ b/test/main.h @@ -902,6 +902,17 @@ void setRandomDataInRange(DataContainer& data_container, typename DataContainer: using namespace Eigen; +template +MatrixType RandomMatrix(Index rows, Index cols, Scalar min, Scalar max) { + MatrixType M = MatrixType(rows, cols); + for (Index i = 0; i < rows; ++i) { + for (Index j = 0; j < cols; ++j) { + M(i, j) = Eigen::internal::random(min, max); + } + } + return M; +} + /** * Set number of repetitions for unit test from input string. * diff --git a/test/product_trmv.cpp b/test/product_trmv.cpp index 2bbe8ed9a..051d47593 100644 --- a/test/product_trmv.cpp +++ b/test/product_trmv.cpp @@ -12,7 +12,6 @@ template void trmv(const MatrixType& m) { typedef typename MatrixType::Scalar Scalar; - typedef typename NumTraits::Real RealScalar; typedef Matrix VectorType; Index rows = m.rows(); diff --git a/test/realview.cpp b/test/realview.cpp index c975284af..0d82ff24b 100644 --- a/test/realview.cpp +++ b/test/realview.cpp @@ -12,7 +12,6 @@ // wrapper that disables array-oriented access to real and imaginary components struct TestComplex : public std::complex { TestComplex() = default; - TestComplex(const TestComplex&) = default; TestComplex(std::complex x) : std::complex(x){}; TestComplex(float x) : std::complex(x){}; };