root/examples/unique_ptr.cpp

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. BOOST_AUTO_TEST_SUITE
  2. BOOST_AUTO_TEST_CASE_TEMPLATE
  3. BOOST_AUTO_TEST_CASE_TEMPLATE
  4. BOOST_AUTO_TEST_CASE_TEMPLATE
  5. BOOST_AUTO_TEST_CASE_TEMPLATE
  6. BOOST_AUTO_TEST_CASE_TEMPLATE
  7. BOOST_AUTO_TEST_CASE_TEMPLATE
  8. BOOST_AUTO_TEST_CASE_TEMPLATE
  9. BOOST_AUTO_TEST_CASE_TEMPLATE
  10. BOOST_AUTO_TEST_CASE_TEMPLATE
  11. BOOST_AUTO_TEST_CASE_TEMPLATE
  12. BOOST_AUTO_TEST_CASE_TEMPLATE
  13. BOOST_AUTO_TEST_CASE_TEMPLATE
  14. BOOST_AUTO_TEST_CASE_TEMPLATE
  15. BOOST_AUTO_TEST_CASE_TEMPLATE
  16. BOOST_AUTO_TEST_CASE_TEMPLATE
  17. BOOST_AUTO_TEST_CASE_TEMPLATE
  18. BOOST_AUTO_TEST_CASE_TEMPLATE
  19. BOOST_AUTO_TEST_CASE_TEMPLATE
  20. BOOST_AUTO_TEST_CASE_TEMPLATE

   1 /******************************************************************************
   2 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/examples/unique_ptr.cpp 2363 2018-10-24 07:02:38Z jmmcg $
   3 **
   4 ** Copyright (c) 2015 by J.M.McGuiness, coder@hussar.me.uk
   5 **
   6 ** This library is free software; you can redistribute it and/or
   7 ** modify it under the terms of the GNU Lesser General Public
   8 ** License as published by the Free Software Foundation; either
   9 ** version 2.1 of the License, or (at your option) any later version.
  10 **
  11 ** This library is distributed in the hope that it will be useful,
  12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14 ** Lesser General Public License for more details.
  15 **
  16 ** You should have received a copy of the GNU Lesser General Public
  17 ** License along with this library; if not, write to the Free Software
  18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 */
  20 
  21 #include "stdafx.h"
  22 
  23 #define BOOST_TEST_MODULE libjmmcg_tests
  24 #include <boost/test/included/unit_test.hpp>
  25 
  26 #include <boost/test/test_case_template.hpp>
  27 #include <boost/mpl/list.hpp>
  28 
  29 #include "core/unique_ptr.hpp"
  30 #include "core/deleter.hpp"
  31 #include "core/thread_api_traits.hpp"
  32 
  33 using namespace jmmcg;
  34 using namespace ppd;
  35 
  36 typedef boost::mpl::list<
  37         sequential_mode,
  38         heavyweight_threading
  39 > thread_types;
  40 
  41 template<class Mdl>
  42 struct base {
  43         using lock_traits=api_lock_traits<platform_api, Mdl>;
  44         using deleter_t=jmmcg::default_delete<base>;
  45 
  46         ~base() noexcept(true) {}
  47 
  48         virtual void deleter() final {
  49                 deleter_t().operator()(this);
  50         }
  51 
  52         bool __fastcall operator<(base const &a) const noexcept(true) {
  53                 return this<&a;
  54         }
  55 };
  56 template<class Mdl>
  57 struct derived final : public base<Mdl> {
  58         using base_t=base<Mdl>;
  59         using lock_traits=typename base_t::lock_traits;
  60         using deleter_t=typename base_t::deleter_t;
  61 
  62         ~derived() noexcept(true) {}
  63 };
  64 template<class Mdl>
  65 struct placement_dtor_test_t {
  66         using lock_traits=api_lock_traits<platform_api, Mdl>;
  67         using deleter_t=placement_dtor<placement_dtor_test_t>;
  68 
  69         const int val;
  70 
  71         placement_dtor_test_t() noexcept(true)
  72         : val(42) {}
  73         ~placement_dtor_test_t() noexcept(true) {}
  74 
  75         void deleter() {
  76                 deleter_t().operator()(this);
  77         }
  78 
  79         bool __fastcall operator<(placement_dtor_test_t const &a) const noexcept(true) {
  80                 return this<&a;
  81         }
  82 };
  83 
  84 template<class Mdl>
  85 struct base_inh_t {
  86         using lock_traits=api_lock_traits<platform_api, Mdl>;
  87         using deleter_t=jmmcg::default_delete<base_inh_t>;
  88 
  89         virtual ~base_inh_t() noexcept(true) {
  90         }
  91         virtual int val() const noexcept(true)=0;
  92 
  93         virtual void deleter() {
  94                 deleter_t().operator()(this);
  95         }
  96 };
  97 template<class Mdl, template<class> class Del>
  98 struct derived_inh_t final : public base_inh_t<Mdl> {
  99         using base_t=base_inh_t<Mdl>;
 100         using lock_traits=typename base_t::lock_traits;
 101         using deleter_t=Del<derived_inh_t>;
 102 
 103         const int val_;
 104 
 105         explicit derived_inh_t(int i) noexcept(true)
 106         : val_(i) {}
 107         ~derived_inh_t() noexcept(true) {}
 108 
 109         int val() const noexcept(true) override {
 110                 return val_;
 111         }
 112 
 113         void deleter() override {
 114                 deleter_t().operator()(this);
 115         }
 116 
 117         bool __fastcall operator<(derived_inh_t const &a) const noexcept(true) {
 118                 return this<&a;
 119         }
 120 };
 121 
 122 template<class Mdl>
 123 struct stack_test_t {
 124         using lock_traits=api_lock_traits<platform_api, Mdl>;
 125         using deleter_t=jmmcg::noop_dtor<stack_test_t>;
 126 
 127         const int val;
 128 
 129         stack_test_t() noexcept(true)
 130         : val(42) {}
 131         ~stack_test_t() noexcept(true) {}
 132 
 133         void deleter() {
 134                 deleter_t().operator()(this);
 135         }
 136 };
 137 
 138 BOOST_AUTO_TEST_SUITE(unique_ptr_tests)
 139 
 140 BOOST_AUTO_TEST_CASE_TEMPLATE(default_ctor_unique_ptr, Mdl, thread_types)
 141 {
 142         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 143 
 144         ptr_t a;
 145         BOOST_CHECK_EQUAL(a.get(), static_cast<typename ptr_t::value_type *>(0));
 146         BOOST_CHECK(!a);
 147 }
 148 
 149 BOOST_AUTO_TEST_CASE_TEMPLATE(ctor_unique_ptr, Mdl, thread_types)
 150 {
 151         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 152 
 153         typename ptr_t::value_type *ptr_a=new typename ptr_t::value_type;
 154         ptr_t a(ptr_a);
 155         BOOST_CHECK_EQUAL(a.get(), ptr_a);
 156         BOOST_CHECK(!(!a));
 157 }
 158 
 159 BOOST_AUTO_TEST_CASE_TEMPLATE(placement_new_ctor_unique_ptr, Mdl, thread_types)
 160 {
 161         typedef unique_ptr<placement_dtor_test_t<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 162 
 163         char buff[sizeof(typename ptr_t::value_type)];
 164         typename ptr_t::value_type *ptr_a=new (buff) typename ptr_t::value_type;
 165         ptr_t a(ptr_a);
 166         BOOST_CHECK_EQUAL(a.get(), ptr_a);
 167         BOOST_CHECK(!(!a));
 168         BOOST_CHECK_EQUAL(a->val, 42);
 169 }
 170 
 171 BOOST_AUTO_TEST_CASE_TEMPLATE(mixed_dtors_placement_new_ctor_unique_ptr, Mdl, thread_types)
 172 {
 173         typedef derived_inh_t<Mdl, jmmcg::default_delete> def_del_t;
 174         typedef derived_inh_t<Mdl, placement_dtor> plment_del_t;
 175         typedef unique_ptr<base_inh_t<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 176 
 177         char buff[sizeof(plment_del_t)];
 178         def_del_t *ptr_def_del=new def_del_t(42);
 179         plment_del_t *ptr_plment_del=new (buff) plment_del_t(1066);
 180         ptr_t sp_def_del(ptr_def_del);
 181         ptr_t sp_plment_del(ptr_plment_del);
 182         BOOST_CHECK_EQUAL(sp_def_del.get(), ptr_def_del);
 183         BOOST_CHECK(!(!sp_def_del));
 184         BOOST_CHECK_EQUAL(sp_def_del->val(), 42);
 185         BOOST_CHECK_EQUAL(sp_plment_del.get(), ptr_plment_del);
 186         BOOST_CHECK(!(!sp_plment_del));
 187         BOOST_CHECK_EQUAL(sp_plment_del->val(), 1066);
 188 }
 189 
 190 BOOST_AUTO_TEST_CASE_TEMPLATE(move_ctor_unique_ptr, Mdl, thread_types)
 191 {
 192         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 193 
 194         typename ptr_t::value_type *ptr_a=new typename ptr_t::value_type;
 195         ptr_t a(ptr_a);
 196         ptr_t a1(std::move(a));
 197         BOOST_CHECK_EQUAL(a1.get(), ptr_a);
 198         BOOST_CHECK_EQUAL(a.get(), typename ptr_t::atomic_ptr_t());
 199 }
 200 
 201 BOOST_AUTO_TEST_CASE_TEMPLATE(unique_ptr_ctor_unique_ptr, Mdl, thread_types)
 202 {
 203         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 204 
 205         std::unique_ptr<typename ptr_t::value_type, typename ptr_t::value_type::deleter_t> ptr_a(new typename ptr_t::value_type);
 206         ptr_t a(std::move(ptr_a));
 207         BOOST_CHECK(a.get());
 208         BOOST_CHECK_EQUAL(ptr_a.get(), static_cast<typename ptr_t::value_type *>(0));
 209 }
 210 
 211 BOOST_AUTO_TEST_CASE_TEMPLATE(unique_ptr_move_ctor_unique_ptr, Mdl, thread_types)
 212 {
 213         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 214 
 215         std::unique_ptr<typename ptr_t::value_type, typename ptr_t::value_type::deleter_t> ptr_a(new typename ptr_t::value_type);
 216         ptr_t a(std::move(ptr_a));
 217         BOOST_CHECK(dynamic_cast<typename ptr_t::value_type const *>(a.get().get()));
 218 }
 219 
 220 BOOST_AUTO_TEST_CASE_TEMPLATE(reset_unique_ptr, Mdl, thread_types)
 221 {
 222         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 223 
 224         typename ptr_t::value_type *ptr_a=new typename ptr_t::value_type;
 225         ptr_t a(ptr_a);
 226         a.reset();
 227         BOOST_CHECK_EQUAL(a.get(), static_cast<typename ptr_t::value_type *>(0));
 228         BOOST_CHECK(!a);
 229 }
 230 
 231 BOOST_AUTO_TEST_CASE_TEMPLATE(assign_move_unique_ptr, Mdl, thread_types)
 232 {
 233         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 234 
 235         typename ptr_t::value_type *ptr_a=new typename ptr_t::value_type;
 236         ptr_t a(ptr_a);
 237         ptr_t a1;
 238         a1=std::move(a);
 239         BOOST_CHECK_EQUAL(a.get(), static_cast<typename ptr_t::value_type *>(0));
 240         BOOST_CHECK_EQUAL(a1.get(), ptr_a);
 241 }
 242 
 243 BOOST_AUTO_TEST_CASE_TEMPLATE(ctor_assign_unique_ptr, Mdl, thread_types)
 244 {
 245         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 246 
 247         ptr_t a;
 248         a=ptr_t(new typename ptr_t::value_type);
 249         BOOST_CHECK(a.get());
 250 }
 251 
 252 BOOST_AUTO_TEST_CASE_TEMPLATE(ctor_assign_move_unique_ptr, Mdl, thread_types)
 253 {
 254         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 255 
 256         std::unique_ptr<typename ptr_t::value_type, typename ptr_t::value_type::deleter_t> ptr_a(new typename ptr_t::value_type);
 257         ptr_t a;
 258         a=ptr_t(std::move(ptr_a));
 259         BOOST_CHECK(a.get());
 260         BOOST_CHECK_EQUAL(ptr_a.get(), static_cast<typename ptr_t::value_type *>(0));
 261 }
 262 
 263 BOOST_AUTO_TEST_CASE_TEMPLATE(equals_move, Mdl, thread_types)
 264 {
 265         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 266 
 267         typename ptr_t::value_type *ptr_a=new typename ptr_t::value_type;
 268         ptr_t a(ptr_a);
 269         ptr_t a1(std::move(a));
 270         BOOST_CHECK(a!=a1);
 271 }
 272 
 273 BOOST_AUTO_TEST_CASE_TEMPLATE(not_equals_unique_ptr, Mdl, thread_types)
 274 {
 275         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 276 
 277         typename ptr_t::value_type *ptr_a=new typename ptr_t::value_type;
 278         typename ptr_t::value_type *ptr_a1=new typename ptr_t::value_type;
 279         ptr_t a(ptr_a);
 280         ptr_t a1(ptr_a1);
 281         BOOST_CHECK(a!=a1);
 282         BOOST_CHECK(!(a==a1));
 283 }
 284 
 285 BOOST_AUTO_TEST_CASE_TEMPLATE(equals_comparators_unique_ptr, Mdl, thread_types)
 286 {
 287         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 288 
 289         typename ptr_t::value_type *ptr_a=new typename ptr_t::value_type;
 290         ptr_t a(ptr_a);
 291         ptr_t a1(std::move(a));
 292         BOOST_CHECK(!(a<a1) && !(a>a1));
 293 }
 294 
 295 BOOST_AUTO_TEST_CASE_TEMPLATE(not_equals_comparators_unique_ptr, Mdl, thread_types)
 296 {
 297         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 298 
 299         typename ptr_t::value_type *ptr_a=new typename ptr_t::value_type;
 300         typename ptr_t::value_type *ptr_a1=new typename ptr_t::value_type;
 301         ptr_t a(ptr_a);
 302         ptr_t a1(ptr_a1);
 303         BOOST_CHECK((a<a1) || (a>a1));
 304 }
 305 
 306 BOOST_AUTO_TEST_CASE_TEMPLATE(inheritance_wrapping, Mdl, thread_types)
 307 {
 308         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 309         typedef unique_ptr<derived<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_b_t;
 310 
 311         typename ptr_b_t::value_type *ptr_b=new typename ptr_b_t::value_type;
 312         ptr_t b(ptr_b);
 313         BOOST_CHECK(b.get()==ptr_b);
 314 }
 315 
 316 BOOST_AUTO_TEST_CASE_TEMPLATE(moves_the_ptr, Mdl, thread_types)
 317 {
 318         typedef unique_ptr<base<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 319 
 320         typename ptr_t::value_type *ptr_a=new typename ptr_t::value_type;
 321         ptr_t a(ptr_a);
 322         {
 323                 ptr_t a1(std::move(a));
 324                 BOOST_CHECK(a.get()!=a1.get());
 325                 BOOST_CHECK(a!=a1);
 326         }
 327         BOOST_CHECK_EQUAL(a.get(), static_cast<typename ptr_t::value_type *>(0));
 328 }
 329 
 330 BOOST_AUTO_TEST_CASE_TEMPLATE(default_ctor_stack_test, Mdl, thread_types)
 331 {
 332         typedef unique_ptr<stack_test_t<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 333 
 334         ptr_t a;
 335         BOOST_CHECK_EQUAL(a.get(), static_cast<typename ptr_t::value_type *>(0));
 336         BOOST_CHECK(!a);
 337 }
 338 
 339 BOOST_AUTO_TEST_CASE_TEMPLATE(ctor_stack_test, Mdl, thread_types)
 340 {
 341         typedef unique_ptr<stack_test_t<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 342 
 343         typename ptr_t::value_type val_a;
 344         ptr_t a(&val_a);
 345         BOOST_CHECK(a.get()==&val_a);
 346 }
 347 
 348 BOOST_AUTO_TEST_CASE_TEMPLATE(moves_stack_test, Mdl, thread_types)
 349 {
 350         typedef unique_ptr<stack_test_t<Mdl>, api_lock_traits<platform_api, Mdl>> ptr_t;
 351 
 352         typename ptr_t::value_type val_a;
 353         ptr_t a(&val_a);
 354         {
 355                 ptr_t a1(std::move(a));
 356                 BOOST_CHECK(a.get()!=a1.get());
 357                 BOOST_CHECK(a!=a1);
 358                 BOOST_CHECK_EQUAL(a1->val, 42);
 359         }
 360         BOOST_CHECK_EQUAL(a.get(), static_cast<typename ptr_t::value_type *>(0));
 361 }
 362 
 363 BOOST_AUTO_TEST_SUITE_END()

/* [<][>][^][v][top][bottom][index][help] */