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>
22 template<int Dummy = 0>
23 class default_init_allocator_base
26 static unsigned char s_pattern;
27 static bool s_ascending;
30 static void reset_pattern(unsigned char value)
31 { s_pattern = value; }
33 static void set_ascending(bool enable)
34 { s_ascending = enable; }
38 unsigned char default_init_allocator_base<Dummy>::s_pattern = 0u;
41 bool default_init_allocator_base<Dummy>::s_ascending = true;
43 template<class Integral>
44 class default_init_allocator
45 : public default_init_allocator_base<0>
47 typedef default_init_allocator_base<0> base_t;
49 typedef Integral value_type;
51 default_init_allocator()
55 default_init_allocator(default_init_allocator<U>)
58 Integral* allocate(std::size_t n)
60 //Initialize memory to a pattern
61 const std::size_t max = sizeof(Integral)*n;
62 unsigned char *puc_raw = ::new unsigned char[max];
64 if(base_t::s_ascending){
65 for(std::size_t i = 0; i != max; ++i){
66 puc_raw[i] = static_cast<unsigned char>(s_pattern++);
70 for(std::size_t i = 0; i != max; ++i){
71 puc_raw[i] = static_cast<unsigned char>(s_pattern--);
74 return (Integral*)puc_raw;;
77 void deallocate(Integral *p, std::size_t)
78 { delete[] (unsigned char*)p; }
81 template<class Integral>
82 inline bool check_ascending_byte_pattern(const Integral&t)
84 const unsigned char *pch = &reinterpret_cast<const unsigned char &>(t);
85 const std::size_t max = sizeof(Integral);
86 for(std::size_t i = 1; i != max; ++i){
87 if( (pch[i-1] != ((unsigned char)(pch[i]-1u))) ){
94 template<class Integral>
95 inline bool check_descending_byte_pattern(const Integral&t)
97 const unsigned char *pch = &reinterpret_cast<const unsigned char &>(t);
98 const std::size_t max = sizeof(Integral);
99 for(std::size_t i = 1; i != max; ++i){
100 if( (pch[i-1] != ((unsigned char)(pch[i]+1u))) ){
107 template<class IntDefaultInitAllocVector>
108 bool default_init_test()//Test for default initialization
110 const std::size_t Capacity = 100;
113 test::default_init_allocator<int>::reset_pattern(0);
114 test::default_init_allocator<int>::set_ascending(true);
115 IntDefaultInitAllocVector v(Capacity, default_init);
116 typename IntDefaultInitAllocVector::iterator it = v.begin();
117 //Compare with the pattern
118 for(std::size_t i = 0; i != Capacity; ++i, ++it){
119 if(!test::check_ascending_byte_pattern(*it))
124 test::default_init_allocator<int>::reset_pattern(0);
125 test::default_init_allocator<int>::set_ascending(true);
126 IntDefaultInitAllocVector v(Capacity, default_init, typename IntDefaultInitAllocVector::allocator_type());
127 typename IntDefaultInitAllocVector::iterator it = v.begin();
128 //Compare with the pattern
129 for(std::size_t i = 0; i != Capacity; ++i, ++it){
130 if(!test::check_ascending_byte_pattern(*it))
135 test::default_init_allocator<int>::reset_pattern(100);
136 test::default_init_allocator<int>::set_ascending(false);
137 IntDefaultInitAllocVector v;
138 v.resize(Capacity, default_init);
139 typename IntDefaultInitAllocVector::iterator it = v.begin();
140 //Compare with the pattern
141 for(std::size_t i = 0; i != Capacity; ++i, ++it){
142 if(!test::check_descending_byte_pattern(*it))
150 } //namespace container {
153 #include <boost/container/detail/config_end.hpp>
155 #endif //BOOST_CONTAINER_TEST_DEFAULT_INIT_TEST_HEADER