1  // -*- C++ -*-
       2  // Testing utilities for the tr1 testsuite.
       3  //
       4  // Copyright (C) 2004-2023 Free Software Foundation, Inc.
       5  //
       6  // This file is part of the GNU ISO C++ Library.  This library is free
       7  // software; you can redistribute it and/or modify it under the
       8  // terms of the GNU General Public License as published by the
       9  // Free Software Foundation; either version 3, or (at your option)
      10  // any later version.
      11  //
      12  // This library is distributed in the hope that it will be useful,
      13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
      14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15  // GNU General Public License for more details.
      16  //
      17  // You should have received a copy of the GNU General Public License along
      18  // with this library; see the file COPYING3.  If not see
      19  // <http://www.gnu.org/licenses/>.
      20  //
      21  
      22  #ifndef _GLIBCXX_TESTSUITE_TR1_H
      23  #define _GLIBCXX_TESTSUITE_TR1_H
      24  
      25  #include <ext/type_traits.h>
      26  #include <testsuite_hooks.h>
      27  
      28  namespace __gnu_test
      29  {
      30    // For tr1/type_traits.
      31    template<template<typename> class Category, typename Type>
      32  #if __cplusplus >= 201103L
      33      constexpr
      34  #endif
      35      bool
      36      test_category(bool value)
      37      {
      38        return (Category<Type>::value == value
      39  	      && Category<const Type>::value == value
      40  	      && Category<volatile Type>::value == value
      41  	      && Category<const volatile Type>::value == value
      42  	      && Category<Type>::type::value == value
      43  	      && Category<const Type>::type::value == value
      44  	      && Category<volatile Type>::type::value == value
      45  	      && Category<const volatile Type>::type::value == value);
      46      }
      47  
      48    // For testing tr1/type_traits/extent, which has a second template
      49    // parameter.
      50    template<template<typename, unsigned> class Property,
      51  	   typename Type, unsigned Uint>
      52  #if __cplusplus >= 201103L
      53      constexpr
      54  #endif
      55      bool
      56      test_property(typename Property<Type, Uint>::value_type value)
      57      {
      58        return (Property<Type, Uint>::value == value
      59  	      && Property<Type, Uint>::type::value == value);
      60      }
      61  
      62  #if __cplusplus >= 201103L
      63    template<template<typename...> class Property,
      64  	   typename Type1, typename... Types>
      65      constexpr bool
      66      test_property(typename Property<Type1, Types...>::value_type value)
      67      {
      68        return (Property<Type1, Types...>::value == value
      69  	      && Property<Type1, Types...>::type::value == value);
      70      }
      71  #else
      72    template<template<typename> class Property, typename Type>
      73      bool
      74      test_property(typename Property<Type>::value_type value)
      75      {
      76        return (Property<Type>::value == value
      77  	      && Property<Type>::type::value == value);
      78      }
      79  #endif
      80  
      81    template<template<typename, typename> class Relationship,
      82  	   typename Type1, typename Type2>
      83  #if __cplusplus >= 201103L
      84      constexpr
      85  #endif
      86      bool
      87      test_relationship(bool value)
      88      {
      89        return (Relationship<Type1, Type2>::value == value
      90  	      && Relationship<Type1, Type2>::type::value == value);
      91      }
      92  
      93    // Test types.
      94    class ClassType { };
      95    typedef const ClassType           cClassType;
      96    typedef volatile ClassType        vClassType;
      97    typedef const volatile ClassType  cvClassType;
      98  
      99    class DerivedType : public ClassType { };
     100  
     101  #if __cplusplus >= 201103L
     102    class FinalType final : public DerivedType { };
     103  #endif
     104  
     105    enum EnumType { e0 };
     106  
     107    struct ConvType
     108    { operator int() const; };
     109  
     110    class AbstractClass
     111    {
     112      virtual void rotate(int) = 0;
     113    };
     114  
     115    class PolymorphicClass
     116    {
     117      virtual void rotate(int);
     118    };
     119  
     120    class DerivedPolymorphic : public PolymorphicClass { };
     121  
     122    class VirtualDestructorClass
     123    {
     124      virtual ~VirtualDestructorClass();
     125    };
     126  
     127    union UnionType { };
     128  
     129    union IncompleteUnion;
     130  
     131    class IncompleteClass;
     132  
     133    struct ExplicitClass
     134    {
     135      ExplicitClass(double&);
     136      explicit ExplicitClass(int&);
     137      ExplicitClass(double&, int&, double&);
     138    };
     139  
     140    struct NothrowExplicitClass
     141    {
     142      NothrowExplicitClass(double&) throw();
     143      explicit NothrowExplicitClass(int&) throw();
     144      NothrowExplicitClass(double&, int&, double&) throw();
     145    };
     146  
     147    struct ThrowExplicitClass
     148    {
     149      ThrowExplicitClass(double&) THROW(int);
     150      explicit ThrowExplicitClass(int&) THROW(int);
     151      ThrowExplicitClass(double&, int&, double&) THROW(int);
     152    };
     153  
     154    struct ThrowDefaultClass
     155    {
     156      ThrowDefaultClass() THROW(int);
     157    };
     158  
     159    struct ThrowCopyConsClass
     160    {
     161      ThrowCopyConsClass(const ThrowCopyConsClass&) THROW(int);
     162    };
     163  
     164  #if __cplusplus >= 201103L
     165    struct ThrowMoveConsClass
     166    {
     167      ThrowMoveConsClass(ThrowMoveConsClass&&) noexcept(false);
     168    };
     169  
     170    struct NoexceptExplicitClass
     171    {
     172      NoexceptExplicitClass(double&) noexcept(true);
     173      explicit NoexceptExplicitClass(int&) noexcept(true);
     174      NoexceptExplicitClass(double&, int&, double&) noexcept(true);
     175    };
     176  
     177    struct ExceptExplicitClass
     178    {
     179      ExceptExplicitClass(double&) noexcept(false);
     180      explicit ExceptExplicitClass(int&) noexcept(false);
     181      ExceptExplicitClass(double&, int&, double&) noexcept(false);
     182    };
     183  
     184    struct NoexceptDefaultClass
     185    {
     186      NoexceptDefaultClass() noexcept(true);
     187    };
     188  
     189    struct ExceptDefaultClass
     190    {
     191      ExceptDefaultClass() noexcept(false);
     192    };
     193  
     194    struct NoexceptCopyConsClass
     195    {
     196      NoexceptCopyConsClass(const NoexceptCopyConsClass&) noexcept(true);
     197    };
     198  
     199    struct ExceptCopyConsClass
     200    {
     201      ExceptCopyConsClass(const ExceptCopyConsClass&) noexcept(false);
     202    };
     203  
     204    struct NoexceptMoveConsClass
     205    {
     206      NoexceptMoveConsClass(NoexceptMoveConsClass&&) noexcept(true);
     207      NoexceptMoveConsClass& operator=(NoexceptMoveConsClass&&) = default;
     208    };
     209  
     210    struct ExceptMoveConsClass
     211    {
     212      ExceptMoveConsClass(ExceptMoveConsClass&&) noexcept(false);
     213    };
     214  
     215    struct NoexceptCopyAssignClass
     216    {
     217      NoexceptCopyAssignClass&
     218      operator=(const NoexceptCopyAssignClass&) noexcept(true);
     219    };
     220  
     221    struct ExceptCopyAssignClass
     222    {
     223      ExceptCopyAssignClass&
     224      operator=(const ExceptCopyAssignClass&) noexcept(false);
     225    };
     226  
     227    struct NoexceptMoveAssignClass
     228    {
     229      NoexceptMoveAssignClass(NoexceptMoveAssignClass&&) = default;
     230      NoexceptMoveAssignClass&
     231      operator=(NoexceptMoveAssignClass&&) noexcept(true);
     232    };
     233  
     234    struct ExceptMoveAssignClass
     235    {
     236      ExceptMoveAssignClass&
     237      operator=(ExceptMoveAssignClass&&) noexcept(false);
     238    };
     239  
     240    struct DeletedCopyAssignClass
     241    {
     242      DeletedCopyAssignClass&
     243      operator=(const DeletedCopyAssignClass&) = delete;
     244    };
     245  
     246    struct DeletedMoveAssignClass
     247    {
     248      DeletedMoveAssignClass&
     249      operator=(DeletedMoveAssignClass&&) = delete;
     250    };
     251  
     252    struct NoexceptMoveConsNoexceptMoveAssignClass
     253    {
     254      NoexceptMoveConsNoexceptMoveAssignClass
     255      (NoexceptMoveConsNoexceptMoveAssignClass&&) noexcept(true);
     256  
     257      NoexceptMoveConsNoexceptMoveAssignClass&
     258      operator=(NoexceptMoveConsNoexceptMoveAssignClass&&) noexcept(true);
     259    };
     260  
     261    struct ExceptMoveConsNoexceptMoveAssignClass
     262    {
     263      ExceptMoveConsNoexceptMoveAssignClass
     264      (ExceptMoveConsNoexceptMoveAssignClass&&) noexcept(false);
     265  
     266      ExceptMoveConsNoexceptMoveAssignClass&
     267      operator=(ExceptMoveConsNoexceptMoveAssignClass&&) noexcept(true);
     268    };
     269  
     270    struct NoexceptMoveConsExceptMoveAssignClass
     271    {
     272      NoexceptMoveConsExceptMoveAssignClass
     273      (NoexceptMoveConsExceptMoveAssignClass&&) noexcept(true);
     274  
     275      NoexceptMoveConsExceptMoveAssignClass&
     276      operator=(NoexceptMoveConsExceptMoveAssignClass&&) noexcept(false);
     277    };
     278  
     279    struct ExceptMoveConsExceptMoveAssignClass
     280    {
     281      ExceptMoveConsExceptMoveAssignClass
     282      (ExceptMoveConsExceptMoveAssignClass&&) noexcept(false);
     283  
     284      ExceptMoveConsExceptMoveAssignClass&
     285      operator=(ExceptMoveConsExceptMoveAssignClass&&) noexcept(false);
     286    };
     287  #endif
     288  
     289    struct NType   // neither trivial nor standard-layout
     290    {
     291      int i;
     292      int j;
     293      virtual ~NType();
     294    };
     295  
     296    struct TType   // trivial but not standard-layout
     297    {
     298      int i;
     299    private:
     300      int j;
     301    };
     302  
     303    struct SLType  // standard-layout but not trivial
     304    {
     305      int i;
     306      int j;
     307      ~SLType();
     308    };
     309  
     310    struct PODType // both trivial and standard-layout
     311    {
     312      int i;
     313      int j;
     314    };
     315  
     316  #if __cplusplus >= 201103L
     317    struct LType // literal type
     318    {
     319      int _M_i;
     320  
     321      constexpr LType(int __i) : _M_i(__i) { }
     322    };
     323  
     324    struct LTypeDerived : public LType
     325    {
     326      constexpr LTypeDerived(int __i) : LType(__i) { }
     327    };
     328  
     329    struct NLType // not literal type
     330    {
     331      int _M_i;
     332  
     333      NLType() : _M_i(0) { }
     334  
     335      constexpr NLType(int __i) : _M_i(__i) { }
     336  
     337      NLType(const NLType& __other) : _M_i(__other._M_i) { }
     338  
     339      ~NLType() { _M_i = 0; }
     340    };
     341  #endif
     342  
     343    int truncate_float(float x) { return (int)x; }
     344    long truncate_double(double x) { return (long)x; }
     345  
     346    struct do_truncate_float_t
     347    {
     348      do_truncate_float_t()
     349      {
     350        ++live_objects;
     351      }
     352  
     353      do_truncate_float_t(const do_truncate_float_t&)
     354      {
     355        ++live_objects;
     356      }
     357  
     358      ~do_truncate_float_t()
     359      {
     360        --live_objects;
     361      }
     362  
     363      int operator()(float x) { return (int)x; }
     364  
     365      static int live_objects;
     366    };
     367  
     368    int do_truncate_float_t::live_objects = 0;
     369  
     370    struct do_truncate_double_t
     371    {
     372      do_truncate_double_t()
     373      {
     374       ++live_objects;
     375      }
     376  
     377      do_truncate_double_t(const do_truncate_double_t&)
     378      {
     379        ++live_objects;
     380      }
     381  
     382      ~do_truncate_double_t()
     383      {
     384        --live_objects;
     385      }
     386  
     387      long operator()(double x) { return (long)x; }
     388  
     389      static int live_objects;
     390    };
     391  
     392    int do_truncate_double_t::live_objects = 0;
     393  
     394    struct X
     395    {
     396      int bar;
     397  
     398      int foo()                   { return 1; }
     399      int foo_c() const           { return 2; }
     400      int foo_v()  volatile       { return 3; }
     401      int foo_cv() const volatile { return 4; }
     402    };
     403  
     404    // For use in 8_c_compatibility.
     405    template<typename R, typename T>
     406      typename __gnu_cxx::__enable_if<std::__are_same<R, T>::__value,
     407  				    bool>::__type
     408      check_ret_type(T)
     409      { return true; }
     410  
     411  #if __cplusplus >= 201103L
     412    namespace construct
     413    {
     414      struct Empty {};
     415  
     416      struct B { int i; B(){} };
     417      struct D : B {};
     418  
     419      enum E { ee1 };
     420      enum E2 { ee2 };
     421      enum class SE { e1 };
     422      enum class SE2 { e2 };
     423  
     424      enum OpE : int;
     425      enum class OpSE : bool;
     426  
     427      union U { int i; Empty b; };
     428  
     429      struct Abstract
     430      {
     431        virtual ~Abstract() = 0;
     432      };
     433  
     434      struct AbstractDelDtor
     435      {
     436        ~AbstractDelDtor() = delete;
     437        virtual void foo() = 0;
     438      };
     439  
     440      struct Ukn;
     441  
     442      template<class To>
     443        struct ImplicitTo
     444        {
     445  	operator To();
     446        };
     447  
     448      template<class To>
     449        struct DelImplicitTo
     450        {
     451  	operator To() = delete;
     452        };
     453  
     454      template<class To>
     455        struct ExplicitTo
     456        {
     457  	explicit operator To();
     458        };
     459  
     460      struct Ellipsis
     461      {
     462        Ellipsis(...){}
     463      };
     464  
     465      struct DelEllipsis
     466      {
     467        DelEllipsis(...) = delete;
     468      };
     469  
     470      struct Any
     471      {
     472        template<class T>
     473        Any(T&&){}
     474      };
     475  
     476      struct nAny
     477      {
     478        template<class... T>
     479        nAny(T&&...){}
     480      };
     481  
     482      struct DelnAny
     483      {
     484        template<class... T>
     485          DelnAny(T&&...) = delete;
     486      };
     487  
     488      template<class... Args>
     489        struct FromArgs
     490        {
     491  	FromArgs(Args...);
     492        };
     493  
     494      struct DelDef
     495      {
     496        DelDef() = delete;
     497      };
     498  
     499      struct DelCopy
     500      {
     501        DelCopy(const DelCopy&) = delete;
     502      };
     503  
     504      struct DelDtor
     505      {
     506        DelDtor() = default;
     507        DelDtor(const DelDtor&) = default;
     508        DelDtor(DelDtor&&) = default;
     509        DelDtor(int);
     510        DelDtor(int, B, U);
     511        ~DelDtor() = delete;
     512      };
     513  
     514      struct Nontrivial
     515      {
     516        Nontrivial();
     517        Nontrivial(const Nontrivial&);
     518        Nontrivial& operator=(const Nontrivial&);
     519        ~Nontrivial();
     520      };
     521  
     522      union NontrivialUnion
     523      {
     524        int i;
     525        Nontrivial n;
     526      };
     527  
     528      struct UnusualCopy
     529      {
     530        UnusualCopy(UnusualCopy&);
     531      };
     532    }
     533  
     534    namespace destruct
     535    {
     536      struct E
     537      {};
     538  
     539      struct NTD1
     540      {
     541        ~NTD1() = default;
     542      };
     543  
     544      struct NTD2
     545      {
     546        ~NTD2();
     547      };
     548  
     549      struct NTD3
     550      {
     551        ~NTD3() throw();
     552      };
     553  
     554      struct TD1
     555      {
     556        ~TD1() noexcept(false);
     557      };
     558  
     559      struct TD2
     560      {
     561        ~TD2() THROW(int);
     562      };
     563  
     564      struct Aggr
     565      {
     566        int i;
     567        bool b;
     568        E e;
     569      };
     570  
     571      struct Aggr2
     572      {
     573        int i;
     574        bool b;
     575        TD1 r;
     576      };
     577  
     578      struct Del
     579      {
     580        ~Del() = delete;
     581      };
     582  
     583      struct Del2
     584      {
     585        ~Del2() noexcept = delete;
     586      };
     587  
     588      struct Del3
     589      {
     590        ~Del3() noexcept(false) = delete;
     591      };
     592  
     593      struct Der : Aggr
     594      {};
     595  
     596      struct Der2 : Aggr2
     597      {};
     598  
     599      union U1
     600      {
     601        int i;
     602        double d;
     603        void* p;
     604        TD1* pt;
     605      };
     606  
     607      union Ut
     608      {
     609        int i;
     610        double d;
     611        void* p;
     612        TD1 pt;
     613      };
     614  
     615      enum class En { a, b, c, d };
     616      enum En2 { En2a, En2b, En2c, En2d };
     617  
     618      enum OpE : int;
     619      enum class OpSE : bool;
     620  
     621      struct Abstract1
     622      {
     623        virtual ~Abstract1() = 0;
     624      };
     625  
     626      struct AbstractDelDtor
     627      {
     628        ~AbstractDelDtor() = delete;
     629        virtual void foo() = 0;
     630      };
     631  
     632      struct Abstract2
     633      {
     634        virtual ~Abstract2() noexcept(false) = 0;
     635      };
     636  
     637      struct Abstract3
     638      {
     639        ~Abstract3() noexcept(false);
     640        virtual void foo() noexcept = 0;
     641      };
     642  
     643      struct Nontrivial
     644      {
     645        Nontrivial();
     646        Nontrivial(const Nontrivial&);
     647        Nontrivial& operator=(const Nontrivial&);
     648        ~Nontrivial();
     649      };
     650  
     651      union NontrivialUnion
     652      {
     653        int i;
     654        Nontrivial n;
     655      };
     656  
     657      struct UnusualCopy
     658      {
     659        UnusualCopy(UnusualCopy&);
     660      };
     661  
     662      struct Ellipsis
     663      {
     664        Ellipsis(...){}
     665      };
     666  
     667      struct DelEllipsis
     668      {
     669        DelEllipsis(...) = delete;
     670      };
     671  
     672      struct DelDef
     673      {
     674        DelDef() = delete;
     675      };
     676  
     677      struct DelCopy
     678      {
     679        DelCopy(const DelCopy&) = delete;
     680      };
     681    }
     682  
     683    namespace assign
     684    {
     685      struct Empty {};
     686  
     687      struct B { int i; B(){} };
     688      struct D : B {};
     689  
     690      enum E { ee1 };
     691      enum E2 { ee2 };
     692      enum class SE { e1 };
     693      enum class SE2 { e2 };
     694  
     695      enum OpE : int;
     696      enum class OpSE : bool;
     697  
     698      union U { int i; Empty b; };
     699  
     700      union UAssignAll
     701      {
     702        bool b;
     703        char c;
     704        template<class T>
     705        void operator=(T&&);
     706      };
     707  
     708      union UDelAssignAll
     709      {
     710        bool b;
     711        char c;
     712        template<class T>
     713        void operator=(T&&) = delete;
     714      };
     715  
     716      struct Abstract
     717      {
     718        virtual ~Abstract() = 0;
     719      };
     720  
     721      struct AbstractDelDtor
     722      {
     723        ~AbstractDelDtor() = delete;
     724        virtual void foo() = 0;
     725      };
     726  
     727      struct Ukn;
     728  
     729      template<class To>
     730        struct ImplicitTo
     731        {
     732  	operator To();
     733        };
     734  
     735      template<class To>
     736        struct ExplicitTo
     737        {
     738  	explicit operator To();
     739        };
     740  
     741      template<class To>
     742        struct DelImplicitTo
     743        {
     744  	operator To() = delete;
     745        };
     746  
     747      template<class To>
     748        struct DelExplicitTo
     749        {
     750  	explicit operator To() = delete;
     751        };
     752  
     753      struct Ellipsis
     754      {
     755        Ellipsis(...){}
     756      };
     757  
     758      struct DelEllipsis
     759      {
     760        DelEllipsis(...) = delete;
     761      };
     762  
     763      struct Any
     764      {
     765        template<class T>
     766          Any(T&&){}
     767      };
     768  
     769      struct nAny
     770      {
     771        template<class... T>
     772          nAny(T&&...){}
     773      };
     774  
     775      struct DelnAny
     776      {
     777        template<class... T>
     778          DelnAny(T&&...) = delete;
     779      };
     780  
     781      template<class... Args>
     782        struct FromArgs
     783        {
     784  	FromArgs(Args...);
     785        };
     786  
     787      template<class... Args>
     788        struct DelFromArgs
     789        {
     790  	DelFromArgs(Args...) = delete;
     791        };
     792  
     793      struct DelDef
     794      {
     795        DelDef() = delete;
     796      };
     797  
     798      struct DelCopy
     799      {
     800        DelCopy(const DelCopy&) = delete;
     801      };
     802  
     803      struct DelDtor
     804      {
     805        DelDtor() = default;
     806        DelDtor(const DelDtor&) = default;
     807        DelDtor(DelDtor&&) = default;
     808        DelDtor(int);
     809        DelDtor(int, B, U);
     810        ~DelDtor() = delete;
     811      };
     812  
     813      struct Nontrivial
     814      {
     815        Nontrivial();
     816        Nontrivial(const Nontrivial&);
     817        Nontrivial& operator=(const Nontrivial&);
     818        ~Nontrivial();
     819      };
     820  
     821      union NontrivialUnion
     822      {
     823        int i;
     824        Nontrivial n;
     825      };
     826  
     827      struct UnusualCopy
     828      {
     829        UnusualCopy(UnusualCopy&);
     830      };
     831  
     832      struct AnyAssign
     833      {
     834        template<class T>
     835          void operator=(T&&);
     836      };
     837  
     838      struct DelAnyAssign
     839      {
     840        template<class T>
     841          void operator=(T&&) = delete;
     842      };
     843  
     844      struct DelCopyAssign
     845      {
     846        DelCopyAssign& operator=(const DelCopyAssign&) = delete;
     847        DelCopyAssign& operator=(DelCopyAssign&&) = default;
     848      };
     849  
     850      struct MO
     851      {
     852        MO(MO&&) = default;
     853        MO& operator=(MO&&) = default;
     854      };
     855    }
     856  
     857    struct CopyConsOnlyType
     858    {
     859      CopyConsOnlyType(int) { }
     860      CopyConsOnlyType(CopyConsOnlyType&&) = delete;
     861      CopyConsOnlyType(const CopyConsOnlyType&) = default;
     862      CopyConsOnlyType& operator=(const CopyConsOnlyType&) = delete;
     863      CopyConsOnlyType& operator=(CopyConsOnlyType&&) = delete;
     864    };
     865  
     866    struct MoveConsOnlyType
     867    {
     868      MoveConsOnlyType(int) { }
     869      MoveConsOnlyType(const MoveConsOnlyType&) = delete;
     870      MoveConsOnlyType(MoveConsOnlyType&&) = default;
     871      MoveConsOnlyType& operator=(const MoveConsOnlyType&) = delete;
     872      MoveConsOnlyType& operator=(MoveConsOnlyType&&) = delete;
     873    };
     874  #endif
     875  
     876  } // namespace __gnu_test
     877  
     878  #endif // _GLIBCXX_TESTSUITE_TR1_H