| 1 |
|
| 2 |
// Copyright Catch2 Authors |
| 3 |
// Distributed under the Boost Software License, Version 1.0. |
| 4 |
// (See accompanying file LICENSE.txt or copy at |
| 5 |
// https://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
// SPDX-License-Identifier: BSL-1.0 |
| 8 |
|
| 9 |
#ifndef CATCH_TEST_HELPERS_RANGE_TEST_HELPERS_HPP_INCLUDED |
| 10 |
#define CATCH_TEST_HELPERS_RANGE_TEST_HELPERS_HPP_INCLUDED |
| 11 |
|
| 12 |
#include <catch2/catch_tostring.hpp> |
| 13 |
|
| 14 |
#include <initializer_list> |
| 15 |
#include <list> |
| 16 |
#include <memory> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
namespace unrelated { |
| 20 |
template <typename T> |
| 21 |
class needs_ADL_begin { |
| 22 |
std::vector<T> m_elements; |
| 23 |
|
| 24 |
public: |
| 25 |
using iterator = typename std::vector<T>::iterator; |
| 26 |
using const_iterator = typename std::vector<T>::const_iterator; |
| 27 |
|
| 28 |
needs_ADL_begin( std::initializer_list<T> init ): m_elements( init ) {} |
| 29 |
|
| 30 |
const_iterator Begin() const { return m_elements.begin(); } |
| 31 |
const_iterator End() const { return m_elements.end(); } |
| 32 |
|
| 33 |
friend const_iterator begin( needs_ADL_begin const& lhs ) { |
| 34 |
return lhs.Begin(); |
| 35 |
} |
| 36 |
friend const_iterator end( needs_ADL_begin const& rhs ) { |
| 37 |
return rhs.End(); |
| 38 |
} |
| 39 |
}; |
| 40 |
|
| 41 |
struct ADL_empty { |
| 42 |
bool Empty() const { return true; } |
| 43 |
|
| 44 |
friend bool empty( ADL_empty e ) { return e.Empty(); } |
| 45 |
}; |
| 46 |
|
| 47 |
struct ADL_size { |
| 48 |
size_t sz() const { return 12; } |
| 49 |
friend size_t size( ADL_size s ) { return s.sz(); } |
| 50 |
}; |
| 51 |
|
| 52 |
} // namespace unrelated |
| 53 |
|
| 54 |
#if defined( __clang__ ) |
| 55 |
# pragma clang diagnostic push |
| 56 |
# pragma clang diagnostic ignored "-Wunused-function" |
| 57 |
#endif |
| 58 |
|
| 59 |
template <typename T> |
| 60 |
class has_different_begin_end_types { |
| 61 |
// Using std::vector<T> leads to annoying issues when T is bool |
| 62 |
// so we just use list because the perf is not critical and ugh. |
| 63 |
std::list<T> m_elements; |
| 64 |
|
| 65 |
// Different type for the "end" iterator |
| 66 |
struct iterator_end {}; |
| 67 |
// Fake-ish forward iterator that only compares to a different type |
| 68 |
class iterator { |
| 69 |
using underlying_iter = typename std::list<T>::const_iterator; |
| 70 |
underlying_iter m_start; |
| 71 |
underlying_iter m_end; |
| 72 |
|
| 73 |
public: |
| 74 |
iterator( underlying_iter start, underlying_iter end ): |
| 75 |
m_start( start ), m_end( end ) {} |
| 76 |
|
| 77 |
using iterator_category = std::forward_iterator_tag; |
| 78 |
using difference_type = std::ptrdiff_t; |
| 79 |
using value_type = T; |
| 80 |
using const_reference = T const&; |
| 81 |
using pointer = T const*; |
| 82 |
|
| 83 |
friend bool operator==( iterator iter, iterator_end ) { |
| 84 |
return iter.m_start == iter.m_end; |
| 85 |
} |
| 86 |
friend bool operator==(iterator lhs, iterator rhs) { |
| 87 |
return lhs.m_start == rhs.m_start && lhs.m_end == rhs.m_end; |
| 88 |
} |
| 89 |
friend bool operator!=( iterator iter, iterator_end ) { |
| 90 |
return iter.m_start != iter.m_end; |
| 91 |
} |
| 92 |
friend bool operator!=( iterator lhs, iterator rhs ) { |
| 93 |
return !( lhs == rhs ); |
| 94 |
} |
| 95 |
iterator& operator++() { |
| 96 |
++m_start; |
| 97 |
return *this; |
| 98 |
} |
| 99 |
iterator operator++( int ) { |
| 100 |
auto tmp( *this ); |
| 101 |
++m_start; |
| 102 |
return tmp; |
| 103 |
} |
| 104 |
const_reference operator*() const { return *m_start; } |
| 105 |
pointer operator->() const { return m_start; } |
| 106 |
}; |
| 107 |
|
| 108 |
public: |
| 109 |
explicit has_different_begin_end_types( std::initializer_list<T> init ): |
| 110 |
m_elements( init ) {} |
| 111 |
|
| 112 |
iterator begin() const { return { m_elements.begin(), m_elements.end() }; } |
| 113 |
|
| 114 |
iterator_end end() const { return {}; } |
| 115 |
}; |
| 116 |
|
| 117 |
#if defined( __clang__ ) |
| 118 |
# pragma clang diagnostic pop |
| 119 |
#endif |
| 120 |
|
| 121 |
template <typename T> |
| 122 |
struct with_mocked_iterator_access { |
| 123 |
std::vector<T> m_elements; |
| 124 |
|
| 125 |
// use plain arrays to have nicer printouts with CHECK(...) |
| 126 |
mutable std::unique_ptr<bool[]> m_derefed; |
| 127 |
|
| 128 |
// We want to check which elements were dereferenced when iterating, so |
| 129 |
// we can check whether iterator-using code traverses range correctly |
| 130 |
template <bool is_const> |
| 131 |
class basic_iterator { |
| 132 |
template <typename U> |
| 133 |
using constify_t = std::conditional_t<is_const, std::add_const_t<U>, U>; |
| 134 |
|
| 135 |
constify_t<with_mocked_iterator_access>* m_origin; |
| 136 |
size_t m_origin_idx; |
| 137 |
|
| 138 |
public: |
| 139 |
using iterator_category = std::forward_iterator_tag; |
| 140 |
using difference_type = std::ptrdiff_t; |
| 141 |
using value_type = constify_t<T>; |
| 142 |
using const_reference = typename std::vector<T>::const_reference; |
| 143 |
using reference = typename std::vector<T>::reference; |
| 144 |
using pointer = typename std::vector<T>::pointer; |
| 145 |
|
| 146 |
basic_iterator( constify_t<with_mocked_iterator_access>* origin, |
| 147 |
std::size_t origin_idx ): |
| 148 |
m_origin{ origin }, m_origin_idx{ origin_idx } {} |
| 149 |
|
| 150 |
friend bool operator==( basic_iterator lhs, basic_iterator rhs ) { |
| 151 |
return lhs.m_origin == rhs.m_origin && |
| 152 |
lhs.m_origin_idx == rhs.m_origin_idx; |
| 153 |
} |
| 154 |
friend bool operator!=( basic_iterator lhs, basic_iterator rhs ) { |
| 155 |
return !( lhs == rhs ); |
| 156 |
} |
| 157 |
basic_iterator& operator++() { |
| 158 |
++m_origin_idx; |
| 159 |
return *this; |
| 160 |
} |
| 161 |
basic_iterator operator++( int ) { |
| 162 |
auto tmp( *this ); |
| 163 |
++( *this ); |
| 164 |
return tmp; |
| 165 |
} |
| 166 |
const_reference operator*() const { |
| 167 |
assert( m_origin_idx < m_origin->m_elements.size() && |
| 168 |
"Attempted to deref invalid position" ); |
| 169 |
m_origin->m_derefed[m_origin_idx] = true; |
| 170 |
return m_origin->m_elements[m_origin_idx]; |
| 171 |
} |
| 172 |
pointer operator->() const { |
| 173 |
assert( m_origin_idx < m_origin->m_elements.size() && |
| 174 |
"Attempted to deref invalid position" ); |
| 175 |
return &m_origin->m_elements[m_origin_idx]; |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
using iterator = basic_iterator<false>; |
| 180 |
using const_iterator = basic_iterator<true>; |
| 181 |
|
| 182 |
with_mocked_iterator_access( std::initializer_list<T> init ): |
| 183 |
m_elements( init ), |
| 184 |
m_derefed( std::make_unique<bool[]>( m_elements.size() ) ) {} |
| 185 |
|
| 186 |
const_iterator begin() const { return { this, 0 }; } |
| 187 |
const_iterator end() const { return { this, m_elements.size() }; } |
| 188 |
iterator begin() { return { this, 0 }; } |
| 189 |
iterator end() { return { this, m_elements.size() }; } |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
namespace Catch { |
| 194 |
// make sure with_mocked_iterator_access is not considered a range by Catch, |
| 195 |
// so that below StringMaker is used instead of the default one for ranges |
| 196 |
template <typename T> |
| 197 |
struct is_range<with_mocked_iterator_access<T>> : std::false_type {}; |
| 198 |
|
| 199 |
template <typename T> |
| 200 |
struct StringMaker<with_mocked_iterator_access<T>> { |
| 201 |
static std::string |
| 202 |
convert( with_mocked_iterator_access<T> const& access ) { |
| 203 |
// We have to avoid the type's iterators, because we check |
| 204 |
// their use in tests |
| 205 |
return ::Catch::Detail::stringify( access.m_elements ); |
| 206 |
} |
| 207 |
}; |
| 208 |
} // namespace Catch |
| 209 |
|
| 210 |
#endif // CATCH_TEST_HELPERS_RANGE_TEST_HELPERS_HPP_INCLUDED |