testsuite_api.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // -*- C++ -*-
  2. // Exception testing utils for the C++ library testsuite.
  3. //
  4. // Copyright (C) 2007-2022 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. #include <exception>
  22. #include <testsuite_hooks.h>
  23. #ifndef _TESTSUITE_API
  24. #define _TESTSUITE_API 1
  25. namespace __gnu_test
  26. {
  27. // Checks for virtual public derivation in exception classes.
  28. // See:
  29. // http://www.boost.org/more/error_handling.html
  30. struct bad_non_virtual : virtual public std::exception { };
  31. template<typename Exception, bool DefaultCons>
  32. struct diamond_derivation_base;
  33. template<typename Exception>
  34. struct diamond_derivation_base<Exception, true>
  35. {
  36. struct diamond_derivation_error
  37. : bad_non_virtual, Exception
  38. {
  39. diamond_derivation_error()
  40. : bad_non_virtual(), Exception() { }
  41. };
  42. };
  43. template<typename Exception>
  44. struct diamond_derivation_base<Exception, false>
  45. {
  46. struct diamond_derivation_error
  47. : bad_non_virtual, Exception
  48. {
  49. diamond_derivation_error()
  50. : bad_non_virtual(), Exception("construct diamond") { }
  51. };
  52. };
  53. template<typename Exception, bool DefaultCons>
  54. struct diamond_derivation
  55. : diamond_derivation_base<Exception, DefaultCons>
  56. {
  57. typedef diamond_derivation_base<Exception, DefaultCons> base_type;
  58. typedef typename base_type::diamond_derivation_error error_type;
  59. // NB: In the libstdc++-v3 testsuite, all the standard exception
  60. // classes (+ a couple of extensions) are checked: since they
  61. // all derive *non* virtually from std::exception, the expected
  62. // behavior is ambiguity.
  63. static void test()
  64. {
  65. try
  66. { throw error_type(); }
  67. catch (std::exception const&)
  68. { VERIFY( false ); }
  69. catch (...)
  70. { VERIFY( true ); }
  71. }
  72. };
  73. // Testing type requirements for template arguments.
  74. struct NonDefaultConstructible
  75. {
  76. NonDefaultConstructible(int) { }
  77. NonDefaultConstructible(const NonDefaultConstructible&) { }
  78. #if __cplusplus >= 201103L
  79. NonDefaultConstructible&
  80. operator=(const NonDefaultConstructible&) = default;
  81. // For std::iota.
  82. NonDefaultConstructible&
  83. operator++()
  84. { return *this; }
  85. #endif
  86. };
  87. // See: 20.1.1 Template argument requirements.
  88. inline bool
  89. operator==(const NonDefaultConstructible&, const NonDefaultConstructible&)
  90. { return false; }
  91. inline bool
  92. operator<(const NonDefaultConstructible&, const NonDefaultConstructible&)
  93. { return false; }
  94. // For 23 unordered_* requirements.
  95. struct NonDefaultConstructible_hash
  96. {
  97. std::size_t
  98. operator()(NonDefaultConstructible) const
  99. { return 1; }
  100. };
  101. // For 26 numeric algorithms requirements, need addable,
  102. // subtractable, multiplicable.
  103. inline NonDefaultConstructible
  104. operator+(const NonDefaultConstructible&, const NonDefaultConstructible&)
  105. { return NonDefaultConstructible(1); }
  106. inline NonDefaultConstructible
  107. operator-(const NonDefaultConstructible&, const NonDefaultConstructible&)
  108. { return NonDefaultConstructible(1); }
  109. inline NonDefaultConstructible
  110. operator*(const NonDefaultConstructible&, const NonDefaultConstructible&)
  111. { return NonDefaultConstructible(1); }
  112. // Like unary_function, but takes no argument. (ie, void).
  113. // Used for generator template parameter.
  114. template<typename _Result>
  115. struct void_function
  116. {
  117. typedef _Result result_type;
  118. result_type
  119. operator()() const
  120. { return result_type(); }
  121. };
  122. template<>
  123. struct void_function<NonDefaultConstructible>
  124. {
  125. typedef NonDefaultConstructible result_type;
  126. result_type
  127. operator()() const
  128. { return result_type(2); }
  129. };
  130. // For std::addressof, etc.
  131. struct OverloadedAddressAux { };
  132. struct OverloadedAddress
  133. {
  134. OverloadedAddressAux
  135. operator&() const { return OverloadedAddressAux(); }
  136. };
  137. inline bool
  138. operator<(const OverloadedAddress&, const OverloadedAddress&)
  139. { return false; }
  140. inline bool
  141. operator==(const OverloadedAddress&, const OverloadedAddress&)
  142. { return false; }
  143. struct OverloadedAddress_hash
  144. {
  145. std::size_t
  146. operator()(const OverloadedAddress&) const
  147. { return 1; }
  148. };
  149. #if __cplusplus >= 201103L
  150. struct NonCopyConstructible
  151. {
  152. NonCopyConstructible() : num(-1) { }
  153. NonCopyConstructible(NonCopyConstructible&& other)
  154. : num(other.num)
  155. { other.num = 0; }
  156. NonCopyConstructible(const NonCopyConstructible&) = delete;
  157. operator int() { return num; }
  158. private:
  159. int num;
  160. };
  161. #endif
  162. }
  163. #endif