1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2013-2013. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_TEST_DEFAULT_INIT_TEST_HEADER
12 #define BOOST_CONTAINER_TEST_DEFAULT_INIT_TEST_HEADER
14 #include <boost/container/detail/config_begin.hpp>
15 #include <boost/container/throw_exception.hpp>
16 #include <boost/move/detail/force_ptr.hpp>
25 template<int Dummy = 0>
26 class default_init_allocator_base
29 static unsigned char s_pattern;
30 static bool s_ascending;
33 static void reset_pattern(unsigned char value)
34 { s_pattern = value; }
36 static void set_ascending(bool enable)
37 { s_ascending = enable; }
41 unsigned char default_init_allocator_base<Dummy>::s_pattern = 0u;
44 bool default_init_allocator_base<Dummy>::s_ascending = true;
46 template<class Integral>
47 class default_init_allocator
48 : public default_init_allocator_base<0>
50 typedef default_init_allocator_base<0> base_t;
52 typedef Integral value_type;
54 default_init_allocator()
58 default_init_allocator(default_init_allocator<U>)
61 Integral* allocate(std::size_t n)
63 const std::size_t max_count = std::size_t(-1)/(2*sizeof(Integral));
64 if(BOOST_UNLIKELY(n > max_count))
67 //Initialize memory to a pattern
68 const std::size_t max = sizeof(Integral)*n;
69 unsigned char *puc_raw = ::new unsigned char[max];
71 if(base_t::s_ascending){
72 for(std::size_t i = 0; i != max; ++i){
73 puc_raw[i] = static_cast<unsigned char>(s_pattern++);
77 for(std::size_t i = 0; i != max; ++i){
78 puc_raw[i] = static_cast<unsigned char>(s_pattern--);
81 return move_detail::force_ptr<Integral*>(puc_raw);
84 void deallocate(Integral *p, std::size_t)
85 { delete[] (unsigned char*)p; }
88 template<class Integral>
89 inline bool check_ascending_byte_pattern(const Integral&t)
91 const unsigned char *pch = &reinterpret_cast<const unsigned char &>(t);
92 const std::size_t max = sizeof(Integral);
93 for(std::size_t i = 1; i != max; ++i){
94 if( (pch[i-1] != ((unsigned char)(pch[i]-1u))) ){
101 template<class Integral>
102 inline bool check_descending_byte_pattern(const Integral&t)
104 const unsigned char *pch = &reinterpret_cast<const unsigned char &>(t);
105 const std::size_t max = sizeof(Integral);
106 for(std::size_t i = 1; i != max; ++i){
107 if( (pch[i-1] != ((unsigned char)(pch[i]+1u))) ){
114 template<class IntDefaultInitAllocVector>
115 bool default_init_test()//Test for default initialization
117 const std::size_t Capacity = 100;
120 test::default_init_allocator<int>::reset_pattern(0);
121 test::default_init_allocator<int>::set_ascending(true);
122 IntDefaultInitAllocVector v(Capacity, default_init);
123 typename IntDefaultInitAllocVector::iterator it = v.begin();
124 //Compare with the pattern
125 for(std::size_t i = 0; i != Capacity; ++i, ++it){
126 if(!test::check_ascending_byte_pattern(*it))
131 test::default_init_allocator<int>::reset_pattern(0);
132 test::default_init_allocator<int>::set_ascending(true);
133 IntDefaultInitAllocVector v(Capacity, default_init, typename IntDefaultInitAllocVector::allocator_type());
134 typename IntDefaultInitAllocVector::iterator it = v.begin();
135 //Compare with the pattern
136 for(std::size_t i = 0; i != Capacity; ++i, ++it){
137 if(!test::check_ascending_byte_pattern(*it))
142 test::default_init_allocator<int>::reset_pattern(100);
143 test::default_init_allocator<int>::set_ascending(false);
144 IntDefaultInitAllocVector v;
145 v.resize(Capacity, default_init);
146 typename IntDefaultInitAllocVector::iterator it = v.begin();
147 //Compare with the pattern
148 for(std::size_t i = 0; i != Capacity; ++i, ++it){
149 if(!test::check_descending_byte_pattern(*it))
157 } //namespace container {
160 #include <boost/container/detail/config_end.hpp>
162 #endif //BOOST_CONTAINER_TEST_DEFAULT_INIT_TEST_HEADER