Fix compiler warnings in tests.

This commit is contained in:
Rasmus Munk Larsen 2023-02-14 02:29:03 +00:00
parent 4a03409569
commit 07aaa62e6f
4 changed files with 44 additions and 41 deletions

View File

@ -251,9 +251,9 @@ void mixed_pow_test() {
unary_pow_test<double, long long>(); unary_pow_test<double, long long>();
// The following cases will test promoting a wider exponent type // The following cases will test promoting a wider exponent type
// to a narrower base type. This should compile but generate a // to a narrower base type. This should compile but would generate a
// deprecation warning: // deprecation warning:
unary_pow_test<float, double>(); // unary_pow_test<float, double>();
} }
void int_pow_test() { void int_pow_test() {

View File

@ -43,11 +43,11 @@ void check_inf_nan(bool dryrun) {
} }
else else
{ {
if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !(numext::isfinite)(m(3)) ); g_test_level=0; if( (std::isfinite)(m(3))) { g_test_level=1; VERIFY( !(numext::isfinite)(m(3)) ); g_test_level=0; }
if( (std::isinf) (m(3))) g_test_level=1; VERIFY( !(numext::isinf)(m(3)) ); g_test_level=0; if( (std::isinf) (m(3))) { g_test_level=1; VERIFY( !(numext::isinf)(m(3)) ); g_test_level=0; }
if(!(std::isnan) (m(3))) g_test_level=1; VERIFY( (numext::isnan)(m(3)) ); g_test_level=0; if(!(std::isnan) (m(3))) { g_test_level=1; VERIFY( (numext::isnan)(m(3)) ); g_test_level=0; }
if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !m.allFinite() ); g_test_level=0; if( (std::isfinite)(m(3))) { g_test_level=1; VERIFY( !m.allFinite() ); g_test_level=0; }
if(!(std::isnan) (m(3))) g_test_level=1; VERIFY( m.hasNaN() ); g_test_level=0; if(!(std::isnan) (m(3))) { g_test_level=1; VERIFY( m.hasNaN() ); g_test_level=0; }
} }
T hidden_zero = (std::numeric_limits<T>::min)()*(std::numeric_limits<T>::min)(); T hidden_zero = (std::numeric_limits<T>::min)()*(std::numeric_limits<T>::min)();
m(4) /= hidden_zero; m(4) /= hidden_zero;
@ -62,11 +62,11 @@ void check_inf_nan(bool dryrun) {
} }
else else
{ {
if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !(numext::isfinite)(m(4)) ); g_test_level=0; if( (std::isfinite)(m(3))) { g_test_level=1; VERIFY( !(numext::isfinite)(m(4)) ); g_test_level=0; }
if(!(std::isinf) (m(3))) g_test_level=1; VERIFY( (numext::isinf)(m(4)) ); g_test_level=0; if(!(std::isinf) (m(3))) { g_test_level=1; VERIFY( (numext::isinf)(m(4)) ); g_test_level=0; }
if( (std::isnan) (m(3))) g_test_level=1; VERIFY( !(numext::isnan)(m(4)) ); g_test_level=0; if( (std::isnan) (m(3))) { g_test_level=1; VERIFY( !(numext::isnan)(m(4)) ); g_test_level=0; }
if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !m.allFinite() ); g_test_level=0; if( (std::isfinite)(m(3))) { g_test_level=1; VERIFY( !m.allFinite() ); g_test_level=0; }
if(!(std::isnan) (m(3))) g_test_level=1; VERIFY( m.hasNaN() ); g_test_level=0; if(!(std::isnan) (m(3))) { g_test_level=1; VERIFY( m.hasNaN() ); g_test_level=0; }
} }
m(3) = 0; m(3) = 0;
if(dryrun) if(dryrun)
@ -80,11 +80,11 @@ void check_inf_nan(bool dryrun) {
} }
else else
{ {
if(!(std::isfinite)(m(3))) g_test_level=1; VERIFY( (numext::isfinite)(m(3)) ); g_test_level=0; if(!(std::isfinite)(m(3))) { g_test_level=1; VERIFY( (numext::isfinite)(m(3)) ); g_test_level=0; }
if( (std::isinf) (m(3))) g_test_level=1; VERIFY( !(numext::isinf)(m(3)) ); g_test_level=0; if( (std::isinf) (m(3))) { g_test_level=1; VERIFY( !(numext::isinf)(m(3)) ); g_test_level=0; }
if( (std::isnan) (m(3))) g_test_level=1; VERIFY( !(numext::isnan)(m(3)) ); g_test_level=0; if( (std::isnan) (m(3))) { g_test_level=1; VERIFY( !(numext::isnan)(m(3)) ); g_test_level=0; }
if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !m.allFinite() ); g_test_level=0; if( (std::isfinite)(m(3))) { g_test_level=1; VERIFY( !m.allFinite() ); g_test_level=0; }
if( (std::isnan) (m(3))) g_test_level=1; VERIFY( !m.hasNaN() ); g_test_level=0; if( (std::isnan) (m(3))) { g_test_level=1; VERIFY( !m.hasNaN() ); g_test_level=0; }
} }
} }

View File

@ -164,7 +164,7 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
// test sort // test sort
if (inner > 1) { if (inner > 1) {
bool StorageOrdersMatch = DenseMatrix::IsRowMajor == SparseMatrixType::IsRowMajor; bool StorageOrdersMatch = int(DenseMatrix::IsRowMajor) == int(SparseMatrixType::IsRowMajor);
DenseMatrix m1(rows, cols); DenseMatrix m1(rows, cols);
m1.setZero(); m1.setZero();
SparseMatrixType m2(rows, cols); SparseMatrixType m2(rows, cols);

View File

@ -12,10 +12,11 @@
#include "main.h" #include "main.h"
#include <unsupported/Eigen/NNLS> #include <unsupported/Eigen/NNLS>
/// Check that 'x' solves the NNLS optimization problem `min ||A*x-b|| s.t. 0 <= x`. /// Check that 'x' solves the NNLS optimization problem `min ||A*x-b|| s.t. 0 <= x`.
/// The \p tolerance parameter is the absolute tolerance on the gradient, A'*(A*x-b). /// The \p tolerance parameter is the absolute tolerance on the gradient, A'*(A*x-b).
template <typename MatrixType, typename VectorB, typename VectorX, typename Scalar> template <typename MatrixType, typename VectorB, typename VectorX, typename Scalar>
static void verify_nnls_optimality(const MatrixType &A, const VectorB &b, const VectorX &x, const Scalar tolerance) { void verify_nnls_optimality(const MatrixType &A, const VectorB &b, const VectorX &x, const Scalar tolerance) {
// The NNLS optimality conditions are: // The NNLS optimality conditions are:
// //
// * 0 = A'*A*x - A'*b - lambda // * 0 = A'*A*x - A'*b - lambda
@ -38,7 +39,7 @@ static void verify_nnls_optimality(const MatrixType &A, const VectorB &b, const
} }
template <typename MatrixType, typename VectorB, typename VectorX> template <typename MatrixType, typename VectorB, typename VectorX>
static void test_nnls_known_solution(const MatrixType &A, const VectorB &b, const VectorX &x_expected) { void test_nnls_known_solution(const MatrixType &A, const VectorB &b, const VectorX &x_expected) {
using Scalar = typename MatrixType::Scalar; using Scalar = typename MatrixType::Scalar;
using std::sqrt; using std::sqrt;
@ -53,7 +54,7 @@ static void test_nnls_known_solution(const MatrixType &A, const VectorB &b, cons
} }
template <typename MatrixType> template <typename MatrixType>
static void test_nnls_random_problem() { void test_nnls_random_problem() {
// //
// SETUP // SETUP
// //
@ -101,7 +102,7 @@ static void test_nnls_random_problem() {
verify_nnls_optimality(A, b, x, tolerance); verify_nnls_optimality(A, b, x, tolerance);
} }
static void test_nnls_handles_zero_rhs() { void test_nnls_handles_zero_rhs() {
// //
// SETUP // SETUP
// //
@ -124,7 +125,7 @@ static void test_nnls_handles_zero_rhs() {
VERIFY_IS_EQUAL(x, VectorXd::Zero(cols)); VERIFY_IS_EQUAL(x, VectorXd::Zero(cols));
} }
static void test_nnls_handles_Mx0_matrix() { void test_nnls_handles_Mx0_matrix() {
// //
// SETUP // SETUP
// //
@ -146,7 +147,7 @@ static void test_nnls_handles_Mx0_matrix() {
VERIFY_IS_EQUAL(x.size(), 0); VERIFY_IS_EQUAL(x.size(), 0);
} }
static void test_nnls_handles_0x0_matrix() { void test_nnls_handles_0x0_matrix() {
// //
// SETUP // SETUP
// //
@ -167,7 +168,7 @@ static void test_nnls_handles_0x0_matrix() {
VERIFY_IS_EQUAL(x.size(), 0); VERIFY_IS_EQUAL(x.size(), 0);
} }
static void test_nnls_handles_dependent_columns() { void test_nnls_handles_dependent_columns() {
// //
// SETUP // SETUP
// //
@ -197,7 +198,7 @@ static void test_nnls_handles_dependent_columns() {
} }
} }
static void test_nnls_handles_wide_matrix() { void test_nnls_handles_wide_matrix() {
// //
// SETUP // SETUP
// //
@ -230,7 +231,7 @@ static void test_nnls_handles_wide_matrix() {
} }
// 4x2 problem, unconstrained solution positive // 4x2 problem, unconstrained solution positive
static void test_nnls_known_1() { void test_nnls_known_1() {
Matrix<double, 4, 2> A(4, 2); Matrix<double, 4, 2> A(4, 2);
Matrix<double, 4, 1> b(4); Matrix<double, 4, 1> b(4);
Matrix<double, 2, 1> x(2); Matrix<double, 2, 1> x(2);
@ -242,7 +243,7 @@ static void test_nnls_known_1() {
} }
// 4x3 problem, unconstrained solution positive // 4x3 problem, unconstrained solution positive
static void test_nnls_known_2() { void test_nnls_known_2() {
Matrix<double, 4, 3> A(4, 3); Matrix<double, 4, 3> A(4, 3);
Matrix<double, 4, 1> b(4); Matrix<double, 4, 1> b(4);
Matrix<double, 3, 1> x(3); Matrix<double, 3, 1> x(3);
@ -255,7 +256,7 @@ static void test_nnls_known_2() {
} }
// Simple 4x4 problem, unconstrained solution non-negative // Simple 4x4 problem, unconstrained solution non-negative
static void test_nnls_known_3() { void test_nnls_known_3() {
Matrix<double, 4, 4> A(4, 4); Matrix<double, 4, 4> A(4, 4);
Matrix<double, 4, 1> b(4); Matrix<double, 4, 1> b(4);
Matrix<double, 4, 1> x(4); Matrix<double, 4, 1> x(4);
@ -268,7 +269,7 @@ static void test_nnls_known_3() {
} }
// Simple 4x3 problem, unconstrained solution non-negative // Simple 4x3 problem, unconstrained solution non-negative
static void test_nnls_known_4() { void test_nnls_known_4() {
Matrix<double, 4, 3> A(4, 3); Matrix<double, 4, 3> A(4, 3);
Matrix<double, 4, 1> b(4); Matrix<double, 4, 1> b(4);
Matrix<double, 3, 1> x(3); Matrix<double, 3, 1> x(3);
@ -281,7 +282,7 @@ static void test_nnls_known_4() {
} }
// Simple 4x3 problem, unconstrained solution indefinite // Simple 4x3 problem, unconstrained solution indefinite
static void test_nnls_known_5() { void test_nnls_known_5() {
Matrix<double, 4, 3> A(4, 3); Matrix<double, 4, 3> A(4, 3);
Matrix<double, 4, 1> b(4); Matrix<double, 4, 1> b(4);
Matrix<double, 3, 1> x(3); Matrix<double, 3, 1> x(3);
@ -294,7 +295,7 @@ static void test_nnls_known_5() {
test_nnls_known_solution(A, b, x); test_nnls_known_solution(A, b, x);
} }
static void test_nnls_small_reference_problems() { void test_nnls_small_reference_problems() {
test_nnls_known_1(); test_nnls_known_1();
test_nnls_known_2(); test_nnls_known_2();
test_nnls_known_3(); test_nnls_known_3();
@ -302,7 +303,7 @@ static void test_nnls_small_reference_problems() {
test_nnls_known_5(); test_nnls_known_5();
} }
static void test_nnls_with_half_precision() { void test_nnls_with_half_precision() {
// The random matrix generation tools don't work with `half`, // The random matrix generation tools don't work with `half`,
// so here's a simpler setup mostly just to check that NNLS compiles & runs with custom scalar types. // so here's a simpler setup mostly just to check that NNLS compiles & runs with custom scalar types.
@ -319,7 +320,7 @@ static void test_nnls_with_half_precision() {
verify_nnls_optimality(A, b, x, half(1e-1)); verify_nnls_optimality(A, b, x, half(1e-1));
} }
static void test_nnls_special_case_solves_in_zero_iterations() { void test_nnls_special_case_solves_in_zero_iterations() {
// The particular NNLS algorithm that is implemented starts with all variables // The particular NNLS algorithm that is implemented starts with all variables
// in the active set. // in the active set.
// This test builds a system where all constraints are active at the solution, // This test builds a system where all constraints are active at the solution,
@ -346,7 +347,7 @@ static void test_nnls_special_case_solves_in_zero_iterations() {
VERIFY(nnls.iterations() == 0); VERIFY(nnls.iterations() == 0);
} }
static void test_nnls_special_case_solves_in_n_iterations() { void test_nnls_special_case_solves_in_n_iterations() {
// The particular NNLS algorithm that is implemented starts with all variables // The particular NNLS algorithm that is implemented starts with all variables
// in the active set and then adds one variable to the inactive set each iteration. // in the active set and then adds one variable to the inactive set each iteration.
// This test builds a system where all variables are inactive at the solution, // This test builds a system where all variables are inactive at the solution,
@ -370,7 +371,7 @@ static void test_nnls_special_case_solves_in_n_iterations() {
VERIFY(nnls.iterations() == n); VERIFY(nnls.iterations() == n);
} }
static void test_nnls_returns_NoConvergence_when_maxIterations_is_too_low() { void test_nnls_returns_NoConvergence_when_maxIterations_is_too_low() {
// Using the special case that takes `n` iterations, // Using the special case that takes `n` iterations,
// from `test_nnls_special_case_solves_in_n_iterations`, // from `test_nnls_special_case_solves_in_n_iterations`,
// we can set max iterations too low and that should cause the solve to fail. // we can set max iterations too low and that should cause the solve to fail.
@ -391,7 +392,7 @@ static void test_nnls_returns_NoConvergence_when_maxIterations_is_too_low() {
VERIFY(nnls.iterations() == max_iters); VERIFY(nnls.iterations() == max_iters);
} }
static void test_nnls_default_maxIterations_is_twice_column_count() { void test_nnls_default_maxIterations_is_twice_column_count() {
const Index cols = internal::random<Index>(1, EIGEN_TEST_MAX_SIZE); const Index cols = internal::random<Index>(1, EIGEN_TEST_MAX_SIZE);
const Index rows = internal::random<Index>(cols, EIGEN_TEST_MAX_SIZE); const Index rows = internal::random<Index>(cols, EIGEN_TEST_MAX_SIZE);
const MatrixXd A = MatrixXd::Random(rows, cols); const MatrixXd A = MatrixXd::Random(rows, cols);
@ -401,7 +402,7 @@ static void test_nnls_default_maxIterations_is_twice_column_count() {
VERIFY_IS_EQUAL(nnls.maxIterations(), 2 * cols); VERIFY_IS_EQUAL(nnls.maxIterations(), 2 * cols);
} }
static void test_nnls_does_not_allocate_during_solve() { void test_nnls_does_not_allocate_during_solve() {
const Index cols = internal::random<Index>(1, EIGEN_TEST_MAX_SIZE); const Index cols = internal::random<Index>(1, EIGEN_TEST_MAX_SIZE);
const Index rows = internal::random<Index>(cols, EIGEN_TEST_MAX_SIZE); const Index rows = internal::random<Index>(cols, EIGEN_TEST_MAX_SIZE);
const MatrixXd A = MatrixXd::Random(rows, cols); const MatrixXd A = MatrixXd::Random(rows, cols);
@ -414,7 +415,7 @@ static void test_nnls_does_not_allocate_during_solve() {
internal::set_is_malloc_allowed(true); internal::set_is_malloc_allowed(true);
} }
static void test_nnls_repeated_calls_to_compute_and_solve() { void test_nnls_repeated_calls_to_compute_and_solve() {
const Index cols2 = internal::random<Index>(1, EIGEN_TEST_MAX_SIZE); const Index cols2 = internal::random<Index>(1, EIGEN_TEST_MAX_SIZE);
const Index rows2 = internal::random<Index>(cols2, EIGEN_TEST_MAX_SIZE); const Index rows2 = internal::random<Index>(cols2, EIGEN_TEST_MAX_SIZE);
const MatrixXd A2 = MatrixXd::Random(rows2, cols2); const MatrixXd A2 = MatrixXd::Random(rows2, cols2);
@ -449,8 +450,10 @@ EIGEN_DECLARE_TEST(NNLS) {
// Essential NNLS properties, across different types. // Essential NNLS properties, across different types.
CALL_SUBTEST_2(test_nnls_random_problem<MatrixXf>()); CALL_SUBTEST_2(test_nnls_random_problem<MatrixXf>());
CALL_SUBTEST_3(test_nnls_random_problem<MatrixXd>()); CALL_SUBTEST_3(test_nnls_random_problem<MatrixXd>());
{
using MatFixed = Matrix<double, 12, 5>; using MatFixed = Matrix<double, 12, 5>;
CALL_SUBTEST_4(test_nnls_random_problem<MatFixed>()); CALL_SUBTEST_4(test_nnls_random_problem<MatFixed>());
}
CALL_SUBTEST_5(test_nnls_with_half_precision()); CALL_SUBTEST_5(test_nnls_with_half_precision());
// Robustness tests: // Robustness tests: