| 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 | // 23.3.4 multiset::swap |
| 21 | |
| 22 | #include <set> |
| 23 | #include <testsuite_hooks.h> |
| 24 | #include <testsuite_allocator.h> |
| 25 | |
| 26 | // uneq_allocator as a non-empty allocator. |
| 27 | void |
| 28 | test01() |
| 29 | { |
| 30 | using namespace std; |
| 31 | |
| 32 | typedef __gnu_test::uneq_allocator<char> my_alloc; |
| 33 | typedef multiset<char, less<char>, my_alloc> my_multiset; |
| 34 | |
| 35 | const char title01[] = "Rivers of sand"; |
| 36 | const char title02[] = "Concret PH"; |
| 37 | const char title03[] = "Sonatas and Interludes for Prepared Piano"; |
| 38 | const char title04[] = "never as tired as when i'm waking up"; |
| 39 | |
| 40 | const size_t N1 = sizeof(title01); |
| 41 | const size_t N2 = sizeof(title02); |
| 42 | const size_t N3 = sizeof(title03); |
| 43 | const size_t N4 = sizeof(title04); |
| 44 | |
| 45 | const multiset<char> mset01_ref(title01, title01 + N1); |
| 46 | const multiset<char> mset02_ref(title02, title02 + N2); |
| 47 | const multiset<char> mset03_ref(title03, title03 + N3); |
| 48 | const multiset<char> mset04_ref(title04, title04 + N4); |
| 49 | |
| 50 | my_multiset::size_type size01, size02; |
| 51 | |
| 52 | my_alloc alloc01(1); |
| 53 | |
| 54 | my_multiset mset01(less<char>(), alloc01); |
| 55 | size01 = mset01.size(); |
| 56 | my_multiset mset02(less<char>(), alloc01); |
| 57 | size02 = mset02.size(); |
| 58 | |
| 59 | mset01.swap(mset02); |
| 60 | VERIFY( mset01.size() == size02 ); |
| 61 | VERIFY( mset01.empty() ); |
| 62 | VERIFY( mset02.size() == size01 ); |
| 63 | VERIFY( mset02.empty() ); |
| 64 | |
| 65 | my_multiset mset03(less<char>(), alloc01); |
| 66 | size01 = mset03.size(); |
| 67 | my_multiset mset04(title02, title02 + N2, less<char>(), alloc01); |
| 68 | size02 = mset04.size(); |
| 69 | |
| 70 | mset03.swap(mset04); |
| 71 | VERIFY( mset03.size() == size02 ); |
| 72 | VERIFY( equal(mset03.begin(), mset03.end(), mset02_ref.begin()) ); |
| 73 | VERIFY( mset04.size() == size01 ); |
| 74 | VERIFY( mset04.empty() ); |
| 75 | |
| 76 | my_multiset mset05(title01, title01 + N1, less<char>(), alloc01); |
| 77 | size01 = mset05.size(); |
| 78 | my_multiset mset06(title02, title02 + N2, less<char>(), alloc01); |
| 79 | size02 = mset06.size(); |
| 80 | |
| 81 | mset05.swap(mset06); |
| 82 | VERIFY( mset05.size() == size02 ); |
| 83 | VERIFY( equal(mset05.begin(), mset05.end(), mset02_ref.begin()) ); |
| 84 | VERIFY( mset06.size() == size01 ); |
| 85 | VERIFY( equal(mset06.begin(), mset06.end(), mset01_ref.begin()) ); |
| 86 | |
| 87 | my_multiset mset07(title01, title01 + N1, less<char>(), alloc01); |
| 88 | size01 = mset07.size(); |
| 89 | my_multiset mset08(title03, title03 + N3, less<char>(), alloc01); |
| 90 | size02 = mset08.size(); |
| 91 | |
| 92 | mset07.swap(mset08); |
| 93 | VERIFY( mset07.size() == size02 ); |
| 94 | VERIFY( equal(mset07.begin(), mset07.end(), mset03_ref.begin()) ); |
| 95 | VERIFY( mset08.size() == size01 ); |
| 96 | VERIFY( equal(mset08.begin(), mset08.end(), mset01_ref.begin()) ); |
| 97 | |
| 98 | my_multiset mset09(title03, title03 + N3, less<char>(), alloc01); |
| 99 | size01 = mset09.size(); |
| 100 | my_multiset mset10(title04, title04 + N4, less<char>(), alloc01); |
| 101 | size02 = mset10.size(); |
| 102 | |
| 103 | mset09.swap(mset10); |
| 104 | VERIFY( mset09.size() == size02 ); |
| 105 | VERIFY( equal(mset09.begin(), mset09.end(), mset04_ref.begin()) ); |
| 106 | VERIFY( mset10.size() == size01 ); |
| 107 | VERIFY( equal(mset10.begin(), mset10.end(), mset03_ref.begin()) ); |
| 108 | |
| 109 | my_multiset mset11(title04, title04 + N4, less<char>(), alloc01); |
| 110 | size01 = mset11.size(); |
| 111 | my_multiset mset12(title01, title01 + N1, less<char>(), alloc01); |
| 112 | size02 = mset12.size(); |
| 113 | |
| 114 | mset11.swap(mset12); |
| 115 | VERIFY( mset11.size() == size02 ); |
| 116 | VERIFY( equal(mset11.begin(), mset11.end(), mset01_ref.begin()) ); |
| 117 | VERIFY( mset12.size() == size01 ); |
| 118 | VERIFY( equal(mset12.begin(), mset12.end(), mset04_ref.begin()) ); |
| 119 | |
| 120 | my_multiset mset13(title03, title03 + N3, less<char>(), alloc01); |
| 121 | size01 = mset13.size(); |
| 122 | my_multiset mset14(title03, title03 + N3, less<char>(), alloc01); |
| 123 | size02 = mset14.size(); |
| 124 | |
| 125 | mset13.swap(mset14); |
| 126 | VERIFY( mset13.size() == size02 ); |
| 127 | VERIFY( equal(mset13.begin(), mset13.end(), mset03_ref.begin()) ); |
| 128 | VERIFY( mset14.size() == size01 ); |
| 129 | VERIFY( equal(mset14.begin(), mset14.end(), mset03_ref.begin()) ); |
| 130 | } |
| 131 | |
| 132 | int main() |
| 133 | { |
| 134 | test01(); |
| 135 | return 0; |
| 136 | } |