| 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 <boost/move/detail/force_ptr.hpp> |
| 17 | #include <cstddef> |
| 18 | |
| 19 | |
| 20 | namespace boost{ |
| 21 | namespace container { |
| 22 | namespace test{ |
| 23 | |
| 24 | // |
| 25 | template<int Dummy = 0> |
| 26 | class default_init_allocator_base |
| 27 | { |
| 28 | protected: |
| 29 | static unsigned char s_pattern; |
| 30 | static bool s_ascending; |
| 31 | |
| 32 | public: |
| 33 | static void reset_pattern(unsigned char value) |
| 34 | { s_pattern = value; } |
| 35 | |
| 36 | static void set_ascending(bool enable) |
| 37 | { s_ascending = enable; } |
| 38 | }; |
| 39 | |
| 40 | template<int Dummy> |
| 41 | unsigned char default_init_allocator_base<Dummy>::s_pattern = 0u; |
| 42 | |
| 43 | template<int Dummy> |
| 44 | bool default_init_allocator_base<Dummy>::s_ascending = true; |
| 45 | |
| 46 | template<class Integral> |
| 47 | class default_init_allocator |
| 48 | : public default_init_allocator_base<0> |
| 49 | { |
| 50 | typedef default_init_allocator_base<0> base_t; |
| 51 | public: |
| 52 | typedef Integral value_type; |
| 53 | |
| 54 | default_init_allocator() |
| 55 | {} |
| 56 | |
| 57 | template <class U> |
| 58 | default_init_allocator(default_init_allocator<U>) |
| 59 | {} |
| 60 | |
| 61 | Integral* allocate(std::size_t n) |
| 62 | { |
| 63 | const std::size_t max_count = std::size_t(-1)/(2*sizeof(Integral)); |
| 64 | if(BOOST_UNLIKELY(n > max_count)) |
| 65 | throw_bad_alloc(); |
| 66 | |
| 67 | //Initialize memory to a pattern |
| 68 | const std::size_t max = sizeof(Integral)*n; |
| 69 | unsigned char *puc_raw = ::new unsigned char[max]; |
| 70 | |
| 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++); |
| 74 | } |
| 75 | } |
| 76 | else{ |
| 77 | for(std::size_t i = 0; i != max; ++i){ |
| 78 | puc_raw[i] = static_cast<unsigned char>(s_pattern--); |
| 79 | } |
| 80 | } |
| 81 | return move_detail::force_ptr<Integral*>(puc_raw); |
| 82 | } |
| 83 | |
| 84 | void deallocate(Integral *p, std::size_t) |
| 85 | { delete[] (unsigned char*)p; } |
| 86 | }; |
| 87 | |
| 88 | template<class Integral> |
| 89 | inline bool check_ascending_byte_pattern(const Integral&t) |
| 90 | { |
| 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))) ){ |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | template<class Integral> |
| 102 | inline bool check_descending_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 IntDefaultInitAllocVector> |
| 115 | bool default_init_test()//Test for default initialization |
| 116 | { |
| 117 | const std::size_t Capacity = 100; |
| 118 | |
| 119 | { |
| 120 | test::default_init_allocator<int>::reset_pattern(value: 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)) |
| 127 | return false; |
| 128 | } |
| 129 | } |
| 130 | { |
| 131 | test::default_init_allocator<int>::reset_pattern(value: 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)) |
| 138 | return false; |
| 139 | } |
| 140 | } |
| 141 | { |
| 142 | test::default_init_allocator<int>::reset_pattern(value: 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)) |
| 150 | return false; |
| 151 | } |
| 152 | } |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | } //namespace test{ |
| 157 | } //namespace container { |
| 158 | } //namespace boost{ |
| 159 | |
| 160 | #include <boost/container/detail/config_end.hpp> |
| 161 | |
| 162 | #endif //BOOST_CONTAINER_TEST_DEFAULT_INIT_TEST_HEADER |
| 163 |
