Unravel Engine C++ Reference
Loading...
Searching...
No Matches
tests_helper.h
Go to the documentation of this file.
1#pragma once
2#include <chrono>
3#include <numeric>
4#include <random>
5
6namespace helper
7{
8namespace details
9{
10auto rd_gen() -> std::mt19937&;
11}
12
13template<typename T>
14struct is_chrono_duration : std::false_type
15{
16};
17template<typename R, typename P>
18struct is_chrono_duration<std::chrono::duration<R, P>> : std::true_type
19{
20};
21
22template<typename T, std::enable_if_t<is_chrono_duration<T>::value, void*> = nullptr>
23inline auto random_value(T min, T max) -> T
24{
25 std::uniform_int_distribution<int64_t> dist(min.count(), max.count());
26 return T{dist(details::rd_gen())};
27}
28
29template<typename T, std::enable_if_t<std::is_integral<T>::value, void*> = nullptr>
30inline auto random_value(T min, T max) -> T
31{
32 std::uniform_int_distribution<T> dist(min, max);
33 return dist(details::rd_gen());
34}
35
36template<typename T, std::enable_if_t<std::is_floating_point<T>::value, void*> = nullptr>
37inline auto random_value(T min, T max) -> T
38{
39 std::uniform_real_distribution<T> dist(min, max);
40 return dist(details::rd_gen());
41}
42
43template<typename T>
44inline T random_value()
45{
46 return random_value(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
47}
48
49template<typename T, std::enable_if_t<std::is_integral<T>::value, void*> = nullptr>
50inline auto compare(const T& a, const T& b) -> bool
51{
52 return a == b;
53}
54
55template<typename T, std::enable_if_t<std::is_floating_point<T>::value, void*> = nullptr>
56inline auto compare(const T& a, const T& b, T epsilon = T(0.001)) -> bool
57{
58 auto diff = std::abs(a - b);
59 if(diff <= epsilon)
60 return true;
61
62 if(diff < std::max(std::abs(a), std::abs(b)) * epsilon)
63 return true;
64
65 return false;
66}
67} // namespace helper
entt::handle b
entt::handle a
auto rd_gen() -> std::mt19937 &
T random_value()
auto compare(const T &a, const T &b) -> bool