diff --git a/common/src/KokkosFFT_utils.hpp b/common/src/KokkosFFT_utils.hpp index bb77a1d4..6e2ca757 100644 --- a/common/src/KokkosFFT_utils.hpp +++ b/common/src/KokkosFFT_utils.hpp @@ -75,11 +75,14 @@ bool is_out_of_range_value_included(const ContainerType& values, IntType max) { static_assert(std::is_same_v, "is_out_of_range_value_included: Container value type must " "match IntType"); - bool is_included = false; - for (auto value : values) { - is_included = value >= max; + if constexpr (std::is_signed_v) { + KOKKOSFFT_THROW_IF( + std::any_of(values.begin(), values.end(), + [](value_type value) { return value < 0; }), + "is_out_of_range_value_included: values must be non-negative"); } - return is_included; + return std::any_of(values.begin(), values.end(), + [max](value_type value) { return value >= max; }); } template < diff --git a/common/unit_test/Test_Utils.cpp b/common/unit_test/Test_Utils.cpp index 609a714e..d104bd9f 100644 --- a/common/unit_test/Test_Utils.cpp +++ b/common/unit_test/Test_Utils.cpp @@ -332,16 +332,40 @@ void test_has_duplicate_values() { template void test_is_out_of_range_value_included() { using IntType = KokkosFFT::Impl::base_container_value_type; - ContainerType v = {0, 1, 2, 3, 4}; + ContainerType v = {0, 1, 2, 3, 4}, v2 = {0, 4, 1}; EXPECT_TRUE(KokkosFFT::Impl::is_out_of_range_value_included( v, static_cast(2))); EXPECT_TRUE(KokkosFFT::Impl::is_out_of_range_value_included( v, static_cast(3))); + EXPECT_TRUE(KokkosFFT::Impl::is_out_of_range_value_included( + v, static_cast(4))); EXPECT_FALSE(KokkosFFT::Impl::is_out_of_range_value_included( v, static_cast(5))); EXPECT_FALSE(KokkosFFT::Impl::is_out_of_range_value_included( v, static_cast(6))); + + EXPECT_TRUE(KokkosFFT::Impl::is_out_of_range_value_included( + v2, static_cast(2))); + EXPECT_TRUE(KokkosFFT::Impl::is_out_of_range_value_included( + v2, static_cast(3))); + EXPECT_TRUE(KokkosFFT::Impl::is_out_of_range_value_included( + v2, static_cast(4))); + EXPECT_FALSE(KokkosFFT::Impl::is_out_of_range_value_included( + v2, static_cast(5))); + EXPECT_FALSE(KokkosFFT::Impl::is_out_of_range_value_included( + v2, static_cast(6))); + + if constexpr (std::is_signed_v) { + // Since non-negative value is included, it should be invalid + ContainerType v3 = {0, 1, -1}; + EXPECT_THROW( + { + KokkosFFT::Impl::is_out_of_range_value_included( + v3, static_cast(2)); + }, + std::runtime_error); + } } template