File indexing completed on 2025-01-18 09:54:04
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef CATCH_GENERATORS_RANGE_HPP_INCLUDED
0009 #define CATCH_GENERATORS_RANGE_HPP_INCLUDED
0010
0011 #include <catch2/generators/catch_generators.hpp>
0012
0013 #include <iterator>
0014 #include <type_traits>
0015
0016 namespace Catch {
0017 namespace Generators {
0018
0019
0020 template <typename T>
0021 class RangeGenerator final : public IGenerator<T> {
0022 T m_current;
0023 T m_end;
0024 T m_step;
0025 bool m_positive;
0026
0027 public:
0028 RangeGenerator(T const& start, T const& end, T const& step):
0029 m_current(start),
0030 m_end(end),
0031 m_step(step),
0032 m_positive(m_step > T(0))
0033 {
0034 assert(m_current != m_end && "Range start and end cannot be equal");
0035 assert(m_step != T(0) && "Step size cannot be zero");
0036 assert(((m_positive && m_current <= m_end) || (!m_positive && m_current >= m_end)) && "Step moves away from end");
0037 }
0038
0039 RangeGenerator(T const& start, T const& end):
0040 RangeGenerator(start, end, (start < end) ? T(1) : T(-1))
0041 {}
0042
0043 T const& get() const override {
0044 return m_current;
0045 }
0046
0047 bool next() override {
0048 m_current += m_step;
0049 return (m_positive) ? (m_current < m_end) : (m_current > m_end);
0050 }
0051 };
0052
0053 template <typename T>
0054 GeneratorWrapper<T> range(T const& start, T const& end, T const& step) {
0055 static_assert(std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, "Type must be numeric");
0056 return GeneratorWrapper<T>(Catch::Detail::make_unique<RangeGenerator<T>>(start, end, step));
0057 }
0058
0059 template <typename T>
0060 GeneratorWrapper<T> range(T const& start, T const& end) {
0061 static_assert(std::is_integral<T>::value && !std::is_same<T, bool>::value, "Type must be an integer");
0062 return GeneratorWrapper<T>(Catch::Detail::make_unique<RangeGenerator<T>>(start, end));
0063 }
0064
0065
0066 template <typename T>
0067 class IteratorGenerator final : public IGenerator<T> {
0068 static_assert(!std::is_same<T, bool>::value,
0069 "IteratorGenerator currently does not support bools"
0070 "because of std::vector<bool> specialization");
0071
0072 std::vector<T> m_elems;
0073 size_t m_current = 0;
0074 public:
0075 template <typename InputIterator, typename InputSentinel>
0076 IteratorGenerator(InputIterator first, InputSentinel last):m_elems(first, last) {
0077 if (m_elems.empty()) {
0078 Detail::throw_generator_exception("IteratorGenerator received no valid values");
0079 }
0080 }
0081
0082 T const& get() const override {
0083 return m_elems[m_current];
0084 }
0085
0086 bool next() override {
0087 ++m_current;
0088 return m_current != m_elems.size();
0089 }
0090 };
0091
0092 template <typename InputIterator,
0093 typename InputSentinel,
0094 typename ResultType = typename std::iterator_traits<InputIterator>::value_type>
0095 GeneratorWrapper<ResultType> from_range(InputIterator from, InputSentinel to) {
0096 return GeneratorWrapper<ResultType>(Catch::Detail::make_unique<IteratorGenerator<ResultType>>(from, to));
0097 }
0098
0099 template <typename Container,
0100 typename ResultType = typename Container::value_type>
0101 GeneratorWrapper<ResultType> from_range(Container const& cnt) {
0102 return GeneratorWrapper<ResultType>(Catch::Detail::make_unique<IteratorGenerator<ResultType>>(cnt.begin(), cnt.end()));
0103 }
0104
0105
0106 }
0107 }
0108
0109
0110 #endif