text: //  Copyright (c) 2014 Grant Mercer
  //
  //  SPDX-License-Identifier: BSL-1.0
  //  Distributed under the Boost Software License, Version 1.0. (See accompanying
  //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  
  #include <pika/local/algorithm.hpp>
  #include <pika/local/init.hpp>
  #include <pika/modules/testing.hpp>
  
  #include <cstddef>
  #include <iostream>
  #include <iterator>
  #include <numeric>
  #include <random>
  #include <string>
  #include <vector>
  
  #include "test_utils.hpp"
  
  ///////////////////////////////////////////////////////////////////////////////
  unsigned int seed = std::random_device{}();
  std::mt19937 gen(seed);
  
  template <typename ExPolicy, typename IteratorTag>
  void test_adjacent_find_exception(ExPolicy policy, IteratorTag)
  {
      static_assert(pika::is_execution_policy<ExPolicy>::value,
          "pika::is_execution_policy<ExPolicy>::value");
  
      typedef std::vector<std::size_t>::iterator base_iterator;
      typedef test::decorated_iterator<base_iterator, IteratorTag>
          decorated_iterator;
      std::vector<std::size_t> c(10007);
      std::iota(std::begin(c), std::end(c), gen() + 1);
  
      bool caught_exception = false;
      try
      {
          pika::adjacent_find(policy,
              decorated_iterator(
                  std::begin(c), []() { throw std::runtime_error("test"); }),
              decorated_iterator(std::end(c)));
          PIKA_TEST(false);
      }
      catch (pika::exception_list const& e)
      {
          caught_exception = true;
          test::test_num_exceptions<ExPolicy, IteratorTag>::call(policy, e);
      }
      catch (...)
      {
          PIKA_TEST(false);
      }
  
      PIKA_TEST(caught_exception);
  }
  
  template <typename ExPolicy, typename IteratorTag>
  void test_adjacent_find_exception_async(ExPolicy p, IteratorTag)
  {
      typedef std::vector<std::size_t>::iterator base_iterator;
      typedef test::decorated_iterator<base_iterator, IteratorTag>
          decorated_iterator;
  
      std::vector<std::size_t> c(10007);
      std::iota(std::begin(c), std::end(c), gen() + 1);
  
      bool caught_exception = false;
      bool returned_from_algorithm = false;
  
      try
      {
          pika::future<decorated_iterator> f = pika::adjacent_find(p,
              decorated_iterator(
                  std::begin(c), []() { throw std::runtime_error("test"); }),
              decorated_iterator(std::end(c)));
  
          returned_from_algorithm = true;
  
          f.get();
  
          PIKA_TEST(false);
      }
      catch (pika::exception_list const& e)
      {
          caught_exception = true;
          test::test_num_exceptions<ExPolicy, IteratorTag>::call(p, e);
      }
      catch (...)
      {
          PIKA_TEST(false);
      }
  
      PIKA_TEST(caught_exception);
      PIKA_TEST(returned_from_algorithm);
  }
  
  template <typename IteratorTag>
  void test_adjacent_find_exception()
  {
      using namespace pika::execution;
  
      // If the execution policy object is of type vector_execution_policy,
      // std::terminate shall be called. therefore we do not test exceptions
      // with a vector execution policy
      test_adjacent_find_exception(seq, IteratorTag());
      test_adjacent_find_exception(par, IteratorTag());
  
      test_adjacent_find_exception_async(seq(task), IteratorTag());
      test_adjacent_find_exception_async(par(task), IteratorTag());
  }
  
  void adjacent_find_exception_test()
  {
      test_adjacent_find_exception<std::random_access_iterator_tag>();
      test_adjacent_find_exception<std::forward_iterator_tag>();
  }
  
  int pika_main(pika::program_options::variables_map& vm)
  {
      if (vm.count("seed"))
          seed = vm["seed"].as<unsigned int>();
  
      std::cout << "using seed: " << seed << std::endl;
      gen.seed(seed);
  
      adjacent_find_exception_test();
      return pika::local::finalize();
  }
  
  int main(int argc, char* argv[])
  {
      // add command line option which controls the random number generator seed
      using namespace pika::program_options;
      options_description desc_commandline(
          "Usage: " PIKA_APPLICATION_STRING " [options]");
  
      desc_commandline.add_options()("seed,s", value<unsigned int>(),
          "the random number generator seed to use for this run");
  
      // By default this test should run on all available cores
      std::vector<std::string> const cfg = {"pika.os_threads=all"};
  
      // Initialize and run pika
      pika::local::init_params init_args;
      init_args.desc_cmdline = desc_commandline;
      init_args.cfg = cfg;
  
      PIKA_TEST_EQ_MSG(pika::local::init(pika_main, argc, argv, init_args), 0,
          "pika main exited with non-zero status");
  
      return pika::util::report_errors();
  }
  