(root)/
gcc-13.2.0/
libstdc++-v3/
testsuite/
23_containers/
list/
modifiers/
swap/
3.h
       1  // 2005-12-20  Paolo Carlini  <pcarlini@suse.de>
       2  
       3  // Copyright (C) 2005-2023 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.2.3 list::swap
      21  
      22  #include <testsuite_hooks.h>
      23  #include <testsuite_allocator.h>
      24  
      25  // uneq_allocator, two different personalities.
      26  template<typename _Tp>
      27  void
      28  swap3()
      29  {
      30    using namespace std;
      31  
      32    typedef _Tp list_type;
      33    typedef typename list_type::allocator_type allocator_type;
      34    typedef typename list_type::size_type size_type;
      35  
      36    const char title01[] = "Rivers of sand";
      37    const char title02[] = "Concret PH";
      38    const char title03[] = "Sonatas and Interludes for Prepared Piano";
      39    const char title04[] = "never as tired as when i'm waking up";
      40  
      41    const size_t N1 = sizeof(title01);
      42    const size_t N2 = sizeof(title02);
      43    const size_t N3 = sizeof(title03);
      44    const size_t N4 = sizeof(title04);
      45  
      46    size_type size01, size02;
      47  
      48    allocator_type alloc01(1), alloc02(2);
      49    int personality01, personality02;
      50  
      51    list_type lis01(alloc01);
      52    size01 = lis01.size();
      53    personality01 = lis01.get_allocator().get_personality();
      54    list_type lis02(alloc02);
      55    size02 = lis02.size();
      56    personality02 = lis02.get_allocator().get_personality();
      57  
      58    lis01.swap(lis02);
      59    VERIFY( lis01.size() == size02 );
      60    VERIFY( lis01.empty() );
      61    VERIFY( lis02.size() == size01 );
      62    VERIFY( lis02.empty() );
      63    VERIFY( lis01.get_allocator().get_personality() == personality02 );
      64    VERIFY( lis02.get_allocator().get_personality() == personality01 );
      65  
      66    list_type lis03(alloc02);
      67    size01 = lis03.size();
      68    personality01 = lis03.get_allocator().get_personality();
      69    list_type lis04(title02, title02 + N2, alloc01);
      70    size02 = lis04.size();
      71    personality02 = lis04.get_allocator().get_personality();
      72  
      73    lis03.swap(lis04);
      74    VERIFY( lis03.size() == size02 );
      75    VERIFY( equal(lis03.begin(), lis03.end(), title02) );
      76    VERIFY( lis04.size() == size01 );
      77    VERIFY( lis04.empty() );
      78    VERIFY( lis03.get_allocator().get_personality() == personality02 );
      79    VERIFY( lis04.get_allocator().get_personality() == personality01 );
      80    
      81    list_type lis05(title01, title01 + N1, alloc01);
      82    size01 = lis05.size();
      83    personality01 = lis05.get_allocator().get_personality();
      84    list_type lis06(title02, title02 + N2, alloc02);
      85    size02 = lis06.size();
      86    personality02 = lis06.get_allocator().get_personality();
      87  
      88    lis05.swap(lis06);
      89    VERIFY( lis05.size() == size02 );
      90    VERIFY( equal(lis05.begin(), lis05.end(), title02) );
      91    VERIFY( lis06.size() == size01 );
      92    VERIFY( equal(lis06.begin(), lis06.end(), title01) );
      93    VERIFY( lis05.get_allocator().get_personality() == personality02 );
      94    VERIFY( lis06.get_allocator().get_personality() == personality01 );
      95  
      96    list_type lis07(title01, title01 + N1, alloc02);
      97    size01 = lis07.size();
      98    personality01 = lis07.get_allocator().get_personality();
      99    list_type lis08(title03, title03 + N3, alloc01);
     100    size02 = lis08.size();
     101    personality02 = lis08.get_allocator().get_personality();
     102  
     103    lis07.swap(lis08);
     104    VERIFY( lis07.size() == size02 );
     105    VERIFY( equal(lis07.begin(), lis07.end(), title03) );
     106    VERIFY( lis08.size() == size01 );
     107    VERIFY( equal(lis08.begin(), lis08.end(), title01) );
     108    VERIFY( lis07.get_allocator().get_personality() == personality02 );
     109    VERIFY( lis08.get_allocator().get_personality() == personality01 );
     110  
     111    list_type lis09(title03, title03 + N3, alloc01);
     112    size01 = lis09.size();
     113    personality01 = lis09.get_allocator().get_personality();
     114    list_type lis10(title04, title04 + N4, alloc02);
     115    size02 = lis10.size();
     116    personality02 = lis10.get_allocator().get_personality();
     117  
     118    lis09.swap(lis10);
     119    VERIFY( lis09.size() == size02 );
     120    VERIFY( equal(lis09.begin(), lis09.end(), title04) );
     121    VERIFY( lis10.size() == size01 );
     122    VERIFY( equal(lis10.begin(), lis10.end(), title03) );
     123    VERIFY( lis09.get_allocator().get_personality() == personality02 );
     124    VERIFY( lis10.get_allocator().get_personality() == personality01 );
     125  
     126    list_type lis11(title04, title04 + N4, alloc02);
     127    size01 = lis11.size();
     128    personality01 = lis11.get_allocator().get_personality();
     129    list_type lis12(title01, title01 + N1, alloc01);
     130    size02 = lis12.size();
     131    personality02 = lis12.get_allocator().get_personality();
     132  
     133    lis11.swap(lis12);
     134    VERIFY( lis11.size() == size02 );
     135    VERIFY( equal(lis11.begin(), lis11.end(), title01) );
     136    VERIFY( lis12.size() == size01 );
     137    VERIFY( equal(lis12.begin(), lis12.end(), title04) );
     138    VERIFY( lis11.get_allocator().get_personality() == personality02 );
     139    VERIFY( lis12.get_allocator().get_personality() == personality01 );
     140  
     141    list_type lis13(title03, title03 + N3, alloc01);
     142    size01 = lis13.size();
     143    personality01 = lis13.get_allocator().get_personality();
     144    list_type lis14(title03, title03 + N3, alloc02);
     145    size02 = lis14.size();
     146    personality02 = lis14.get_allocator().get_personality();
     147  
     148    lis13.swap(lis14);
     149    VERIFY( lis13.size() == size02 );
     150    VERIFY( equal(lis13.begin(), lis13.end(), title03) );
     151    VERIFY( lis14.size() == size01 );
     152    VERIFY( equal(lis14.begin(), lis14.end(), title03) );
     153    VERIFY( lis13.get_allocator().get_personality() == personality02 );
     154    VERIFY( lis14.get_allocator().get_personality() == personality01 );
     155  }