Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r76466 - sandbox/closure/libs/scope_exit/test
From: lorcaminiti_at_[hidden]
Date: 2012-01-13 15:09:29


Author: lcaminiti
Date: 2012-01-13 15:09:28 EST (Fri, 13 Jan 2012)
New Revision: 76466
URL: http://svn.boost.org/trac/boost/changeset/76466

Log:
Added ScopeExit original regression tests.
Added:
   sandbox/closure/libs/scope_exit/test/native.cpp (contents, props changed)
   sandbox/closure/libs/scope_exit/test/native_const_error.cpp (contents, props changed)
   sandbox/closure/libs/scope_exit/test/native_cv_error.cpp (contents, props changed)
   sandbox/closure/libs/scope_exit/test/native_tpl.cpp (contents, props changed)
   sandbox/closure/libs/scope_exit/test/native_tu1.cpp (contents, props changed)
   sandbox/closure/libs/scope_exit/test/native_tu2.cpp (contents, props changed)
   sandbox/closure/libs/scope_exit/test/native_tu_test.cpp (contents, props changed)
   sandbox/closure/libs/scope_exit/test/tu_test.hpp (contents, props changed)

Added: sandbox/closure/libs/scope_exit/test/native.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/scope_exit/test/native.cpp 2012-01-13 15:09:28 EST (Fri, 13 Jan 2012)
@@ -0,0 +1,220 @@
+// Copyright Alexander Nasonov 2007-2008, 2011
+//
+// 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;
+}
+
+bool foo2()
+{
+ return false;
+}
+
+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();
+ pf = &foo2; // modify a copy
+ }
+ BOOST_SCOPE_EXIT_END
+
+ pf = 0;
+
+ BOOST_CHECK(results[0] == true);
+ BOOST_CHECK(results[1] == true);
+ }
+
+ BOOST_CHECK(pf == 0);
+ BOOST_CHECK(results[0] == false);
+ BOOST_CHECK(results[1] == false);
+}
+
+void test_cxx0x()
+{
+#if defined(BOOST_SCOPE_EXIT_AUX_CXX0X)
+ int i = 0, j = 1;
+
+ {
+ BOOST_SCOPE_EXIT((=))
+ {
+ i = j = 1; // modify copies
+ };
+ }
+ BOOST_CHECK(i == 0);
+ BOOST_CHECK(j == 1);
+
+ {
+ BOOST_SCOPE_EXIT((&))
+ {
+ i = 1;
+ j = 2;
+ };
+ BOOST_CHECK(i == 0);
+ BOOST_CHECK(j == 1);
+ }
+ BOOST_CHECK(i == 1);
+ BOOST_CHECK(j == 2);
+
+ {
+ BOOST_SCOPE_EXIT((=)(&j))
+ {
+ i = 2; // modify a copy
+ j = 3;
+ };
+ BOOST_CHECK(i == 1);
+ BOOST_CHECK(j == 2);
+ }
+ BOOST_CHECK(i == 1);
+ BOOST_CHECK(j == 3);
+
+#endif
+}
+
+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 ));
+ framework::master_test_suite().add( BOOST_TEST_CASE( &test_cxx0x ));
+ return 0;
+}

Added: sandbox/closure/libs/scope_exit/test/native_const_error.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/scope_exit/test/native_const_error.cpp 2012-01-13 15:09:28 EST (Fri, 13 Jan 2012)
@@ -0,0 +1,17 @@
+// 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 <boost/scope_exit.hpp>
+
+int main()
+{
+ int const i = 0;
+ BOOST_SCOPE_EXIT( (&i) )
+ {
+ i = 5;
+ } BOOST_SCOPE_EXIT_END
+}
+

Added: sandbox/closure/libs/scope_exit/test/native_cv_error.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/scope_exit/test/native_cv_error.cpp 2012-01-13 15:09:28 EST (Fri, 13 Jan 2012)
@@ -0,0 +1,22 @@
+// 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 <string>
+
+#include <boost/scope_exit.hpp>
+
+#include <boost/typeof/typeof.hpp>
+#include <boost/typeof/std/string.hpp>
+
+int main()
+{
+ std::string const volatile s;
+ BOOST_SCOPE_EXIT( (&s) )
+ {
+ s = "";
+ } BOOST_SCOPE_EXIT_END
+}
+

Added: sandbox/closure/libs/scope_exit/test/native_tpl.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/scope_exit/test/native_tpl.cpp 2012-01-13 15:09:28 EST (Fri, 13 Jan 2012)
@@ -0,0 +1,87 @@
+// 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 <vector>
+
+#include <boost/scope_exit.hpp>
+
+#include <boost/rational.hpp>
+#include <boost/typeof/typeof.hpp>
+#include <boost/typeof/std/vector.hpp>
+#include <boost/test/unit_test.hpp>
+
+using namespace boost::unit_test;
+
+template<class type>
+void tpl_long( type tval
+ , type& t
+ , type const& tc
+ , type volatile& tv
+ , type const volatile& tcv
+ )
+{
+ int i = 0; // non-dependent name
+ type const remember(tval);
+
+ {
+ BOOST_SCOPE_EXIT_TPL( (&tval)(&t)(&tc)(&tv)(&tcv)(&i) )
+ {
+ tval = 1;
+ ++t;
+ ++tv;
+ }
+ BOOST_SCOPE_EXIT_END
+
+ BOOST_CHECK(t == remember);
+ BOOST_CHECK(tval == remember);
+ }
+
+ BOOST_CHECK(tval == 1);
+ BOOST_CHECK(t == remember + 2);
+}
+
+template<class Vector, int Value>
+void tpl_vector( Vector vval
+ , Vector& v
+ , Vector const& vc
+ )
+{
+ Vector const remember(vval);
+
+ {
+ BOOST_SCOPE_EXIT_TPL( (&vval)(&v)(&vc) )
+ {
+ v.push_back(-Value);
+ vval.push_back(Value);
+ }
+ BOOST_SCOPE_EXIT_END
+
+ BOOST_CHECK(v.size() == remember.size());
+ BOOST_CHECK(vval.size() == remember.size());
+ }
+
+ BOOST_CHECK(v.size() == 1 + remember.size());
+ BOOST_CHECK(vval.size() == 1 + remember.size());
+}
+
+void test_tpl()
+{
+ long l = 137;
+ tpl_long(l, l, l, l, l);
+
+ std::vector<int> v(10, 137);
+ tpl_vector<std::vector<int>, 13>(v, v, v);
+}
+
+test_suite* init_unit_test_suite( int, char* [] )
+{
+ framework::master_test_suite().p_name.value = "Unit test for ScopeExit TPL";
+ framework::master_test_suite().add( BOOST_TEST_CASE( &test_tpl ));
+ return 0;
+}
+

Added: sandbox/closure/libs/scope_exit/test/native_tu1.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/scope_exit/test/native_tu1.cpp 2012-01-13 15:09:28 EST (Fri, 13 Jan 2012)
@@ -0,0 +1,13 @@
+// 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 "tu_test.hpp"
+
+int tu1()
+{
+ return inline_f() + template_f(1);
+}
+

Added: sandbox/closure/libs/scope_exit/test/native_tu2.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/scope_exit/test/native_tu2.cpp 2012-01-13 15:09:28 EST (Fri, 13 Jan 2012)
@@ -0,0 +1,13 @@
+// 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 "tu_test.hpp"
+
+int tu2()
+{
+ return inline_f() + template_f(2);
+}
+

Added: sandbox/closure/libs/scope_exit/test/native_tu_test.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/scope_exit/test/native_tu_test.cpp 2012-01-13 15:09:28 EST (Fri, 13 Jan 2012)
@@ -0,0 +1,24 @@
+// 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 "tu_test.hpp"
+
+#include <boost/test/unit_test.hpp>
+
+using namespace boost::unit_test;
+
+void test()
+{
+ BOOST_CHECK(tu1() == 1);
+ BOOST_CHECK(tu2() == 2);
+}
+
+test_suite* init_unit_test_suite( int, char* [] )
+{
+ framework::master_test_suite().p_name.value = "TU unit test for ScopeExit";
+ framework::master_test_suite().add( BOOST_TEST_CASE( &test ));
+ return 0;
+}

Added: sandbox/closure/libs/scope_exit/test/tu_test.hpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/scope_exit/test/tu_test.hpp 2012-01-13 15:09:28 EST (Fri, 13 Jan 2012)
@@ -0,0 +1,39 @@
+// 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 <boost/scope_exit.hpp>
+
+int tu1();
+int tu2();
+
+inline int inline_f()
+{
+ int i = 99;
+ {
+ BOOST_SCOPE_EXIT( (&i) ) { i = -1; } BOOST_SCOPE_EXIT_END
+ }
+ return i;
+}
+
+#if !defined(BOOST_SCOPE_EXIT_AUX_GCC) || BOOST_SCOPE_EXIT_AUX_GCC >= 304
+template<class Int>
+Int template_f(Int i)
+{
+ {
+ BOOST_SCOPE_EXIT_TPL( (&i) ) { ++i; } BOOST_SCOPE_EXIT_END
+ }
+ return i;
+}
+#else
+inline int template_f(int i)
+{
+ {
+ BOOST_SCOPE_EXIT( (&i) ) { ++i; } BOOST_SCOPE_EXIT_END
+ }
+ return i;
+}
+#endif
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk