Skip to content

Commit

Permalink
Check whether input points are sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
tpadioleau committed Dec 3, 2024
1 parent 58d3b65 commit fcc2a6e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions include/ddc/non_uniform_point_sampling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

#pragma once

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <initializer_list>
#include <ostream>
#include <stdexcept>
#include <tuple>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -66,6 +68,9 @@ class NonUniformPointSampling : detail::NonUniformPointSamplingBase
/// @brief Construct a `NonUniformPointSampling` using a brace-list, i.e. `NonUniformPointSampling mesh({0., 1.})`
Impl(std::initializer_list<continuous_element_type> points)
{
if (!std::is_sorted(points.begin(), points.end())) {
throw std::runtime_error("Input points must be sorted");
}
std::vector<continuous_element_type> host_points(points.begin(), points.end());
Kokkos::View<continuous_element_type*, Kokkos::HostSpace> const
host(host_points.data(), host_points.size());
Expand All @@ -77,6 +82,9 @@ class NonUniformPointSampling : detail::NonUniformPointSamplingBase
template <class InputRange>
explicit Impl(InputRange const& points)
{
if (!std::is_sorted(points.begin(), points.end())) {
throw std::runtime_error("Input points must be sorted");
}
if constexpr (Kokkos::is_view_v<InputRange>) {
Kokkos::deep_copy(m_points, points);
} else {
Expand All @@ -92,6 +100,9 @@ class NonUniformPointSampling : detail::NonUniformPointSamplingBase
template <class InputIt>
Impl(InputIt points_begin, InputIt points_end)
{
if (!std::is_sorted(points_begin, points_end)) {
throw std::runtime_error("Input points must be sorted");
}
std::vector<continuous_element_type> host_points(points_begin, points_end);
Kokkos::View<continuous_element_type*, Kokkos::HostSpace> const
host(host_points.data(), host_points.size());
Expand Down
10 changes: 10 additions & 0 deletions tests/non_uniform_point_sampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <array>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#include <ddc/ddc.hpp>
Expand Down Expand Up @@ -75,6 +76,15 @@ TEST(NonUniformPointSamplingTest, VectorConstructor)
EXPECT_EQ(ddim_x.coordinate(point_ix), point_rx);
}

TEST(NonUniformPointSamplingTest, NotSortedVectorConstructor)
{
std::vector unordered_vector_points_x = vector_points_x;
std::swap(unordered_vector_points_x.front(), unordered_vector_points_x.back());
EXPECT_THROW(
(DDimX::Impl<DDimX, Kokkos::HostSpace>(unordered_vector_points_x)),
std::runtime_error);
}

TEST(NonUniformPointSamplingTest, IteratorConstructor)
{
DDimX::Impl<DDimX, Kokkos::HostSpace> const
Expand Down

0 comments on commit fcc2a6e

Please sign in to comment.