PageRenderTime 26ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/Src/Dependencies/Boost/libs/unordered/test/unordered/simple_tests.cpp

http://hadesmem.googlecode.com/
C++ | 132 lines | 102 code | 26 blank | 4 comment | 17 complexity | 72d9854f392f97b1d88e2f812579ba8e 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// This test checks the runtime requirements of containers.
  7
  8#include "../helpers/prefix.hpp"
  9
 10#include <boost/unordered_set.hpp>
 11#include <boost/unordered_map.hpp>
 12#include "../helpers/test.hpp"
 13#include <cstdlib>
 14#include <algorithm>
 15#include "../helpers/equivalent.hpp"
 16
 17template <class X>
 18void simple_test(X const& a)
 19{
 20    test::unordered_equivalence_tester<X> equivalent(a);
 21
 22    {
 23        X u;
 24        BOOST_TEST(u.size() == 0);
 25        BOOST_TEST(X().size() == 0);
 26    }
 27
 28    {
 29        BOOST_TEST(equivalent(X(a)));
 30    }
 31
 32    {
 33        X u(a);
 34        BOOST_TEST(equivalent(u));
 35    }
 36
 37    {
 38        X u = a;
 39        BOOST_TEST(equivalent(u));
 40    }
 41
 42    {
 43        X b(a);
 44        BOOST_TEST(b.begin() == const_cast<X const&>(b).cbegin());
 45        BOOST_TEST(b.end() == const_cast<X const&>(b).cend());
 46    }
 47
 48    {
 49        X b(a);
 50        X c;
 51        BOOST_TEST(equivalent(b));
 52        BOOST_TEST(c.empty());
 53        b.swap(c);
 54        BOOST_TEST(b.empty());
 55        BOOST_TEST(equivalent(c));
 56        b.swap(c);
 57        BOOST_TEST(c.empty());
 58        BOOST_TEST(equivalent(b));
 59    }
 60
 61    {
 62        X u;
 63        X& r = u;
 64        BOOST_TEST(&(r = r) == &r);
 65
 66        BOOST_TEST(r.empty());
 67        BOOST_TEST(&(r = a) == &r);
 68        BOOST_TEST(equivalent(r));
 69        BOOST_TEST(&(r = r) == &r);
 70        BOOST_TEST(equivalent(r));
 71    }
 72
 73    {
 74        BOOST_TEST(a.size() ==
 75            static_cast<BOOST_DEDUCED_TYPENAME X::size_type>(
 76                std::distance(a.begin(), a.end())));
 77    }
 78
 79    {
 80        BOOST_TEST(a.empty() == (a.size() == 0));
 81    }
 82
 83    {
 84        BOOST_TEST(a.empty() == (a.begin() == a.end()));
 85        X u;
 86        BOOST_TEST(u.begin() == u.end());
 87    }
 88}
 89
 90UNORDERED_AUTO_TEST(simple_tests)
 91{
 92    using namespace std;
 93    srand(14878);
 94
 95    std::cout<<"Test unordered_set.\n";
 96    boost::unordered_set<int> set;
 97    simple_test(set);
 98
 99    set.insert(1); set.insert(2); set.insert(1456);
100    simple_test(set);
101
102    std::cout<<"Test unordered_multiset.\n";
103    boost::unordered_multiset<int> multiset;
104    simple_test(multiset);
105    
106    for(int i1 = 0; i1 < 1000; ++i1) {
107        int count = rand() % 10, index = rand();
108        for(int j = 0; j < count; ++j)
109            multiset.insert(index);
110    }
111    simple_test(multiset);
112    
113    std::cout<<"Test unordered_map.\n";
114    boost::unordered_map<int, int> map;
115
116    for(int i2 = 0; i2 < 1000; ++i2) {
117        map.insert(std::pair<const int, int>(rand(), rand()));
118    }
119    simple_test(map);
120
121    std::cout<<"Test unordered_multimap.\n";
122    boost::unordered_multimap<int, int> multimap;
123
124    for(int i3 = 0; i3 < 1000; ++i3) {
125        int count = rand() % 10, index = rand();
126        for(int j = 0; j < count; ++j)
127            multimap.insert(std::pair<const int, int>(index, rand()));
128    }
129    simple_test(multimap);
130}
131
132RUN_TESTS()