| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-09 20:56:23 +00:00
										 |  |  | // Copyright 2006-2009 Daniel James.
 | 
					
						
							| 
									
										
										
										
											2006-07-01 22:31:26 +00:00
										 |  |  | // 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)
 | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <boost/unordered_set.hpp>
 | 
					
						
							|  |  |  | #include <boost/unordered_map.hpp>
 | 
					
						
							| 
									
										
										
										
											2008-03-24 17:03:15 +00:00
										 |  |  | #include "../helpers/test.hpp"
 | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | #include <boost/next_prior.hpp>
 | 
					
						
							|  |  |  | #include "../objects/test.hpp"
 | 
					
						
							|  |  |  | #include "../helpers/random_values.hpp"
 | 
					
						
							|  |  |  | #include "../helpers/tracker.hpp"
 | 
					
						
							|  |  |  | #include "../helpers/equivalent.hpp"
 | 
					
						
							|  |  |  | #include "../helpers/helpers.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-24 17:03:15 +00:00
										 |  |  | namespace erase_tests | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-06 11:42:28 +00:00
										 |  |  | test::seed_t seed(85638); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | template <class Container> | 
					
						
							| 
									
										
										
										
											2008-01-20 18:55:57 +00:00
										 |  |  | void erase_tests1(Container*, test::random_generator generator = test::default_generator) | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     std::cerr<<"Erase by key.\n"; | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-01-20 18:55:57 +00:00
										 |  |  |         test::random_values<Container> v(1000, generator); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |         Container x(v.begin(), v.end()); | 
					
						
							| 
									
										
										
										
											2008-01-10 22:30:46 +00:00
										 |  |  |         for(BOOST_DEDUCED_TYPENAME test::random_values<Container>::iterator it = v.begin(); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |             it != v.end(); ++it) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             std::size_t count = x.count(test::get_key<Container>(*it)); | 
					
						
							|  |  |  |             std::size_t old_size = x.size(); | 
					
						
							| 
									
										
										
										
											2009-05-27 17:44:09 +00:00
										 |  |  |             BOOST_TEST(count == x.erase(test::get_key<Container>(*it))); | 
					
						
							|  |  |  |             BOOST_TEST(x.size() == old_size - count); | 
					
						
							|  |  |  |             BOOST_TEST(x.count(test::get_key<Container>(*it)) == 0); | 
					
						
							|  |  |  |             BOOST_TEST(x.find(test::get_key<Container>(*it)) == x.end()); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::cerr<<"erase(begin()).\n"; | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-01-20 18:55:57 +00:00
										 |  |  |         test::random_values<Container> v(1000, generator); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |         Container x(v.begin(), v.end()); | 
					
						
							|  |  |  |         std::size_t size = x.size(); | 
					
						
							|  |  |  |         while(size > 0 && !x.empty()) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2008-01-10 22:30:46 +00:00
										 |  |  |             BOOST_DEDUCED_TYPENAME Container::key_type key = test::get_key<Container>(*x.begin()); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |             std::size_t count = x.count(key); | 
					
						
							| 
									
										
										
										
											2008-01-10 22:30:46 +00:00
										 |  |  |             BOOST_DEDUCED_TYPENAME Container::iterator pos = x.erase(x.begin()); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |             --size; | 
					
						
							| 
									
										
										
										
											2009-05-27 17:44:09 +00:00
										 |  |  |             BOOST_TEST(pos == x.begin()); | 
					
						
							|  |  |  |             BOOST_TEST(x.count(key) == count - 1); | 
					
						
							|  |  |  |             BOOST_TEST(x.size() == size); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-05-27 17:44:09 +00:00
										 |  |  |         BOOST_TEST(x.empty()); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::cerr<<"erase(random position).\n"; | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-01-20 18:55:57 +00:00
										 |  |  |         test::random_values<Container> v(1000, generator); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |         Container x(v.begin(), v.end()); | 
					
						
							|  |  |  |         std::size_t size = x.size(); | 
					
						
							|  |  |  |         while(size > 0 && !x.empty()) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             using namespace std; | 
					
						
							|  |  |  |             int index = rand() % x.size(); | 
					
						
							| 
									
										
										
										
											2008-01-10 22:30:46 +00:00
										 |  |  |             BOOST_DEDUCED_TYPENAME Container::const_iterator prev, pos, next; | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |             if(index == 0) { | 
					
						
							|  |  |  |                 prev = pos = x.begin(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 prev = boost::next(x.begin(), index - 1); | 
					
						
							|  |  |  |                 pos = boost::next(prev); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             next = boost::next(pos); | 
					
						
							| 
									
										
										
										
											2008-01-10 22:30:46 +00:00
										 |  |  |             BOOST_DEDUCED_TYPENAME Container::key_type key = test::get_key<Container>(*pos); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |             std::size_t count = x.count(key); | 
					
						
							| 
									
										
										
										
											2009-05-27 17:44:09 +00:00
										 |  |  |             BOOST_TEST(next == x.erase(pos)); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |             --size; | 
					
						
							|  |  |  |             if(size > 0) | 
					
						
							| 
									
										
										
										
											2009-05-27 17:44:09 +00:00
										 |  |  |                 BOOST_TEST(index == 0 ? next == x.begin() : | 
					
						
							| 
									
										
										
										
											2008-01-13 16:19:26 +00:00
										 |  |  |                         next == boost::next(prev)); | 
					
						
							| 
									
										
										
										
											2009-05-27 17:44:09 +00:00
										 |  |  |             BOOST_TEST(x.count(key) == count - 1); | 
					
						
							|  |  |  |             BOOST_TEST(x.size() == size); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-05-27 17:44:09 +00:00
										 |  |  |         BOOST_TEST(x.empty()); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::cerr<<"erase(ranges).\n"; | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-01-20 18:55:57 +00:00
										 |  |  |         test::random_values<Container> v(500, generator); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |         Container x(v.begin(), v.end()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         std::size_t size = x.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // I'm actually stretching it a little here, as the standard says it
 | 
					
						
							|  |  |  |         // returns 'the iterator immediately following the erase elements'
 | 
					
						
							|  |  |  |         // and if nothing is erased, then there's nothing to follow. But I
 | 
					
						
							|  |  |  |         // think this is the only sensible option...
 | 
					
						
							| 
									
										
										
										
											2009-05-27 17:44:09 +00:00
										 |  |  |         BOOST_TEST(x.erase(x.end(), x.end()) == x.end()); | 
					
						
							|  |  |  |         BOOST_TEST(x.erase(x.begin(), x.begin()) == x.begin()); | 
					
						
							|  |  |  |         BOOST_TEST(x.size() == size); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-27 17:44:09 +00:00
										 |  |  |         BOOST_TEST(x.erase(x.begin(), x.end()) == x.end()); | 
					
						
							|  |  |  |         BOOST_TEST(x.empty()); | 
					
						
							|  |  |  |         BOOST_TEST(x.begin() == x.end()); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-27 17:44:09 +00:00
										 |  |  |         BOOST_TEST(x.erase(x.begin(), x.end()) == x.begin()); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::cerr<<"clear().\n"; | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-01-20 18:55:57 +00:00
										 |  |  |         test::random_values<Container> v(500, generator); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |         Container x(v.begin(), v.end()); | 
					
						
							|  |  |  |         x.clear(); | 
					
						
							| 
									
										
										
										
											2009-05-27 17:44:09 +00:00
										 |  |  |         BOOST_TEST(x.empty()); | 
					
						
							|  |  |  |         BOOST_TEST(x.begin() == x.end()); | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-01-20 18:55:57 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     std::cerr<<"\n"; | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-24 17:03:15 +00:00
										 |  |  | boost::unordered_set<test::object, test::hash, test::equal_to, test::allocator<test::object> >* test_set; | 
					
						
							|  |  |  | boost::unordered_multiset<test::object, test::hash, test::equal_to, test::allocator<test::object> >* test_multiset; | 
					
						
							|  |  |  | boost::unordered_map<test::object, test::object, test::hash, test::equal_to, test::allocator<test::object> >* test_map; | 
					
						
							|  |  |  | boost::unordered_multimap<test::object, test::object, test::hash, test::equal_to, test::allocator<test::object> >* test_multimap; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using test::default_generator; | 
					
						
							|  |  |  | using test::generate_collisions; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | UNORDERED_TEST(erase_tests1, | 
					
						
							|  |  |  |     ((test_set)(test_multiset)(test_map)(test_multimap)) | 
					
						
							|  |  |  |     ((default_generator)(generate_collisions)) | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-17 17:19:16 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-03-24 17:03:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | RUN_TESTS() |