Reverting merge from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_set / max_load_factor / robustness.cc
blobe158d63a933c796de2a83fc43dadfcdef272c081
1 // { dg-options "-std=gnu++0x" }
3 // Copyright (C) 2011-2013 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 #include <unordered_set>
21 #include <limits>
22 #include <ext/throw_allocator.h>
23 #include <testsuite_hooks.h>
25 void test01()
27 bool test __attribute__((unused)) = true;
29 typedef std::numeric_limits<std::size_t> nl_size_t;
30 std::unordered_set<int, std::hash<int>, std::equal_to<int>,
31 __gnu_cxx::throw_allocator_limit<int> > us;
32 int val = 0;
33 for (; val != 100; ++val)
35 VERIFY( us.insert(val).second) ;
36 VERIFY( us.load_factor() <= us.max_load_factor() );
39 float cur_max_load_factor = us.max_load_factor();
40 int counter = 0;
41 std::size_t thrown_exceptions = 0;
42 while (true)
44 __gnu_cxx::limit_condition::set_limit(counter++);
45 bool do_break = false;
46 try
48 us.max_load_factor(.5f);
49 do_break = true;
51 catch (const __gnu_cxx::forced_error&)
53 VERIFY( us.max_load_factor() == cur_max_load_factor );
54 ++thrown_exceptions;
56 // Lets check that unordered_set will still be correctly resized
57 // when needed
58 __gnu_cxx::limit_condition::set_limit(nl_size_t::max());
59 for (;;)
61 VERIFY( us.load_factor() <= us.max_load_factor() );
62 size_t nbkts = us.bucket_count();
63 VERIFY( us.insert(val++).second );
64 if (us.bucket_count() != nbkts)
65 break;
67 if (do_break)
68 break;
70 VERIFY( thrown_exceptions > 0 );
73 int main()
75 test01();
76 return 0;