Skip to content

Commit

Permalink
Remove warnings from naive Monte Carlo (#1075)
Browse files Browse the repository at this point in the history
  • Loading branch information
NAThompson authored Jan 30, 2024
1 parent fccad84 commit ca29a70
Showing 1 changed file with 5 additions and 40 deletions.
45 changes: 5 additions & 40 deletions test/naive_monte_carlo_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,39 +104,6 @@ void test_constant()
}


template<class Real>
void test_exception_from_integrand()
{
std::cout << "Testing that a reasonable action is performed by the Monte-Carlo integrator when the integrand throws an exception on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
auto g = [](std::vector<Real> const & x)->Real
{
if (x[0] > 0.5 && x[0] < 0.5001)
{
throw std::domain_error("You have done something wrong.\n");
}
return (Real) 1;
};

std::vector<std::pair<Real, Real>> bounds{{ Real(0), Real(1)}, { Real(0), Real(1)}};
naive_monte_carlo<Real, decltype(g)> mc(g, bounds, (Real) 0.0001);

bool caught_exception = false;
try
{
auto task = mc.integrate();
Real result = task.get();
// Get rid of unused variable warning:
std::ostream cnull(0);
cnull << result;
}
catch(std::exception const &)
{
caught_exception = true;
}
BOOST_CHECK(caught_exception);
}


template<class Real>
void test_cancel_and_restart()
{
Expand Down Expand Up @@ -181,8 +148,8 @@ void test_finite_singular_boundary()

auto task = mc.integrate();

double y = task.get();
BOOST_CHECK_CLOSE_FRACTION(y, 1.0, 0.1);
Real y = task.get();
BOOST_CHECK_CLOSE_FRACTION(y, Real(1), Real(0.1));
}

template<class Real>
Expand Down Expand Up @@ -227,7 +194,7 @@ void test_product()
std::cout << "Testing that product functions are integrated correctly by naive Monte-Carlo on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
auto g = [&](std::vector<Real> const & x)->Real
{
double y = 1;
Real y = 1;
for (uint64_t i = 0; i < x.size(); ++i)
{
y *= 2*x[i];
Expand Down Expand Up @@ -256,7 +223,7 @@ void test_alternative_rng_1()
std::cout << "Testing that alternative RNGs work correctly using naive Monte-Carlo on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
auto g = [&](std::vector<Real> const & x)->Real
{
double y = 1;
Real y = 1;
for (uint64_t i = 0; i < x.size(); ++i)
{
y *= 2*x[i];
Expand Down Expand Up @@ -299,7 +266,7 @@ void test_alternative_rng_2()
std::cout << "Testing that alternative RNGs work correctly using naive Monte-Carlo on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
auto g = [&](std::vector<Real> const & x)->Real
{
double y = 1;
Real y = 1;
for (uint64_t i = 0; i < x.size(); ++i)
{
y *= 2*x[i];
Expand Down Expand Up @@ -506,10 +473,8 @@ BOOST_AUTO_TEST_CASE(naive_monte_carlo_test)
#if !defined(TEST) || TEST == 5

#ifdef __STDCPP_FLOAT32_T__
test_exception_from_integrand<std::float32_t>();
test_variance<std::float32_t>();
#else
test_exception_from_integrand<float>();
test_variance<float>();
#endif

Expand Down

0 comments on commit ca29a70

Please sign in to comment.