From 73729025a4740d344bae98dfbae36a69405e217f Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Wed, 4 Apr 2018 13:45:34 +0200 Subject: bug #1521: add unit test dedicated to numbest::hypos --- test/stable_norm.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'test') diff --git a/test/stable_norm.cpp b/test/stable_norm.cpp index 3c02474b8..e4b97a6da 100644 --- a/test/stable_norm.cpp +++ b/test/stable_norm.cpp @@ -190,13 +190,52 @@ template void stable_norm(const MatrixType& m) } } +template +void test_hypot() +{ + typedef typename NumTraits::Real RealScalar; + Scalar factor = internal::random(); + while(numext::abs2(factor)(); + Scalar big = factor * ((std::numeric_limits::max)() * RealScalar(1e-4)); + + factor = internal::random(); + while(numext::abs2(factor)(); + Scalar small = factor * ((std::numeric_limits::min)() * RealScalar(1e4)); + + std::cout << big << " " << small << "\n"; + + Scalar one (1), + zero (0), + sqrt2 (std::sqrt(2)), + nan (std::numeric_limits::quiet_NaN()); + + Scalar a = internal::random(-1,1); + Scalar b = internal::random(-1,1); + VERIFY_IS_APPROX(numext::hypot(a,b),std::sqrt(numext::abs2(a)+numext::abs2(b))); + VERIFY_IS_EQUAL(numext::hypot(zero,zero), zero); + VERIFY_IS_APPROX(numext::hypot(one, one), sqrt2); + VERIFY_IS_APPROX(numext::hypot(big,big), sqrt2*numext::abs(big)); + VERIFY_IS_APPROX(numext::hypot(small,small), sqrt2*numext::abs(small)); + VERIFY_IS_APPROX(numext::hypot(small,big), numext::abs(big)); + VERIFY((numext::isnan)(numext::hypot(nan,a))); + VERIFY((numext::isnan)(numext::hypot(a,nan))); +} + void test_stable_norm() { for(int i = 0; i < g_repeat; i++) { + CALL_SUBTEST_3( test_hypot() ); + CALL_SUBTEST_4( test_hypot() ); + CALL_SUBTEST_5( test_hypot >() ); + CALL_SUBTEST_6( test_hypot >() ); + CALL_SUBTEST_1( stable_norm(Matrix()) ); CALL_SUBTEST_2( stable_norm(Vector4d()) ); CALL_SUBTEST_3( stable_norm(VectorXd(internal::random(10,2000))) ); CALL_SUBTEST_4( stable_norm(VectorXf(internal::random(10,2000))) ); CALL_SUBTEST_5( stable_norm(VectorXcd(internal::random(10,2000))) ); + CALL_SUBTEST_6( stable_norm(VectorXcf(internal::random(10,2000))) ); } } -- cgit v1.2.3