| 1 |
// Copyright (C) 2013-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++11 } } |
| 19 |
|
| 20 |
#include <unordered_set> |
| 21 |
|
| 22 |
#include <testsuite_hooks.h> |
| 23 |
#include <testsuite_allocator.h> |
| 24 |
#include <testsuite_counter_type.h> |
| 25 |
|
| 26 |
using __gnu_test::propagating_allocator; |
| 27 |
using __gnu_test::counter_type; |
| 28 |
using __gnu_test::tracker_allocator; |
| 29 |
using __gnu_test::tracker_allocator_counter; |
| 30 |
|
| 31 |
void test01() |
| 32 |
{ |
| 33 |
tracker_allocator_counter::reset(); |
| 34 |
{ |
| 35 |
typedef propagating_allocator<counter_type, false, |
| 36 |
tracker_allocator<counter_type>> alloc_type; |
| 37 |
typedef __gnu_test::counter_type_hasher hash; |
| 38 |
typedef std::unordered_set<counter_type, hash, |
| 39 |
std::equal_to<counter_type>, |
| 40 |
alloc_type> test_type; |
| 41 |
|
| 42 |
test_type v1(alloc_type(1)); |
| 43 |
v1.emplace(0); |
| 44 |
|
| 45 |
test_type v2(alloc_type(2)); |
| 46 |
v2.emplace(1); |
| 47 |
|
| 48 |
counter_type::reset(); |
| 49 |
|
| 50 |
v2 = std::move(v1); |
| 51 |
|
| 52 |
VERIFY( 1 == v1.get_allocator().get_personality() ); |
| 53 |
VERIFY( 2 == v2.get_allocator().get_personality() ); |
| 54 |
|
| 55 |
VERIFY( counter_type::move_count == 0 ); |
| 56 |
// 1 element in v1 destroyed. |
| 57 |
VERIFY( counter_type::destructor_count == 1 ); |
| 58 |
} |
| 59 |
|
| 60 |
// Check there's nothing left allocated or constructed. |
| 61 |
VERIFY( tracker_allocator_counter::get_construct_count() |
| 62 |
== tracker_allocator_counter::get_destruct_count() ); |
| 63 |
VERIFY( tracker_allocator_counter::get_allocation_count() |
| 64 |
== tracker_allocator_counter::get_deallocation_count() ); |
| 65 |
} |
| 66 |
|
| 67 |
void test02() |
| 68 |
{ |
| 69 |
tracker_allocator_counter::reset(); |
| 70 |
{ |
| 71 |
typedef propagating_allocator<counter_type, true, |
| 72 |
tracker_allocator<counter_type>> alloc_type; |
| 73 |
typedef __gnu_test::counter_type_hasher hash; |
| 74 |
typedef std::unordered_set<counter_type, hash, |
| 75 |
std::equal_to<counter_type>, |
| 76 |
alloc_type> test_type; |
| 77 |
|
| 78 |
test_type v1(alloc_type(1)); |
| 79 |
v1.emplace(0); |
| 80 |
|
| 81 |
auto it = v1.begin(); |
| 82 |
|
| 83 |
test_type v2(alloc_type(2)); |
| 84 |
v2.emplace(0); |
| 85 |
|
| 86 |
counter_type::reset(); |
| 87 |
|
| 88 |
v2 = std::move(v1); |
| 89 |
|
| 90 |
VERIFY(1 == v1.get_allocator().get_personality()); |
| 91 |
VERIFY(1 == v2.get_allocator().get_personality()); |
| 92 |
|
| 93 |
VERIFY( counter_type::move_count == 0 ); |
| 94 |
VERIFY( counter_type::copy_count == 0 ); |
| 95 |
VERIFY( counter_type::destructor_count == 1 ); |
| 96 |
|
| 97 |
VERIFY( it == v2.begin() ); |
| 98 |
} |
| 99 |
|
| 100 |
// Check there's nothing left allocated or constructed. |
| 101 |
VERIFY( tracker_allocator_counter::get_construct_count() |
| 102 |
== tracker_allocator_counter::get_destruct_count() ); |
| 103 |
VERIFY( tracker_allocator_counter::get_allocation_count() |
| 104 |
== tracker_allocator_counter::get_deallocation_count() ); |
| 105 |
} |
| 106 |
|
| 107 |
void test03() |
| 108 |
{ |
| 109 |
tracker_allocator_counter::reset(); |
| 110 |
{ |
| 111 |
typedef propagating_allocator<counter_type, false, |
| 112 |
tracker_allocator<counter_type>> alloc_type; |
| 113 |
typedef __gnu_test::counter_type_hasher hash; |
| 114 |
typedef std::unordered_set<counter_type, hash, |
| 115 |
std::equal_to<counter_type>, |
| 116 |
alloc_type> test_type; |
| 117 |
|
| 118 |
test_type v1(alloc_type(1)); |
| 119 |
v1.emplace(0); |
| 120 |
|
| 121 |
test_type v2(alloc_type(2)); |
| 122 |
int i = 0; |
| 123 |
v2.emplace(i++); |
| 124 |
for (; v2.bucket_count() == v1.bucket_count(); ++i) |
| 125 |
v2.emplace(i); |
| 126 |
|
| 127 |
counter_type::reset(); |
| 128 |
|
| 129 |
v2 = std::move(v1); |
| 130 |
|
| 131 |
VERIFY( 1 == v1.get_allocator().get_personality() ); |
| 132 |
VERIFY( 2 == v2.get_allocator().get_personality() ); |
| 133 |
|
| 134 |
VERIFY( counter_type::move_count == 0 ); |
| 135 |
// (i - 1) elements in v2 destroyed, and 1 element in v1 destroyed. |
| 136 |
VERIFY( counter_type::destructor_count == i ); |
| 137 |
} |
| 138 |
|
| 139 |
// Check there's nothing left allocated or constructed. |
| 140 |
VERIFY( tracker_allocator_counter::get_construct_count() |
| 141 |
== tracker_allocator_counter::get_destruct_count() ); |
| 142 |
VERIFY( tracker_allocator_counter::get_allocation_count() |
| 143 |
== tracker_allocator_counter::get_deallocation_count() ); |
| 144 |
} |
| 145 |
|
| 146 |
int main() |
| 147 |
{ |
| 148 |
test01(); |
| 149 |
test02(); |
| 150 |
test03(); |
| 151 |
return 0; |
| 152 |
} |