From 52eef989dae3fcc319e21ed787e5b966aaeb0770 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Tue, 26 Feb 2013 22:03:44 +0000 Subject: [PATCH] Clean up merge failure [SVN r83170] --- test/gather_fail1.cpp | 38 ++++++ test/gather_test1.cpp | 140 +++++++++++++++++++ test/iterator_test.hpp | 296 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 474 insertions(+) create mode 100644 test/gather_fail1.cpp create mode 100644 test/gather_test1.cpp create mode 100644 test/iterator_test.hpp diff --git a/test/gather_fail1.cpp b/test/gather_fail1.cpp new file mode 100644 index 0000000..2670bd0 --- /dev/null +++ b/test/gather_fail1.cpp @@ -0,0 +1,38 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + 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) + + For more information, see http://www.boost.org +*/ + +#include + +#include +#include + +#include +#include +#include + +#include "iterator_test.hpp" + +namespace ba = boost::algorithm; + +bool is_ten ( int i ) { return i == 10; } + +void test_sequence1 () { + std::vector v; + typedef input_iterator::iterator> II; + +// This should fail to compile, since gather doesn't work with input iterators + (void) ba::gather ( II( v.begin ()), II( v.end ()), II( v.begin ()), is_even ); + } + + +int main () +{ + test_sequence1 (); + return 0; +} diff --git a/test/gather_test1.cpp b/test/gather_test1.cpp new file mode 100644 index 0000000..0e57b6e --- /dev/null +++ b/test/gather_test1.cpp @@ -0,0 +1,140 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + 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) + + For more information, see http://www.boost.org +*/ + +#include + +#include +#include + +#define BOOST_TEST_MAIN +#include + +#include +#include +#include + +#include "iterator_test.hpp" + +namespace ba = boost::algorithm; + +template +void print ( const char *prompt, const Container &c ) { + std::cout << prompt << " { "; + std::copy ( c.begin (), c.end (), std::ostream_iterator(std::cout, " ")); + std::cout << std::endl; + } + +template +void test_iterators ( Iterator first, Iterator last, Predicate comp, std::size_t offset ) { +// Create the pivot point + Iterator off = first; + std::advance(off, offset); + +// Gather the elements + std::pair res = ba::gather ( first, last, off, comp ); + +// We should now have three sequences, any of which may be empty: +// * [begin .. result.first) - items that do not satisfy the predicate +// * [result.first .. result.second) - items that do satisfy the predicate +// * [result.second .. end) - items that do not satisfy the predicate + Iterator iter = first; + for ( ; iter != res.first; ++iter ) + BOOST_CHECK ( !comp ( *iter )); + for ( ; iter != res.second; ++iter) + BOOST_CHECK ( comp ( *iter )); + for ( ; iter != last; ++iter ) + BOOST_CHECK ( !comp ( *iter )); + } + +template +void test_iterator_types ( const Container &c, Predicate comp, std::size_t offset ) { + typedef std::vector vec; + typedef forward_iterator FI; + typedef bidirectional_iterator BDI; + typedef random_access_iterator RAI; + + vec v; + v.assign ( c.begin (), c.end ()); + test_iterators ( FI ( v.begin ()), FI ( v.end ()), comp, offset ); + v.assign ( c.begin (), c.end ()); + test_iterators ( BDI ( v.begin ()), BDI ( v.end ()), comp, offset ); + v.assign ( c.begin (), c.end ()); + test_iterators ( RAI ( v.begin ()), RAI ( v.end ()), comp, offset ); + } + + +template +struct less_than { +public: +// typedef T argument_type; +// typedef bool result_type; + + less_than ( T foo ) : val ( foo ) {} + less_than ( const less_than &rhs ) : val ( rhs.val ) {} + + bool operator () ( const T &v ) const { return v < val; } +private: + less_than (); + less_than operator = ( const less_than &rhs ); + T val; + }; + +bool is_even ( int i ) { return i % 2 == 0; } +bool is_ten ( int i ) { return i == 10; } + +void test_sequence1 () { + std::vector v; + + for ( int i = 5; i < 15; ++i ) + v.push_back ( i ); + test_iterator_types ( v, less_than(10), 0 ); // at beginning + test_iterator_types ( v, less_than(10), 5 ); + test_iterator_types ( v, less_than(10), v.size () - 1 ); // at end + + test_iterator_types ( v, is_even, 0 ); + test_iterator_types ( v, is_even, 5 ); + test_iterator_types ( v, is_even, v.size () - 1 ); + +// Exactly one element in the sequence matches + test_iterator_types ( v, is_ten, 0 ); + test_iterator_types ( v, is_ten, 5 ); + test_iterator_types ( v, is_ten, v.size () - 1 ); + +// Everything in the sequence matches + test_iterator_types ( v, less_than(99), 0 ); + test_iterator_types ( v, less_than(99), 5 ); + test_iterator_types ( v, less_than(99), v.size () - 1 ); + +// Nothing in the sequence matches + test_iterator_types ( v, less_than(0), 0 ); + test_iterator_types ( v, less_than(0), 5 ); + test_iterator_types ( v, less_than(0), v.size () - 1 ); + +// All the elements in the sequence are the same + v.clear (); + for ( int i = 0; i < 11; ++i ) + v.push_back ( 10 ); + +// Everything in the sequence matches + test_iterator_types ( v, is_ten, 0 ); + test_iterator_types ( v, is_ten, 5 ); + test_iterator_types ( v, is_ten, v.size () - 1 ); + +// Nothing in the sequence matches + test_iterator_types ( v, less_than(5), 0 ); + test_iterator_types ( v, less_than(5), 5 ); + test_iterator_types ( v, less_than(5), v.size () - 1 ); + + } + + +BOOST_AUTO_TEST_CASE( test_main ) +{ + test_sequence1 (); +} diff --git a/test/iterator_test.hpp b/test/iterator_test.hpp new file mode 100644 index 0000000..cd77fb6 --- /dev/null +++ b/test/iterator_test.hpp @@ -0,0 +1,296 @@ +#ifndef ITERATOR_TEST_H +#define ITERATOR_TEST_H + +/* + A set of iterator adapters for constructing test cases + From an iterator (or a pointer), you can make any class of iterator. + Assuming you want to degrade the capabilities. + + Modeled closely on work that Howard Hinnant did for libc++. +*/ + +#include + +// == Input Iterator == +template +class input_iterator { +public: + typedef std::input_iterator_tag iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::reference reference; + + It base() const {return it_;} + + input_iterator() : it_() {} + explicit input_iterator(It it) : it_(it) {} + + template + input_iterator(const input_iterator& u) :it_(u.it_) {} + + reference operator*() const {return *it_;} + pointer operator->() const {return it_;} + + input_iterator& operator++() {++it_; return *this;} + input_iterator operator++(int) {input_iterator tmp(*this); ++(*this); return tmp;} + + friend bool operator==(const input_iterator& x, const input_iterator& y) + {return x.it_ == y.it_;} + friend bool operator!=(const input_iterator& x, const input_iterator& y) + {return !(x == y);} + +private: + It it_; + template friend class input_iterator; +}; + +template +inline bool +operator==(const input_iterator& x, const input_iterator& y) +{ + return x.base() == y.base(); +} + +template +inline bool +operator!=(const input_iterator& x, const input_iterator& y) +{ + return !(x == y); +} + + +// == Forward Iterator == +template +class forward_iterator { +public: + typedef std::forward_iterator_tag iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::reference reference; + + It base() const {return it_;} + + forward_iterator() : it_() {} + explicit forward_iterator(It it) : it_(it) {} + template + forward_iterator(const forward_iterator& u) :it_(u.it_) {} + + reference operator*() const {return *it_;} + pointer operator->() const {return it_;} + + forward_iterator& operator++() {++it_; return *this;} + forward_iterator operator++(int) {forward_iterator tmp(*this); ++(*this); return tmp;} + + friend bool operator==(const forward_iterator& x, const forward_iterator& y) + {return x.it_ == y.it_;} + friend bool operator!=(const forward_iterator& x, const forward_iterator& y) + {return !(x == y);} +private: + It it_; + + template friend class forward_iterator; +}; + +template +inline bool +operator==(const forward_iterator& x, const forward_iterator& y) +{ + return x.base() == y.base(); +} + +template +inline bool +operator!=(const forward_iterator& x, const forward_iterator& y) +{ + return !(x == y); +} + +// == Bidirectional Iterator == +template +class bidirectional_iterator +{ +public: + typedef std::bidirectional_iterator_tag iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::reference reference; + + It base() const {return it_;} + + bidirectional_iterator() : it_() {} + explicit bidirectional_iterator(It it) : it_(it) {} + template + bidirectional_iterator(const bidirectional_iterator& u) :it_(u.it_) {} + + reference operator*() const {return *it_;} + pointer operator->() const {return it_;} + + bidirectional_iterator& operator++() {++it_; return *this;} + bidirectional_iterator operator++(int) {bidirectional_iterator tmp(*this); ++(*this); return tmp;} + + bidirectional_iterator& operator--() {--it_; return *this;} + bidirectional_iterator operator--(int) {bidirectional_iterator tmp(*this); --(*this); return tmp;} +private: + It it_; + template friend class bidirectional_iterator; +}; + +template +inline bool +operator==(const bidirectional_iterator& x, const bidirectional_iterator& y) +{ + return x.base() == y.base(); +} + +template +inline bool +operator!=(const bidirectional_iterator& x, const bidirectional_iterator& y) +{ + return !(x == y); +} + + +// == Random Access Iterator == +template +class random_access_iterator { +public: + typedef std::random_access_iterator_tag iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::reference reference; + + It base() const {return it_;} + + random_access_iterator() : it_() {} + explicit random_access_iterator(It it) : it_(it) {} + template + random_access_iterator(const random_access_iterator& u) :it_(u.it_) {} + + reference operator*() const {return *it_;} + pointer operator->() const {return it_;} + + random_access_iterator& operator++() {++it_; return *this;} + random_access_iterator operator++(int) {random_access_iterator tmp(*this); ++(*this); return tmp;} + + random_access_iterator& operator--() {--it_; return *this;} + random_access_iterator operator--(int) {random_access_iterator tmp(*this); --(*this); return tmp;} + + random_access_iterator& operator+=(difference_type n) {it_ += n; return *this;} + random_access_iterator operator+ (difference_type n) const {random_access_iterator tmp(*this); tmp += n; return tmp;} + friend random_access_iterator operator+(difference_type n, random_access_iterator x) {x += n; return x;} + + random_access_iterator& operator-=(difference_type n) {return *this += -n;} + random_access_iterator operator- (difference_type n) const {random_access_iterator tmp(*this); tmp -= n; return tmp;} + + reference operator[](difference_type n) const {return it_[n];} +private: + It it_; + + template friend class random_access_iterator; +}; + +template +inline bool +operator==(const random_access_iterator& x, const random_access_iterator& y) +{ + return x.base() == y.base(); +} + +template +inline bool +operator!=(const random_access_iterator& x, const random_access_iterator& y) +{ + return !(x == y); +} + +template +inline bool +operator<(const random_access_iterator& x, const random_access_iterator& y) +{ + return x.base() < y.base(); +} + +template +inline bool +operator<=(const random_access_iterator& x, const random_access_iterator& y) +{ + return !(y < x); +} + +template +inline bool +operator>(const random_access_iterator& x, const random_access_iterator& y) +{ + return y < x; +} + +template +inline bool +operator>=(const random_access_iterator& x, const random_access_iterator& y) +{ + return !(x < y); +} + +template +inline typename std::iterator_traits::difference_type +operator-(const random_access_iterator& x, const random_access_iterator& y) +{ + return x.base() - y.base(); +} + + +// == Output Iterator == +template +class output_iterator { +public: + typedef std::output_iterator_tag iterator_category; + typedef void value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::reference reference; + + It base() const {return it_;} + + output_iterator () {} + explicit output_iterator(It it) : it_(it) {} + + template + output_iterator(const output_iterator& u) :it_(u.it_) {} + + reference operator*() const {return *it_;} + + output_iterator& operator++() {++it_; return *this;} + output_iterator operator++(int) {output_iterator tmp(*this); ++(*this); return tmp;} + +private: + It it_; + template friend class output_iterator; + }; + +// No comparison operators for output iterators + + +// == Get the base of an iterator; used for comparisons == +template +inline Iter base(output_iterator i) { return i.base(); } + +template +inline Iter base(input_iterator i) { return i.base(); } + +template +inline Iter base(forward_iterator i) { return i.base(); } + +template +inline Iter base(bidirectional_iterator i) { return i.base(); } + +template +inline Iter base(random_access_iterator i) { return i.base(); } + +template // everything else +inline Iter base(Iter i) { return i; } + +#endif // ITERATORS_H