1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/test/runtime/src/terminate_handler_test.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,67 @@
1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// Name : terminate_handler_test.cpp
1.18 +// Part of : standard c++ tests.
1.19 +//
1.20 +//
1.21 +
1.22 +#include <exception>
1.23 +#include "test_decls.h"
1.24 +#include <cstddef>
1.25 +#include <cstdlib>
1.26 +
1.27 +int glob_val = 0;
1.28 +
1.29 +class UserClass{
1.30 +public:
1.31 + ~UserClass() {
1.32 + throw int (0); // This causes std::terminate to be called since the
1.33 + // destructor is throwing while someone is unwinding the stack.
1.34 + // Since we ahve the terminate_handler set, my_handler must get called.
1.35 + }
1.36 +};
1.37 +
1.38 +
1.39 +void foo() {
1.40 + UserClass uc;
1.41 + throw int (1); // causes stack unwinding - hence calls ~UserClass()
1.42 +}
1.43 +
1.44 +
1.45 +void bar() {
1.46 + try {
1.47 + foo();
1.48 + }catch(...){}
1.49 +}
1.50 +
1.51 +void my_handler()
1.52 +{
1.53 + glob_val = 1;
1.54 + CPP_TESTS_ASSERT_ALLWAYS(1);
1.55 + abort();
1.56 +}
1.57 +
1.58 +int main()
1.59 +{
1.60 + std::terminate_handler h_old, h_new;
1.61 + h_new = (std::terminate_handler)&my_handler;
1.62 + h_old = std::set_terminate(h_new);
1.63 + h_old = h_old;
1.64 + bar();
1.65 +
1.66 + //Failed to call terminate handler
1.67 + CPP_TESTS_ASSERT_ALLWAYS(0);
1.68 +
1.69 + return 0;
1.70 +}