Brian Silverman | fad8f55 | 2018-08-04 23:36:19 -0700 | [diff] [blame^] | 1 | ////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // (C) Copyright Ion Gaztanaga 2004-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 | #define STABLE_VECTOR_ENABLE_INVARIANT_CHECKING |
| 11 | #include <boost/container/detail/config_begin.hpp> |
| 12 | #include <memory> |
| 13 | |
| 14 | #include <boost/container/stable_vector.hpp> |
| 15 | #include <boost/container/node_allocator.hpp> |
| 16 | |
| 17 | #include "check_equal_containers.hpp" |
| 18 | #include "movable_int.hpp" |
| 19 | #include "expand_bwd_test_allocator.hpp" |
| 20 | #include "expand_bwd_test_template.hpp" |
| 21 | #include "dummy_test_allocator.hpp" |
| 22 | #include "propagate_allocator_test.hpp" |
| 23 | #include "vector_test.hpp" |
| 24 | #include "default_init_test.hpp" |
| 25 | #include "../../intrusive/test/iterator_test.hpp" |
| 26 | |
| 27 | using namespace boost::container; |
| 28 | |
| 29 | namespace boost { |
| 30 | namespace container { |
| 31 | |
| 32 | //Explicit instantiation to detect compilation errors |
| 33 | template class stable_vector<test::movable_and_copyable_int, |
| 34 | test::simple_allocator<test::movable_and_copyable_int> >; |
| 35 | |
| 36 | template class stable_vector |
| 37 | < test::movable_and_copyable_int |
| 38 | , node_allocator<test::movable_and_copyable_int> >; |
| 39 | |
| 40 | template class stable_vector_iterator<int*, false>; |
| 41 | template class stable_vector_iterator<int*, true >; |
| 42 | |
| 43 | }} |
| 44 | |
| 45 | class recursive_vector |
| 46 | { |
| 47 | public: |
| 48 | int id_; |
| 49 | stable_vector<recursive_vector> vector_; |
| 50 | stable_vector<recursive_vector>::iterator it_; |
| 51 | stable_vector<recursive_vector>::const_iterator cit_; |
| 52 | stable_vector<recursive_vector>::reverse_iterator rit_; |
| 53 | stable_vector<recursive_vector>::const_reverse_iterator crit_; |
| 54 | |
| 55 | recursive_vector &operator=(const recursive_vector &o) |
| 56 | { vector_ = o.vector_; return *this; } |
| 57 | }; |
| 58 | |
| 59 | void recursive_vector_test()//Test for recursive types |
| 60 | { |
| 61 | stable_vector<recursive_vector> recursive, copy; |
| 62 | //Test to test both move emulations |
| 63 | if(!copy.size()){ |
| 64 | copy = recursive; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | template<class VoidAllocator> |
| 69 | struct GetAllocatorCont |
| 70 | { |
| 71 | template<class ValueType> |
| 72 | struct apply |
| 73 | { |
| 74 | typedef stable_vector< ValueType |
| 75 | , typename allocator_traits<VoidAllocator> |
| 76 | ::template portable_rebind_alloc<ValueType>::type |
| 77 | > type; |
| 78 | }; |
| 79 | }; |
| 80 | |
| 81 | template<class VoidAllocator> |
| 82 | int test_cont_variants() |
| 83 | { |
| 84 | typedef typename GetAllocatorCont<VoidAllocator>::template apply<int>::type MyCont; |
| 85 | typedef typename GetAllocatorCont<VoidAllocator>::template apply<test::movable_int>::type MyMoveCont; |
| 86 | typedef typename GetAllocatorCont<VoidAllocator>::template apply<test::movable_and_copyable_int>::type MyCopyMoveCont; |
| 87 | typedef typename GetAllocatorCont<VoidAllocator>::template apply<test::copyable_int>::type MyCopyCont; |
| 88 | |
| 89 | if(test::vector_test<MyCont>()) |
| 90 | return 1; |
| 91 | if(test::vector_test<MyMoveCont>()) |
| 92 | return 1; |
| 93 | if(test::vector_test<MyCopyMoveCont>()) |
| 94 | return 1; |
| 95 | if(test::vector_test<MyCopyCont>()) |
| 96 | return 1; |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | struct boost_container_stable_vector; |
| 102 | |
| 103 | namespace boost { namespace container { namespace test { |
| 104 | |
| 105 | template<> |
| 106 | struct alloc_propagate_base<boost_container_stable_vector> |
| 107 | { |
| 108 | template <class T, class Allocator> |
| 109 | struct apply |
| 110 | { |
| 111 | typedef boost::container::stable_vector<T, Allocator> type; |
| 112 | }; |
| 113 | }; |
| 114 | |
| 115 | }}} //namespace boost::container::test |
| 116 | |
| 117 | |
| 118 | int main() |
| 119 | { |
| 120 | recursive_vector_test(); |
| 121 | { |
| 122 | //Now test move semantics |
| 123 | stable_vector<recursive_vector> original; |
| 124 | stable_vector<recursive_vector> move_ctor(boost::move(original)); |
| 125 | stable_vector<recursive_vector> move_assign; |
| 126 | move_assign = boost::move(move_ctor); |
| 127 | move_assign.swap(original); |
| 128 | } |
| 129 | |
| 130 | //Test non-copy-move operations |
| 131 | { |
| 132 | stable_vector<test::non_copymovable_int> sv; |
| 133 | sv.emplace_back(); |
| 134 | sv.resize(10); |
| 135 | sv.resize(1); |
| 136 | } |
| 137 | |
| 138 | //////////////////////////////////// |
| 139 | // Testing allocator implementations |
| 140 | //////////////////////////////////// |
| 141 | // std:allocator |
| 142 | if(test_cont_variants< std::allocator<void> >()){ |
| 143 | std::cerr << "test_cont_variants< std::allocator<void> > failed" << std::endl; |
| 144 | return 1; |
| 145 | } |
| 146 | // boost::container::node_allocator |
| 147 | if(test_cont_variants< node_allocator<void> >()){ |
| 148 | std::cerr << "test_cont_variants< node_allocator<void> > failed" << std::endl; |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | //////////////////////////////////// |
| 153 | // Default init test |
| 154 | //////////////////////////////////// |
| 155 | if(!test::default_init_test< stable_vector<int, test::default_init_allocator<int> > >()){ |
| 156 | std::cerr << "Default init test failed" << std::endl; |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | //////////////////////////////////// |
| 161 | // Emplace testing |
| 162 | //////////////////////////////////// |
| 163 | const test::EmplaceOptions Options = (test::EmplaceOptions)(test::EMPLACE_BACK | test::EMPLACE_BEFORE); |
| 164 | if(!boost::container::test::test_emplace |
| 165 | < stable_vector<test::EmplaceInt>, Options>()) |
| 166 | return 1; |
| 167 | |
| 168 | //////////////////////////////////// |
| 169 | // Allocator propagation testing |
| 170 | //////////////////////////////////// |
| 171 | if(!boost::container::test::test_propagate_allocator<boost_container_stable_vector>()) |
| 172 | return 1; |
| 173 | |
| 174 | //////////////////////////////////// |
| 175 | // Initializer lists testing |
| 176 | //////////////////////////////////// |
| 177 | if(!boost::container::test::test_vector_methods_with_initializer_list_as_argument_for |
| 178 | < boost::container::stable_vector<int> >()) |
| 179 | { |
| 180 | std::cerr << "test_methods_with_initializer_list_as_argument failed" << std::endl; |
| 181 | return 1; |
| 182 | } |
| 183 | |
| 184 | //////////////////////////////////// |
| 185 | // Iterator testing |
| 186 | //////////////////////////////////// |
| 187 | { |
| 188 | typedef boost::container::stable_vector<int> cont_int; |
| 189 | cont_int a; a.push_back(0); a.push_back(1); a.push_back(2); |
| 190 | boost::intrusive::test::test_iterator_random< cont_int >(a); |
| 191 | if(boost::report_errors() != 0) { |
| 192 | return 1; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | #if __cplusplus >= 201703L |
| 197 | //////////////////////////////////// |
| 198 | // Constructor Template Auto Deduction testing |
| 199 | //////////////////////////////////// |
| 200 | { |
| 201 | auto gold = std::vector{ 1, 2, 3 }; |
| 202 | auto test = boost::container::stable_vector(gold.begin(), gold.end()); |
| 203 | if (test.size() != 3) { |
| 204 | return 1; |
| 205 | } |
| 206 | if (!(test[0] == 1 && test[1] == 2 && test[2] == 3)) { |
| 207 | return 1; |
| 208 | } |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | #include <boost/container/detail/config_end.hpp> |