PageRenderTime 76ms CodeModel.GetById 44ms app.highlight 24ms RepoModel.GetById 6ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/unordered/test/exception/swap_exception_tests.cpp

http://hadesmem.googlecode.com/
C++ | 125 lines | 98 code | 24 blank | 3 comment | 10 complexity | fb62df6282ef16db06b9b98f7f24a0af MD5 | raw file
  1
  2// Copyright 2006-2009 Daniel James.
  3// Distributed under the Boost Software License, Version 1.0. (See accompanying
  4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5
  6#include "../helpers/prefix.hpp"
  7
  8#include "./containers.hpp"
  9#include "../helpers/random_values.hpp"
 10#include "../helpers/invariants.hpp"
 11
 12#if defined(BOOST_MSVC)
 13#pragma warning(disable:4512) // assignment operator could not be generated
 14#endif
 15
 16test::seed_t seed(9387);
 17
 18template <class T>
 19struct self_swap_base : public test::exception_base
 20{
 21    test::random_values<T> values;
 22    self_swap_base(int count = 0) : values(count) {}
 23
 24    typedef T data_type;
 25    T init() const { return T(values.begin(), values.end()); }
 26    void run(T& x) const { x.swap(x); }
 27    void check BOOST_PREVENT_MACRO_SUBSTITUTION(T const& x) const {
 28        std::string scope(test::scope);
 29
 30#if BOOST_UNORDERED_SWAP_METHOD != 2
 31        BOOST_TEST(
 32                scope == "hash::operator(hash)" ||
 33                scope == "hash::operator=(hash)" ||
 34                scope == "equal_to::operator(equal_to)" ||
 35                scope == "equal_to::operator=(equal_to)");
 36#endif
 37
 38        test::check_equivalent_keys(x);
 39    }
 40};
 41
 42template <class T>
 43struct self_swap_test1 : self_swap_base<T> {};
 44
 45template <class T>
 46struct self_swap_test2 : self_swap_base<T>
 47{
 48    self_swap_test2() : self_swap_base<T>(100) {}
 49};
 50
 51template <class T>
 52struct swap_base : public test::exception_base
 53{
 54    const test::random_values<T> x_values, y_values;
 55    const T initial_x, initial_y;
 56
 57    typedef BOOST_DEDUCED_TYPENAME T::hasher hasher;
 58    typedef BOOST_DEDUCED_TYPENAME T::key_equal key_equal;
 59    typedef BOOST_DEDUCED_TYPENAME T::allocator_type allocator_type;
 60
 61    swap_base(unsigned int count1, unsigned int count2, int tag1, int tag2)
 62        : x_values(count1), y_values(count2),
 63        initial_x(x_values.begin(), x_values.end(), 0, hasher(tag1),
 64                key_equal(tag1), allocator_type(tag1)),
 65        initial_y(y_values.begin(), y_values.end(), 0, hasher(tag2),
 66                key_equal(tag2), allocator_type(tag2))
 67    {}
 68
 69    struct data_type {
 70        data_type(T const& x, T const& y)
 71            : x(x), y(y) {}
 72
 73        T x, y;
 74    };
 75
 76    data_type init() const { return data_type(initial_x, initial_y); }
 77    void run(data_type& d) const {
 78        try {
 79            d.x.swap(d.y);
 80        } catch (std::runtime_error) {}
 81    }
 82    void check BOOST_PREVENT_MACRO_SUBSTITUTION(data_type const& d) const {
 83        std::string scope(test::scope);
 84
 85#if BOOST_UNORDERED_SWAP_METHOD != 2
 86        BOOST_TEST(
 87                scope == "hash::operator(hash)" ||
 88                scope == "hash::operator=(hash)" ||
 89                scope == "equal_to::operator(equal_to)" ||
 90                scope == "equal_to::operator=(equal_to)");
 91#endif
 92        
 93        test::check_equivalent_keys(d.x);
 94        test::check_equivalent_keys(d.y);
 95    }
 96};
 97
 98template <class T>
 99struct swap_test1 : swap_base<T>
100{
101    swap_test1() : swap_base<T>(0, 0, 0, 0) {}
102};
103
104template <class T>
105struct swap_test2 : swap_base<T>
106{
107    swap_test2() : swap_base<T>(60, 0, 0, 0) {}    
108};
109
110template <class T>
111struct swap_test3 : swap_base<T>
112{
113    swap_test3() : swap_base<T>(0, 60, 0, 0) {}    
114};
115
116template <class T>
117struct swap_test4 : swap_base<T>
118{
119    swap_test4() : swap_base<T>(10, 10, 1, 2) {}
120};
121
122RUN_EXCEPTION_TESTS(
123    (self_swap_test1)(self_swap_test2)
124    (swap_test1)(swap_test2)(swap_test3)(swap_test4),
125    CONTAINER_SEQ)