emplace_test.hpp
来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 641 行 · 第 1/2 页
HPP
641 行
////////////////////////////////////////////////////////////////////////////////// (C) Copyright Ion Gaztanaga 2008. Distributed under the Boost// Software License, Version 1.0. (See accompanying file// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)//// See http://www.boost.org/libs/interprocess for documentation.////////////////////////////////////////////////////////////////////////////////#ifndef BOOST_INTERPROCESS_TEST_EMPLACE_TEST_HPP#define BOOST_INTERPROCESS_TEST_EMPLACE_TEST_HPP#include <iostream>#include <boost/interprocess/detail/config_begin.hpp>#include <boost/interprocess/detail/workaround.hpp>#include <boost/interprocess/detail/mpl.hpp>#include <boost/interprocess/detail/move.hpp>#include <boost/interprocess/detail/utilities.hpp>#include <boost/aligned_storage.hpp>namespace boost{namespace interprocess{namespace test{class EmplaceInt{ private: EmplaceInt (const EmplaceInt &o); EmplaceInt& operator=(const EmplaceInt &o); public: EmplaceInt(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0) : a_(a), b_(b), c_(c), d_(d), e_(e) {} #ifdef BOOST_INTERPROCESS_RVALUE_REFERENCE EmplaceInt(EmplaceInt &&o) : a_(o.a_), b_(o.b_), c_(o.c_), d_(o.d_), e_(o.e_) #else EmplaceInt(detail::moved_object<EmplaceInt> mo) : a_(mo.get().a_), b_(mo.get().b_), c_(mo.get().c_), d_(mo.get().d_), e_(mo.get().e_) #endif {} #ifdef BOOST_INTERPROCESS_RVALUE_REFERENCE EmplaceInt& operator=(EmplaceInt &&o) { #else EmplaceInt& operator=(detail::moved_object<EmplaceInt> mo) { EmplaceInt &o = mo.get(); #endif this->a_ = o.a_; this->b_ = o.b_; this->c_ = o.c_; this->d_ = o.d_; this->e_ = o.e_; return *this; } friend bool operator==(const EmplaceInt &l, const EmplaceInt &r) { return l.a_ == r.a_ && l.b_ == r.b_ && l.c_ == r.c_ && l.d_ == r.d_ && l.e_ == r.e_; } friend bool operator<(const EmplaceInt &l, const EmplaceInt &r) { return l.sum() < r.sum(); } friend bool operator>(const EmplaceInt &l, const EmplaceInt &r) { return l.sum() > r.sum(); } friend bool operator!=(const EmplaceInt &l, const EmplaceInt &r) { return !(l == r); } friend std::ostream &operator <<(std::ostream &os, const EmplaceInt &v) { os << "EmplaceInt: " << v.a_ << ' ' << v.b_ << ' ' << v.c_ << ' ' << v.d_ << ' ' << v.e_; return os; } //private: int sum() const { return this->a_ + this->b_ + this->c_ + this->d_ + this->e_; } int a_, b_, c_, d_, e_; int padding[6];};} //namespace test {template<>struct is_movable<test::EmplaceInt>{ static const bool value = true;};namespace test {enum EmplaceOptions{ EMPLACE_BACK = 1 << 0, EMPLACE_FRONT = 1 << 1, EMPLACE_BEFORE = 1 << 2, EMPLACE_AFTER = 1 << 3, EMPLACE_ASSOC = 1 << 4, EMPLACE_HINT = 1 << 5, EMPLACE_ASSOC_PAIR = 1 << 6, EMPLACE_HINT_PAIR = 1 << 7};template<class Container>bool test_expected_container(const Container &ec, const EmplaceInt *Expected, unsigned int only_first_n){ typedef typename Container::const_iterator const_iterator; const_iterator itb(ec.begin()), ite(ec.end()); unsigned int cur = 0; if(only_first_n > ec.size()){ return false; } for(; itb != ite && only_first_n--; ++itb, ++cur){ const EmplaceInt & cr = *itb; if(cr != Expected[cur]){ return false; } } return true;}template<class Container>bool test_expected_container(const Container &ec, const std::pair<EmplaceInt, EmplaceInt> *Expected, unsigned int only_first_n){ typedef typename Container::const_iterator const_iterator; const_iterator itb(ec.begin()), ite(ec.end()); unsigned int cur = 0; if(only_first_n > ec.size()){ return false; } for(; itb != ite && only_first_n--; ++itb, ++cur){ if(itb->first != Expected[cur].first){ std::cout << "Error in first: " << itb->first << ' ' << Expected[cur].first << std::endl; return false; } else if(itb->second != Expected[cur].second){ std::cout << "Error in second: " << itb->second << ' ' << Expected[cur].second << std::endl; return false; } } return true;}static EmplaceInt expected [10];typedef std::pair<EmplaceInt, EmplaceInt> EmplaceIntPair;static boost::aligned_storage<sizeof(EmplaceIntPair)*10>::type pair_storage;static EmplaceIntPair* initialize_emplace_int_pair(){ EmplaceIntPair* ret = reinterpret_cast<EmplaceIntPair*>(&pair_storage); for(unsigned int i = 0; i != 10; ++i){ new(&ret->first)EmplaceInt(); new(&ret->second)EmplaceInt(); } return ret;}static EmplaceIntPair * expected_pair = initialize_emplace_int_pair();template<class Container>bool test_emplace_back(detail::true_){ std::cout << "Starting test_emplace_back." << std::endl << " Class: " << typeid(Container).name() << std::endl; { new(&expected [0]) EmplaceInt(); new(&expected [1]) EmplaceInt(1); new(&expected [2]) EmplaceInt(1, 2); new(&expected [3]) EmplaceInt(1, 2, 3); new(&expected [4]) EmplaceInt(1, 2, 3, 4); new(&expected [5]) EmplaceInt(1, 2, 3, 4, 5); Container c; c.emplace_back(); if(!test_expected_container(c, &expected[0], 1)) return false; c.emplace_back(1); if(!test_expected_container(c, &expected[0], 2)) return false; c.emplace_back(1, 2); if(!test_expected_container(c, &expected[0], 3)) return false; c.emplace_back(1, 2, 3); if(!test_expected_container(c, &expected[0], 4)) return false; c.emplace_back(1, 2, 3, 4); if(!test_expected_container(c, &expected[0], 5)) return false; c.emplace_back(1, 2, 3, 4, 5); if(!test_expected_container(c, &expected[0], 6)) return false; } return true;}template<class Container>bool test_emplace_back(detail::false_){ return true; }template<class Container>bool test_emplace_front(detail::true_){ std::cout << "Starting test_emplace_front." << std::endl << " Class: " << typeid(Container).name() << std::endl; { new(&expected [0]) EmplaceInt(1, 2, 3, 4, 5); new(&expected [1]) EmplaceInt(1, 2, 3, 4); new(&expected [2]) EmplaceInt(1, 2, 3); new(&expected [3]) EmplaceInt(1, 2); new(&expected [4]) EmplaceInt(1); new(&expected [5]) EmplaceInt(); Container c; c.emplace_front(); if(!test_expected_container(c, &expected[0] + 5, 1)) return false; c.emplace_front(1); if(!test_expected_container(c, &expected[0] + 4, 2)) return false; c.emplace_front(1, 2); if(!test_expected_container(c, &expected[0] + 3, 3)) return false; c.emplace_front(1, 2, 3); if(!test_expected_container(c, &expected[0] + 2, 4)) return false; c.emplace_front(1, 2, 3, 4); if(!test_expected_container(c, &expected[0] + 1, 5)) return false; c.emplace_front(1, 2, 3, 4, 5); if(!test_expected_container(c, &expected[0] + 0, 6)) return false; } return true;}template<class Container>bool test_emplace_front(detail::false_){ return true; }template<class Container>bool test_emplace_before(detail::true_){ std::cout << "Starting test_emplace_before." << std::endl << " Class: " << typeid(Container).name() << std::endl; { new(&expected [0]) EmplaceInt(); new(&expected [1]) EmplaceInt(1); new(&expected [2]) EmplaceInt(); Container c; c.emplace(c.cend(), 1); c.emplace(c.cbegin()); if(!test_expected_container(c, &expected[0], 2)) return false; c.emplace(c.cend()); if(!test_expected_container(c, &expected[0], 3)) return false; } { new(&expected [0]) EmplaceInt(); new(&expected [1]) EmplaceInt(1); new(&expected [2]) EmplaceInt(1, 2); new(&expected [3]) EmplaceInt(1, 2, 3); new(&expected [4]) EmplaceInt(1, 2, 3, 4); new(&expected [5]) EmplaceInt(1, 2, 3, 4, 5); //emplace_front-like Container c; c.emplace(c.cbegin(), 1, 2, 3, 4, 5); c.emplace(c.cbegin(), 1, 2, 3, 4); c.emplace(c.cbegin(), 1, 2, 3); c.emplace(c.cbegin(), 1, 2); c.emplace(c.cbegin(), 1); c.emplace(c.cbegin()); if(!test_expected_container(c, &expected[0], 6)) return false; c.clear(); //emplace_back-like typename Container::const_iterator i = c.emplace(c.cend()); if(!test_expected_container(c, &expected[0], 1)) return false; i = c.emplace(++i, 1); if(!test_expected_container(c, &expected[0], 2)) return false; i = c.emplace(++i, 1, 2); if(!test_expected_container(c, &expected[0], 3)) return false; i = c.emplace(++i, 1, 2, 3); if(!test_expected_container(c, &expected[0], 4)) return false; i = c.emplace(++i, 1, 2, 3, 4); if(!test_expected_container(c, &expected[0], 5)) return false; i = c.emplace(++i, 1, 2, 3, 4, 5); if(!test_expected_container(c, &expected[0], 6)) return false; c.clear(); //emplace in the middle c.emplace(c.cbegin()); i = c.emplace(c.cend(), 1, 2, 3, 4, 5); i = c.emplace(i, 1, 2, 3, 4); i = c.emplace(i, 1, 2, 3); i = c.emplace(i, 1, 2); i = c.emplace(i, 1); if(!test_expected_container(c, &expected[0], 6)) return false;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?