Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / container / test / default_init_test.hpp
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 <algorithm>
16 #include <memory>
17 #include <vector>
18 #include <iostream>
19 #include <functional>
20 #include <list>
21
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"
27 #include <string>
28 #include <vector>
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"
36
37 namespace boost{
38 namespace container {
39 namespace test{
40
41 //
42 template<int Dummy = 0>
43 class default_init_allocator_base
44 {
45    protected:
46    static unsigned char s_pattern;
47    static bool          s_ascending;
48
49    public:
50    static void reset_pattern(unsigned char value)
51    {  s_pattern = value;   }
52
53    static void set_ascending(bool enable)
54    {  s_ascending = enable;   }
55 };
56
57 template<int Dummy>
58 unsigned char default_init_allocator_base<Dummy>::s_pattern = 0u;
59
60 template<int Dummy>
61 bool default_init_allocator_base<Dummy>::s_ascending = true;
62
63 template<class Integral>
64 class default_init_allocator
65    : public default_init_allocator_base<0>
66 {
67    typedef default_init_allocator_base<0> base_t;
68    public:
69    typedef Integral value_type;
70
71    default_init_allocator()
72    {}
73
74    template <class U>
75    default_init_allocator(default_init_allocator<U>)
76    {}
77
78    Integral* allocate(std::size_t n)
79    {
80       //Initialize memory to a pattern
81       const std::size_t max = sizeof(Integral)*n;
82       unsigned char *puc_raw = ::new unsigned char[max];
83
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++);
87          }
88       }
89       else{
90          for(std::size_t i = 0; i != max; ++i){
91             puc_raw[i] = static_cast<unsigned char>(s_pattern--);
92          }
93       }
94       return (Integral*)puc_raw;;
95    }
96
97    void deallocate(Integral *p, std::size_t)
98    {  delete[] (unsigned char*)p;  }
99 };
100
101 template<class Integral>
102 inline bool check_ascending_byte_pattern(const Integral&t)
103 {
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))) ){
108          return false;
109       }
110    }
111    return true;
112 }
113
114 template<class Integral>
115 inline bool check_descending_byte_pattern(const Integral&t)
116 {
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))) ){
121          return false;
122       }
123    }
124    return true;
125 }
126
127 template<class IntDefaultInitAllocVector>
128 bool default_init_test()//Test for default initialization
129 {
130    const std::size_t Capacity = 100;
131
132    {
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))
140             return false;
141       }
142    }
143    {
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))
152             return false;
153       }
154    }
155    return true;
156 }
157
158 }  //namespace test{
159 }  //namespace container {
160 }  //namespace boost{
161
162 #include <boost/container/detail/config_end.hpp>
163
164 #endif //BOOST_CONTAINER_TEST_DEFAULT_INIT_TEST_HEADER