set_terminate.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright (C) 2019-2022 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. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. // You should have received a copy of the GNU General Public License along
  13. // with this library; see the file COPYING3. If not see
  14. // <http://www.gnu.org/licenses/>.
  15. // { dg-options "-std=gnu++11" }
  16. // { dg-do run { target c++11 } }
  17. #include <exception>
  18. #include <testsuite_hooks.h>
  19. void term_handler() { __builtin_abort(); }
  20. void
  21. test01()
  22. {
  23. const std::terminate_handler orig = std::get_terminate();
  24. VERIFY( orig != 0 ); // GNU-specific behaviour
  25. std::terminate_handler prev = std::set_terminate(term_handler);
  26. VERIFY( std::get_terminate() == term_handler );
  27. VERIFY( prev == orig );
  28. prev = std::set_terminate(orig);
  29. VERIFY( std::get_terminate() == orig );
  30. VERIFY( prev == term_handler );
  31. }
  32. void
  33. test02()
  34. {
  35. // PR libstdc++/90682
  36. std::set_terminate(0); // Undefined in C++98, unspecified in C++11 and later
  37. const std::terminate_handler dfault = std::get_terminate();
  38. VERIFY( dfault != 0 ); // GNU-specific behaviour
  39. const std::terminate_handler prev = std::set_terminate(0);
  40. VERIFY( prev == dfault );
  41. }
  42. int
  43. main()
  44. {
  45. test01();
  46. test02();
  47. }