forked from catchorg/Catch2
This is both a really big and a really small commit. It is small in that it only contains renaming, moving and modification of include directives caused by this. It is really big in the obvious way of touching something like 200 files. The new rules for naming files is simple: headers use the `.hpp` extension. The rules for physical file layout is still kinda in progress, but the basics are also simple: * Significant parts of functionality get their own subfolder * Benchmarking is in `catch2/benchmark` * Matchers are in `catch2/matchers` * Generators are in `catch2/generators` * Reporters are in `catch2/reporters` * Baseline testing facilities are in `catch2/` * Various top level folders also contain `internal` subfolder, with files that users probably do not want to include directly, at least not until they have to write something like their own reporter. * The exact files in these subfolders is likely to change later on Note that while some includes were cleaned up in this commit, it is only the low hanging fruit and further cleanup using automatic tooling will happen later. Also note that various include guards, copyright notices and file headers will also be standardized later, rather than in this commit.
110 lines
3.4 KiB
C++
110 lines
3.4 KiB
C++
/*
|
|
* 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)
|
|
*/
|
|
#ifndef TWOBLUECUBES_CATCH_GENERATORS_RANGE_HPP_INCLUDED
|
|
#define TWOBLUECUBES_CATCH_GENERATORS_RANGE_HPP_INCLUDED
|
|
|
|
#include <catch2/generators/catch_generators.hpp>
|
|
|
|
#include <iterator>
|
|
#include <type_traits>
|
|
|
|
namespace Catch {
|
|
namespace Generators {
|
|
|
|
|
|
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) {
|
|
static_assert(std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, "Type must be numeric");
|
|
return GeneratorWrapper<T>(std::make_unique<RangeGenerator<T>>(start, end, step));
|
|
}
|
|
|
|
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");
|
|
return GeneratorWrapper<T>(std::make_unique<RangeGenerator<T>>(start, end));
|
|
}
|
|
|
|
|
|
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()) {
|
|
Detail::throw_generator_exception("IteratorGenerator received no valid values");
|
|
}
|
|
}
|
|
|
|
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) {
|
|
return GeneratorWrapper<ResultType>(std::make_unique<IteratorGenerator<ResultType>>(from, to));
|
|
}
|
|
|
|
template <typename Container,
|
|
typename ResultType = typename Container::value_type>
|
|
GeneratorWrapper<ResultType> from_range(Container const& cnt) {
|
|
return GeneratorWrapper<ResultType>(std::make_unique<IteratorGenerator<ResultType>>(cnt.begin(), cnt.end()));
|
|
}
|
|
|
|
|
|
} // namespace Generators
|
|
} // namespace Catch
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_GENERATORS_RANGE_HPP_INCLUDED
|