(root)/
gcc-13.2.0/
libstdc++-v3/
testsuite/
util/
debug/
unordered_checks.h
       1  // Copyright (C) 2011-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  
      19  #include <testsuite_hooks.h>
      20  
      21  namespace __gnu_test
      22  {
      23    template<typename _Tp>
      24      struct CopyableValueType
      25      {
      26        typedef _Tp value_type;
      27      };
      28  
      29    template<typename _Tp1, typename _Tp2>
      30      struct CopyableValueType<std::pair<const _Tp1, _Tp2> >
      31      {
      32        typedef std::pair<_Tp1, _Tp2> value_type;
      33      };
      34  
      35    template<typename _Tp>
      36      struct generate_unique
      37      {
      38        typedef _Tp value_type;
      39  
      40        value_type build()
      41        {
      42  	static value_type _S_;
      43  	++_S_;
      44  	return _S_;
      45        }
      46      };
      47  
      48    template<typename _Tp1, typename _Tp2>
      49      struct generate_unique<std::pair<_Tp1, _Tp2> >
      50      {
      51        typedef _Tp1 first_type;
      52        typedef _Tp2 second_type;
      53        typedef std::pair<_Tp1, _Tp2> pair_type;
      54  
      55        pair_type build()
      56        {
      57  	static first_type _S_1;
      58  	static second_type _S_2;
      59  	++_S_1;
      60  	++_S_2;
      61  	return pair_type(_S_1, _S_2);
      62        }
      63      };
      64  
      65    template<typename _Tp>
      66      struct KeyExtractor
      67      {
      68        static _Tp get_key(const _Tp& val)
      69        { return val; }
      70      };
      71  
      72    template<typename _Tp1, typename _Tp2>
      73      struct KeyExtractor<std::pair<const _Tp1, _Tp2>>
      74      {
      75        static _Tp1 get_key(const std::pair<const _Tp1, _Tp2>& val)
      76        { return val.first; }
      77      };
      78  
      79    template<typename _Tp>
      80      void use_erased_local_iterator()
      81      {
      82        typedef _Tp cont_type;
      83        typedef typename cont_type::value_type cont_val_type;
      84        typedef typename CopyableValueType<cont_val_type>::value_type val_type;
      85        generate_unique<val_type> gu;
      86  
      87        cont_type c;
      88        for (size_t i = 0; i != 5; ++i)
      89  	c.insert(gu.build());
      90  
      91        typename cont_type::local_iterator it, end;
      92        for (size_t i = 0; i != c.bucket_count(); ++i)
      93        {
      94  	it = c.begin(i);
      95  	end = c.end(i);
      96  	if (it != end)
      97  	  break;
      98        }
      99        typename cont_type::key_type key = KeyExtractor<cont_val_type>::get_key(*it);
     100        c.erase(key);
     101        VERIFY( it != end );
     102    }
     103  
     104    template<typename _Tp>
     105      void use_invalid_local_iterator()
     106      {
     107        typedef _Tp cont_type;
     108        typedef typename cont_type::value_type cont_val_type;
     109        typedef typename CopyableValueType<cont_val_type>::value_type val_type;
     110        generate_unique<val_type> gu;
     111  
     112        cont_type c;
     113        for (size_t i = 0; i != 5; ++i)
     114  	c.insert(gu.build());
     115  
     116        typename cont_type::local_iterator it;
     117        for (size_t i = 0; i != c.bucket_count(); ++i)
     118        {
     119  	it = c.begin(i);
     120  	if (it != c.end(i))
     121  	  break;
     122        }
     123        cont_val_type val = *it;
     124        c.clear();
     125        VERIFY( *it == val );
     126      }
     127  
     128    template<typename _Tp>
     129      void invalid_local_iterator_pre_increment()
     130      {
     131        typedef _Tp cont_type;
     132        typedef typename cont_type::value_type cont_val_type;
     133        typedef typename CopyableValueType<cont_val_type>::value_type val_type;
     134        generate_unique<val_type> gu;
     135  
     136        cont_type c;
     137        for (size_t i = 0; i != 5; ++i)
     138  	c.insert(gu.build());
     139  
     140        auto lit = c.begin(0);
     141        for (size_t i = 0; i != 6; ++i)
     142  	++lit;
     143      }
     144  
     145    template<typename _Tp>
     146      void invalid_local_iterator_post_increment()
     147      {
     148        typedef _Tp cont_type;
     149        typedef typename cont_type::value_type cont_val_type;
     150        typedef typename CopyableValueType<cont_val_type>::value_type val_type;
     151        generate_unique<val_type> gu;
     152  
     153        cont_type c;
     154        for (size_t i = 0; i != 5; ++i)
     155  	c.insert(gu.build());
     156  
     157        auto lit = c.begin(0);
     158        for (size_t i = 0; i != 6; ++i)
     159  	lit++;
     160      }
     161  
     162    template<typename _Tp>
     163      void invalid_local_iterator_compare()
     164      {
     165        typedef _Tp cont_type;
     166        typedef typename cont_type::value_type cont_val_type;
     167        typedef typename CopyableValueType<cont_val_type>::value_type val_type;
     168        generate_unique<val_type> gu;
     169  
     170        cont_type c;
     171        for (size_t i = 0; i != 5; ++i)
     172  	c.insert(gu.build());
     173  
     174        typename cont_type::local_iterator it1, it2;
     175        size_t i;
     176        for (i = 0; i != c.bucket_count(); ++i)
     177        {
     178  	it1 = c.begin(i);
     179  	if (it1 != c.end(i))
     180  	  break;
     181        }
     182        VERIFY( i != c.bucket_count() );
     183        for (++i; i != c.bucket_count(); ++i)
     184        {
     185  	it2 = c.begin(i);
     186  	if (it2 != c.end(i))
     187  	  break;
     188        }
     189  
     190        VERIFY( it1 != it2 );
     191      }
     192  
     193    template<typename _Tp>
     194      void invalid_local_iterator_range()
     195      {
     196        typedef _Tp cont_type;
     197        typedef typename cont_type::value_type cont_val_type;
     198        typedef typename CopyableValueType<cont_val_type>::value_type val_type;
     199        generate_unique<val_type> gu;
     200  
     201        cont_type c;
     202        for (size_t i = 0; i != 5; ++i)
     203  	c.insert(gu.build());
     204  
     205        typename cont_type::local_iterator it, end;
     206        for (size_t i = 0; i != c.bucket_count(); ++i)
     207        {
     208  	it = c.begin(i);
     209  	end = c.end(i);
     210  	if (it != end)
     211  	  break;
     212        }
     213        c.insert(end, it);
     214      }
     215  }
     216