]> git.llucax.com Git - software/posixx.git/commitdiff
Add reverse iteration to basic_buffer
authorLeandro Lucarella <llucarella@integratech.com.ar>
Mon, 1 Dec 2008 18:51:27 +0000 (16:51 -0200)
committerLeandro Lucarella <llucarella@integratech.com.ar>
Mon, 1 Dec 2008 18:51:27 +0000 (16:51 -0200)
Reverse iteration was broken in basic_buffer, and thus commented out. Now
the reverse iterators are defined properly and all seems to work fine
according to the added test cases for reverse iteration.

src/basic_buffer.hpp
test/buffer.cpp

index 873a999fd5f6d2a768b4a880279128b7fbdd1301..1e53f9641019a770fffb300636c56aa4e221e5df 100644 (file)
@@ -36,7 +36,7 @@ struct basic_buffer {
        typedef const value_type& const_reference;
 
        ///
-       typedef value_type* iterator; // TODO: make a real iterator
+       typedef value_type* iterator;
 
        ///
        typedef const value_type* const_iterator;
@@ -54,12 +54,10 @@ struct basic_buffer {
        typedef const pointer const_pointer;
 
        ///
-       // TODO: reverse iterator
-       //typedef typename std::reverse_iterator< iterator > reverse_iterator;
+       typedef std::reverse_iterator< iterator > reverse_iterator;
 
        ///
-       //typedef typename std::reverse_iterator< const iterator >
-       //              const_reverse_iterator;
+       typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
 
 
        // Construct/Copy/Destroy
@@ -179,29 +177,29 @@ struct basic_buffer {
         * Returns a random access reverse_iterator that points to the
         * past-the-end value.
         */
-       //reverse_iterator rbegin()
-       //{ return std::reverse_iterator< iterator >(end()); }
+       reverse_iterator rbegin()
+       { return reverse_iterator(end()); }
 
        /**
         * Returns a random access const_reverse_iterator that points to the
         * past-the-end value.
         */
-       //const_reverse_iterator rbegin() const
-       //{ return std::reverse_iterator< const iterator >(end()); }
+       const_reverse_iterator rbegin() const
+       { return const_reverse_iterator(end()); }
 
        /**
         * Returns a random access reverse_iterator that points to the first
         * element.
         */
-       //reverse_iterator rend()
-       //{ return std::reverse_iterator< iterator >(begin()); }
+       reverse_iterator rend()
+       { return reverse_iterator(begin()); }
 
        /**
         * Returns a random access const_reverse_iterator that points to the
         * first element.
         */
-       //const_reverse_iterator rend() const
-       //{ return std::reverse_iterator< const iterator >(begin()); }
+       const_reverse_iterator rend() const
+       { return const_reverse_iterator(begin()); }
 
 
        // Capacity
index f455ea51ee1dd8623d17927f51da98149dfdda8f..9a0039cfb03c1196609daad712da10380825f5c4 100644 (file)
@@ -23,6 +23,8 @@ std::ostream& operator << (std::ostream& os, const posixx::buffer& b)
 BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer ) // FIXME
 BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer::iterator )
 BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer::const_iterator )
+BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer::reverse_iterator )
+BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer::const_reverse_iterator )
 
 // avoid compilation warning concerning unused variables
 void dummy(buffer::const_iterator i) {}
@@ -241,6 +243,37 @@ BOOST_AUTO_TEST_CASE( iterators_test )
        BOOST_CHECK_EQUAL(*(b.begin() + 1), 77);
 }
 
+BOOST_AUTO_TEST_CASE( reverse_iterators_test )
+{
+       buffer::value_type a[] = { 56, 123, 253 };
+       buffer b(a, a + 3);
+       BOOST_CHECK_NE(b.rbegin(), b.rend());
+       BOOST_CHECK_LT(b.rbegin(), b.rend());
+       buffer::reverse_iterator i = b.rbegin();
+       BOOST_CHECK_EQUAL(*i, 253);
+       i++;
+       BOOST_CHECK_EQUAL(*i, 123);
+       ++i;
+       BOOST_CHECK_EQUAL(*i, 56);
+       i += 1;
+       BOOST_CHECK_EQUAL(i, b.rend());
+       i--;
+       BOOST_CHECK_EQUAL(*i, 56);
+       --i;
+       BOOST_CHECK_EQUAL(*i, 123);
+       i -= 1;
+       BOOST_CHECK_EQUAL(*i, 253);
+       i++;
+       BOOST_CHECK_EQUAL(*(i + 1), 56);
+       BOOST_CHECK_EQUAL(*(i - 1), 253);
+       BOOST_CHECK_EQUAL(i - 1, b.rbegin());
+       BOOST_CHECK_EQUAL(i + 2, b.rend());
+       BOOST_CHECK_EQUAL(i - b.rbegin(), 1);
+       *i = 77;
+       BOOST_CHECK_EQUAL(*i, 77);
+       BOOST_CHECK_EQUAL(*(b.rbegin() + 1), 77);
+}
+
 BOOST_AUTO_TEST_CASE( resize_test )
 {
        buffer b;