1 // { dg-timeout-factor 2.0 }
5 // Copyright (C) 2005-2023 Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the terms
9 // of the GNU General Public License as published by the Free Software
10 // Foundation; either version 3, or (at your option) any later
13 // This library is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
23 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
25 // Permission to use, copy, modify, sell, and distribute this software
26 // is hereby granted without fee, provided that the above copyright
27 // notice appears in all copies, and that both that copyright notice
28 // and this permission notice appear in supporting documentation. None
29 // of the above authors, nor IBM Haifa Research Laboratories, make any
30 // representation about the suitability of this software for any
31 // purpose. It is provided "as is" without express or implied
35 * @file hash_illegal_resize_example.cpp
36 * An example of illegally externally resizing a hash-based container object.
40 * This example shows the case where a hash-based container object is
41 * resized to a value which it cannot accommodate at runtime. Illegal
42 * runtime resizes cause an exception.
45 #include <ext/pb_ds/assoc_container.hpp>
46 #include <ext/pb_ds/hash_policy.hpp>
47 #include <ext/pb_ds/exception.hpp>
50 // size of test containers
60 using namespace __gnu_pbds
;
62 // A simple hash functor.
63 // hash could serve instead of this functor, but it is not yet
64 // standard everywhere.
68 operator()(const int& r_i
) const
75 // A probing hash table mapping ints to chars.
82 // Combining function.
83 direct_mod_range_hashing
<>,
87 hash_standard_resize_policy
<
88 hash_prime_size_policy
,
89 hash_load_check_resize_trigger
<>,
90 /* Allow external access to size.
91 * Without setting this to true, external resizing
99 // Insert some elements.
102 for (i
= 0; i
< SIZE
; ++i
)
106 assert(g
.size() == SIZE
);
107 for (i
= 0; i
< SIZE
; ++i
)
108 assert(g
.find(i
) != g
.end() && g
.find(i
)->second
== 2 * i
);
110 // Now attempt to resize the table to 200 (impossible).
111 bool ex_thrown
= false;
117 catch(__gnu_pbds::resize_error
& )
122 // Assert an exception was thrown. A probing table cannot contain
123 // 1000 entries in less than 1000 places.
126 // Irrespective of the fact that the resize was not successful, the
127 // container object should still be in a valid state; the following
130 assert(g
.size() == SIZE
);
131 for (i
= 0; i
< SIZE
; ++i
)
132 assert(g
.find(i
) != g
.end() && g
.find(i
)->second
== 2 * i
);