mirror of
https://github.com/boostorg/iterator.git
synced 2025-05-10 07:33:53 +00:00
С++03 support was deprecated in 1.85 and now can be removed. This PR clears many of workarounds, which are no longer needed now. * Remove unused workaround macros (many of). * Remove BOOST_STATIC_ASSERT usages. * Minimize Boost::type_traits dependency (in favour of STL's type_traits). Closes https://github.com/boostorg/iterator/pull/82. Squashed commit of the following: commit 741a627b736ba81fe0054e5bf373141b04a8a597 Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Jan 25 12:13:05 2025 +0300 Replace testers with standard metafunctions. commit bf4cce611454713f1c8e5f46a2c3e599c548656d Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Jan 25 11:51:32 2025 +0300 Refactor is_lvalue_iterator.hpp. commit 8d080c6c58726269cf55aedd64aa239f7d098fc7 Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Jan 25 10:27:32 2025 +0300 Remove more workarounds. commit 5a4ba24d361ac0676d2cce95fdff22824ecdc287 Author: Georgy Guminov <gogagum@gmail.com> Date: Sun Jan 19 16:38:30 2025 +0300 Fixes. commit fdfafce2b9a71b1d85cbc697983652788f1c4bb7 Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Oct 26 15:06:43 2024 +0300 Remove BOOST_NO_STRICT_ITERATOR_INTEROPERABILITY Correct static_assert messages. Fix messages & replace is_standard_layout with is_copy_constructible. commit c69ac1408af2fe5a105b288644367bbb6e0bc30d Author: Georgy Guminov <gogagum@gmail.com> Date: Sat Oct 26 14:48:51 2024 +0300 Correct static_assert messages. commit b5df827151a6d752168a59fa8c20c9ffd3766c0b Author: Georqy Guminov <gogagum@gmail.com> Date: Sun Jun 23 16:12:29 2024 +0300 Fixes. Remove some Boost.MPL usages. Remove unused includes. commit 01fd35e9f87d43f4bb46ed525a0e8eec46cba44a Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 17:14:21 2024 +0300 abstract conjunction. commit c02def8acf68e0829082761d26955320974e5078 Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 16:35:43 2024 +0300 return addressof & conjunction. commit 3b3d1625752cbe8c9ec62662e594a1af1fd9a458 Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 16:30:44 2024 +0300 Make macro more readable. commit 4ab19e045fc535dc80228062d11bb6584ccd17ff Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 15:56:49 2024 +0300 Add static_assert messages. commit 82b5c44cd34435d63af5cdbc7fcc1b07b39394de Author: Georgiy Guminov <gogagum@gmail.com> Date: Wed Jun 12 14:12:10 2024 +0300 Return is iterator CXX17 test. commit 2d58d65462e837430a0990c2a414d5c397e9fa31 Author: Georgiy Guminov <gogagum@gmail.com> Date: Tue Jun 11 14:04:17 2024 +0300 Omitted. commit a0d04d9491de818df990188436e04afc3ddba3ad Author: Georgiy Guminov <gogagum@gmail.com> Date: Tue Jun 11 14:00:35 2024 +0300 Replace move with static_cast commit 4a49b8a1a2d44d2da728dd064fe810ca86d8d1fd Author: Georgiy Guminov <gogagum@gmail.com> Date: Mon Jun 10 21:38:53 2024 +0300 Return BOOST_NOEXCEPT commit 054c013bba75c42710537a819de91d6abfe25658 Author: Georgiy Guminov <gogagum@gmail.com> Date: Sun Jun 9 15:20:41 2024 +0300 CXX11
301 lines
9.1 KiB
C++
301 lines
9.1 KiB
C++
// (C) Copyright David Abrahams 2001.
|
|
// Distributed under the Boost Software License, Version 1.0. (See
|
|
// accompanying file LICENSE_1_0.txt or copy at
|
|
// http://www.boost.org/LICENSE_1_0.txt)
|
|
//
|
|
// See http://www.boost.org for most recent version including documentation.
|
|
//
|
|
// Revision History
|
|
// 16 Feb 2001 Added a missing const. Made the tests run (somewhat) with
|
|
// plain MSVC again. (David Abrahams)
|
|
// 11 Feb 2001 #if 0'd out use of counting_iterator on non-numeric types in
|
|
// MSVC without STLport, so that the other tests may proceed
|
|
// (David Abrahams)
|
|
// 04 Feb 2001 Added use of iterator_tests.hpp (David Abrahams)
|
|
// 28 Jan 2001 Removed not_an_iterator detritus (David Abrahams)
|
|
// 24 Jan 2001 Initial revision (David Abrahams)
|
|
|
|
#include <boost/config.hpp>
|
|
|
|
#ifdef BOOST_BORLANDC // Borland mis-detects our custom iterators
|
|
# pragma warn -8091 // template argument ForwardIterator passed to '...' is a output iterator
|
|
# pragma warn -8071 // Conversion may lose significant digits (due to counting_iterator<char> += n).
|
|
#endif
|
|
|
|
#ifdef BOOST_MSVC
|
|
# pragma warning(disable:4786) // identifier truncated in debug info
|
|
#endif
|
|
|
|
#include <boost/iterator/counting_iterator.hpp>
|
|
#include <boost/iterator/new_iterator_tests.hpp>
|
|
|
|
#include <boost/next_prior.hpp>
|
|
#include <boost/detail/workaround.hpp>
|
|
#include <boost/limits.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <iterator>
|
|
#include <type_traits>
|
|
#include <stdlib.h>
|
|
#ifndef BOOST_BORLANDC
|
|
# include <boost/tuple/tuple.hpp>
|
|
#endif
|
|
#include <vector>
|
|
#include <list>
|
|
#include <boost/core/lightweight_test.hpp>
|
|
#ifndef BOOST_NO_SLIST
|
|
# ifdef BOOST_SLIST_HEADER
|
|
# include BOOST_SLIST_HEADER
|
|
# else
|
|
# include <slist>
|
|
# endif
|
|
#endif
|
|
|
|
|
|
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
|
template <class T>
|
|
struct signed_assert_nonnegative
|
|
{
|
|
static void test(T x) { BOOST_TEST(x >= 0); }
|
|
};
|
|
|
|
template <class T>
|
|
struct unsigned_assert_nonnegative
|
|
{
|
|
static void test(T x) {}
|
|
};
|
|
|
|
template <class T>
|
|
struct assert_nonnegative
|
|
: std::conditional<
|
|
std::numeric_limits<T>::is_signed
|
|
, signed_assert_nonnegative<T>
|
|
, unsigned_assert_nonnegative<T>
|
|
>::type
|
|
{
|
|
};
|
|
#endif
|
|
|
|
// Special tests for RandomAccess CountingIterators.
|
|
template <class CountingIterator, class Value>
|
|
void category_test(
|
|
CountingIterator start,
|
|
CountingIterator finish,
|
|
Value,
|
|
std::random_access_iterator_tag)
|
|
{
|
|
typedef typename
|
|
std::iterator_traits<CountingIterator>::difference_type
|
|
difference_type;
|
|
difference_type distance = std::distance(start, finish);
|
|
|
|
// Pick a random position internal to the range
|
|
difference_type offset = (unsigned)rand() % distance;
|
|
|
|
#ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
|
BOOST_TEST(offset >= 0);
|
|
#else
|
|
assert_nonnegative<difference_type>::test(offset);
|
|
#endif
|
|
|
|
CountingIterator internal = start;
|
|
std::advance(internal, offset);
|
|
|
|
// Try some binary searches on the range to show that it's ordered
|
|
BOOST_TEST(std::binary_search(start, finish, *internal));
|
|
|
|
// #including tuple crashed borland, so I had to give up on tie().
|
|
std::pair<CountingIterator,CountingIterator> xy(
|
|
std::equal_range(start, finish, *internal));
|
|
CountingIterator x = xy.first, y = xy.second;
|
|
|
|
BOOST_TEST(std::distance(x, y) == 1);
|
|
|
|
// Show that values outside the range can't be found
|
|
BOOST_TEST(!std::binary_search(start, boost::prior(finish), *finish));
|
|
|
|
// Do the generic random_access_iterator_test
|
|
typedef typename CountingIterator::value_type value_type;
|
|
std::vector<value_type> v;
|
|
for (value_type z = *start; !(z == *finish); ++z)
|
|
v.push_back(z);
|
|
|
|
// Note that this test requires a that the first argument is
|
|
// dereferenceable /and/ a valid iterator prior to the first argument
|
|
boost::random_access_iterator_test(start, v.size(), v.begin());
|
|
}
|
|
|
|
// Special tests for bidirectional CountingIterators
|
|
template <class CountingIterator, class Value>
|
|
void category_test(CountingIterator start, Value v1, std::bidirectional_iterator_tag)
|
|
{
|
|
Value v2 = v1;
|
|
++v2;
|
|
|
|
// Note that this test requires a that the first argument is
|
|
// dereferenceable /and/ a valid iterator prior to the first argument
|
|
boost::bidirectional_iterator_test(start, v1, v2);
|
|
}
|
|
|
|
template <class CountingIterator, class Value>
|
|
void category_test(CountingIterator start, CountingIterator finish, Value v1, std::forward_iterator_tag)
|
|
{
|
|
Value v2 = v1;
|
|
++v2;
|
|
if (finish != start && finish != boost::next(start))
|
|
boost::forward_readable_iterator_test(start, finish, v1, v2);
|
|
}
|
|
|
|
template <class CountingIterator, class Value>
|
|
void test_aux(CountingIterator start, CountingIterator finish, Value v1)
|
|
{
|
|
typedef typename CountingIterator::iterator_category category;
|
|
|
|
// If it's a RandomAccessIterator we can do a few delicate tests
|
|
category_test(start, finish, v1, category());
|
|
|
|
// Okay, brute force...
|
|
for (CountingIterator p = start
|
|
; p != finish && boost::next(p) != finish
|
|
; ++p)
|
|
{
|
|
BOOST_TEST(boost::next(*p) == *boost::next(p));
|
|
}
|
|
|
|
// prove that a reference can be formed to these values
|
|
typedef typename CountingIterator::value_type value;
|
|
const value* q = &*start;
|
|
(void)q; // suppress unused variable warning
|
|
}
|
|
|
|
template <class Incrementable>
|
|
void test(Incrementable start, Incrementable finish)
|
|
{
|
|
test_aux(boost::make_counting_iterator(start), boost::make_counting_iterator(finish), start);
|
|
}
|
|
|
|
template <class Integer>
|
|
void test_integer(Integer* = 0) // default arg works around MSVC bug
|
|
{
|
|
Integer start = 0;
|
|
Integer finish = 120;
|
|
test(start, finish);
|
|
}
|
|
|
|
template <class Integer, class Category, class Difference>
|
|
void test_integer3(Integer* = 0, Category* = 0, Difference* = 0) // default arg works around MSVC bug
|
|
{
|
|
Integer start = 0;
|
|
Integer finish = 120;
|
|
typedef boost::counting_iterator<Integer,Category,Difference> iterator;
|
|
test_aux(iterator(start), iterator(finish), start);
|
|
}
|
|
|
|
template <class Container>
|
|
void test_container(Container* = 0) // default arg works around MSVC bug
|
|
{
|
|
Container c(1 + (unsigned)rand() % 1673);
|
|
|
|
const typename Container::iterator start = c.begin();
|
|
|
|
// back off by 1 to leave room for dereferenceable value at the end
|
|
typename Container::iterator finish = start;
|
|
std::advance(finish, c.size() - 1);
|
|
|
|
test(start, finish);
|
|
|
|
typedef typename Container::const_iterator const_iterator;
|
|
test(const_iterator(start), const_iterator(finish));
|
|
}
|
|
|
|
class my_int1 {
|
|
public:
|
|
my_int1() { }
|
|
my_int1(int x) : m_int(x) { }
|
|
my_int1& operator++() { ++m_int; return *this; }
|
|
bool operator==(const my_int1& x) const { return m_int == x.m_int; }
|
|
private:
|
|
int m_int;
|
|
};
|
|
|
|
class my_int2 {
|
|
public:
|
|
typedef void value_type;
|
|
typedef void pointer;
|
|
typedef void reference;
|
|
typedef std::ptrdiff_t difference_type;
|
|
typedef std::bidirectional_iterator_tag iterator_category;
|
|
|
|
my_int2() { }
|
|
my_int2(int x) : m_int(x) { }
|
|
my_int2& operator++() { ++m_int; return *this; }
|
|
my_int2& operator--() { --m_int; return *this; }
|
|
bool operator==(const my_int2& x) const { return m_int == x.m_int; }
|
|
private:
|
|
int m_int;
|
|
};
|
|
|
|
class my_int3 {
|
|
public:
|
|
typedef void value_type;
|
|
typedef void pointer;
|
|
typedef void reference;
|
|
typedef std::ptrdiff_t difference_type;
|
|
typedef std::random_access_iterator_tag iterator_category;
|
|
|
|
my_int3() { }
|
|
my_int3(int x) : m_int(x) { }
|
|
my_int3& operator++() { ++m_int; return *this; }
|
|
my_int3& operator+=(std::ptrdiff_t n) { m_int += n; return *this; }
|
|
std::ptrdiff_t operator-(const my_int3& x) const { return m_int - x.m_int; }
|
|
my_int3& operator--() { --m_int; return *this; }
|
|
bool operator==(const my_int3& x) const { return m_int == x.m_int; }
|
|
bool operator!=(const my_int3& x) const { return m_int != x.m_int; }
|
|
bool operator<(const my_int3& x) const { return m_int < x.m_int; }
|
|
private:
|
|
int m_int;
|
|
};
|
|
|
|
int main()
|
|
{
|
|
// Test the built-in integer types.
|
|
test_integer<char>();
|
|
test_integer<unsigned char>();
|
|
test_integer<signed char>();
|
|
test_integer<wchar_t>();
|
|
test_integer<short>();
|
|
test_integer<unsigned short>();
|
|
test_integer<int>();
|
|
test_integer<unsigned int>();
|
|
test_integer<long>();
|
|
test_integer<unsigned long>();
|
|
#if defined(BOOST_HAS_LONG_LONG)
|
|
test_integer<boost::long_long_type>();
|
|
test_integer<boost::ulong_long_type>();
|
|
#endif
|
|
#if defined(BOOST_HAS_INT128)
|
|
test_integer<boost::int128_type>();
|
|
test_integer<boost::uint128_type>();
|
|
#endif
|
|
|
|
// Test user-defined type.
|
|
|
|
test_integer3<my_int1, std::forward_iterator_tag, int>();
|
|
test_integer3<long, std::random_access_iterator_tag, int>();
|
|
test_integer<my_int2>();
|
|
test_integer<my_int3>();
|
|
|
|
// Some tests on container iterators, to prove we handle a few different categories
|
|
test_container<std::vector<int> >();
|
|
test_container<std::list<int> >();
|
|
# ifndef BOOST_NO_SLIST
|
|
test_container<BOOST_STD_EXTENSION_NAMESPACE::slist<int> >();
|
|
# endif
|
|
|
|
// Also prove that we can handle raw pointers.
|
|
int array[2000];
|
|
test(boost::make_counting_iterator(array), boost::make_counting_iterator(array+2000-1));
|
|
|
|
return boost::report_errors();
|
|
}
|