diff options
author | Philip Chimento <philip.chimento@gmail.com> | 2022-11-05 18:01:36 -0700 |
---|---|---|
committer | Philip Chimento <philip.chimento@gmail.com> | 2023-02-20 23:12:40 -0800 |
commit | 4bfdaa1218c7ef9a57b1349d02da78fdb76c56f9 (patch) | |
tree | 8e2ac1a254390a61947ea1e4c7fb25ff0d586c32 | |
parent | 35e1d81ce97045c6009998dc46b6bc6159e6eb15 (diff) | |
download | gjs-4bfdaa1218c7ef9a57b1349d02da78fdb76c56f9.tar.gz |
tests: Avoid using char type in uniform_int_distribution<T> template
This is undefined behaviour. GCC and pre-15.x Clang accept it, so we
didn't notice it before.
Closes: #514
-rw-r--r-- | test/gjs-tests.cpp | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp index 49011bba..e48734aa 100644 --- a/test/gjs-tests.cpp +++ b/test/gjs-tests.cpp @@ -56,11 +56,29 @@ static unsigned cpp_random_seed = 0; using Gjs::Test::assert_equal; template <typename T> +struct is_char_helper : public std::false_type {}; +template <> +struct is_char_helper<char> : public std::true_type {}; +template <> +struct is_char_helper<wchar_t> : public std::true_type {}; +template <> +struct is_char_helper<char16_t> : public std::true_type {}; +template <> +struct is_char_helper<char32_t> : public std::true_type {}; +template <typename T> +struct is_char : public is_char_helper<std::remove_cv_t<T>>::type {}; +template <typename T> +inline constexpr bool is_char_v = is_char<T>::value; + +template <typename T> T get_random_number() { std::mt19937_64 gen(cpp_random_seed); if constexpr (std::is_same_v<T, bool>) { return g_random_boolean(); + } else if constexpr (is_char_v<T>) { + return std::char_traits<T>::to_char_type( + get_random_number<typename std::char_traits<T>::int_type>()); } else if constexpr (std::is_integral_v<T>) { T lowest_value = std::numeric_limits<T>::lowest(); |