xref: /llvm-project/libcxx/test/std/depr/exception.unexpected/set.unexpected/get_unexpected.pass.cpp (revision 31cbe0f240f660f15602c96b787c58a26f17e179)
15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
9*31cbe0f2SLouis Dionne // REQUIRES: c++03 || c++11 || c++14
102a1bfa98SEric Fiselier 
115a83710eSEric Fiselier // test get_unexpected
125a83710eSEric Fiselier 
135a83710eSEric Fiselier #include <exception>
145a83710eSEric Fiselier #include <cassert>
155a83710eSEric Fiselier #include <cstdlib>
165a83710eSEric Fiselier 
177fc6a556SMarshall Clow #include "test_macros.h"
187fc6a556SMarshall Clow 
f1()195a83710eSEric Fiselier void f1() {}
f2()205a83710eSEric Fiselier void f2() {}
215a83710eSEric Fiselier 
f3()225a83710eSEric Fiselier void f3()
235a83710eSEric Fiselier {
245a83710eSEric Fiselier     std::exit(0);
255a83710eSEric Fiselier }
265a83710eSEric Fiselier 
main(int,char **)272df59c50SJF Bastien int main(int, char**)
285a83710eSEric Fiselier {
295a83710eSEric Fiselier 
305a83710eSEric Fiselier     std::unexpected_handler old = std::get_unexpected();
315a83710eSEric Fiselier     // verify there is a previous unexpected handler
325a83710eSEric Fiselier     assert(old);
335a83710eSEric Fiselier     std::set_unexpected(f1);
345a83710eSEric Fiselier     assert(std::get_unexpected() == f1);
355a83710eSEric Fiselier     // verify f1 was replace with f2
365a83710eSEric Fiselier     std::set_unexpected(f2);
375a83710eSEric Fiselier     assert(std::get_unexpected() == f2);
385a83710eSEric Fiselier     // verify calling original unexpected handler calls terminate
395a83710eSEric Fiselier     std::set_terminate(f3);
405a83710eSEric Fiselier     (*old)();
415a83710eSEric Fiselier     assert(0);
422df59c50SJF Bastien 
432df59c50SJF Bastien   return 0;
445a83710eSEric Fiselier }
45