Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
catch_generators_specific.hpp
Go to the documentation of this file.
1/*
2 * Created by Martin on 15/6/2018.
3 *
4 * Distributed under the Boost Software License, Version 1.0. (See accompanying
5 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 */
7#ifndef TWOBLUECUBES_CATCH_GENERATORS_SPECIFIC_HPP_INCLUDED
8#define TWOBLUECUBES_CATCH_GENERATORS_SPECIFIC_HPP_INCLUDED
9
10#include "catch_context.h"
11#include "catch_generators.hpp"
13
14#include <random>
15
16namespace Catch {
17namespace Generators {
18
19template <typename Float>
20class RandomFloatingGenerator final : public IGenerator<Float> {
21 // FIXME: What is the right seed?
22 std::minstd_rand m_rand;
23 std::uniform_real_distribution<Float> m_dist;
24 Float m_current_number;
25public:
26
27 RandomFloatingGenerator(Float a, Float b):
28 m_rand(getCurrentContext().getConfig()->rngSeed()),
29 m_dist(a, b) {
30 static_cast<void>(next());
31 }
32
33 Float const& get() const override {
34 return m_current_number;
35 }
36 bool next() override {
37 m_current_number = m_dist(m_rand);
38 return true;
39 }
40};
41
42template <typename Integer>
43class RandomIntegerGenerator final : public IGenerator<Integer> {
44 std::minstd_rand m_rand;
45 std::uniform_int_distribution<Integer> m_dist;
46 Integer m_current_number;
47public:
48
49 RandomIntegerGenerator(Integer a, Integer b):
50 m_rand(getCurrentContext().getConfig()->rngSeed()),
51 m_dist(a, b) {
52 static_cast<void>(next());
53 }
54
55 Integer const& get() const override {
56 return m_current_number;
57 }
58 bool next() override {
59 m_current_number = m_dist(m_rand);
60 return true;
61 }
62};
63
64// TODO: Ideally this would be also constrained against the various char types,
65// but I don't expect users to run into that in practice.
66template <typename T>
67typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value,
68GeneratorWrapper<T>>::type
74
75template <typename T>
76typename std::enable_if<std::is_floating_point<T>::value,
77GeneratorWrapper<T>>::type
83
84
85template <typename T>
86class RangeGenerator final : public IGenerator<T> {
87 T m_current;
88 T m_end;
89 T m_step;
90 bool m_positive;
91
92public:
93 RangeGenerator(T const& start, T const& end, T const& step):
94 m_current(start),
95 m_end(end),
96 m_step(step),
97 m_positive(m_step > T(0))
98 {
99 assert(m_current != m_end && "Range start and end cannot be equal");
100 assert(m_step != T(0) && "Step size cannot be zero");
101 assert(((m_positive && m_current <= m_end) || (!m_positive && m_current >= m_end)) && "Step moves away from end");
102 }
103
104 RangeGenerator(T const& start, T const& end):
105 RangeGenerator(start, end, (start < end) ? T(1) : T(-1))
106 {}
107
108 T const& get() const override {
109 return m_current;
110 }
111
112 bool next() override {
113 m_current += m_step;
114 return (m_positive) ? (m_current < m_end) : (m_current > m_end);
115 }
116};
117
118template <typename T>
119GeneratorWrapper<T> range(T const& start, T const& end, T const& step) {
120 static_assert(std::is_integral<T>::value && !std::is_same<T, bool>::value, "Type must be an integer");
121 return GeneratorWrapper<T>(pf::make_unique<RangeGenerator<T>>(start, end, step));
122}
123
124template <typename T>
125GeneratorWrapper<T> range(T const& start, T const& end) {
126 static_assert(std::is_integral<T>::value && !std::is_same<T, bool>::value, "Type must be an integer");
128}
129
130
131} // namespace Generators
132} // namespace Catch
133
134
135#endif // TWOBLUECUBES_CATCH_GENERATORS_SPECIFIC_HPP_INCLUDED
RangeGenerator(T const &start, T const &end, T const &step)
RangeGenerator(T const &start, T const &end)
std::unique_ptr< T > make_unique(Args &&... args)
GeneratorWrapper< T > range(T const &start, T const &end, T const &step)
IContext & getCurrentContext()
FloatingPoint< float > Float
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define T(meth, val, expected)
uint16_t random
Definition yubico_otp.c:47