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 #include <boost/move/utility_core.hpp>
23 #include <boost/container/detail/mpl.hpp>
24 #include "print_container.hpp"
25 #include "check_equal_containers.hpp"
26 #include "movable_int.hpp"
29 #include "emplace_test.hpp"
30 #include "input_from_forward_iterator.hpp"
31 #include <boost/move/utility_core.hpp>
32 #include <boost/move/iterator.hpp>
33 #include <boost/core/no_exceptions_support.hpp>
34 #include <boost/static_assert.hpp>
35 #include "insert_test.hpp"
42 template<int Dummy = 0>
43 class default_init_allocator_base
46 static unsigned char s_pattern;
47 static bool s_ascending;
50 static void reset_pattern(unsigned char value)
51 { s_pattern = value; }
53 static void set_ascending(bool enable)
54 { s_ascending = enable; }
58 unsigned char default_init_allocator_base<Dummy>::s_pattern = 0u;
61 bool default_init_allocator_base<Dummy>::s_ascending = true;
63 template<class Integral>
64 class default_init_allocator
65 : public default_init_allocator_base<0>
67 typedef default_init_allocator_base<0> base_t;
69 typedef Integral value_type;
71 default_init_allocator()
75 default_init_allocator(default_init_allocator<U>)
78 Integral* allocate(std::size_t n)
80 //Initialize memory to a pattern
81 const std::size_t max = sizeof(Integral)*n;
82 unsigned char *puc_raw = ::new unsigned char[max];
84 if(base_t::s_ascending){
85 for(std::size_t i = 0; i != max; ++i){
86 puc_raw[i] = static_cast<unsigned char>(s_pattern++);
90 for(std::size_t i = 0; i != max; ++i){
91 puc_raw[i] = static_cast<unsigned char>(s_pattern--);
94 return (Integral*)puc_raw;;
97 void deallocate(Integral *p, std::size_t)
98 { delete[] (unsigned char*)p; }
101 template<class Integral>
102 inline bool check_ascending_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 Integral>
115 inline bool check_descending_byte_pattern(const Integral&t)
117 const unsigned char *pch = &reinterpret_cast<const unsigned char &>(t);
118 const std::size_t max = sizeof(Integral);
119 for(std::size_t i = 1; i != max; ++i){
120 if( (pch[i-1] != ((unsigned char)(pch[i]+1u))) ){
127 template<class IntDefaultInitAllocVector>
128 bool default_init_test()//Test for default initialization
130 const std::size_t Capacity = 100;
133 test::default_init_allocator<int>::reset_pattern(0);
134 test::default_init_allocator<int>::set_ascending(true);
135 IntDefaultInitAllocVector v(Capacity, default_init);
136 typename IntDefaultInitAllocVector::iterator it = v.begin();
137 //Compare with the pattern
138 for(std::size_t i = 0; i != Capacity; ++i, ++it){
139 if(!test::check_ascending_byte_pattern(*it))
144 test::default_init_allocator<int>::reset_pattern(100);
145 test::default_init_allocator<int>::set_ascending(false);
146 IntDefaultInitAllocVector v;
147 v.resize(Capacity, default_init);
148 typename IntDefaultInitAllocVector::iterator it = v.begin();
149 //Compare with the pattern
150 for(std::size_t i = 0; i != Capacity; ++i, ++it){
151 if(!test::check_descending_byte_pattern(*it))
159 } //namespace container {
162 #include <boost/container/detail/config_end.hpp>
164 #endif //BOOST_CONTAINER_TEST_DEFAULT_INIT_TEST_HEADER