| 1 | // 2005-12-20 Paolo Carlini <[email protected]> |
| 2 | |
| 3 | // Copyright (C) 2005-2025 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. |
| 10 | |
| 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. |
| 15 | |
| 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/>. |
| 19 | |
| 20 | // 6.3.4.4 unordered_map::swap |
| 21 | |
| 22 | #include <tr1/unordered_map> |
| 23 | #include <map> |
| 24 | #include <testsuite_hooks.h> |
| 25 | #include <testsuite_allocator.h> |
| 26 | |
| 27 | // uneq_allocator as a non-empty allocator. |
| 28 | void |
| 29 | test01() |
| 30 | { |
| 31 | using namespace std::tr1; |
| 32 | using std::pair; |
| 33 | using std::equal_to; |
| 34 | using std::map; |
| 35 | |
| 36 | typedef pair<const char, int> my_pair; |
| 37 | typedef __gnu_test::uneq_allocator<my_pair> my_alloc; |
| 38 | typedef unordered_map<char, int, hash<char>, equal_to<char>, my_alloc> |
| 39 | my_umap; |
| 40 | |
| 41 | const char title01[] = "Rivers of sand"; |
| 42 | const char title02[] = "Concret PH"; |
| 43 | const char title03[] = "Sonatas and Interludes for Prepared Piano"; |
| 44 | const char title04[] = "never as tired as when i'm waking up"; |
| 45 | |
| 46 | const size_t N1 = sizeof(title01); |
| 47 | const size_t N2 = sizeof(title02); |
| 48 | const size_t N3 = sizeof(title03); |
| 49 | const size_t N4 = sizeof(title04); |
| 50 | |
| 51 | typedef map<char, int> my_map; |
| 52 | my_map map01_ref; |
| 53 | for (size_t i = 0; i < N1; ++i) |
| 54 | map01_ref.insert(my_pair(title01[i], i)); |
| 55 | my_map map02_ref; |
| 56 | for (size_t i = 0; i < N2; ++i) |
| 57 | map02_ref.insert(my_pair(title02[i], i)); |
| 58 | my_map map03_ref; |
| 59 | for (size_t i = 0; i < N3; ++i) |
| 60 | map03_ref.insert(my_pair(title03[i], i)); |
| 61 | my_map map04_ref; |
| 62 | for (size_t i = 0; i < N4; ++i) |
| 63 | map04_ref.insert(my_pair(title04[i], i)); |
| 64 | |
| 65 | my_umap::size_type size01, size02; |
| 66 | |
| 67 | my_alloc alloc01(1); |
| 68 | |
| 69 | my_umap umap01(10, hash<char>(), equal_to<char>(), alloc01); |
| 70 | size01 = umap01.size(); |
| 71 | my_umap umap02(10, hash<char>(), equal_to<char>(), alloc01); |
| 72 | size02 = umap02.size(); |
| 73 | |
| 74 | umap01.swap(umap02); |
| 75 | VERIFY( umap01.size() == size02 ); |
| 76 | VERIFY( umap01.empty() ); |
| 77 | VERIFY( umap02.size() == size01 ); |
| 78 | VERIFY( umap02.empty() ); |
| 79 | |
| 80 | my_umap umap03(10, hash<char>(), equal_to<char>(), alloc01); |
| 81 | size01 = umap03.size(); |
| 82 | my_umap umap04(map02_ref.begin(), map02_ref.end(), 10, hash<char>(), |
| 83 | equal_to<char>(), alloc01); |
| 84 | size02 = umap04.size(); |
| 85 | |
| 86 | umap03.swap(umap04); |
| 87 | VERIFY( umap03.size() == size02 ); |
| 88 | VERIFY( my_map(umap03.begin(), umap03.end()) == map02_ref ); |
| 89 | VERIFY( umap04.size() == size01 ); |
| 90 | VERIFY( umap04.empty() ); |
| 91 | |
| 92 | my_umap umap05(map01_ref.begin(), map01_ref.end(), 10, hash<char>(), |
| 93 | equal_to<char>(), alloc01); |
| 94 | size01 = umap05.size(); |
| 95 | my_umap umap06(map02_ref.begin(), map02_ref.end(), 10, hash<char>(), |
| 96 | equal_to<char>(), alloc01); |
| 97 | size02 = umap06.size(); |
| 98 | |
| 99 | umap05.swap(umap06); |
| 100 | VERIFY( umap05.size() == size02 ); |
| 101 | VERIFY( my_map(umap05.begin(), umap05.end()) == map02_ref ); |
| 102 | VERIFY( umap06.size() == size01 ); |
| 103 | VERIFY( my_map(umap06.begin(), umap06.end()) == map01_ref ); |
| 104 | |
| 105 | my_umap umap07(map01_ref.begin(), map01_ref.end(), 10, hash<char>(), |
| 106 | equal_to<char>(), alloc01); |
| 107 | size01 = umap07.size(); |
| 108 | my_umap umap08(map03_ref.begin(), map03_ref.end(), 10, hash<char>(), |
| 109 | equal_to<char>(), alloc01); |
| 110 | size02 = umap08.size(); |
| 111 | |
| 112 | umap07.swap(umap08); |
| 113 | VERIFY( umap07.size() == size02 ); |
| 114 | VERIFY( my_map(umap07.begin(), umap07.end()) == map03_ref ); |
| 115 | VERIFY( umap08.size() == size01 ); |
| 116 | VERIFY( my_map(umap08.begin(), umap08.end()) == map01_ref ); |
| 117 | |
| 118 | my_umap umap09(map03_ref.begin(), map03_ref.end(), 10, hash<char>(), |
| 119 | equal_to<char>(), alloc01); |
| 120 | size01 = umap09.size(); |
| 121 | my_umap umap10(map04_ref.begin(), map04_ref.end(), 10, hash<char>(), |
| 122 | equal_to<char>(), alloc01); |
| 123 | size02 = umap10.size(); |
| 124 | |
| 125 | umap09.swap(umap10); |
| 126 | VERIFY( umap09.size() == size02 ); |
| 127 | VERIFY( my_map(umap09.begin(), umap09.end()) == map04_ref ); |
| 128 | VERIFY( umap10.size() == size01 ); |
| 129 | VERIFY( my_map(umap10.begin(), umap10.end()) == map03_ref ); |
| 130 | |
| 131 | my_umap umap11(map04_ref.begin(), map04_ref.end(), 10, hash<char>(), |
| 132 | equal_to<char>(), alloc01); |
| 133 | size01 = umap11.size(); |
| 134 | my_umap umap12(map01_ref.begin(), map01_ref.end(), 10, hash<char>(), |
| 135 | equal_to<char>(), alloc01); |
| 136 | size02 = umap12.size(); |
| 137 | |
| 138 | umap11.swap(umap12); |
| 139 | VERIFY( umap11.size() == size02 ); |
| 140 | VERIFY( my_map(umap11.begin(), umap11.end()) == map01_ref ); |
| 141 | VERIFY( umap12.size() == size01 ); |
| 142 | VERIFY( my_map(umap12.begin(), umap12.end()) == map04_ref ); |
| 143 | |
| 144 | my_umap umap13(map03_ref.begin(), map03_ref.end(), 10, hash<char>(), |
| 145 | equal_to<char>(), alloc01); |
| 146 | size01 = umap13.size(); |
| 147 | my_umap umap14(map03_ref.begin(), map03_ref.end(), 10, hash<char>(), |
| 148 | equal_to<char>(), alloc01); |
| 149 | size02 = umap14.size(); |
| 150 | |
| 151 | umap13.swap(umap14); |
| 152 | VERIFY( umap13.size() == size02 ); |
| 153 | VERIFY( my_map(umap13.begin(), umap13.end()) == map03_ref ); |
| 154 | VERIFY( umap14.size() == size01 ); |
| 155 | VERIFY( my_map(umap14.begin(), umap14.end()) == map03_ref ); |
| 156 | } |
| 157 | |
| 158 | int main() |
| 159 | { |
| 160 | test01(); |
| 161 | return 0; |
| 162 | } |