Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/Progmasoft/Catch3/Interfaces/IGeneratorTracker.cpp
blob: c0a8afadd3c13042dc0f4e5e3858adf1dcc0f0a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

//              Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
//   (See accompanying file LICENSE.txt or copy at
//        https://www.boost.org/LICENSE_1_0.txt)

// SPDX-License-Identifier: BSL-1.0

#include <Progmasoft/Catch3/Interfaces/IGeneratorTracker.hpp>
#include <Progmasoft/Catch3/Generators/Generators.hpp>

#include <string>

namespace Catch {
    namespace Generators {

        bool GeneratorUntypedBase::countedNext() {
            auto ret = next();
            if ( ret ) {
                m_stringReprCache.clear();
                ++m_currentElementIndex;
            }
            return ret;
        }

        void GeneratorUntypedBase::skipToNthElementImpl( std::size_t n ) {
            for ( size_t i = m_currentElementIndex; i < n; ++i ) {
                bool isValid = next();
                if ( !isValid ) {
                    Detail::throw_generator_exception(
                        "Could not jump to Nth element: not enough elements" );
                }
            }
        }

        void GeneratorUntypedBase::skipToNthElement( std::size_t n ) {
            if ( n < m_currentElementIndex ) {
                Detail::throw_generator_exception(
                    "Tried to jump generator backwards" );
            }
            if ( n == m_currentElementIndex ) { return; }

            skipToNthElementImpl(n);
            // Fixup tracking after moving the generator forward
            //  * Ensure that the correct element index is set after skipping
            //  * Invalidate cache
            m_currentElementIndex = n;
            m_stringReprCache.clear();
        }

        StringRef GeneratorUntypedBase::currentElementAsString() const {
            if ( m_stringReprCache.empty() ) {
                m_stringReprCache = stringifyImpl();
            }
            return m_stringReprCache;
        }

        bool GeneratorUntypedBase::isFinite() const { return true; }

    } // namespace Generators
} // namespace Catch