Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r81978 - in sandbox-branches/geometry/index: boost/geometry/extensions/index test
From: adam.wulkiewicz_at_[hidden]
Date: 2012-12-15 15:07:20


Author: awulkiew
Date: 2012-12-15 15:07:19 EST (Sat, 15 Dec 2012)
New Revision: 81978
URL: http://svn.boost.org/trac/boost/changeset/81978

Log:
Added static_vector::assign(count, value) + test.
Added static_vector iterators test.
Text files modified:
   sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp | 67 ++++++++++++++++++++++++---------------
   sandbox-branches/geometry/index/test/static_vector.cpp | 66 +++++++++++++++++++++++++++++---------
   2 files changed, 91 insertions(+), 42 deletions(-)

Modified: sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp
==============================================================================
--- sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp (original)
+++ sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp 2012-12-15 15:07:19 EST (Sat, 15 Dec 2012)
@@ -58,18 +58,17 @@
     {}
 
     // strong
- explicit static_vector(size_type s)
+ explicit static_vector(size_type count)
         : m_size(0)
     {
- resize(s); // may throw
+ resize(count); // may throw
     }
 
     // strong
- //template <typename Value>
- static_vector(size_type s, value_type const& value)
+ static_vector(size_type count, value_type const& value)
         : m_size(0)
     {
- resize(s, value); // may throw
+ resize(count, value); // may throw
     }
 
     // strong
@@ -105,47 +104,45 @@
     }
 
     // strong
- void resize(size_type s)
+ void resize(size_type count)
     {
- if ( s < m_size )
+ if ( count < m_size )
         {
- this->destroy(this->begin() + s, this->end());
+ this->destroy(this->begin() + count, this->end());
         }
         else
         {
- BOOST_ASSERT_MSG(s <= Capacity, "size can't exceed the capacity");
- //if ( Capacity <= s ) throw std::bad_alloc();
- this->construct(this->ptr(m_size), this->ptr(s)); // may throw
+ BOOST_ASSERT_MSG(count <= Capacity, "size can't exceed the capacity");
+ //if ( Capacity <= count ) throw std::bad_alloc();
+ this->construct(this->ptr(m_size), this->ptr(count)); // may throw
         }
- m_size = s; // update end
+ m_size = count; // update end
     }
 
     // strong
- //template <typename Value>
- void resize(size_type s, value_type const& value)
+ void resize(size_type count, value_type const& value)
     {
- if ( s < m_size )
+ if ( count < m_size )
         {
- this->destroy(this->begin() + s, this->end());
+ this->destroy(this->begin() + count, this->end());
         }
         else
         {
- BOOST_ASSERT_MSG(s <= Capacity, "size can't exceed the capacity");
- //if ( Capacity <= s ) throw std::bad_alloc();
- std::uninitialized_fill(this->ptr(m_size), this->ptr(s), value); // may throw
+ BOOST_ASSERT_MSG(count <= Capacity, "size can't exceed the capacity");
+ //if ( Capacity <= count ) throw std::bad_alloc();
+ std::uninitialized_fill(this->ptr(m_size), this->ptr(count), value); // may throw
         }
- m_size = s; // update end
+ m_size = count; // update end
     }
 
     // nothrow
- void reserve(size_type BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(s))
+ void reserve(size_type BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(count))
     {
- BOOST_ASSERT_MSG(s <= Capacity, "size can't exceed the capacity");
- //if ( Capacity <= s ) throw std::bad_alloc();
+ BOOST_ASSERT_MSG(count <= Capacity, "size can't exceed the capacity");
+ //if ( Capacity <= count ) throw std::bad_alloc();
     }
 
     // strong
- //template <typename Value>
     void push_back(value_type const& value)
     {
         BOOST_ASSERT_MSG(m_size < Capacity, "size can't exceed the capacity");
@@ -170,6 +167,24 @@
         assign_dispatch(first, last, traversal()); // may throw
     }
 
+ // basic
+ void assign(size_type count, value_type const& value)
+ {
+ if ( count < m_size )
+ {
+ std::fill_n(this->begin(), count, value);
+ this->destroy(this->begin() + count, this->end());
+ }
+ else
+ {
+ std::fill_n(this->begin(), m_size, value);
+ BOOST_ASSERT_MSG(count <= Capacity, "size can't exceed the capacity");
+ //if ( Capacity <= count ) throw std::bad_alloc();
+ std::uninitialized_fill(this->ptr(m_size), this->ptr(count), value); // may throw
+ }
+ m_size = count; // update end
+ }
+
     // nothrow
     void clear()
     {
@@ -236,8 +251,8 @@
     }
 
     // nothrow
- Value * data() { return this->ptr(0); }
- const Value * data() const { return this->ptr(0); }
+ Value * data() { return this->ptr(); }
+ const Value * data() const { return this->ptr(); }
 
     // nothrow
     iterator begin() { return this->ptr(); }

Modified: sandbox-branches/geometry/index/test/static_vector.cpp
==============================================================================
--- sandbox-branches/geometry/index/test/static_vector.cpp (original)
+++ sandbox-branches/geometry/index/test/static_vector.cpp 2012-12-15 15:07:19 EST (Sat, 15 Dec 2012)
@@ -17,7 +17,7 @@
 class value_ndc
 {
 public:
- value_ndc(int a) : aa(a) {}
+ explicit value_ndc(int a) : aa(a) {}
     ~value_ndc() {}
     bool operator==(value_ndc const& v) const { return aa == v.aa; }
 private:
@@ -29,7 +29,7 @@
 class value_nd
 {
 public:
- value_nd(int a) : aa(a) {}
+ explicit value_nd(int a) : aa(a) {}
     ~value_nd() {}
     bool operator==(value_nd const& v) const { return aa == v.aa; }
 private:
@@ -39,7 +39,7 @@
 class value_nc
 {
 public:
- value_nc(int a = 0) : aa(a) {}
+ explicit value_nc(int a = 0) : aa(a) {}
     ~value_nc() {}
     bool operator==(value_nc const& v) const { return aa == v.aa; }
 private:
@@ -51,7 +51,7 @@
 class counting_value
 {
 public:
- counting_value(int a = 0) : aa(a) { ++c(); }
+ explicit counting_value(int a = 0) : aa(a) { ++c(); }
     counting_value(counting_value const& v) : aa(v.aa) { ++c(); }
     counting_value & operator=(counting_value const& v) { aa = v.aa; return *this; }
     ~counting_value() { --c(); }
@@ -181,7 +181,7 @@
     static_vector<T, N> s;
 
     for ( size_t i = 0 ; i < N ; ++i )
- s.push_back(i);
+ s.push_back(T(i));
 
     for ( size_t i = N ; i > 1 ; --i )
     {
@@ -204,7 +204,7 @@
 }
 
 template <typename T, size_t N>
-void test_copy_and_assign_nd()
+void test_copy_and_assign_nd(T const& val)
 {
     static_vector<T, N> s;
     std::vector<T> v;
@@ -212,9 +212,9 @@
 
     for ( size_t i = 0 ; i < N ; ++i )
     {
- s.push_back(i);
- v.push_back(i);
- l.push_back(i);
+ s.push_back(T(i));
+ v.push_back(T(i));
+ l.push_back(T(i));
     }
     // copy ctor
     {
@@ -268,6 +268,35 @@
         BOOST_CHECK(l.size() == s1.size());
         test_compare_ranges(l.begin(), l.end(), s1.begin(), s1.end());
     }
+ // assign(N, V)
+ {
+ static_vector<T, N> s1(s);
+ test_compare_ranges(s.begin(), s.end(), s1.begin(), s1.end());
+ std::vector<T> a(N, val);
+ s1.assign(N, val);
+ test_compare_ranges(a.begin(), a.end(), s1.begin(), s1.end());
+ }
+}
+
+template <typename T, size_t N>
+void test_iterators_nd()
+{
+ static_vector<T, N> s;
+ std::vector<T> v;
+
+ for ( size_t i = 0 ; i < N ; ++i )
+ {
+ s.push_back(T(i));
+ v.push_back(T(i));
+ }
+
+ test_compare_ranges(s.begin(), s.end(), v.begin(), v.end());
+ test_compare_ranges(s.rbegin(), s.rend(), v.rbegin(), v.rend());
+
+ s.assign(v.rbegin(), v.rend());
+
+ test_compare_ranges(s.begin(), s.end(), v.rbegin(), v.rend());
+ test_compare_ranges(s.rbegin(), s.rend(), v.begin(), v.end());
 }
 
 int test_main(int, char* [])
@@ -285,8 +314,8 @@
     BOOST_CHECK(counting_value::count() == 0);
 
     test_ctor_nd<int, 10>(5, 1);
- test_ctor_nd<value_nd, 10>(5, 1);
- test_ctor_nd<counting_value, 10>(5, 1);
+ test_ctor_nd<value_nd, 10>(5, value_nd(1));
+ test_ctor_nd<counting_value, 10>(5, counting_value(1));
     BOOST_CHECK(counting_value::count() == 0);
 
     test_resize_nc<int, 10>(5);
@@ -295,8 +324,8 @@
     BOOST_CHECK(counting_value::count() == 0);
 
     test_resize_nd<int, 10>(5, 1);
- test_resize_nd<value_nd, 10>(5, 1);
- test_resize_nd<counting_value, 10>(5, 1);
+ test_resize_nd<value_nd, 10>(5, value_nd(1));
+ test_resize_nd<counting_value, 10>(5, counting_value(1));
     BOOST_CHECK(counting_value::count() == 0);
 
     test_push_back_nd<int, 10>();
@@ -309,9 +338,14 @@
     test_pop_back_nd<counting_value, 10>();
     BOOST_CHECK(counting_value::count() == 0);
 
- test_copy_and_assign_nd<int, 10>();
- test_copy_and_assign_nd<value_nd, 10>();
- test_copy_and_assign_nd<counting_value, 10>();
+ test_copy_and_assign_nd<int, 10>(1);
+ test_copy_and_assign_nd<value_nd, 10>(value_nd(1));
+ test_copy_and_assign_nd<counting_value, 10>(counting_value(1));
+ BOOST_CHECK(counting_value::count() == 0);
+
+ test_iterators_nd<int, 10>();
+ test_iterators_nd<value_nd, 10>();
+ test_iterators_nd<counting_value, 10>();
     BOOST_CHECK(counting_value::count() == 0);
 
     return 0;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk