diff options
Diffstat (limited to 'libs/scope_exit/test/native.cpp')
-rw-r--r-- | libs/scope_exit/test/native.cpp | 208 |
1 files changed, 115 insertions, 93 deletions
diff --git a/libs/scope_exit/test/native.cpp b/libs/scope_exit/test/native.cpp index 629e0073aa..133060eb75 100644 --- a/libs/scope_exit/test/native.cpp +++ b/libs/scope_exit/test/native.cpp @@ -1,135 +1,121 @@ -// Copyright Alexander Nasonov 2007-2008 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -#include <iostream> -#include <ostream> -#include <string> +// Copyright (C) 2006-2009, 2012 Alexander Nasonov +// Copyright (C) 2012 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 +// (see accompanying file LICENSE_1_0.txt or a copy at +// http://www.boost.org/LICENSE_1_0.txt) +// Home at http://www.boost.org/libs/scope_exit #include <boost/scope_exit.hpp> - +#include <boost/config.hpp> #include <boost/typeof/typeof.hpp> #include <boost/typeof/std/string.hpp> -#include <boost/test/unit_test.hpp> - -using namespace boost::unit_test; +#include <boost/detail/lightweight_test.hpp> +#include <iostream> +#include <ostream> +#include <string> std::string g_str; template<int Dummy = 0> -struct Holder -{ +struct Holder { static long g_long; }; template<int Dummy> long Holder<Dummy>::g_long; -void test_non_local() -{ +void test_non_local(void) { // ... and one local variable as well: int i = 0; - BOOST_SCOPE_EXIT( (i) ) - { - BOOST_CHECK(i == 0); - BOOST_CHECK(Holder<>::g_long == 3); - BOOST_CHECK(g_str == "try: g_str"); + BOOST_SCOPE_EXIT(void) { + BOOST_TEST(Holder<>::g_long == 3); } BOOST_SCOPE_EXIT_END - BOOST_SCOPE_EXIT( (&i) ) - { - BOOST_CHECK(i == 3); - BOOST_CHECK(Holder<>::g_long == 3); - BOOST_CHECK(g_str == "try: g_str"); + BOOST_SCOPE_EXIT( (i) ) { + BOOST_TEST(i == 0); + BOOST_TEST(Holder<>::g_long == 3); + BOOST_TEST(g_str == "try: g_str"); + } BOOST_SCOPE_EXIT_END + + BOOST_SCOPE_EXIT( (&i) ) { + BOOST_TEST(i == 3); + BOOST_TEST(Holder<>::g_long == 3); + BOOST_TEST(g_str == "try: g_str"); } BOOST_SCOPE_EXIT_END { g_str = ""; Holder<>::g_long = 1; - BOOST_SCOPE_EXIT( (&i) ) - { + BOOST_SCOPE_EXIT( (&i) ) { i = 1; g_str = "g_str"; } BOOST_SCOPE_EXIT_END - BOOST_SCOPE_EXIT( (&i) ) - { - try - { - i = 2; - Holder<>::g_long = 2; - throw 0; - } catch(...) {} + BOOST_SCOPE_EXIT( (&i) ) { + try { + i = 2; + Holder<>::g_long = 2; + throw 0; + } catch(...) {} } BOOST_SCOPE_EXIT_END - BOOST_CHECK(i == 0); - BOOST_CHECK(g_str == ""); - BOOST_CHECK(Holder<>::g_long == 1); + BOOST_TEST(i == 0); + BOOST_TEST(g_str == ""); + BOOST_TEST(Holder<>::g_long == 1); } - BOOST_CHECK(Holder<>::g_long == 2); - BOOST_CHECK(g_str == "g_str"); - BOOST_CHECK(i == 1); // Check that first declared is executed last + BOOST_TEST(Holder<>::g_long == 2); + BOOST_TEST(g_str == "g_str"); + BOOST_TEST(i == 1); // Check that first declared is executed last. - BOOST_SCOPE_EXIT( (&i) ) - { - BOOST_CHECK(i == 3); - BOOST_CHECK(Holder<>::g_long == 3); - BOOST_CHECK(g_str == "try: g_str"); + BOOST_SCOPE_EXIT( (&i) ) { + BOOST_TEST(i == 3); + BOOST_TEST(Holder<>::g_long == 3); + BOOST_TEST(g_str == "try: g_str"); } BOOST_SCOPE_EXIT_END - BOOST_SCOPE_EXIT( (i) ) - { - BOOST_CHECK(i == 1); - BOOST_CHECK(Holder<>::g_long == 3); - BOOST_CHECK(g_str == "try: g_str"); + BOOST_SCOPE_EXIT( (i) ) { + BOOST_TEST(i == 1); + BOOST_TEST(Holder<>::g_long == 3); + BOOST_TEST(g_str == "try: g_str"); } BOOST_SCOPE_EXIT_END - try - { - BOOST_SCOPE_EXIT( (&i) ) - { + try { + BOOST_SCOPE_EXIT( (&i) ) { i = 3; g_str = "try: g_str"; } BOOST_SCOPE_EXIT_END - BOOST_SCOPE_EXIT( (&i) ) - { + BOOST_SCOPE_EXIT( (&i) ) { i = 4; Holder<>::g_long = 3; } BOOST_SCOPE_EXIT_END - BOOST_CHECK(i == 1); - BOOST_CHECK(g_str == "g_str"); - BOOST_CHECK(Holder<>::g_long == 2); + BOOST_TEST(i == 1); + BOOST_TEST(g_str == "g_str"); + BOOST_TEST(Holder<>::g_long == 2); throw 0; - } - catch(int) - { - BOOST_CHECK(Holder<>::g_long == 3); - BOOST_CHECK(g_str == "try: g_str"); - BOOST_CHECK(i == 3); // Check that first declared is executed last + } catch(int) { + BOOST_TEST(Holder<>::g_long == 3); + BOOST_TEST(g_str == "try: g_str"); + BOOST_TEST(i == 3); // Check that first declared is executed last. } } -bool foo() -{ - return true; -} +bool foo(void) { return true; } + +bool foo2(void) { return false; } -void test_types() -{ - bool (*pf)() = 0; - bool (&rf)() = foo; +void test_types(void) { + bool (*pf)(void) = 0; + bool (&rf)(void) = foo; bool results[2] = {}; { - BOOST_SCOPE_EXIT( (&results)(&pf)(&rf) ) - { + BOOST_SCOPE_EXIT( (&results) (&pf) (&rf) ) { results[0] = pf(); results[1] = rf(); } @@ -137,36 +123,72 @@ void test_types() pf = &foo; - BOOST_CHECK(results[0] == false); - BOOST_CHECK(results[1] == false); + BOOST_TEST(results[0] == false); + BOOST_TEST(results[1] == false); } - BOOST_CHECK(results[0] == true); - BOOST_CHECK(results[1] == true); + BOOST_TEST(results[0] == true); + BOOST_TEST(results[1] == true); { - BOOST_SCOPE_EXIT( (&results)(pf) ) - { + BOOST_SCOPE_EXIT( (&results) (pf) ) { results[0] = !pf(); results[1] = !pf(); + pf = &foo2; // modify a copy } BOOST_SCOPE_EXIT_END pf = 0; - BOOST_CHECK(results[0] == true); - BOOST_CHECK(results[1] == true); + BOOST_TEST(results[0] == true); + BOOST_TEST(results[1] == true); + } + + BOOST_TEST(pf == 0); + BOOST_TEST(results[0] == false); + BOOST_TEST(results[1] == false); +} + +void test_capture_all(void) { +#ifndef BOOST_NO_LAMBDAS + int i = 0, j = 1; + + { + BOOST_SCOPE_EXIT_ALL(=) { + i = j = 1; // modify copies + }; + } + BOOST_TEST(i == 0); + BOOST_TEST(j == 1); + + { + BOOST_SCOPE_EXIT_ALL(&) { + i = 1; + j = 2; + }; + BOOST_TEST(i == 0); + BOOST_TEST(j == 1); } + BOOST_TEST(i == 1); + BOOST_TEST(j == 2); - BOOST_CHECK(results[0] == false); - BOOST_CHECK(results[1] == false); + { + BOOST_SCOPE_EXIT_ALL(=, &j) { + i = 2; // modify a copy + j = 3; + }; + BOOST_TEST(i == 1); + BOOST_TEST(j == 2); + } + BOOST_TEST(i == 1); + BOOST_TEST(j == 3); +#endif // lambdas } -test_suite* init_unit_test_suite( int, char* [] ) -{ - framework::master_test_suite().p_name.value = "Unit test for ScopeExit"; - framework::master_test_suite().add( BOOST_TEST_CASE( &test_non_local )); - framework::master_test_suite().add( BOOST_TEST_CASE( &test_types )); - return 0; +int main(void) { + test_non_local(); + test_types(); + test_capture_all(); + return boost::report_errors(); } |