2019-02-19 22:09:14 +01:00
|
|
|
/*
|
|
|
|
* Created by Martin on 15/6/2018.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
2020-03-30 10:34:21 +02:00
|
|
|
#ifndef TWOBLUECUBES_CATCH_GENERATORS_RANGE_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_GENERATORS_RANGE_HPP_INCLUDED
|
2019-02-19 22:09:14 +01:00
|
|
|
|
2020-03-30 10:34:21 +02:00
|
|
|
#include <catch2/generators/catch_generators.hpp>
|
2019-02-19 22:09:14 +01:00
|
|
|
|
2020-03-30 10:34:21 +02:00
|
|
|
#include <iterator>
|
|
|
|
#include <type_traits>
|
2019-02-19 22:09:14 +01:00
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
namespace Generators {
|
|
|
|
|
2019-02-23 17:16:28 +01:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class RangeGenerator final : public IGenerator<T> {
|
|
|
|
T m_current;
|
|
|
|
T m_end;
|
|
|
|
T m_step;
|
|
|
|
bool m_positive;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RangeGenerator(T const& start, T const& end, T const& step):
|
|
|
|
m_current(start),
|
|
|
|
m_end(end),
|
|
|
|
m_step(step),
|
|
|
|
m_positive(m_step > T(0))
|
|
|
|
{
|
|
|
|
assert(m_current != m_end && "Range start and end cannot be equal");
|
|
|
|
assert(m_step != T(0) && "Step size cannot be zero");
|
|
|
|
assert(((m_positive && m_current <= m_end) || (!m_positive && m_current >= m_end)) && "Step moves away from end");
|
|
|
|
}
|
|
|
|
|
|
|
|
RangeGenerator(T const& start, T const& end):
|
|
|
|
RangeGenerator(start, end, (start < end) ? T(1) : T(-1))
|
|
|
|
{}
|
|
|
|
|
|
|
|
T const& get() const override {
|
|
|
|
return m_current;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool next() override {
|
|
|
|
m_current += m_step;
|
|
|
|
return (m_positive) ? (m_current < m_end) : (m_current > m_end);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
GeneratorWrapper<T> range(T const& start, T const& end, T const& step) {
|
2019-10-16 16:03:42 +02:00
|
|
|
static_assert(std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, "Type must be numeric");
|
2020-02-01 21:43:00 +01:00
|
|
|
return GeneratorWrapper<T>(std::make_unique<RangeGenerator<T>>(start, end, step));
|
2019-02-23 17:16:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
GeneratorWrapper<T> range(T const& start, T const& end) {
|
|
|
|
static_assert(std::is_integral<T>::value && !std::is_same<T, bool>::value, "Type must be an integer");
|
2020-02-01 21:43:00 +01:00
|
|
|
return GeneratorWrapper<T>(std::make_unique<RangeGenerator<T>>(start, end));
|
2019-02-23 17:16:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-06 13:49:50 +02:00
|
|
|
template <typename T>
|
|
|
|
class IteratorGenerator final : public IGenerator<T> {
|
|
|
|
static_assert(!std::is_same<T, bool>::value,
|
|
|
|
"IteratorGenerator currently does not support bools"
|
|
|
|
"because of std::vector<bool> specialization");
|
|
|
|
|
|
|
|
std::vector<T> m_elems;
|
|
|
|
size_t m_current = 0;
|
|
|
|
public:
|
|
|
|
template <typename InputIterator, typename InputSentinel>
|
|
|
|
IteratorGenerator(InputIterator first, InputSentinel last):m_elems(first, last) {
|
|
|
|
if (m_elems.empty()) {
|
2020-02-14 10:32:30 +01:00
|
|
|
Detail::throw_generator_exception("IteratorGenerator received no valid values");
|
2019-10-06 13:49:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T const& get() const override {
|
|
|
|
return m_elems[m_current];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool next() override {
|
|
|
|
++m_current;
|
|
|
|
return m_current != m_elems.size();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename InputIterator,
|
|
|
|
typename InputSentinel,
|
|
|
|
typename ResultType = typename std::iterator_traits<InputIterator>::value_type>
|
|
|
|
GeneratorWrapper<ResultType> from_range(InputIterator from, InputSentinel to) {
|
2020-02-01 21:43:00 +01:00
|
|
|
return GeneratorWrapper<ResultType>(std::make_unique<IteratorGenerator<ResultType>>(from, to));
|
2019-10-06 13:49:50 +02:00
|
|
|
}
|
|
|
|
|
2019-10-09 14:51:24 +02:00
|
|
|
template <typename Container,
|
|
|
|
typename ResultType = typename Container::value_type>
|
|
|
|
GeneratorWrapper<ResultType> from_range(Container const& cnt) {
|
2020-02-01 21:43:00 +01:00
|
|
|
return GeneratorWrapper<ResultType>(std::make_unique<IteratorGenerator<ResultType>>(cnt.begin(), cnt.end()));
|
2019-10-09 14:51:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-19 22:09:14 +01:00
|
|
|
} // namespace Generators
|
|
|
|
} // namespace Catch
|
|
|
|
|
|
|
|
|
2020-03-30 10:34:21 +02:00
|
|
|
#endif // TWOBLUECUBES_CATCH_GENERATORS_RANGE_HPP_INCLUDED
|