diff --git a/counting_iterator_test.cpp b/counting_iterator_test.cpp index dc999e6..0032dda 100644 --- a/counting_iterator_test.cpp +++ b/counting_iterator_test.cpp @@ -6,9 +6,24 @@ // 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 +#ifdef BOOST_MSVC +# pragma warning(disable:4786) // identifier truncated in debug info +#endif + +#include #include #include +#include #include #include #include @@ -37,8 +52,6 @@ template struct is_numeric }; }; -struct not_an_iterator_tag {}; - // Special tests for RandomAccess CountingIterators. template void category_test( @@ -65,12 +78,39 @@ void category_test( // Show that values outside the range can't be found assert(!std::binary_search(start, boost::prior(finish), *finish)); + + // Do the generic random_access_iterator_test + typedef typename CountingIterator::value_type value_type; + std::vector v; + for (value_type z = *start; z != *finish; ++z) + v.push_back(z); + if (v.size() >= 2) + { + // 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 + 1, v.size() - 1, v.begin() + 1); + } } -// Otherwise, we'll have to skip those. +// Special tests for bidirectional CountingIterators template -void category_test(CountingIterator, CountingIterator, std::forward_iterator_tag) +void category_test(CountingIterator start, CountingIterator finish, std::bidirectional_iterator_tag) { + if (finish != start + && finish != boost::next(start) + && finish != boost::next(boost::next(start))) + { + // 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(boost::next(start), boost::next(*start), boost::next(boost::next(*start))); + } +} + +template +void category_test(CountingIterator start, CountingIterator finish, std::forward_iterator_tag) +{ + if (finish != start && finish != boost::next(start)) + boost::forward_iterator_test(start, *start, boost::next(*start)); } template @@ -97,7 +137,7 @@ void test_aux(CountingIterator start, CountingIterator finish) template void test(Incrementable start, Incrementable finish) { - test_aux(boost::counting_iterator(start), boost::counting_iterator(finish)); + test_aux(boost::make_counting_iterator(start), boost::make_counting_iterator(finish)); } template @@ -112,7 +152,7 @@ template 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 @@ -120,11 +160,67 @@ void test_container(Container* = 0) // default arg works around MSVC bug std::advance(finish, c.size() - 1); test(start, finish); - - test(static_cast(start), - static_cast(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; +}; + +namespace boost { + template <> + struct counting_iterator_traits { + typedef std::ptrdiff_t difference_type; + typedef std::forward_iterator_tag iterator_category; + }; +} + +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. @@ -142,14 +238,26 @@ int main() test_integer(); test_integer(); #endif - // Some tests on container iterators, to prove we handle a few different categories + + // wrapping an iterator or non-built-in integer type causes an INTERNAL + // COMPILER ERROR in MSVC without STLport. I'm clueless as to why. +#if !defined(BOOST_MSVC) || defined(__SGI_STL_PORT) + // Test user-defined type. + test_integer(); + test_integer(); + test_integer(); + + // Some tests on container iterators, to prove we handle a few different categories test_container >(); test_container >(); -#ifndef BOOST_NO_SLIST +# ifndef BOOST_NO_SLIST test_container >(); -#endif +# endif + // Also prove that we can handle raw pointers. int array[2000]; - test(boost::counting_iterator(array), boost::counting_iterator(array+2000-1)); + test(boost::make_counting_iterator(array), boost::make_counting_iterator(array+2000-1)); +#endif + std::cout << "test successful " << std::endl; return 0; } diff --git a/iter_adaptor_fail_expected1.cpp b/iter_adaptor_fail_expected1.cpp index a28771c..2d0288c 100644 --- a/iter_adaptor_fail_expected1.cpp +++ b/iter_adaptor_fail_expected1.cpp @@ -11,15 +11,15 @@ // Revision History // 21 Jan 01 Initial version (Jeremy Siek) -#include #include +#include #include -#include int main() { - typedef boost::iterator_adaptor > adaptor_type; + typedef boost::iterator_adaptor::iterator, + boost::default_iterator_policies, + int,int&,int*,std::bidirectional_iterator_tag> adaptor_type; adaptor_type i; i += 4; diff --git a/iter_adaptor_fail_expected2.cpp b/iter_adaptor_fail_expected2.cpp index 02d980c..592801d 100644 --- a/iter_adaptor_fail_expected2.cpp +++ b/iter_adaptor_fail_expected2.cpp @@ -11,16 +11,16 @@ // Revision History // 21 Jan 01 Initial version (Jeremy Siek) +#include #include #include -#include #include -#include int main() { - typedef boost::iterator_adaptor > adaptor_type; + typedef boost::iterator_adaptor, + boost::default_iterator_policies, + int,int&,int*,std::input_iterator_tag> adaptor_type; adaptor_type iter; --iter; diff --git a/iter_adaptor_fail_expected3.cpp b/iter_adaptor_fail_expected3.cpp deleted file mode 100644 index 3140042..0000000 --- a/iter_adaptor_fail_expected3.cpp +++ /dev/null @@ -1,32 +0,0 @@ -// Test boost/pending/iterator_adaptors.hpp - -// (C) Copyright Jeremy Siek 1999. Permission to copy, use, modify, -// sell and distribute this software is granted provided this -// copyright notice appears in all copies. This software is provided -// "as is" without express or implied warranty, and with no claim as -// to its suitability for any purpose. - -// See http://www.boost.org for most recent version including documentation. - -// Revision History -// 21 Jan 01 Initial version (Jeremy Siek) - -#include -#include -#include -#include - -class foo { -public: - void bar() { } -}; - -int main() -{ - typedef boost::iterator_adaptor > adaptor_type; - - adaptor_type i; - i->bar(); - return 0; -}