Skip to content

Commit

Permalink
added tests for overloads that don't take in an execution policy and …
Browse files Browse the repository at this point in the history
…fixed sentinel tests
  • Loading branch information
Jedi18 committed May 24, 2021
1 parent 9adf3f5 commit 42ab176
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 37 deletions.
Expand Up @@ -165,6 +165,7 @@ namespace hpx {
#include <hpx/execution/algorithms/detail/predicates.hpp>
#include <hpx/executors/execution_policy.hpp>
#include <hpx/parallel/algorithms/detail/dispatch.hpp>
#include <hpx/parallel/algorithms/detail/distance.hpp>
#include <hpx/parallel/algorithms/for_each.hpp>
#include <hpx/parallel/algorithms/mismatch.hpp>
#include <hpx/parallel/util/detail/algorithm_result.hpp>
Expand Down Expand Up @@ -226,8 +227,8 @@ namespace hpx { namespace parallel { inline namespace v1 {
zip_iterator;
typedef typename zip_iterator::reference reference;

std::size_t count1 = std::distance(first1, last1);
std::size_t count2 = std::distance(first2, last2);
std::size_t count1 = detail::distance(first1, last1);
std::size_t count2 = detail::distance(first2, last2);

// An empty range is lexicographically less than any non-empty
// range
Expand Down Expand Up @@ -316,11 +317,18 @@ namespace hpx { namespace parallel { inline namespace v1 {
static_assert(hpx::traits::is_forward_iterator<FwdIter2>::value,
"Requires at least forward iterator.");

#if defined(HPX_GCC_VERSION) && HPX_GCC_VERSION >= 100000
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return detail::lexicographical_compare().call(
std::forward<ExPolicy>(policy), first1, last1, first2, last2,
std::forward<Pred>(pred),
hpx::parallel::util::projection_identity{},
hpx::parallel::util::projection_identity{});
#if defined(HPX_GCC_VERSION) && HPX_GCC_VERSION >= 100000
#pragma GCC diagnostic pop
#endif
}

}}} // namespace hpx::parallel::v1
Expand Down
Expand Up @@ -22,6 +22,25 @@
int seed = std::random_device{}();
std::mt19937 gen(seed);

template <typename IteratorTag>
void test_lexicographical_compare1(IteratorTag)
{
typedef std::vector<std::size_t>::iterator base_iterator;
typedef test::test_iterator<base_iterator, IteratorTag> iterator;

std::vector<std::size_t> c(10007);
std::iota(std::begin(c), std::end(c), 0);

//d is lexicographical less than c
std::vector<std::size_t> d(10006);
std::iota(std::begin(d), std::end(d), 0);

bool res = hpx::lexicographical_compare(iterator(std::begin(c)),
iterator(std::end(c)), std::begin(d), std::end(d));

HPX_TEST(!res);
}

template <typename ExPolicy, typename IteratorTag>
void test_lexicographical_compare1(ExPolicy policy, IteratorTag)
{
Expand All @@ -38,9 +57,8 @@ void test_lexicographical_compare1(ExPolicy policy, IteratorTag)
std::vector<std::size_t> d(10006);
std::iota(std::begin(d), std::end(d), 0);

bool res =
hpx::parallel::lexicographical_compare(policy, iterator(std::begin(c)),
iterator(std::end(c)), std::begin(d), std::end(d));
bool res = hpx::lexicographical_compare(policy, iterator(std::begin(c)),
iterator(std::end(c)), std::begin(d), std::end(d));

HPX_TEST(!res);
}
Expand All @@ -59,7 +77,7 @@ void test_lexicographical_compare1_async(ExPolicy p, IteratorTag)
std::iota(std::begin(d), std::end(d), 0);

hpx::future<bool> f =
hpx::parallel::lexicographical_compare(p, iterator(std::begin(c)),
hpx::lexicographical_compare(p, iterator(std::begin(c)),
iterator(std::end(c)), std::begin(d), std::end(d));

f.wait();
Expand All @@ -73,6 +91,7 @@ template <typename IteratorTag>
void test_lexicographical_compare1()
{
using namespace hpx::execution;
test_lexicographical_compare1(IteratorTag());
test_lexicographical_compare1(seq, IteratorTag());
test_lexicographical_compare1(par, IteratorTag());
test_lexicographical_compare1(par_unseq, IteratorTag());
Expand All @@ -88,6 +107,25 @@ void lexicographical_compare_test1()
}

////////////////////////////////////////////////////////////////////////////
template <typename IteratorTag>
void test_lexicographical_compare2(IteratorTag)
{
typedef std::vector<std::size_t>::iterator base_iterator;
typedef test::test_iterator<base_iterator, IteratorTag> iterator;

// lexicographically equal, so result is false
std::vector<std::size_t> c(10007);
std::iota(std::begin(c), std::end(c), 0);

std::vector<std::size_t> d(10007);
std::iota(std::begin(d), std::end(d), 0);

bool res = hpx::lexicographical_compare(iterator(std::begin(c)),
iterator(std::end(c)), std::begin(d), std::end(d));

HPX_TEST(!res);
}

template <typename ExPolicy, typename IteratorTag>
void test_lexicographical_compare2(ExPolicy policy, IteratorTag)
{
Expand All @@ -104,9 +142,8 @@ void test_lexicographical_compare2(ExPolicy policy, IteratorTag)
std::vector<std::size_t> d(10007);
std::iota(std::begin(d), std::end(d), 0);

bool res =
hpx::parallel::lexicographical_compare(policy, iterator(std::begin(c)),
iterator(std::end(c)), std::begin(d), std::end(d));
bool res = hpx::lexicographical_compare(policy, iterator(std::begin(c)),
iterator(std::end(c)), std::begin(d), std::end(d));

HPX_TEST(!res);
}
Expand All @@ -125,7 +162,7 @@ void test_lexicographical_compare2_async(ExPolicy p, IteratorTag)
std::iota(std::begin(d), std::end(d), 0);

hpx::future<bool> f =
hpx::parallel::lexicographical_compare(p, iterator(std::begin(c)),
hpx::lexicographical_compare(p, iterator(std::begin(c)),
iterator(std::end(c)), std::begin(d), std::end(d));

f.wait();
Expand All @@ -137,6 +174,7 @@ template <typename IteratorTag>
void test_lexicographical_compare2()
{
using namespace hpx::execution;
test_lexicographical_compare2(IteratorTag());
test_lexicographical_compare2(seq, IteratorTag());
test_lexicographical_compare2(par, IteratorTag());
test_lexicographical_compare2(par_unseq, IteratorTag());
Expand All @@ -152,6 +190,28 @@ void lexicographical_compare_test2()
}

////////////////////////////////////////////////////////////////////////////
template <typename IteratorTag>
void test_lexicographical_compare3(IteratorTag)
{
typedef std::vector<std::size_t>::iterator base_iterator;
typedef test::test_iterator<base_iterator, IteratorTag> iterator;

// C is lexicographically less due to the (gen() % size + 1)th
// element being less than D
std::vector<std::size_t> c(10007);
std::iota(std::begin(c), std::end(c), 0);
std::uniform_int_distribution<> dis(1, 5000);
c[dis(gen)] = 0; //-V108

std::vector<std::size_t> d(10007);
std::iota(std::begin(d), std::end(d), 0);

bool res = hpx::lexicographical_compare(iterator(std::begin(c)),
iterator(std::end(c)), std::begin(d), std::end(d));

HPX_TEST(res);
}

template <typename ExPolicy, typename IteratorTag>
void test_lexicographical_compare3(ExPolicy policy, IteratorTag)
{
Expand All @@ -171,9 +231,8 @@ void test_lexicographical_compare3(ExPolicy policy, IteratorTag)
std::vector<std::size_t> d(10007);
std::iota(std::begin(d), std::end(d), 0);

bool res =
hpx::parallel::lexicographical_compare(policy, iterator(std::begin(c)),
iterator(std::end(c)), std::begin(d), std::end(d));
bool res = hpx::lexicographical_compare(policy, iterator(std::begin(c)),
iterator(std::end(c)), std::begin(d), std::end(d));

HPX_TEST(res);
}
Expand All @@ -193,7 +252,7 @@ void test_lexicographical_compare3_async(ExPolicy p, IteratorTag)
std::iota(std::begin(d), std::end(d), 0);

hpx::future<bool> f =
hpx::parallel::lexicographical_compare(p, iterator(std::begin(c)),
hpx::lexicographical_compare(p, iterator(std::begin(c)),
iterator(std::end(c)), std::begin(d), std::end(d));

f.wait();
Expand All @@ -205,6 +264,7 @@ template <typename IteratorTag>
void test_lexicographical_compare3()
{
using namespace hpx::execution;
test_lexicographical_compare3(IteratorTag());
test_lexicographical_compare3(seq, IteratorTag());
test_lexicographical_compare3(par, IteratorTag());
test_lexicographical_compare3(par_unseq, IteratorTag());
Expand Down Expand Up @@ -239,7 +299,7 @@ void test_lexicographical_compare_exception(ExPolicy policy, IteratorTag)
bool caught_exception = false;
try
{
hpx::parallel::lexicographical_compare(policy,
hpx::lexicographical_compare(policy,
decorated_iterator(
std::begin(c), []() { throw std::runtime_error("test"); }),
decorated_iterator(
Expand Down Expand Up @@ -277,7 +337,7 @@ void test_lexicographical_compare_async_exception(ExPolicy p, IteratorTag)
bool returned_from_algorithm = false;
try
{
hpx::future<bool> f = hpx::parallel::lexicographical_compare(p,
hpx::future<bool> f = hpx::lexicographical_compare(p,
decorated_iterator(
std::begin(c), []() { throw std::runtime_error("test"); }),
decorated_iterator(
Expand Down Expand Up @@ -342,7 +402,7 @@ void test_lexicographical_compare_bad_alloc(ExPolicy policy, IteratorTag)
bool caught_bad_alloc = false;
try
{
hpx::parallel::lexicographical_compare(policy,
hpx::lexicographical_compare(policy,
decorated_iterator(std::begin(c), []() { throw std::bad_alloc(); }),
decorated_iterator(std::end(c), []() { throw std::bad_alloc(); }),
std::begin(h), std::end(h));
Expand Down Expand Up @@ -377,7 +437,7 @@ void test_lexicographical_compare_async_bad_alloc(ExPolicy p, IteratorTag)
bool returned_from_algorithm = false;
try
{
hpx::future<bool> f = hpx::parallel::lexicographical_compare(p,
hpx::future<bool> f = hpx::lexicographical_compare(p,
decorated_iterator(std::begin(c), []() { throw std::bad_alloc(); }),
decorated_iterator(std::end(c), []() { throw std::bad_alloc(); }),
std::begin(h), std::end(h));
Expand Down
Expand Up @@ -10,13 +10,13 @@
#include <hpx/modules/testing.hpp>
#include <hpx/parallel/container_algorithms/lexicographical_compare.hpp>

#include <cstddef>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <random>
#include <string>
#include <unordered_set>
#include <vector>
#include <algorithm>

#include "test_utils.hpp"

Expand All @@ -27,13 +27,31 @@ std::uniform_int_distribution<> dis(0, 25);

void test_fill_sent()
{
std::vector<char> c1(10);
std::generate(
std::begin(c1), std::end(c1), []() { return 'a' + dis(gen); });

std::vector<char> c2(10);
std::generate(
std::begin(c2), std::end(c2), []() { return 'a' + dis(gen); });
// ensure all characters are unique
std::unordered_set<char> uset;

std::vector<char> c1(7);
std::generate(std::begin(c1), std::end(c1), [&uset]() {
char c = 'a' + dis(gen);
while (uset.find(c) != uset.end())
{
c = 'a' + dis(gen);
}
uset.insert(c);
return c;
});

uset.clear();
std::vector<char> c2(7);
std::generate(std::begin(c2), std::end(c2), [&uset]() {
char c = 'a' + dis(gen);
while (uset.find(c) != uset.end())
{
c = 'a' + dis(gen);
}
uset.insert(c);
return c;
});

bool actual_result1 = std::lexicographical_compare(
std::begin(c1), std::begin(c1) + 5, std::begin(c2), std::begin(c2) + 5);
Expand Down Expand Up @@ -75,13 +93,31 @@ void test_fill_sent(ExPolicy policy)
static_assert(hpx::is_execution_policy<ExPolicy>::value,
"hpx::is_execution_policy<ExPolicy>::value");

std::vector<char> c1(10);
std::generate(
std::begin(c1), std::end(c1), []() { return 'a' + dis(gen); });

std::vector<char> c2(10);
std::generate(
std::begin(c2), std::end(c2), []() { return 'a' + dis(gen); });
// ensure all characters are unique
std::unordered_set<char> uset;

std::vector<char> c1(7);
std::generate(std::begin(c1), std::end(c1), [&uset]() {
char c = 'a' + dis(gen);
while (uset.find(c) != uset.end())
{
c = 'a' + dis(gen);
}
uset.insert(c);
return c;
});

uset.clear();
std::vector<char> c2(7);
std::generate(std::begin(c2), std::end(c2), [&uset]() {
char c = 'a' + dis(gen);
while (uset.find(c) != uset.end())
{
c = 'a' + dis(gen);
}
uset.insert(c);
return c;
});

bool actual_result1 = std::lexicographical_compare(
std::begin(c1), std::begin(c1) + 5, std::begin(c2), std::begin(c2) + 5);
Expand Down Expand Up @@ -121,8 +157,8 @@ template <typename IteratorTag>
void test_lexicographical_compare(IteratorTag)
{
std::vector<char> c1(10);
std::generate(std::begin(c1), std::end(c1),
[]() { return 'a' + dis(gen); });
std::generate(
std::begin(c1), std::end(c1), []() { return 'a' + dis(gen); });

std::vector<char> c2(10);
std::generate(
Expand Down

0 comments on commit 42ab176

Please sign in to comment.