| 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.2.4.3 vector::swap |
| 21 | |
| 22 | #include <vector> |
| 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 vector<char, my_alloc> my_vector; |
| 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 | my_vector::size_type size01, size02; |
| 46 | |
| 47 | my_alloc alloc01(1); |
| 48 | |
| 49 | my_vector vec01(alloc01); |
| 50 | size01 = vec01.size(); |
| 51 | my_vector vec02(alloc01); |
| 52 | size02 = vec02.size(); |
| 53 | |
| 54 | vec01.swap(vec02); |
| 55 | VERIFY( vec01.size() == size02 ); |
| 56 | VERIFY( vec01.empty() ); |
| 57 | VERIFY( vec02.size() == size01 ); |
| 58 | VERIFY( vec02.empty() ); |
| 59 | |
| 60 | my_vector vec03(alloc01); |
| 61 | size01 = vec03.size(); |
| 62 | my_vector vec04(title02, title02 + N2, alloc01); |
| 63 | size02 = vec04.size(); |
| 64 | |
| 65 | vec03.swap(vec04); |
| 66 | VERIFY( vec03.size() == size02 ); |
| 67 | VERIFY( equal(vec03.begin(), vec03.end(), title02) ); |
| 68 | VERIFY( vec04.size() == size01 ); |
| 69 | VERIFY( vec04.empty() ); |
| 70 | |
| 71 | my_vector vec05(title01, title01 + N1, alloc01); |
| 72 | size01 = vec05.size(); |
| 73 | my_vector vec06(title02, title02 + N2, alloc01); |
| 74 | size02 = vec06.size(); |
| 75 | |
| 76 | vec05.swap(vec06); |
| 77 | VERIFY( vec05.size() == size02 ); |
| 78 | VERIFY( equal(vec05.begin(), vec05.end(), title02) ); |
| 79 | VERIFY( vec06.size() == size01 ); |
| 80 | VERIFY( equal(vec06.begin(), vec06.end(), title01) ); |
| 81 | |
| 82 | my_vector vec07(title01, title01 + N1, alloc01); |
| 83 | size01 = vec07.size(); |
| 84 | my_vector vec08(title03, title03 + N3, alloc01); |
| 85 | size02 = vec08.size(); |
| 86 | |
| 87 | vec07.swap(vec08); |
| 88 | VERIFY( vec07.size() == size02 ); |
| 89 | VERIFY( equal(vec07.begin(), vec07.end(), title03) ); |
| 90 | VERIFY( vec08.size() == size01 ); |
| 91 | VERIFY( equal(vec08.begin(), vec08.end(), title01) ); |
| 92 | |
| 93 | my_vector vec09(title03, title03 + N3, alloc01); |
| 94 | size01 = vec09.size(); |
| 95 | my_vector vec10(title04, title04 + N4, alloc01); |
| 96 | size02 = vec10.size(); |
| 97 | |
| 98 | vec09.swap(vec10); |
| 99 | VERIFY( vec09.size() == size02 ); |
| 100 | VERIFY( equal(vec09.begin(), vec09.end(), title04) ); |
| 101 | VERIFY( vec10.size() == size01 ); |
| 102 | VERIFY( equal(vec10.begin(), vec10.end(), title03) ); |
| 103 | |
| 104 | my_vector vec11(title04, title04 + N4, alloc01); |
| 105 | size01 = vec11.size(); |
| 106 | my_vector vec12(title01, title01 + N1, alloc01); |
| 107 | size02 = vec12.size(); |
| 108 | |
| 109 | vec11.swap(vec12); |
| 110 | VERIFY( vec11.size() == size02 ); |
| 111 | VERIFY( equal(vec11.begin(), vec11.end(), title01) ); |
| 112 | VERIFY( vec12.size() == size01 ); |
| 113 | VERIFY( equal(vec12.begin(), vec12.end(), title04) ); |
| 114 | |
| 115 | my_vector vec13(title03, title03 + N3, alloc01); |
| 116 | size01 = vec13.size(); |
| 117 | my_vector vec14(title03, title03 + N3, alloc01); |
| 118 | size02 = vec14.size(); |
| 119 | |
| 120 | vec13.swap(vec14); |
| 121 | VERIFY( vec13.size() == size02 ); |
| 122 | VERIFY( equal(vec13.begin(), vec13.end(), title03) ); |
| 123 | VERIFY( vec14.size() == size01 ); |
| 124 | VERIFY( equal(vec14.begin(), vec14.end(), title03) ); |
| 125 | } |
| 126 | |
| 127 | int main() |
| 128 | { |
| 129 | test01(); |
| 130 | return 0; |
| 131 | } |