container_tests.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 815 行 · 第 1/2 页

HPP
815
字号
/*=============================================================================    Copyright (c) 2004 Angus Leeming    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)==============================================================================*/#if !defined(CONTAINER_TESTS_HPP)#define CONTAINER_TESTS_HPP#include <boost/detail/lightweight_test.hpp>#include <boost/spirit/include/phoenix_core.hpp>#include <boost/spirit/home/phoenix/stl/container/container.hpp>#include <iostream>#include <typeinfo>#include <deque>#include <list>#include <map>#include <vector>#include <utility>#ifdef BOOST_MSVC#pragma warning(disable : 4800)#endifusing std::cerr;namespace phx = boost::phoenix;std::deque<int> const build_deque();std::list<int> const build_list();std::map<int, int> const build_map();std::multimap<int, int> const build_multimap();std::vector<int> const build_vector();inline booltest(bool fail){    BOOST_TEST(!fail);    return fail;}template <typename Container>void test_assign(Container c){    using phx::arg_names::arg1;    using phx::assign;    typename Container::size_type count = 2;    typename Container::const_iterator first = c.begin();    typename Container::const_iterator second = first;    typename Container::value_type value = *first;    assign(arg1, count, value)(c);    // iterators may be invalidated!    first = c.begin();    second = first;    std::advance(second, 1);    if (test(*first != *second)) {        cerr << "Failed " << typeid(Container).name() << " test_assign 1\n";        return;    }#if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)    // Should not --- does not, Yay! --- compile.    Container const const_c = c;    assign(const_c, count, value);#endif}template <typename Container>void test_assign2(Container c){    using phx::arg_names::arg1;    using phx::arg_names::arg2;    using phx::arg_names::arg3;    using phx::assign;    Container c2 = c;    typename Container::const_iterator first = c2.begin();    typename Container::const_iterator last = c2.end();    typename Container::size_type size = c2.size();    c.clear();    assign(arg1, arg2, arg3)(c, first, last);    if (test(c.size() != size)) {        cerr << "Failed " << typeid(Container).name()       << " test_assign2 1\n"       << "size == " << c.size() << '\n';        return;    }#if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)    // Should not --- does not, Yay! --- compile.    Container const const_c = c;    assign(const_c, first, second);#endif}template <typename Container>void test_at(Container c){    using phx::arg_names::arg1;    using phx::at;    typename Container::reference r1 = at(arg1, 0)(c);    if (test(r1 != c.at(0))) {        cerr << "Failed " << typeid(Container).name() << " test_at 1\n";        return;    }    typename Container::const_reference r2 = at(arg1, 0)(c);    if (test(r2 != c.at(0))) {        cerr << "Failed " << typeid(Container).name() << " test_at 2\n";        return;    }    Container const const_c = c;#if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)    // Should not --- does not, Yay! --- compile.    typename Container::reference r3 = at(arg1, 0)(const_c);#endif    typename Container::const_reference r4 = at(arg1, 0)(const_c);    if (test(r4 != c.at(0))) {        cerr << "Failed " << typeid(Container).name() << " test_at 4\n";        return;    }}template <typename Container>void test_back(Container c){    using phx::arg_names::arg1;    using phx::back;    typename Container::reference r1 = back(arg1)(c);    if (test(r1 != c.back())) {        cerr << "Failed " << typeid(Container).name() << " test_back 1\n";        return;    }    typename Container::const_reference r2 = back(arg1)(c);    if (test(r2 != c.back())) {        cerr << "Failed " << typeid(Container).name() << " test_back 2\n";        return;    }    Container const const_c = c;#if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)    // Should not --- does not, Yay! --- compile.    typename Container::reference r3 = back(arg1)(const_c);#endif    typename Container::const_reference r4 = back(arg1)(const_c);    if (test(r4 != c.back())) {        cerr << "Failed " << typeid(Container).name() << " test_back 4\n";        return;    }}template <typename Container>void test_begin(Container c){    using phx::arg_names::arg1;    using phx::begin;    typename Container::iterator it1 = begin(arg1)(c);    if (test(it1 != c.begin())) {        cerr << "Failed " << typeid(Container).name() << " test_begin 1\n";        return;    }    typename Container::const_iterator it2 = begin(arg1)(c);    if (test(it2 != c.begin())) {        cerr << "Failed " << typeid(Container).name() << " test_begin 2\n";        return;    }    Container const const_c = c;#if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)    // Should not --- does not, Yay! --- compile.    typename Container::iterator it3 = begin(arg1)(const_c);#endif    typename Container::const_iterator it4 = begin(arg1)(const_c);    if (test(it4 != const_c.begin())) {        cerr << "Failed " << typeid(Container).name() << " test_begin 4\n";        return;    }}template <typename Container>void test_capacity(Container c){    using phx::arg_names::arg1;    using phx::capacity;    typename Container::size_type s1 = capacity(arg1)(c);    if (test(s1 != c.capacity())) {        cerr << "Failed " << typeid(Container).name() << " test_capacity 1\n";        return;    }    Container const const_c = c;    typename Container::size_type s2 = capacity(arg1)(const_c);    if (test(s2 != const_c.capacity())) {        cerr << "Failed " << typeid(Container).name() << " test_capacity 2\n";        return;    }}template <typename Container>void test_clear(Container c){    using phx::arg_names::arg1;    using phx::clear;    clear(arg1)(c);    if (test(!c.empty())) {        cerr << "Failed " << typeid(Container).name() << " test_clear 1\n";        return;    }#if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)    Container const const_c = c;    clear(arg1)(const_c);#endif}template <typename Container>void test_empty(Container c){    using phx::arg_names::arg1;    using phx::empty;    typename Container::size_type s1 = empty(arg1)(c);    if (test(bool(s1) != c.empty())) {        cerr << "Failed " << typeid(Container).name() << " test_empty 1\n";        return;    }    Container const const_c = c;    typename Container::size_type s2 = empty(arg1)(const_c);    if (test(bool(s2) != const_c.empty())) {        cerr << "Failed " << typeid(Container).name() << " test_empty 2\n";        return;    }}template <typename Container>void test_end(Container c){    using phx::arg_names::arg1;    using phx::end;    typename Container::iterator it1 = end(arg1)(c);    if (test(it1 != c.end())) {        cerr << "Failed " << typeid(Container).name() << " test_end 1\n";        return;    }    typename Container::const_iterator it2 = end(arg1)(c);    if (test(it2 != c.end())) {        cerr << "Failed " << typeid(Container).name() << " test_end 2\n";        return;    }    Container const const_c = c;#if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)    // Should not --- does not, Yay! --- compile.    typename Container::iterator it3 = end(arg1)(const_c);#endif    typename Container::const_iterator it4 = end(arg1)(const_c);    if (test(it4 != const_c.end())) {        cerr << "Failed " << typeid(Container).name() << " test_end 4\n";        return;    }}template <typename Container>void test_erase(Container c){    using phx::arg_names::arg1;    using phx::arg_names::arg2;    using phx::arg_names::arg3;    using phx::erase;    Container const const_c = c;    typename Container::size_type size = c.size();    typename Container::iterator c_begin = c.begin();    erase(arg1, arg2)(c, c_begin);    if (test(c.size() + 1 != size)) {        cerr << "Failed " << typeid(Container).name() << " test_erase 1\n";        return;    }    c_begin = c.begin();    typename Container::iterator c_end = c.end();    erase(arg1, arg2, arg3)(c, c_begin, c_end);    if (test(!c.empty())) {        cerr << "Failed " << typeid(Container).name() << " test_erase 2\n";        return;    }#if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)    erase(arg1, const_c.begin())(const_c);    erase(arg1, const_c.begin(), const_c.end())(const_c);#endif}template <typename Container>void test_map_erase(Container c){    test_erase(c);    if (boost::report_errors() != 0)      return;    using phx::arg_names::arg1;    using phx::arg_names::arg2;    using phx::erase;    typename Container::value_type const value = *c.begin();    typename Container::key_type const key = value.first;    typename Container::size_type const removed =      erase(arg1, arg2)(c, key);    if (test(removed != 1)) {        cerr << "Failed " << typeid(Container).name() << " test_map_erase 1\n";        return;    }}template <typename Container>void test_front(Container c){    using phx::arg_names::arg1;    using phx::front;    typename Container::reference r1 = front(arg1)(c);    if (test(r1 != c.front())) {        cerr << "Failed " << typeid(Container).name() << " test_front 1\n";        return;    }    typename Container::const_reference r2 = front(arg1)(c);    if (test(r2 != c.front())) {        cerr << "Failed " << typeid(Container).name() << " test_front 2\n";        return;    }    Container const const_c = c;#if defined(BOOST_PHOENIX_COMPILE_FAIL_TEST)    // Should not --- does not, Yay! --- compile.    typename Container::reference r3 = front(arg1)(const_c);#endif    typename Container::const_reference r4 = front(arg1)(const_c);    if (test(r4 != c.front())) {        cerr << "Failed " << typeid(Container).name() << " test_front 4\n";        return;    }}template <typename Container>void test_get_allocator(Container c){    using phx::arg_names::arg1;    using phx::get_allocator;    Container const const_c = c;    typename Container::allocator_type a1 = get_allocator(arg1)(c);    if (test(a1 != c.get_allocator())) {        cerr << "Failed " << typeid(Container).name() << " test_get_allocator 1\n";        return;    }    typename Container::allocator_type a2 = get_allocator(arg1)(const_c);    if (test(a2 != const_c.get_allocator())) {        cerr << "Failed " << typeid(Container).name() << " test_get_allocator 2\n";        return;    }}template <typename Container>void test_insert(Container c){    using phx::arg_names::arg1;    using phx::insert;    typename Container::value_type const value = *c.begin();    typename Container::iterator it = insert(arg1, c.begin(), value)(c);    if (test(it != c.begin() || *it != *(++it))) {        cerr << "Failed " << typeid(Container).name() << " test_insert 1\n";        return;    }    typename Container::size_type size = c.size();    insert(arg1, c.begin(), 3, value)(c);    if (test(c.size() != size + 3)) {        cerr << "Failed " << typeid(Container).name() << " test_insert 2\n";        return;    }    Container const const_c = c;    size = c.size();    insert(arg1, c.begin(), const_c.begin(), const_c.end())(c);    if (test(c.size() != 2 * size)) {        cerr << "Failed " << typeid(Container).name() << " test_insert 3\n";        return;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?