diff options
| author | Martin Hořeňovský | 2019-10-06 13:49:50 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2019-10-06 13:55:10 +0200 |
| commit | 319cb9e1daec5076ff1a3acd10b959c2bce05c5b (patch) | |
| tree | 7a9d9bf9e8e257593283692b2fdbbeee454d0fdb /include/internal | |
| parent | b8b765d55e552d5892b46ccafd3a40a8ca3b9e0a (diff) | |
| download | catch3-319cb9e1daec5076ff1a3acd10b959c2bce05c5b.tar.gz | |
Add a generator that takes an iterator pair
Diffstat (limited to 'include/internal')
| -rw-r--r-- | include/internal/catch_generators_specific.hpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/include/internal/catch_generators_specific.hpp b/include/internal/catch_generators_specific.hpp index 7aae06bd..7985ac70 100644 --- a/include/internal/catch_generators_specific.hpp +++ b/include/internal/catch_generators_specific.hpp @@ -128,6 +128,39 @@ GeneratorWrapper<T> range(T const& start, T const& 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()) { + Catch::throw_exception(GeneratorException("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>(pf::make_unique<IteratorGenerator<ResultType>>(from, to)); +} + } // namespace Generators } // namespace Catch |