1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
// 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>
#include <boost/scope_exit.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/typeof/std/string.hpp>
#include <boost/test/unit_test.hpp>
using namespace boost::unit_test;
std::string g_str;
template<int Dummy = 0>
struct Holder
{
static long g_long;
};
template<int Dummy> long Holder<Dummy>::g_long;
void test_non_local()
{
// ... 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_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_END
{
g_str = "";
Holder<>::g_long = 1;
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_END
BOOST_CHECK(i == 0);
BOOST_CHECK(g_str == "");
BOOST_CHECK(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_SCOPE_EXIT( (&i) )
{
BOOST_CHECK(i == 3);
BOOST_CHECK(Holder<>::g_long == 3);
BOOST_CHECK(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_END
try
{
BOOST_SCOPE_EXIT( (&i) )
{
i = 3;
g_str = "try: g_str";
} BOOST_SCOPE_EXIT_END
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);
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
}
}
bool foo()
{
return true;
}
void test_types()
{
bool (*pf)() = 0;
bool (&rf)() = foo;
bool results[2] = {};
{
BOOST_SCOPE_EXIT( (&results)(&pf)(&rf) )
{
results[0] = pf();
results[1] = rf();
}
BOOST_SCOPE_EXIT_END
pf = &foo;
BOOST_CHECK(results[0] == false);
BOOST_CHECK(results[1] == false);
}
BOOST_CHECK(results[0] == true);
BOOST_CHECK(results[1] == true);
{
BOOST_SCOPE_EXIT( (&results)(pf) )
{
results[0] = !pf();
results[1] = !pf();
}
BOOST_SCOPE_EXIT_END
pf = 0;
BOOST_CHECK(results[0] == true);
BOOST_CHECK(results[1] == true);
}
BOOST_CHECK(results[0] == false);
BOOST_CHECK(results[1] == false);
}
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;
}
|