PageRenderTime 35ms CodeModel.GetById 13ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://hadesmem.googlecode.com/
C++ | 205 lines | 174 code | 24 blank | 7 comment | 41 complexity | b5a9728fdc8cc358ac4501ec3dcef58c 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 <boost/unordered_set.hpp>
  9#include <boost/unordered_map.hpp>
 10#include "../helpers/test.hpp"
 11#include <boost/next_prior.hpp>
 12#include "../objects/test.hpp"
 13#include "../helpers/random_values.hpp"
 14#include "../helpers/tracker.hpp"
 15#include "../helpers/equivalent.hpp"
 16#include "../helpers/helpers.hpp"
 17
 18#include <iostream>
 19
 20namespace erase_tests
 21{
 22
 23test::seed_t seed(85638);
 24
 25template <class Container>
 26void erase_tests1(Container*,
 27    test::random_generator generator = test::default_generator)
 28{
 29    std::cerr<<"Erase by key.\n";
 30    {
 31        test::random_values<Container> v(1000, generator);
 32        Container x(v.begin(), v.end());
 33        for(BOOST_DEDUCED_TYPENAME test::random_values<Container>::iterator
 34            it = v.begin(); it != v.end(); ++it)
 35        {
 36            std::size_t count = x.count(test::get_key<Container>(*it));
 37            std::size_t old_size = x.size();
 38            BOOST_TEST(count == x.erase(test::get_key<Container>(*it)));
 39            BOOST_TEST(x.size() == old_size - count);
 40            BOOST_TEST(x.count(test::get_key<Container>(*it)) == 0);
 41            BOOST_TEST(x.find(test::get_key<Container>(*it)) == x.end());
 42        }
 43    }
 44
 45    std::cerr<<"erase(begin()).\n";
 46    {
 47        test::random_values<Container> v(1000, generator);
 48        Container x(v.begin(), v.end());
 49        std::size_t size = x.size();
 50        while(size > 0 && !x.empty())
 51        {
 52            BOOST_DEDUCED_TYPENAME Container::key_type
 53                key = test::get_key<Container>(*x.begin());
 54            std::size_t count = x.count(key);
 55            BOOST_DEDUCED_TYPENAME Container::iterator
 56                pos = x.erase(x.begin());
 57            --size;
 58            BOOST_TEST(pos == x.begin());
 59            BOOST_TEST(x.count(key) == count - 1);
 60            BOOST_TEST(x.size() == size);
 61        }
 62        BOOST_TEST(x.empty());
 63    }
 64
 65    std::cerr<<"erase(random position).\n";
 66    {
 67        test::random_values<Container> v(1000, generator);
 68        Container x(v.begin(), v.end());
 69        std::size_t size = x.size();
 70        while(size > 0 && !x.empty())
 71        {
 72            using namespace std;
 73            int index = rand() % (int) x.size();
 74            BOOST_DEDUCED_TYPENAME Container::const_iterator prev, pos, next;
 75            if(index == 0) {
 76                prev = pos = x.begin();
 77            }
 78            else {
 79                prev = boost::next(x.begin(), index - 1);
 80                pos = boost::next(prev);
 81            }
 82            next = boost::next(pos);
 83            BOOST_DEDUCED_TYPENAME Container::key_type
 84                key = test::get_key<Container>(*pos);
 85            std::size_t count = x.count(key);
 86            BOOST_TEST(next == x.erase(pos));
 87            --size;
 88            if(size > 0)
 89                BOOST_TEST(index == 0 ? next == x.begin() :
 90                        next == boost::next(prev));
 91            BOOST_TEST(x.count(key) == count - 1);
 92            BOOST_TEST(x.size() == size);
 93        }
 94        BOOST_TEST(x.empty());
 95    }
 96
 97    std::cerr<<"erase(ranges).\n";
 98    {
 99        test::random_values<Container> v(500, generator);
100        Container x(v.begin(), v.end());
101
102        std::size_t size = x.size();
103
104        // I'm actually stretching it a little here, as the standard says it
105        // returns 'the iterator immediately following the erase elements'
106        // and if nothing is erased, then there's nothing to follow. But I
107        // think this is the only sensible option...
108        BOOST_TEST(x.erase(x.end(), x.end()) == x.end());
109        BOOST_TEST(x.erase(x.begin(), x.begin()) == x.begin());
110        BOOST_TEST(x.size() == size);
111
112        BOOST_TEST(x.erase(x.begin(), x.end()) == x.end());
113        BOOST_TEST(x.empty());
114        BOOST_TEST(x.begin() == x.end());
115
116        BOOST_TEST(x.erase(x.begin(), x.end()) == x.begin());
117    }
118
119    std::cerr<<"quick_erase(begin()).\n";
120    {
121        test::random_values<Container> v(1000, generator);
122        Container x(v.begin(), v.end());
123        std::size_t size = x.size();
124        while(size > 0 && !x.empty())
125        {
126            BOOST_DEDUCED_TYPENAME Container::key_type
127                key = test::get_key<Container>(*x.begin());
128            std::size_t count = x.count(key);
129            x.quick_erase(x.begin());
130            --size;
131            BOOST_TEST(x.count(key) == count - 1);
132            BOOST_TEST(x.size() == size);
133        }
134        BOOST_TEST(x.empty());
135    }
136
137    std::cerr<<"quick_erase(random position).\n";
138    {
139        test::random_values<Container> v(1000, generator);
140        Container x(v.begin(), v.end());
141        std::size_t size = x.size();
142        while(size > 0 && !x.empty())
143        {
144            using namespace std;
145            int index = rand() % (int) x.size();
146            BOOST_DEDUCED_TYPENAME Container::const_iterator prev, pos, next;
147            if(index == 0) {
148                prev = pos = x.begin();
149            }
150            else {
151                prev = boost::next(x.begin(), index - 1);
152                pos = boost::next(prev);
153            }
154            next = boost::next(pos);
155            BOOST_DEDUCED_TYPENAME Container::key_type
156                key = test::get_key<Container>(*pos);
157            std::size_t count = x.count(key);
158            x.quick_erase(pos);
159            --size;
160            if(size > 0)
161                BOOST_TEST(index == 0 ? next == x.begin() :
162                        next == boost::next(prev));
163            BOOST_TEST(x.count(key) == count - 1);
164            BOOST_TEST(x.size() == size);
165        }
166        BOOST_TEST(x.empty());
167    }
168
169
170    std::cerr<<"clear().\n";
171    {
172        test::random_values<Container> v(500, generator);
173        Container x(v.begin(), v.end());
174        x.clear();
175        BOOST_TEST(x.empty());
176        BOOST_TEST(x.begin() == x.end());
177    }
178
179    std::cerr<<"\n";
180}
181
182boost::unordered_set<test::object,
183    test::hash, test::equal_to,
184    test::allocator<test::object> >* test_set;
185boost::unordered_multiset<test::object,
186    test::hash, test::equal_to,
187    test::allocator<test::object> >* test_multiset;
188boost::unordered_map<test::object, test::object,
189    test::hash, test::equal_to,
190    test::allocator<test::object> >* test_map;
191boost::unordered_multimap<test::object, test::object,
192    test::hash, test::equal_to,
193    test::allocator<test::object> >* test_multimap;
194
195using test::default_generator;
196using test::generate_collisions;
197
198UNORDERED_TEST(erase_tests1,
199    ((test_set)(test_multiset)(test_map)(test_multimap))
200    ((default_generator)(generate_collisions))
201)
202
203}
204
205RUN_TESTS()