From 60b89c5c088f3a633c955eefeb88d249b57f1ac3 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sun, 4 Aug 2024 19:12:42 -0500 Subject: [PATCH] updated build system --- CMakeLists.txt | 5 --- support/include/utils/common.hpp | 14 ++++++-- support/test/src/utils/common_test.cpp | 50 ++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 83de1276..5b436d57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,11 +18,6 @@ if(HAS_SETXATTR) add_definitions(-DHAS_SETXATTR) endif() -check_include_files("wordexp.h" HAS_WORDEXP_H) -if(HAS_WORDEXP_H) - add_definitions(-DHAS_WORDEXP_H) -endif() - include(cmake/versions.cmake) include(cmake/arch.cmake) include(cmake/os.cmake) diff --git a/support/include/utils/common.hpp b/support/include/utils/common.hpp index 8ff5e0cb..fd670173 100644 --- a/support/include/utils/common.hpp +++ b/support/include/utils/common.hpp @@ -66,11 +66,13 @@ template template [[nodiscard]] inline auto generate_random(std::size_t size) -> data_t; +#endif // defined(PROJECT_ENABLE_LIBSODIUM) template [[nodiscard]] inline auto generate_random_between(data_t begin, data_t end) -> data_t; +#if defined(PROJECT_ENABLE_LIBSODIUM) [[nodiscard]] auto generate_random_string(std::size_t length) -> std::string; [[nodiscard]] auto generate_random_wstring(std::size_t length) -> std::wstring; @@ -121,12 +123,20 @@ inline auto generate_random(std::size_t size) -> data_t { randombytes_buf(ret.data(), ret.size() * sizeof(typename data_t::value_type)); return ret; } +#endif // defined(PROJECT_ENABLE_LIBSODIUM) template inline auto generate_random_between(data_t begin, data_t end) -> data_t { - return begin + generate_random() % ((end + data_t{1}) - begin); + static_assert(std::is_integral_v>, + "data_t must be an integral type"); + if (end <= begin) { + throw std::range_error("end must be greater than begin"); + } + + static std::mt19937 gen(std::random_device{}()); + std::uniform_int_distribution dis(begin, end); + return dis(gen); } -#endif // defined(PROJECT_ENABLE_LIBSODIUM) } // namespace repertory::utils #endif // REPERTORY_INCLUDE_UTILS_COMMON_HPP_ diff --git a/support/test/src/utils/common_test.cpp b/support/test/src/utils/common_test.cpp index c1ad9e85..5215c9c2 100644 --- a/support/test/src/utils/common_test.cpp +++ b/support/test/src/utils/common_test.cpp @@ -184,4 +184,54 @@ TEST(utils_common, divide_with_ceiling) { r = utils::divide_with_ceiling(0, 2); EXPECT_EQ(0, r); } + +TEST(utils_common, generate_random_between_for_signed_integers) { + static constexpr const auto max_iterations{1000000UL}; + + for (std::size_t idx = 0U; idx < max_iterations; ++idx) { + auto res = utils::generate_random_between(5, 12); + EXPECT_GE(res, 5); + EXPECT_LE(res, 12); + } + + for (std::size_t idx = 0U; idx < max_iterations; ++idx) { + auto res = utils::generate_random_between(-5, 12); + EXPECT_GE(res, -5); + EXPECT_LE(res, 12); + } +} + +TEST(utils_common, generate_random_between_for_unsigned_integers) { + static constexpr const auto max_iterations{1000000UL}; + + for (std::size_t idx = 0U; idx < max_iterations; ++idx) { + auto res = utils::generate_random_between(5U, 12U); + EXPECT_GE(res, 5); + EXPECT_LE(res, 12); + } +} + +TEST(utils_common, generate_random_between_throws_error_on_invalid_range) { + EXPECT_THROW( + { + try { + [[maybe_unused]] auto res = utils::generate_random_between(12, 5); + } catch (const std::range_error &e) { + EXPECT_STREQ("end must be greater than begin", e.what()); + throw; + } + }, + std::range_error); + + EXPECT_THROW( + { + try { + [[maybe_unused]] auto res = utils::generate_random_between(12, 12); + } catch (const std::range_error &e) { + EXPECT_STREQ("end must be greater than begin", e.what()); + throw; + } + }, + std::range_error); +} } // namespace repertory