|
| 1 | +////////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// (C) Copyright Ion Gaztanaga 2025-2025. 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 | +#include <boost/container/uses_allocator_construction.hpp> |
| 11 | +#include <boost/container/detail/type_traits.hpp> |
| 12 | +#include <boost/core/lightweight_test.hpp> |
| 13 | + |
| 14 | +typedef int arg1_t; |
| 15 | +typedef void * arg2_t; |
| 16 | +typedef void (*arg3_t) (int); |
| 17 | + |
| 18 | +typedef void *(*non_alloc_arg_t) (double*); |
| 19 | +typedef int alloc_arg_t; |
| 20 | + |
| 21 | +using namespace boost::container; |
| 22 | + |
| 23 | +struct not_uses_allocator |
| 24 | +{}; |
| 25 | + |
| 26 | +struct uses_allocator_and_not_convertible_arg |
| 27 | +{ |
| 28 | + bool allocator_called; |
| 29 | + unsigned args_called; |
| 30 | + typedef void (*allocator_type) (uses_allocator_and_not_convertible_arg); |
| 31 | + |
| 32 | + uses_allocator_and_not_convertible_arg() |
| 33 | + : allocator_called(false), args_called(0) |
| 34 | + {} |
| 35 | + |
| 36 | + explicit uses_allocator_and_not_convertible_arg(allocator_type) |
| 37 | + : allocator_called(true), args_called(0) |
| 38 | + {} |
| 39 | + |
| 40 | + explicit uses_allocator_and_not_convertible_arg(arg1_t, arg2_t, arg3_t, allocator_type) |
| 41 | + : allocator_called(true), args_called(3) |
| 42 | + {} |
| 43 | + |
| 44 | + explicit uses_allocator_and_not_convertible_arg(arg1_t, arg2_t, arg3_t) |
| 45 | + : allocator_called(false), args_called(3) |
| 46 | + {} |
| 47 | + |
| 48 | + explicit uses_allocator_and_not_convertible_arg(allocator_arg_t, allocator_type, arg1_t, arg2_t) |
| 49 | + : allocator_called(true), args_called(2) |
| 50 | + {} |
| 51 | + |
| 52 | + explicit uses_allocator_and_not_convertible_arg(arg1_t, arg2_t) |
| 53 | + : allocator_called(false), args_called(2) |
| 54 | + {} |
| 55 | +}; |
| 56 | + |
| 57 | +struct uses_allocator_and_convertible_arg |
| 58 | +{ |
| 59 | + bool allocator_called; |
| 60 | + unsigned args_called; |
| 61 | + typedef long allocator_type; |
| 62 | + |
| 63 | + uses_allocator_and_convertible_arg() |
| 64 | + : allocator_called(false), args_called(0) |
| 65 | + {} |
| 66 | + |
| 67 | + explicit uses_allocator_and_convertible_arg(allocator_type) |
| 68 | + : allocator_called(true), args_called(0) |
| 69 | + {} |
| 70 | + |
| 71 | + explicit uses_allocator_and_convertible_arg(arg1_t, arg2_t, arg3_t, allocator_type) |
| 72 | + : allocator_called(true), args_called(3) |
| 73 | + {} |
| 74 | + |
| 75 | + explicit uses_allocator_and_convertible_arg(arg1_t, arg2_t, arg3_t) |
| 76 | + : allocator_called(false), args_called(3) |
| 77 | + {} |
| 78 | + |
| 79 | + explicit uses_allocator_and_convertible_arg(allocator_arg_t, allocator_type, arg1_t, arg2_t) |
| 80 | + : allocator_called(true), args_called(2) |
| 81 | + {} |
| 82 | + |
| 83 | + explicit uses_allocator_and_convertible_arg(arg1_t, arg2_t) |
| 84 | + : allocator_called(false), args_called(2) |
| 85 | + {} |
| 86 | +}; |
| 87 | + |
| 88 | +struct uses_erased_type_allocator |
| 89 | +{ |
| 90 | + bool allocator_called; |
| 91 | + unsigned args_called; |
| 92 | + |
| 93 | + typedef boost::container::erased_type allocator_type; |
| 94 | + typedef long constructible_from_int_t; |
| 95 | + |
| 96 | + uses_erased_type_allocator() |
| 97 | + : allocator_called(false), args_called(0) |
| 98 | + {} |
| 99 | + |
| 100 | + explicit uses_erased_type_allocator(int) |
| 101 | + : allocator_called(true), args_called(0) |
| 102 | + {} |
| 103 | + |
| 104 | + explicit uses_erased_type_allocator(arg1_t, arg2_t, arg3_t, constructible_from_int_t) |
| 105 | + : allocator_called(true), args_called(3) |
| 106 | + {} |
| 107 | + |
| 108 | + explicit uses_erased_type_allocator(arg1_t, arg2_t, arg3_t) |
| 109 | + : allocator_called(false), args_called(3) |
| 110 | + {} |
| 111 | + |
| 112 | + explicit uses_erased_type_allocator(allocator_arg_t, constructible_from_int_t, arg1_t, arg2_t) |
| 113 | + : allocator_called(true), args_called(2) |
| 114 | + {} |
| 115 | + |
| 116 | + explicit uses_erased_type_allocator(arg1_t, arg2_t) |
| 117 | + : allocator_called(false), args_called(2) |
| 118 | + {} |
| 119 | +}; |
| 120 | + |
| 121 | +typedef boost::container::dtl::aligned_storage<sizeof(uses_allocator_and_not_convertible_arg)>::type storage_t; |
| 122 | + |
| 123 | +//Make sure aligned_storage will be big enough |
| 124 | +BOOST_CONTAINER_STATIC_ASSERT( sizeof(storage_t) >= sizeof(uses_allocator_and_not_convertible_arg) ); |
| 125 | +BOOST_CONTAINER_STATIC_ASSERT( sizeof(storage_t) >= sizeof(not_uses_allocator) ); |
| 126 | +BOOST_CONTAINER_STATIC_ASSERT( sizeof(storage_t) >= sizeof(uses_allocator_and_convertible_arg) ); |
| 127 | +BOOST_CONTAINER_STATIC_ASSERT( sizeof(storage_t) >= sizeof(uses_erased_type_allocator) ); |
| 128 | + |
| 129 | +int main() |
| 130 | +{ |
| 131 | + storage_t storage; |
| 132 | + void *const st_ptr = static_cast<void*>(storage.data); |
| 133 | + |
| 134 | + not_uses_allocator *nua_ptr = reinterpret_cast<not_uses_allocator*>(st_ptr); |
| 135 | + uses_allocator_and_not_convertible_arg *uanci_ptr = reinterpret_cast<uses_allocator_and_not_convertible_arg*>(st_ptr); |
| 136 | + uses_allocator_and_convertible_arg *uaci_ptr = reinterpret_cast<uses_allocator_and_convertible_arg*>(st_ptr); |
| 137 | + uses_erased_type_allocator *ueta_ptr = reinterpret_cast<uses_erased_type_allocator*>(st_ptr); |
| 138 | + |
| 139 | + //not_uses_allocator |
| 140 | + nua_ptr = uninitialized_construct_using_allocator(nua_ptr, alloc_arg_t()); |
| 141 | + |
| 142 | + //uses_allocator_and_convertible_arg |
| 143 | + uanci_ptr = uninitialized_construct_using_allocator(uanci_ptr, alloc_arg_t()); |
| 144 | + BOOST_TEST(uanci_ptr->allocator_called == false); |
| 145 | + BOOST_TEST(uanci_ptr->args_called == 0u); |
| 146 | + |
| 147 | + uanci_ptr = uninitialized_construct_using_allocator(uanci_ptr, alloc_arg_t()); |
| 148 | + BOOST_TEST(uanci_ptr->allocator_called == false); |
| 149 | + BOOST_TEST(uanci_ptr->args_called == 0u); |
| 150 | + |
| 151 | + uanci_ptr = uninitialized_construct_using_allocator(uanci_ptr, alloc_arg_t(), arg1_t(), arg2_t(), arg3_t()); |
| 152 | + BOOST_TEST(uanci_ptr->allocator_called == false); |
| 153 | + BOOST_TEST(uanci_ptr->args_called == 3u); |
| 154 | + |
| 155 | + uanci_ptr = uninitialized_construct_using_allocator(uanci_ptr, alloc_arg_t(), arg1_t(), arg2_t()); |
| 156 | + BOOST_TEST(uanci_ptr->allocator_called == false); |
| 157 | + BOOST_TEST(uanci_ptr->args_called == 2u); |
| 158 | + |
| 159 | + uanci_ptr = uninitialized_construct_using_allocator(uanci_ptr, non_alloc_arg_t(), arg1_t(), arg2_t(), arg3_t()); |
| 160 | + BOOST_TEST(uanci_ptr->allocator_called == false); |
| 161 | + BOOST_TEST(uanci_ptr->args_called == 3u); |
| 162 | + |
| 163 | + uanci_ptr = uninitialized_construct_using_allocator(uanci_ptr, non_alloc_arg_t(), arg1_t(), arg2_t()); |
| 164 | + BOOST_TEST(uanci_ptr->allocator_called == false); |
| 165 | + BOOST_TEST(uanci_ptr->args_called == 2u); |
| 166 | + |
| 167 | + //uses_allocator_and_not_convertible_arg |
| 168 | + uaci_ptr = uninitialized_construct_using_allocator(uaci_ptr, alloc_arg_t()); |
| 169 | + BOOST_TEST(uaci_ptr->allocator_called == true); |
| 170 | + BOOST_TEST(uaci_ptr->args_called == 0u); |
| 171 | + |
| 172 | + uaci_ptr = uninitialized_construct_using_allocator(uaci_ptr, alloc_arg_t(), arg1_t(), arg2_t(), arg3_t()); |
| 173 | + BOOST_TEST(uaci_ptr->allocator_called == true); |
| 174 | + BOOST_TEST(uaci_ptr->args_called == 3u); |
| 175 | + |
| 176 | + uaci_ptr = uninitialized_construct_using_allocator(uaci_ptr, alloc_arg_t(), arg1_t(), arg2_t()); |
| 177 | + BOOST_TEST(uaci_ptr->allocator_called == true); |
| 178 | + BOOST_TEST(uaci_ptr->args_called == 2u); |
| 179 | + |
| 180 | + //uses_erased_type_allocator |
| 181 | + ueta_ptr = uninitialized_construct_using_allocator(ueta_ptr, alloc_arg_t()); |
| 182 | + BOOST_TEST(ueta_ptr->allocator_called == true); |
| 183 | + BOOST_TEST(ueta_ptr->args_called == 0u); |
| 184 | + |
| 185 | + ueta_ptr = uninitialized_construct_using_allocator(ueta_ptr, alloc_arg_t(), arg1_t(), arg2_t(), arg3_t()); |
| 186 | + BOOST_TEST(ueta_ptr->allocator_called == true); |
| 187 | + BOOST_TEST(ueta_ptr->args_called == 3u); |
| 188 | + |
| 189 | + ueta_ptr = uninitialized_construct_using_allocator(ueta_ptr, alloc_arg_t(), arg1_t(), arg2_t()); |
| 190 | + BOOST_TEST(ueta_ptr->allocator_called == true); |
| 191 | + BOOST_TEST(ueta_ptr->args_called == 2u); |
| 192 | + |
| 193 | + ueta_ptr = uninitialized_construct_using_allocator(ueta_ptr, non_alloc_arg_t(), arg1_t(), arg2_t()); |
| 194 | + BOOST_TEST(ueta_ptr->allocator_called == false); |
| 195 | + BOOST_TEST(ueta_ptr->args_called == 2u); |
| 196 | + |
| 197 | + ueta_ptr = uninitialized_construct_using_allocator(ueta_ptr, non_alloc_arg_t(), arg1_t(), arg2_t(), arg3_t()); |
| 198 | + BOOST_TEST(ueta_ptr->allocator_called == false); |
| 199 | + BOOST_TEST(ueta_ptr->args_called == 3u); |
| 200 | + |
| 201 | + return boost::report_errors(); |
| 202 | +} |
0 commit comments