diff options
Diffstat (limited to 'abseil-cpp/absl/random/beta_distribution_test.cc')
-rw-r--r-- | abseil-cpp/absl/random/beta_distribution_test.cc | 104 |
1 files changed, 50 insertions, 54 deletions
diff --git a/abseil-cpp/absl/random/beta_distribution_test.cc b/abseil-cpp/absl/random/beta_distribution_test.cc index 277e4dc..c93b2a3 100644 --- a/abseil-cpp/absl/random/beta_distribution_test.cc +++ b/abseil-cpp/absl/random/beta_distribution_test.cc @@ -15,18 +15,21 @@ #include "absl/random/beta_distribution.h" #include <algorithm> +#include <cfloat> #include <cstddef> #include <cstdint> #include <iterator> #include <random> #include <sstream> #include <string> +#include <type_traits> #include <unordered_map> #include <vector> #include "gmock/gmock.h" #include "gtest/gtest.h" -#include "absl/base/internal/raw_logging.h" +#include "absl/log/log.h" +#include "absl/numeric/internal/representation.h" #include "absl/random/internal/chi_square.h" #include "absl/random/internal/distribution_test_util.h" #include "absl/random/internal/pcg_engine.h" @@ -42,8 +45,26 @@ namespace { template <typename IntType> class BetaDistributionInterfaceTest : public ::testing::Test {}; -using RealTypes = ::testing::Types<float, double, long double>; -TYPED_TEST_CASE(BetaDistributionInterfaceTest, RealTypes); +constexpr bool ShouldExerciseLongDoubleTests() { + // long double arithmetic is not supported well by either GCC or Clang on + // most platforms specifically not when implemented in terms of double-double; + // see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99048, + // https://bugs.llvm.org/show_bug.cgi?id=49131, and + // https://bugs.llvm.org/show_bug.cgi?id=49132. + // So a conservative choice here is to disable long-double tests pretty much + // everywhere except on x64 but only if long double is not implemented as + // double-double. +#if defined(__i686__) && defined(__x86_64__) + return !absl::numeric_internal::IsDoubleDouble(); +#else + return false; +#endif +} + +using RealTypes = std::conditional<ShouldExerciseLongDoubleTests(), + ::testing::Types<float, double, long double>, + ::testing::Types<float, double>>::type; +TYPED_TEST_SUITE(BetaDistributionInterfaceTest, RealTypes); TYPED_TEST(BetaDistributionInterfaceTest, SerializeTest) { // The threshold for whether std::exp(1/a) is finite. @@ -53,9 +74,6 @@ TYPED_TEST(BetaDistributionInterfaceTest, SerializeTest) { const TypeParam kLargeA = std::exp(std::log((std::numeric_limits<TypeParam>::max)()) - std::log(std::log((std::numeric_limits<TypeParam>::max)()))); - const TypeParam kLargeAPPC = std::exp( - std::log((std::numeric_limits<TypeParam>::max)()) - - std::log(std::log((std::numeric_limits<TypeParam>::max)())) - 10.0f); using param_type = typename absl::beta_distribution<TypeParam>::param_type; constexpr int kCount = 1000; @@ -76,9 +94,6 @@ TYPED_TEST(BetaDistributionInterfaceTest, SerializeTest) { kLargeA, // std::nextafter(kLargeA, TypeParam(0)), // std::nextafter(kLargeA, std::numeric_limits<TypeParam>::max()), - kLargeAPPC, // - std::nextafter(kLargeAPPC, TypeParam(0)), - std::nextafter(kLargeAPPC, std::numeric_limits<TypeParam>::max()), // Boundary cases. std::numeric_limits<TypeParam>::max(), std::numeric_limits<TypeParam>::epsilon(), @@ -92,8 +107,8 @@ TYPED_TEST(BetaDistributionInterfaceTest, SerializeTest) { }; for (TypeParam alpha : kValues) { for (TypeParam beta : kValues) { - ABSL_INTERNAL_LOG( - INFO, absl::StrFormat("Smoke test for Beta(%a, %a)", alpha, beta)); + LOG(INFO) << absl::StreamFormat("Smoke test for Beta(%a, %a)", alpha, + beta); param_type param(alpha, beta); absl::beta_distribution<TypeParam> before(alpha, beta); @@ -125,28 +140,6 @@ TYPED_TEST(BetaDistributionInterfaceTest, SerializeTest) { ss >> after; -#if defined(__powerpc64__) || defined(__PPC64__) || defined(__powerpc__) || \ - defined(__ppc__) || defined(__PPC__) - if (std::is_same<TypeParam, long double>::value) { - // Roundtripping floating point values requires sufficient precision - // to reconstruct the exact value. It turns out that long double - // has some errors doing this on ppc. - if (alpha <= std::numeric_limits<double>::max() && - alpha >= std::numeric_limits<double>::lowest()) { - EXPECT_EQ(static_cast<double>(before.alpha()), - static_cast<double>(after.alpha())) - << ss.str(); - } - if (beta <= std::numeric_limits<double>::max() && - beta >= std::numeric_limits<double>::lowest()) { - EXPECT_EQ(static_cast<double>(before.beta()), - static_cast<double>(after.beta())) - << ss.str(); - } - continue; - } -#endif - EXPECT_EQ(before.alpha(), after.alpha()); EXPECT_EQ(before.beta(), after.beta()); EXPECT_EQ(before, after) // @@ -334,15 +327,13 @@ bool BetaDistributionTest::SingleZTestOnMeanAndVariance(double p, absl::random_internal::Near("z", z_mean, 0.0, max_err) && absl::random_internal::Near("z_variance", z_variance, 0.0, max_err); if (!pass) { - ABSL_INTERNAL_LOG( - INFO, - absl::StrFormat( - "Beta(%f, %f), " - "mean: sample %f, expect %f, which is %f stddevs away, " - "variance: sample %f, expect %f, which is %f stddevs away.", - alpha_, beta_, m.mean, Mean(), - std::abs(m.mean - Mean()) / mean_stddev, m.variance, Variance(), - std::abs(m.variance - Variance()) / variance_stddev)); + LOG(INFO) << "Beta(" << alpha_ << ", " << beta_ << "), mean: sample " + << m.mean << ", expect " << Mean() << ", which is " + << std::abs(m.mean - Mean()) / mean_stddev + << " stddevs away, variance: sample " << m.variance << ", expect " + << Variance() << ", which is " + << std::abs(m.variance - Variance()) / variance_stddev + << " stddevs away."; } return pass; } @@ -403,18 +394,15 @@ bool BetaDistributionTest::SingleChiSquaredTest(double p, size_t samples, const bool pass = (absl::random_internal::ChiSquarePValue(chi_square, dof) >= p); if (!pass) { - for (int i = 0; i < cutoffs.size(); i++) { - ABSL_INTERNAL_LOG( - INFO, absl::StrFormat("cutoff[%d] = %f, actual count %d, expected %d", - i, cutoffs[i], counts[i], - static_cast<int>(expected[i]))); + for (size_t i = 0; i < cutoffs.size(); i++) { + LOG(INFO) << "cutoff[" << i << "] = " << cutoffs[i] << ", actual count " + << counts[i] << ", expected " << static_cast<int>(expected[i]); } - ABSL_INTERNAL_LOG( - INFO, absl::StrFormat( - "Beta(%f, %f) %s %f, p = %f", alpha_, beta_, - absl::random_internal::kChiSquared, chi_square, - absl::random_internal::ChiSquarePValue(chi_square, dof))); + LOG(INFO) << "Beta(" << alpha_ << ", " << beta_ << ") " + << absl::random_internal::kChiSquared << " " << chi_square + << ", p = " + << absl::random_internal::ChiSquarePValue(chi_square, dof); } return pass; } @@ -448,13 +436,13 @@ std::string ParamName( return absl::StrReplaceAll(name, {{"+", "_"}, {"-", "_"}, {".", "_"}}); } -INSTANTIATE_TEST_CASE_P( +INSTANTIATE_TEST_SUITE_P( TestSampleStatisticsCombinations, BetaDistributionTest, ::testing::Combine(::testing::Values(0.1, 0.2, 0.9, 1.1, 2.5, 10.0, 123.4), ::testing::Values(0.1, 0.2, 0.9, 1.1, 2.5, 10.0, 123.4)), ParamName); -INSTANTIATE_TEST_CASE_P( +INSTANTIATE_TEST_SUITE_P( TestSampleStatistics_SelectedPairs, BetaDistributionTest, ::testing::Values(std::make_pair(0.5, 1000), std::make_pair(1000, 0.5), std::make_pair(900, 1000), std::make_pair(10000, 20000), @@ -576,6 +564,14 @@ TEST(BetaDistributionTest, StabilityTest) { // dependencies of the distribution change, such as RandU64ToDouble, then this // is also likely to change. TEST(BetaDistributionTest, AlgorithmBounds) { +#if (defined(__i386__) || defined(_M_IX86)) && FLT_EVAL_METHOD != 0 + // We're using an x87-compatible FPU, and intermediate operations are + // performed with 80-bit floats. This produces slightly different results from + // what we expect below. + GTEST_SKIP() + << "Skipping the test because we detected x87 floating-point semantics"; +#endif + { absl::random_internal::sequence_urbg urbg( {0x7fbe76c8b4395800ull, 0x8000000000000000ull}); |