mirror of
https://github.com/ETLCPP/etl.git
synced 2026-10-28 18:07:10 +08:00
Work in progress
This commit is contained in:
parent
868329f9e4
commit
64fa058f6b
408
include/etl/circular_iterator.h
Normal file
408
include/etl/circular_iterator.h
Normal file
@ -0,0 +1,408 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2022 John Wellbelove
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files(the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef ETL_CIRCULAR_ITERATOR_INCLUDED
|
||||
#define ETL_CIRCULAR_ITERATOR_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "iterator.h"
|
||||
#include "span.h"
|
||||
|
||||
///\defgroup iterator Iterator types
|
||||
|
||||
namespace etl
|
||||
{
|
||||
/// A circular iterator class.
|
||||
/// This iterator can be given a pair of iterator values, which will loop if the start or end of the range is reached.
|
||||
///\ingroup iterator
|
||||
template <typename TIterator>
|
||||
class circular_iterator : etl::iterator<typename etl::iterator_traits<TIterator>::iterator_category, typename etl::iterator_traits<TIterator>::value_type>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef typename etl::iterator_traits<TIterator>::value_type value_type;
|
||||
typedef typename etl::iterator_traits<TIterator>::difference_type difference_type;
|
||||
typedef typename etl::iterator_traits<TIterator>::pointer pointer;
|
||||
typedef typename etl::iterator_traits<TIterator>::reference reference;
|
||||
typedef typename etl::iterator_traits<TIterator>::iterator_category iterator_category;
|
||||
|
||||
//***************************************************************************
|
||||
/// Default constructor.
|
||||
//***************************************************************************
|
||||
circular_iterator()
|
||||
: itr_begin(TIterator())
|
||||
, itr_end(TIterator())
|
||||
, itr(TIterator())
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from iterators.
|
||||
//***************************************************************************
|
||||
circular_iterator(TIterator itr_begin_, TIterator itr_end_)
|
||||
: itr_begin(itr_begin_)
|
||||
, itr_end(itr_end_)
|
||||
, itr(itr_begin_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from span.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
circular_iterator(const etl::span<T>& span_)
|
||||
: itr_begin(span_.begin())
|
||||
, itr_end(span_.end())
|
||||
, itr(itr_begin)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Copy constructor
|
||||
//***************************************************************************
|
||||
circular_iterator(const circular_iterator& other)
|
||||
: itr_begin(other.itr_begin)
|
||||
, itr_end(other.itr_end)
|
||||
, itr(other.itr)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Beginning of the range.
|
||||
//***************************************************************************
|
||||
TIterator begin() const
|
||||
{
|
||||
return itr_begin;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// End of the range.
|
||||
//***************************************************************************
|
||||
TIterator end() const
|
||||
{
|
||||
return itr_end;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// How long is the range?
|
||||
//***************************************************************************
|
||||
size_t size() const
|
||||
{
|
||||
return etl::distance(itr_begin, itr_end);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Is there nothing to iteratte over?
|
||||
//***************************************************************************
|
||||
bool empty() const
|
||||
{
|
||||
return (itr_begin == itr_end);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Increment.
|
||||
//***************************************************************************
|
||||
circular_iterator& operator ++()
|
||||
{
|
||||
if (++itr == itr_end)
|
||||
{
|
||||
itr = itr_begin;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Increment.
|
||||
//***************************************************************************
|
||||
circular_iterator operator ++(int)
|
||||
{
|
||||
circular_iterator original(*this);
|
||||
|
||||
++(*this);
|
||||
|
||||
return (original);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Decrement.
|
||||
//***************************************************************************
|
||||
circular_iterator& operator --()
|
||||
{
|
||||
if (itr == itr_begin)
|
||||
{
|
||||
typename etl::reverse_iterator<TIterator> ritr(itr_end);
|
||||
++ritr;
|
||||
itr = ritr.base();
|
||||
}
|
||||
else
|
||||
{
|
||||
--itr;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Decrement.
|
||||
//***************************************************************************
|
||||
circular_iterator operator --(int)
|
||||
{
|
||||
circular_iterator original(*this);
|
||||
|
||||
--(*this);
|
||||
|
||||
return (original);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Dereference operator.
|
||||
//***************************************************************************
|
||||
value_type operator *()
|
||||
{
|
||||
return *itr;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Dereference operator.
|
||||
//***************************************************************************
|
||||
const value_type operator *() const
|
||||
{
|
||||
return *itr;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// -> operator.
|
||||
//***************************************************************************
|
||||
TIterator operator ->()
|
||||
{
|
||||
return itr;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// -> operator.
|
||||
//***************************************************************************
|
||||
const TIterator operator ->() const
|
||||
{
|
||||
return itr;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Conversion operator.
|
||||
//***************************************************************************
|
||||
operator TIterator() const
|
||||
{
|
||||
return itr;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// += operator.
|
||||
//***************************************************************************
|
||||
circular_iterator& operator +=(difference_type offset)
|
||||
{
|
||||
if (offset != 0)
|
||||
{
|
||||
if (offset > 0)
|
||||
{
|
||||
difference_type distance_to_end = etl::distance(itr, itr_end);
|
||||
offset %= difference_type(size());
|
||||
|
||||
if (distance_to_end > offset)
|
||||
{
|
||||
etl::advance(itr, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
offset -= distance_to_end;
|
||||
itr = itr_begin;
|
||||
etl::advance(itr, offset);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
difference_type distance_to_begin = etl::distance(itr_begin, itr);
|
||||
offset %= difference_type(size());
|
||||
|
||||
if (distance_to_begin > -offset)
|
||||
{
|
||||
etl::advance(itr, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
offset += distance_to_begin;
|
||||
|
||||
typename etl::reverse_iterator<TIterator> ritr(itr_end);
|
||||
etl::advance(ritr, -offset);
|
||||
itr = ritr.base();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// -= operator.
|
||||
//***************************************************************************
|
||||
circular_iterator& operator -=(typename etl::iterator_traits<TIterator>::difference_type offset)
|
||||
{
|
||||
return operator +=(-offset);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Assignment from span.
|
||||
//***************************************************************************
|
||||
circular_iterator& operator =(const etl::span<TIterator>& span_)
|
||||
{
|
||||
itr_begin = span_.begin();
|
||||
itr_end = span_.end();
|
||||
itr = itr_begin;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Assignment from circular_iterator.
|
||||
//***************************************************************************
|
||||
circular_iterator& operator =(circular_iterator other)
|
||||
{
|
||||
itr_begin = other.itr_begin;
|
||||
itr_end = other.itr_end;
|
||||
itr = other.itr;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
TIterator itr_begin; ///< The underlying begin iterator.
|
||||
TIterator itr_end; ///< The underlying end iterator.
|
||||
TIterator itr; ///< The underlying iterator.
|
||||
};
|
||||
|
||||
//*****************************************************************************
|
||||
/// + difference operator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
etl::circular_iterator<TIterator> operator +(etl::circular_iterator<TIterator>& lhs,
|
||||
typename etl::iterator_traits<TIterator>::difference_type offset)
|
||||
{
|
||||
etl::circular_iterator<TIterator> result(lhs);
|
||||
result += offset;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// - difference operator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
etl::circular_iterator<TIterator> operator -(etl::circular_iterator<TIterator>& lhs,
|
||||
typename etl::iterator_traits<TIterator>::difference_type offset)
|
||||
{
|
||||
etl::circular_iterator<TIterator> result(lhs);
|
||||
result -= offset;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
////*****************************************************************************
|
||||
///// - circular_iterator operator.
|
||||
////*****************************************************************************
|
||||
//template <typename TIterator>
|
||||
//typename etl::iterator_traits<TIterator>::difference_type operator -(etl::circular_iterator<TIterator>& lhs,
|
||||
// etl::circular_iterator<TIterator>& rhs)
|
||||
//{
|
||||
// return TIterator(lhs) - TIterator(rhs);
|
||||
//}
|
||||
|
||||
//*****************************************************************************
|
||||
/// Equality operator. circular_iterator == circular_iterator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
bool operator ==(const etl::circular_iterator<TIterator>& lhs,
|
||||
const etl::circular_iterator<TIterator>& rhs)
|
||||
{
|
||||
return TIterator(lhs) == TIterator(rhs);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// Equality operator. circular_iterator == iterator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
bool operator ==(const etl::circular_iterator<TIterator>& lhs,
|
||||
TIterator rhs)
|
||||
{
|
||||
return TIterator(lhs) == rhs;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// Equality operator. iterator == circular_iterator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
bool operator ==(TIterator lhs,
|
||||
const etl::circular_iterator<TIterator>& rhs)
|
||||
{
|
||||
return lhs == TIterator(rhs);
|
||||
}
|
||||
|
||||
|
||||
//*****************************************************************************
|
||||
/// Inequality operator. circular_iterator == circular_iterator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
bool operator !=(const etl::circular_iterator<TIterator>& lhs,
|
||||
const etl::circular_iterator<TIterator>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// Inequality operator. circular_iterator == iterator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
bool operator !=(const etl::circular_iterator<TIterator>& lhs,
|
||||
TIterator rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// Inequality operator. iterator == circular_iterator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
bool operator !=(TIterator& lhs,
|
||||
const etl::circular_iterator<TIterator>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -34,8 +34,6 @@ SOFTWARE.
|
||||
#include "platform.h"
|
||||
#include "iterator.h"
|
||||
|
||||
#include "iterator.h"
|
||||
|
||||
///\defgroup iterator Iterator types
|
||||
|
||||
namespace etl
|
||||
@ -208,8 +206,8 @@ namespace etl
|
||||
/// - fixed_iterator operator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
typename etl::iterator_traits<TIterator>::difference_type operator -(etl::fixed_iterator<TIterator>& lhs,
|
||||
etl::fixed_iterator<TIterator>& rhs)
|
||||
typename etl::iterator_traits<TIterator>::difference_type operator -(const etl::fixed_iterator<TIterator>& lhs,
|
||||
const etl::fixed_iterator<TIterator>& rhs)
|
||||
{
|
||||
return TIterator(lhs) - TIterator(rhs);
|
||||
}
|
||||
@ -219,7 +217,7 @@ namespace etl
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
bool operator ==(const etl::fixed_iterator<TIterator>& lhs,
|
||||
const etl::fixed_iterator<TIterator>& rhs)
|
||||
const etl::fixed_iterator<TIterator>& rhs)
|
||||
{
|
||||
return TIterator(lhs) == TIterator(rhs);
|
||||
}
|
||||
@ -229,7 +227,7 @@ namespace etl
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
bool operator ==(const etl::fixed_iterator<TIterator>& lhs,
|
||||
TIterator rhs)
|
||||
TIterator rhs)
|
||||
{
|
||||
return TIterator(lhs) == rhs;
|
||||
}
|
||||
@ -239,7 +237,7 @@ namespace etl
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
bool operator ==(TIterator lhs,
|
||||
const etl::fixed_iterator<TIterator>& rhs)
|
||||
const etl::fixed_iterator<TIterator>& rhs)
|
||||
{
|
||||
return lhs == TIterator(rhs);
|
||||
}
|
||||
@ -250,7 +248,7 @@ namespace etl
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
bool operator !=(const etl::fixed_iterator<TIterator>& lhs,
|
||||
const etl::fixed_iterator<TIterator>& rhs)
|
||||
const etl::fixed_iterator<TIterator>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
@ -260,7 +258,7 @@ namespace etl
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
bool operator !=(const etl::fixed_iterator<TIterator>& lhs,
|
||||
TIterator rhs)
|
||||
TIterator rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
@ -270,7 +268,7 @@ namespace etl
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
bool operator !=(TIterator& lhs,
|
||||
const etl::fixed_iterator<TIterator>& rhs)
|
||||
const etl::fixed_iterator<TIterator>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
578
test/test_circular_iterator.cpp
Normal file
578
test/test_circular_iterator.cpp
Normal file
@ -0,0 +1,578 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2022 John Wellbelove
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining data copy
|
||||
of this software and associated documentation ciles(the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
#include "unit_test_framework.h"
|
||||
#include <list>
|
||||
#include <ostream>
|
||||
|
||||
#include "etl/circular_iterator.h"
|
||||
|
||||
//template <typename TIterator>
|
||||
//std::ostream& operator << (std::ostream& os, const etl::circular_iterator<TIterator>& ci)
|
||||
//{
|
||||
// os << ci.begin() "," << ci.end();
|
||||
//
|
||||
// return os;
|
||||
//}
|
||||
|
||||
namespace
|
||||
{
|
||||
using Data = std::list<int>;
|
||||
|
||||
SUITE(test_cixed_iterator)
|
||||
{
|
||||
//*************************************************************************
|
||||
TEST(test_default_constructor)
|
||||
{
|
||||
etl::circular_iterator<const int*> ci;
|
||||
|
||||
CHECK_EQUAL(0U, size_t(std::distance(ci.begin(), ci.end())));
|
||||
CHECK_EQUAL(0U, ci.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_from_iterators)
|
||||
{
|
||||
const Data data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::circular_iterator<Data::const_iterator> ci(std::begin(data), std::end(data));
|
||||
|
||||
CHECK(data.begin() == ci.begin());
|
||||
CHECK(data.end() == ci.end());
|
||||
CHECK_EQUAL(data.size(), size_t(std::distance(ci.begin(), ci.end())));
|
||||
CHECK_EQUAL(data.size(), ci.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_from_reverse_iterators)
|
||||
{
|
||||
const Data data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::circular_iterator<Data::const_reverse_iterator> ci(std::rbegin(data), std::rend(data));
|
||||
|
||||
CHECK(data.rbegin() == ci.begin());
|
||||
CHECK(data.rend() == ci.end());
|
||||
CHECK_EQUAL(data.size(), size_t(std::distance(ci.begin(), ci.end())));
|
||||
CHECK_EQUAL(data.size(), ci.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constructor_from_span)
|
||||
{
|
||||
const int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
const etl::span<const int> sp(std::begin(data), std::end(data));
|
||||
|
||||
etl::circular_iterator<const int*> ci(sp);
|
||||
|
||||
CHECK(std::begin(data) == ci.begin());
|
||||
CHECK(std::end(data) == ci.end());
|
||||
CHECK_EQUAL(sp.size(), size_t(std::distance(ci.begin(), ci.end())));
|
||||
CHECK_EQUAL(sp.size(), ci.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_copy_constructor)
|
||||
{
|
||||
int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
const etl::circular_iterator<const int*> ci1(std::begin(data), std::end(data));
|
||||
etl::circular_iterator<const int*> ci2(ci1);
|
||||
|
||||
CHECK(ci1.begin() == ci2.begin());
|
||||
CHECK(ci1.end() == ci2.end());
|
||||
CHECK_EQUAL(ci1.size(), ci2.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_assignment_from_circular_iterator)
|
||||
{
|
||||
const Data data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::circular_iterator<Data::const_iterator> ci1(std::begin(data), std::end(data));
|
||||
etl::circular_iterator<Data::const_iterator>ci2;
|
||||
ci2 = ci1;
|
||||
|
||||
CHECK(ci1.begin() == ci2.begin());
|
||||
CHECK(ci1.end() == ci2.end());
|
||||
CHECK_EQUAL(ci1.size(), ci2.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_assignment_from_span)
|
||||
{
|
||||
int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::span<const int> sp(std::begin(data), std::end(data));
|
||||
|
||||
etl::circular_iterator<const int*> ci;
|
||||
ci = sp;
|
||||
|
||||
CHECK(std::begin(data) == ci.begin());
|
||||
CHECK(std::end(data) == ci.end());
|
||||
CHECK_EQUAL(sp.size(), size_t(std::distance(ci.begin(), ci.end())));
|
||||
CHECK_EQUAL(sp.size(), ci.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_pre_increment)
|
||||
{
|
||||
const Data data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::circular_iterator<Data::const_iterator> ci(std::begin(data), std::end(data));
|
||||
|
||||
for (int i = 1; i < 21; ++i)
|
||||
{
|
||||
Data::const_iterator itr = data.begin();
|
||||
std::advance(itr, i % 10);
|
||||
CHECK_EQUAL(*itr, *++ci);
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_post_increment)
|
||||
{
|
||||
const Data data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::circular_iterator<Data::const_iterator> ci(std::begin(data), std::end(data));
|
||||
|
||||
for (int i = 0; i < 20; ++i)
|
||||
{
|
||||
Data::const_iterator itr = data.begin();
|
||||
std::advance(itr, i % 10);
|
||||
CHECK_EQUAL(*itr, *ci++);
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_pre_increment_reverse_iterator)
|
||||
{
|
||||
const Data data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::circular_iterator<Data::const_reverse_iterator> ci(std::rbegin(data), std::rend(data));
|
||||
|
||||
for (int i = 1; i < 21; ++i)
|
||||
{
|
||||
Data::const_reverse_iterator itr = data.rbegin();
|
||||
std::advance(itr, i % 10);
|
||||
CHECK_EQUAL(*itr, *++ci);
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_post_increment_reverse_iterator)
|
||||
{
|
||||
const Data data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::circular_iterator<Data::const_reverse_iterator> ci(std::rbegin(data), std::rend(data));
|
||||
|
||||
for (int i = 0; i < 20; ++i)
|
||||
{
|
||||
Data::const_reverse_iterator itr = data.rbegin();
|
||||
std::advance(itr, i % 10);
|
||||
CHECK_EQUAL(*itr, *ci++);
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_pre_decrement)
|
||||
{
|
||||
const Data data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::circular_iterator<Data::const_iterator> ci(std::begin(data), std::end(data));
|
||||
|
||||
for (int i = 0; i < 20; ++i)
|
||||
{
|
||||
Data::const_reverse_iterator itr = data.rbegin();
|
||||
std::advance(itr, i % 10);
|
||||
CHECK_EQUAL(*itr, *--ci);
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_pre_decrement_reverse_iterator)
|
||||
{
|
||||
const Data data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::circular_iterator<Data::const_reverse_iterator> ci(std::rbegin(data), std::rend(data));
|
||||
|
||||
for (int i = 0; i < 20; ++i)
|
||||
{
|
||||
Data::const_iterator itr = data.begin();
|
||||
std::advance(itr, i % 10);
|
||||
CHECK_EQUAL(*itr, *--ci);
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_post_decrement)
|
||||
{
|
||||
const Data data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::circular_iterator<Data::const_iterator> ci(std::begin(data), std::end(data));
|
||||
ci--;
|
||||
|
||||
for (int i = 0; i < 20; ++i)
|
||||
{
|
||||
Data::const_reverse_iterator itr = data.rbegin();
|
||||
std::advance(itr, i % 10);
|
||||
CHECK_EQUAL(*itr, *ci--);
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_post_decrement_reverse_iterator)
|
||||
{
|
||||
const Data data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::circular_iterator<Data::const_reverse_iterator> ci(std::rbegin(data), std::rend(data));
|
||||
ci--;
|
||||
|
||||
for (int i = 0; i < 20; ++i)
|
||||
{
|
||||
Data::const_iterator itr = data.begin();
|
||||
std::advance(itr, i % 10);
|
||||
CHECK_EQUAL(*itr, *ci--);
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_member_dereference_operator)
|
||||
{
|
||||
struct Test
|
||||
{
|
||||
int data;
|
||||
int b;
|
||||
};
|
||||
|
||||
Test test[] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
|
||||
|
||||
etl::circular_iterator<Test*> ci(std::begin(test), std::end(test));
|
||||
|
||||
CHECK_EQUAL(test[0].data, ci->data);
|
||||
CHECK_EQUAL(test[0].b, ci->b);
|
||||
|
||||
*ci = { 7, 8 };
|
||||
|
||||
CHECK_EQUAL(test[0].data, ci->data);
|
||||
CHECK_EQUAL(test[0].b, ci->b);
|
||||
|
||||
++ci;
|
||||
CHECK_EQUAL(test[1].data, ci->data);
|
||||
CHECK_EQUAL(test[1].b, ci->b);
|
||||
|
||||
++ci;
|
||||
CHECK_EQUAL(test[2].data, ci->data);
|
||||
CHECK_EQUAL(test[2].b, ci->b);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_conversion_operator)
|
||||
{
|
||||
const int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::circular_iterator<const int*> ci(std::begin(data), std::end(data));
|
||||
|
||||
CHECK_EQUAL(data, ci);
|
||||
|
||||
++ci;
|
||||
CHECK_EQUAL(data + 1, ci);
|
||||
|
||||
++ci;
|
||||
CHECK_EQUAL(data + 2, ci);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_operator_plus_equals)
|
||||
{
|
||||
const int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
for (int step = 1; step < 20; ++step)
|
||||
{
|
||||
etl::circular_iterator<const int*> ci(std::begin(data), std::end(data));
|
||||
|
||||
for (int i = 0; i < 20; i += step)
|
||||
{
|
||||
CHECK_EQUAL(data[i % 10], *ci);
|
||||
ci += step;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_operator_plus)
|
||||
{
|
||||
const int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
for (int step = 1; step < 20; ++step)
|
||||
{
|
||||
etl::circular_iterator<const int*> ci(std::begin(data), std::end(data));
|
||||
|
||||
for (int i = 0; i < 20; i += step)
|
||||
{
|
||||
CHECK_EQUAL(data[i % 10], *ci);
|
||||
ci = ci + step;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_operator_minus_equals)
|
||||
{
|
||||
const int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
etl::circular_iterator<const int*> initial(std::begin(data), std::end(data));
|
||||
etl::circular_iterator<const int*> ci;
|
||||
int step;
|
||||
|
||||
step = 1;
|
||||
ci = initial;
|
||||
|
||||
for (int i = 0; i < 20; i += step)
|
||||
{
|
||||
CHECK_EQUAL(data[(10 - i) % 10], *ci);
|
||||
ci -= step;
|
||||
}
|
||||
|
||||
//step = 2;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 3;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 4;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 5;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 6;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 7;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 8;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 9;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 10;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 11;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 12;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 13;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 14;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 15;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 16;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 17;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 18;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 19;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
|
||||
//step = 20;
|
||||
//ci = initial;
|
||||
|
||||
//for (int i = 0; i < 20; i += step)
|
||||
//{
|
||||
// CHECK_EQUAL(data[abs(i - step) % 10], *ci);
|
||||
// ci -= step;
|
||||
//}
|
||||
}
|
||||
|
||||
////*************************************************************************
|
||||
//TEST(test_operator_minus)
|
||||
//{
|
||||
// int compare[] = { 1, 2, 3, 4 };
|
||||
|
||||
// etl::circular_iterator<const int*> ci = &compare[1];
|
||||
|
||||
// for (int i = 0; i < 10; ++i)
|
||||
// {
|
||||
// CHECK_EQUAL(compare[1], *ci);
|
||||
// ci = ci - 1;
|
||||
// }
|
||||
//}
|
||||
|
||||
////*************************************************************************
|
||||
//TEST(test_assignment)
|
||||
//{
|
||||
// int data;
|
||||
// int b;
|
||||
|
||||
// etl::circular_iterator<int*> ci = &data;
|
||||
// ci = &b;
|
||||
|
||||
// CHECK_EQUAL(&b, ci);
|
||||
|
||||
// ci = &data;
|
||||
|
||||
// CHECK_EQUAL(&data, ci);
|
||||
//}
|
||||
|
||||
////*************************************************************************
|
||||
//TEST(test_equality)
|
||||
//{
|
||||
// int data;
|
||||
// int b;
|
||||
|
||||
// etl::circular_iterator<int*> ci1 = &data;
|
||||
// etl::circular_iterator<int*> ci2 = &data;
|
||||
// etl::circular_iterator<int*> ci3 = &b;
|
||||
|
||||
// CHECK(ci1 == ci2);
|
||||
// CHECK(ci1 != ci3);
|
||||
//}
|
||||
};
|
||||
}
|
||||
@ -11270,6 +11270,7 @@
|
||||
<ClCompile Include="..\test_char_traits.cpp" />
|
||||
<ClCompile Include="..\test_circular_buffer.cpp" />
|
||||
<ClCompile Include="..\test_circular_buffer_external_buffer.cpp" />
|
||||
<ClCompile Include="..\test_circular_iterator.cpp" />
|
||||
<ClCompile Include="..\test_compiler_settings.cpp" />
|
||||
<ClCompile Include="..\test_correlation.cpp" />
|
||||
<ClCompile Include="..\test_covariance.cpp" />
|
||||
|
||||
@ -3353,6 +3353,9 @@
|
||||
<ClCompile Include="..\test_bitset_new_explicit_single_element_type.cpp">
|
||||
<Filter>Tests\Binary</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\test_circular_iterator.cpp">
|
||||
<Filter>Tests\Memory & Iterators</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\library.properties">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user