1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
6 //
7 // See http://www.boost.org/libs/container for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef BOOST_CONTAINER_TEST_DEFAULT_INIT_TEST_HEADER
12 #define BOOST_CONTAINER_TEST_DEFAULT_INIT_TEST_HEADER
13 
14 #include <boost/container/detail/config_begin.hpp>
15 #include <boost/container/throw_exception.hpp>
16 #include <cstddef>
17 
18 
19 namespace boost{
20 namespace container {
21 namespace test{
22 
23 //
24 template<int Dummy = 0>
25 class default_init_allocator_base
26 {
27    protected:
28    static unsigned char s_pattern;
29    static bool          s_ascending;
30 
31    public:
reset_pattern(unsigned char value)32    static void reset_pattern(unsigned char value)
33    {  s_pattern = value;   }
34 
set_ascending(bool enable)35    static void set_ascending(bool enable)
36    {  s_ascending = enable;   }
37 };
38 
39 template<int Dummy>
40 unsigned char default_init_allocator_base<Dummy>::s_pattern = 0u;
41 
42 template<int Dummy>
43 bool default_init_allocator_base<Dummy>::s_ascending = true;
44 
45 template<class Integral>
46 class default_init_allocator
47    : public default_init_allocator_base<0>
48 {
49    typedef default_init_allocator_base<0> base_t;
50    public:
51    typedef Integral value_type;
52 
default_init_allocator()53    default_init_allocator()
54    {}
55 
56    template <class U>
default_init_allocator(default_init_allocator<U>)57    default_init_allocator(default_init_allocator<U>)
58    {}
59 
allocate(std::size_t n)60    Integral* allocate(std::size_t n)
61    {
62       const std::size_t max_count = std::size_t(-1)/(2*sizeof(Integral));
63       if(BOOST_UNLIKELY(n > max_count))
64          throw_bad_alloc();
65 
66       //Initialize memory to a pattern
67       const std::size_t max = sizeof(Integral)*n;
68       unsigned char *puc_raw = ::new unsigned char[max];
69 
70       if(base_t::s_ascending){
71          for(std::size_t i = 0; i != max; ++i){
72             puc_raw[i] = static_cast<unsigned char>(s_pattern++);
73          }
74       }
75       else{
76          for(std::size_t i = 0; i != max; ++i){
77             puc_raw[i] = static_cast<unsigned char>(s_pattern--);
78          }
79       }
80       return (Integral*)puc_raw;;
81    }
82 
deallocate(Integral * p,std::size_t)83    void deallocate(Integral *p, std::size_t)
84    {  delete[] (unsigned char*)p;  }
85 };
86 
87 template<class Integral>
check_ascending_byte_pattern(const Integral & t)88 inline bool check_ascending_byte_pattern(const Integral&t)
89 {
90    const unsigned char *pch = &reinterpret_cast<const unsigned char &>(t);
91    const std::size_t max = sizeof(Integral);
92    for(std::size_t i = 1; i != max; ++i){
93       if( (pch[i-1] != ((unsigned char)(pch[i]-1u))) ){
94          return false;
95       }
96    }
97    return true;
98 }
99 
100 template<class Integral>
check_descending_byte_pattern(const Integral & t)101 inline bool check_descending_byte_pattern(const Integral&t)
102 {
103    const unsigned char *pch = &reinterpret_cast<const unsigned char &>(t);
104    const std::size_t max = sizeof(Integral);
105    for(std::size_t i = 1; i != max; ++i){
106       if( (pch[i-1] != ((unsigned char)(pch[i]+1u))) ){
107          return false;
108       }
109    }
110    return true;
111 }
112 
113 template<class IntDefaultInitAllocVector>
default_init_test()114 bool default_init_test()//Test for default initialization
115 {
116    const std::size_t Capacity = 100;
117 
118    {
119       test::default_init_allocator<int>::reset_pattern(0);
120       test::default_init_allocator<int>::set_ascending(true);
121       IntDefaultInitAllocVector v(Capacity, default_init);
122       typename IntDefaultInitAllocVector::iterator it = v.begin();
123       //Compare with the pattern
124       for(std::size_t i = 0; i != Capacity; ++i, ++it){
125          if(!test::check_ascending_byte_pattern(*it))
126             return false;
127       }
128    }
129    {
130       test::default_init_allocator<int>::reset_pattern(0);
131       test::default_init_allocator<int>::set_ascending(true);
132       IntDefaultInitAllocVector v(Capacity, default_init, typename IntDefaultInitAllocVector::allocator_type());
133       typename IntDefaultInitAllocVector::iterator it = v.begin();
134       //Compare with the pattern
135       for(std::size_t i = 0; i != Capacity; ++i, ++it){
136          if(!test::check_ascending_byte_pattern(*it))
137             return false;
138       }
139    }
140    {
141       test::default_init_allocator<int>::reset_pattern(100);
142       test::default_init_allocator<int>::set_ascending(false);
143       IntDefaultInitAllocVector v;
144       v.resize(Capacity, default_init);
145       typename IntDefaultInitAllocVector::iterator it = v.begin();
146       //Compare with the pattern
147       for(std::size_t i = 0; i != Capacity; ++i, ++it){
148          if(!test::check_descending_byte_pattern(*it))
149             return false;
150       }
151    }
152    return true;
153 }
154 
155 }  //namespace test{
156 }  //namespace container {
157 }  //namespace boost{
158 
159 #include <boost/container/detail/config_end.hpp>
160 
161 #endif //BOOST_CONTAINER_TEST_DEFAULT_INIT_TEST_HEADER
162