Fixes for matcher testing helpers when testing bools

This commit is contained in:
Martin Hořeňovský 2022-07-16 10:19:07 +02:00
parent a6d59b62b2
commit caf1264588

View File

@ -53,43 +53,49 @@ namespace unrelated {
template <typename T> template <typename T>
class has_different_begin_end_types { class has_different_begin_end_types {
std::vector<T> m_elements; // Using std::vector<T> leads to annoying issues when T is bool
// so we just use list because the perf is not critical and ugh.
std::list<T> m_elements;
// Different type for the "end" iterator // Different type for the "end" iterator
struct iterator_end {}; struct iterator_end {};
// Just a fake forward iterator, that only compares to a different // Fake-ish forward iterator that only compares to a different type
// type (so we can test two-type ranges). class iterator {
struct iterator { using underlying_iter = typename std::list<T>::const_iterator;
T const* start; underlying_iter m_start;
T const* end; underlying_iter m_end;
public:
iterator( underlying_iter start, underlying_iter end ):
m_start( start ), m_end( end ) {}
using iterator_category = std::forward_iterator_tag; using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
using value_type = T; using value_type = T;
using reference = T const&; using const_reference = T const&;
using pointer = T const*; using pointer = T const*;
friend bool operator==( iterator iter, iterator_end ) { friend bool operator==( iterator iter, iterator_end ) {
return iter.start == iter.end; return iter.m_start == iter.m_end;
} }
friend bool operator!=( iterator iter, iterator_end ) { friend bool operator!=( iterator iter, iterator_end ) {
return iter.start != iter.end; return iter.m_start != iter.m_end;
} }
iterator& operator++() { iterator& operator++() {
++start; ++m_start;
return *this; return *this;
} }
iterator operator++(int) { iterator operator++(int) {
auto tmp(*this); auto tmp(*this);
++start; ++m_start;
return tmp; return tmp;
} }
reference operator*() const { const_reference operator*() const {
return *start; return *m_start;
} }
pointer operator->() const { pointer operator->() const {
return start; return m_start;
} }
}; };
@ -99,7 +105,7 @@ public:
m_elements( init ) {} m_elements( init ) {}
iterator begin() const { iterator begin() const {
return { m_elements.data(), m_elements.data() + m_elements.size() }; return { m_elements.begin(), m_elements.end() };
} }
iterator_end end() const { iterator_end end() const {
@ -130,8 +136,9 @@ template <typename T> struct with_mocked_iterator_access {
using iterator_category = std::forward_iterator_tag; using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
using value_type = constify_t<T>; using value_type = constify_t<T>;
using reference = value_type&; using const_reference = typename std::vector<T>::const_reference;
using pointer = value_type*; using reference = typename std::vector<T>::reference;
using pointer = typename std::vector<T>::pointer;
basic_iterator( constify_t<with_mocked_iterator_access>* origin, basic_iterator( constify_t<with_mocked_iterator_access>* origin,
std::size_t origin_idx ): std::size_t origin_idx ):
@ -153,7 +160,7 @@ template <typename T> struct with_mocked_iterator_access {
++( *this ); ++( *this );
return tmp; return tmp;
} }
reference operator*() const { const_reference operator*() const {
assert( m_origin_idx < m_origin->m_elements.size() && "Attempted to deref invalid position" ); assert( m_origin_idx < m_origin->m_elements.size() && "Attempted to deref invalid position" );
m_origin->m_derefed[m_origin_idx] = true; m_origin->m_derefed[m_origin_idx] = true;
return m_origin->m_elements[m_origin_idx]; return m_origin->m_elements[m_origin_idx];