catch_generators_specific.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include "catch_context.h"
  10. #include "catch_generators.hpp"
  11. #include "catch_interfaces_config.h"
  12. #include "catch_random_number_generator.h"
  13. #include <random>
  14. namespace Catch {
  15. namespace Generators {
  16. template <typename Float>
  17. class RandomFloatingGenerator final : public IGenerator<Float> {
  18. Catch::SimplePcg32& m_rng;
  19. std::uniform_real_distribution<Float> m_dist;
  20. Float m_current_number;
  21. public:
  22. RandomFloatingGenerator(Float a, Float b):
  23. m_rng(rng()),
  24. m_dist(a, b) {
  25. static_cast<void>(next());
  26. }
  27. Float const& get() const override {
  28. return m_current_number;
  29. }
  30. bool next() override {
  31. m_current_number = m_dist(m_rng);
  32. return true;
  33. }
  34. };
  35. template <typename Integer>
  36. class RandomIntegerGenerator final : public IGenerator<Integer> {
  37. Catch::SimplePcg32& m_rng;
  38. std::uniform_int_distribution<Integer> m_dist;
  39. Integer m_current_number;
  40. public:
  41. RandomIntegerGenerator(Integer a, Integer b):
  42. m_rng(rng()),
  43. m_dist(a, b) {
  44. static_cast<void>(next());
  45. }
  46. Integer const& get() const override {
  47. return m_current_number;
  48. }
  49. bool next() override {
  50. m_current_number = m_dist(m_rng);
  51. return true;
  52. }
  53. };
  54. // TODO: Ideally this would be also constrained against the various char types,
  55. // but I don't expect users to run into that in practice.
  56. template <typename T>
  57. typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value,
  58. GeneratorWrapper<T>>::type
  59. random(T a, T b) {
  60. return GeneratorWrapper<T>(
  61. pf::make_unique<RandomIntegerGenerator<T>>(a, b)
  62. );
  63. }
  64. template <typename T>
  65. typename std::enable_if<std::is_floating_point<T>::value,
  66. GeneratorWrapper<T>>::type
  67. random(T a, T b) {
  68. return GeneratorWrapper<T>(
  69. pf::make_unique<RandomFloatingGenerator<T>>(a, b)
  70. );
  71. }
  72. template <typename T>
  73. class RangeGenerator final : public IGenerator<T> {
  74. T m_current;
  75. T m_end;
  76. T m_step;
  77. bool m_positive;
  78. public:
  79. RangeGenerator(T const& start, T const& end, T const& step):
  80. m_current(start),
  81. m_end(end),
  82. m_step(step),
  83. m_positive(m_step > T(0))
  84. {
  85. assert(m_current != m_end && "Range start and end cannot be equal");
  86. assert(m_step != T(0) && "Step size cannot be zero");
  87. assert(((m_positive && m_current <= m_end) || (!m_positive && m_current >= m_end)) && "Step moves away from end");
  88. }
  89. RangeGenerator(T const& start, T const& end):
  90. RangeGenerator(start, end, (start < end) ? T(1) : T(-1))
  91. {}
  92. T const& get() const override {
  93. return m_current;
  94. }
  95. bool next() override {
  96. m_current += m_step;
  97. return (m_positive) ? (m_current < m_end) : (m_current > m_end);
  98. }
  99. };
  100. template <typename T>
  101. GeneratorWrapper<T> range(T const& start, T const& end, T const& step) {
  102. static_assert(std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, "Type must be numeric");
  103. return GeneratorWrapper<T>(pf::make_unique<RangeGenerator<T>>(start, end, step));
  104. }
  105. template <typename T>
  106. GeneratorWrapper<T> range(T const& start, T const& end) {
  107. static_assert(std::is_integral<T>::value && !std::is_same<T, bool>::value, "Type must be an integer");
  108. return GeneratorWrapper<T>(pf::make_unique<RangeGenerator<T>>(start, end));
  109. }
  110. template <typename T>
  111. class IteratorGenerator final : public IGenerator<T> {
  112. static_assert(!std::is_same<T, bool>::value,
  113. "IteratorGenerator currently does not support bools"
  114. "because of std::vector<bool> specialization");
  115. std::vector<T> m_elems;
  116. size_t m_current = 0;
  117. public:
  118. template <typename InputIterator, typename InputSentinel>
  119. IteratorGenerator(InputIterator first, InputSentinel last):m_elems(first, last) {
  120. if (m_elems.empty()) {
  121. Catch::throw_exception(GeneratorException("IteratorGenerator received no valid values"));
  122. }
  123. }
  124. T const& get() const override {
  125. return m_elems[m_current];
  126. }
  127. bool next() override {
  128. ++m_current;
  129. return m_current != m_elems.size();
  130. }
  131. };
  132. template <typename InputIterator,
  133. typename InputSentinel,
  134. typename ResultType = typename std::iterator_traits<InputIterator>::value_type>
  135. GeneratorWrapper<ResultType> from_range(InputIterator from, InputSentinel to) {
  136. return GeneratorWrapper<ResultType>(pf::make_unique<IteratorGenerator<ResultType>>(from, to));
  137. }
  138. template <typename Container,
  139. typename ResultType = typename Container::value_type>
  140. GeneratorWrapper<ResultType> from_range(Container const& cnt) {
  141. return GeneratorWrapper<ResultType>(pf::make_unique<IteratorGenerator<ResultType>>(cnt.begin(), cnt.end()));
  142. }
  143. } // namespace Generators
  144. } // namespace Catch
  145. #endif // TWOBLUECUBES_CATCH_GENERATORS_SPECIFIC_HPP_INCLUDED