1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 14:59:54 +00:00
openmw/apps/components_tests/detournavigator/generate.hpp
elsid 7d1cd11ba9
Split openmw_test_suite into openmw and components tests
To avoid duplicated compilation for openmw translation units. Link openmw-lib to
openmw-tests instead.
2024-06-10 23:12:14 +02:00

46 lines
1.4 KiB
C++

#ifndef OPENMW_TEST_SUITE_DETOURNAVIGATOR_GENERATE_H
#define OPENMW_TEST_SUITE_DETOURNAVIGATOR_GENERATE_H
#include <algorithm>
#include <numeric>
#include <random>
#include <type_traits>
namespace DetourNavigator
{
namespace Tests
{
template <class T, class Random>
inline auto generateValue(T& value, Random& random) -> std::enable_if_t<sizeof(T) >= 2>
{
using Distribution = std::conditional_t<std::is_floating_point_v<T>, std::uniform_real_distribution<T>,
std::uniform_int_distribution<T>>;
Distribution distribution(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
value = distribution(random);
}
template <class T, class Random>
inline auto generateValue(T& value, Random& random) -> std::enable_if_t<sizeof(T) == 1>
{
unsigned short v;
generateValue(v, random);
value = static_cast<T>(v % 256);
}
template <class Random>
inline void generateValue(unsigned char& value, Random& random)
{
unsigned short v;
generateValue(v, random);
value = static_cast<unsigned char>(v % 256);
}
template <class I, class Random>
inline void generateRange(I begin, I end, Random& random)
{
std::for_each(begin, end, [&](auto& v) { generateValue(v, random); });
}
}
}
#endif