1  // Copyright (C) 2001-2023 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  // 23.2.2.3 list modifiers [lib.list.modifiers]
      19  
      20  #include <testsuite_hooks.h>
      21  
      22  // This test verifies the following.
      23  //
      24  // 23.2.2.3     void push_front(const T& x)
      25  // 23.2.2.3     void push_back(const T& x)
      26  // 23.2.2.3 (1) iterator and reference non-invalidation
      27  // 23.2.2.3 (1) exception effects
      28  // 23.2.2.3 (2) complexity requirements
      29  //
      30  // 23.2.2.3     void pop_front()
      31  // 23.2.2.3     void pop_back()
      32  // 23.2.2.3 (3) iterator and reference non-invalidation
      33  // 23.2.2.3 (5) complexity requirements
      34  //
      35  // 23.2.2       const_iterator begin() const
      36  // 23.2.2       iterator end()
      37  // 23.2.2       const_reverse_iterator rbegin() const
      38  // 23.2.2       _reference front()
      39  // 23.2.2       const_reference front() const
      40  // 23.2.2       reference back()
      41  // 23.2.2       const_reference back() const
      42  //
      43  template<typename _Tp>
      44  void
      45  modifiers3()
      46  {
      47    typedef _Tp list_type;
      48    typedef typename list_type::iterator iterator;
      49    typedef typename list_type::value_type value_type;
      50    typedef typename list_type::const_iterator const_iterator;
      51    typedef typename list_type::const_reverse_iterator const_reverse_iterator;
      52  
      53    using __gnu_test::copy_constructor;
      54    using __gnu_test::destructor;
      55  
      56    list_type list0101;
      57    const_iterator i;
      58    const_reverse_iterator j;
      59    iterator k;
      60    value_type::reset();
      61  
      62    list0101.push_back(value_type(1));     // list should be [1]
      63    VERIFY(list0101.size() == 1);
      64    VERIFY(copy_constructor::count() == 1);
      65  
      66    k = list0101.end();
      67    --k;
      68    VERIFY(k->id() == 1);
      69    VERIFY(k->id() == list0101.front().id());
      70    VERIFY(k->id() == list0101.back().id());
      71  
      72    list0101.push_front(value_type(2));    // list should be [2 1]
      73    VERIFY(list0101.size() == 2);
      74    VERIFY(copy_constructor::count() == 2);
      75    VERIFY(k->id() == 1);
      76  
      77    list0101.push_back(value_type(3));     // list should be [2 1 3]
      78    VERIFY(list0101.size() == 3);
      79    VERIFY(copy_constructor::count() == 3);
      80    VERIFY(k->id() == 1);
      81  
      82    try
      83    {
      84      list0101.push_back(value_type(4, true));
      85      VERIFY(false);
      86    }
      87    catch (...)
      88    {
      89      VERIFY(list0101.size() == 3);
      90      VERIFY(copy_constructor::count() == 4);
      91    }
      92  
      93    i = list0101.begin();
      94    VERIFY(i->id() == 2);
      95    VERIFY(i->id() == list0101.front().id());
      96  
      97    j = list0101.rbegin();
      98    VERIFY(j->id() == 3);
      99    VERIFY(j->id() == list0101.back().id());
     100  
     101    ++i;
     102    VERIFY(i->id() == 1);
     103  
     104    ++j;
     105    VERIFY(j->id() == 1);
     106  
     107    value_type::reset();
     108  
     109    list0101.pop_back();          // list should be [2 1]
     110    VERIFY(list0101.size() == 2);
     111    VERIFY(destructor::count() == 1);
     112    VERIFY(i->id() == 1);
     113    VERIFY(k->id() == 1);
     114  
     115    list0101.pop_front();          // list should be [1]
     116    VERIFY(list0101.size() == 1);
     117    VERIFY(destructor::count() == 2);
     118    VERIFY(i->id() == 1);
     119    VERIFY(k->id() == 1);
     120  }