Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r76619 - sandbox/closure/libs/local_function/test
From: lorcaminiti_at_[hidden]
Date: 2012-01-21 13:24:31


Author: lcaminiti
Date: 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
New Revision: 76619
URL: http://svn.boost.org/trac/boost/changeset/76619

Log:
Examples and tests.
Added:
   sandbox/closure/libs/local_function/test/add.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/add_classifiers.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/add_default.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/add_except.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/add_inline.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/add_lambda.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/add_nobind.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/add_seq.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/add_template.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/add_this.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/add_typed.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/add_with_default.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/all_inputs.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/factorial.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/nesting.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/overload.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/return_assign.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/ten.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/transform.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/typeof.cpp (contents, props changed)
   sandbox/closure/libs/local_function/test/typeof_template.cpp (contents, props changed)

Added: sandbox/closure/libs/local_function/test/add.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/add.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,23 @@
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAdd
+#include <boost/test/unit_test.hpp>
+#include <algorithm>
+
+BOOST_AUTO_TEST_CASE( test_add )
+//[test_add
+{ // Some local scope.
+ int sum = 0, factor = 10; // Variables in scope to bind.
+
+ void BOOST_LOCAL_FUNCTION(const bind factor, bind& sum, int num) {
+ sum += factor * num;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ add(1); // Call the local function (with one parameter `num`).
+ int nums[] = {2, 3};
+ std::for_each(nums, nums + 2, add); // Pass it to an algorithm.
+
+ BOOST_CHECK( sum == 60 ); // Assert final summation value.
+}
+//]
+

Added: sandbox/closure/libs/local_function/test/add_classifiers.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/add_classifiers.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,20 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddClassifiers
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_CASE( test_add_classifiers ) {
+ //[test_add_classifiers
+ int BOOST_LOCAL_FUNCTION(auto int x, register int y) { // Classifers.
+ return x + y;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+ //]
+
+ BOOST_CHECK( add(1, 2) == 3 );
+}
+

Added: sandbox/closure/libs/local_function/test/add_default.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/add_default.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,21 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddDefault
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_CASE( test_add_default ) {
+ //[test_add_default
+ int BOOST_LOCAL_FUNCTION(int x, int y, default 2) { // Default.
+ return x + y;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ BOOST_CHECK( add(1) == 3 );
+ //]
+}
+//]
+

Added: sandbox/closure/libs/local_function/test/add_except.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/add_except.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,26 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddExcept
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_CASE( test_except ) {
+ //[test_add_except
+ double sum = 0.0;
+ int factor = 10;
+
+ void BOOST_LOCAL_FUNCTION(const bind factor, bind& sum,
+ double num) throw() { // Throw nothing.
+ sum += factor * num;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ add(100);
+ //]
+
+ BOOST_CHECK( sum == 1000 );
+}
+

Added: sandbox/closure/libs/local_function/test/add_inline.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/add_inline.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,30 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+//[ add_function_inline_cpp
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddInline
+#include <boost/test/unit_test.hpp>
+#include <vector>
+#include <algorithm>
+
+BOOST_AUTO_TEST_CASE( test_add_inline ) {
+ //[test_add_inline
+ int sum = 0, factor = 10;
+
+ void BOOST_LOCAL_FUNCTION(const bind factor, bind& sum, int num) {
+ sum += factor * num;
+ } BOOST_LOCAL_FUNCTION_NAME(inline add) // Inlined.
+
+ std::vector<int> v(100);
+ std::fill(v.begin(), v.end(), 1);
+
+ for(size_t i = 0; i < v.size(); ++i) add(v[i]); // Cannot use for_each.
+ //]
+
+ BOOST_CHECK( sum == 1000 );
+}
+

Added: sandbox/closure/libs/local_function/test/add_lambda.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/add_lambda.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,32 @@
+
+#include <boost/config.hpp>
+
+#ifndef BOOST_NO_LAMBDAS
+
+#define BOOST_TEST_MODULE TestAddLambda
+#include <boost/test/unit_test.hpp>
+#include <algorithm>
+
+BOOST_AUTO_TEST_CASE( test_add_lambda )
+//[test_add_lambda
+{ // Some local scope.
+ int sum = 0, factor = 10; // Variables in scope to bind.
+
+ auto add = [factor, &sum](int num) { // C++11 only.
+ sum += factor * num;
+ };
+
+ add(1); // Call the lambda (with one parameter `num`).
+ int nums[] = {2, 3};
+ std::for_each(nums, nums + 2, add); // Pass it to an algorithm.
+
+ BOOST_CHECK( sum == 60 ); // Assert final summation value.
+}
+//]
+
+#else // LAMBDAS
+
+int main(void) { return 0; } // Trivial test.
+
+#endif // LAMBDAS
+

Added: sandbox/closure/libs/local_function/test/add_nobind.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/add_nobind.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,21 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddNobind
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_CASE( test_add_nobind ) {
+ //[test_add_nobind
+ int BOOST_LOCAL_FUNCTION(int x, int y) { // Local function.
+ return x + y;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ BOOST_CHECK( add(1, 2) == 3 ); // Local function call.
+ //]
+}
+//]
+

Added: sandbox/closure/libs/local_function/test/add_seq.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/add_seq.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,23 @@
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddSeq
+#include <boost/test/unit_test.hpp>
+#include <algorithm>
+
+BOOST_AUTO_TEST_CASE( test_add_seq ) {
+ //[test_add_seq
+ int sum = 0, factor = 10; // Variables in scope to bind.
+
+ void BOOST_LOCAL_FUNCTION( (const bind factor) (bind& sum) (int num) ) {
+ sum += factor * num;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ add(1); // Call the local function (with one parameter `num`).
+
+ int nums[] = {2, 3};
+ std::for_each(nums, nums + 2, add); // Pass it to an algorithm.
+ //]
+
+ BOOST_CHECK( sum == 60 );
+}
+

Added: sandbox/closure/libs/local_function/test/add_template.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/add_template.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,34 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddTemplate
+#include <boost/test/unit_test.hpp>
+#include <algorithm>
+
+//[test_add_template
+template<typename T>
+T total(const T& x, const T& y, const T& z) {
+ T sum = T(), factor = 10;
+
+ // Using the `..._TPL` macro.
+ T BOOST_LOCAL_FUNCTION_TPL(const bind factor, bind& sum, T num) {
+ return sum += factor * num;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ add(x);
+
+ T nums[2]; nums[0] = y; nums[1] = z;
+ std::for_each(nums, nums + 2, add);
+
+ return sum;
+}
+//]
+
+BOOST_AUTO_TEST_CASE( test_add_template ) {
+ BOOST_CHECK( total(1, 2, 3) == 60 );
+}
+

Added: sandbox/closure/libs/local_function/test/add_this.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/add_this.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,37 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddThis
+#include <boost/test/unit_test.hpp>
+#include <vector>
+#include <algorithm>
+
+//[test_add_this
+struct adder {
+ adder(): sum_(0) {}
+
+ int sum(const std::vector<int>& nums, const int factor = 10) {
+
+ void BOOST_LOCAL_FUNCTION(const bind factor, bind this_, int num) {
+ this_->sum_ += factor * num; // Use `this_` instead of `this`.
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ std::for_each(nums.begin(), nums.end(), add);
+ return sum_;
+ }
+private:
+ int sum_;
+};
+//]
+
+BOOST_AUTO_TEST_CASE( test_add_this ) {
+ std::vector<int> v(3);
+ v[0] = 1; v[1] = 2; v[2] = 3;
+
+ BOOST_CHECK( adder().sum(v) == 60 );
+}
+

Added: sandbox/closure/libs/local_function/test/add_typed.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/add_typed.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,38 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddTyped
+#include <boost/test/unit_test.hpp>
+#include <vector>
+#include <algorithm>
+
+//[test_add_typed
+struct adder {
+ adder(): sum_(0) {}
+
+ int sum(const std::vector<int>& nums, const int& factor = 10) {
+ // Explicitly specify bound variable and result types.
+ BOOST_LOCAL_FUNCTION(const bind(const int&) factor,
+ bind(adder*) this_, int num, return int) {
+ return this_->sum_ += factor * num;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ std::for_each(nums.begin(), nums.end(), add);
+ return sum_;
+ }
+private:
+ int sum_;
+};
+//]
+
+BOOST_AUTO_TEST_CASE( test_add_typed ) {
+ std::vector<int> v(3);
+ v[0] = 1; v[1] = 2; v[2] = 3;
+
+ BOOST_CHECK( adder().sum(v) == 60 );
+}
+

Added: sandbox/closure/libs/local_function/test/add_with_default.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/add_with_default.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,25 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddWithDefault
+#include <boost/test/unit_test.hpp>
+
+//[test_add_with_default_macro
+#define WITH_DEFAULT , default
+//]
+
+BOOST_AUTO_TEST_CASE( test_add_with_default ) {
+ //[test_add_with_default
+ int BOOST_LOCAL_FUNCTION(int x, int y WITH_DEFAULT 2) { // Default.
+ return x + y;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ BOOST_CHECK( add(1) == 3 );
+ //]
+}
+//]
+

Added: sandbox/closure/libs/local_function/test/all_inputs.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/all_inputs.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,164 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+//[params_all_cpp
+#include <boost/local/function.hpp>
+
+struct s {
+ void f(double p = 1.23, double q = -1.23) {
+ { // Only params.
+ void BOOST_LOCAL_FUNCTION_PARAMS( (int x) (int y)(default 0) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(l)
+ l(1);
+ }
+ { // Only const binds.
+ int a, b;
+
+ const int& BOOST_LOCAL_FUNCTION_PARAMS( (const bind a)
+ (const bind& b) (const bind& p) (const bind q) ) {
+ return b;
+ } BOOST_LOCAL_FUNCTION_NAME(l)
+ l();
+
+ const s& BOOST_LOCAL_FUNCTION_PARAMS( (const bind this) ) {
+ return *this_;
+ } BOOST_LOCAL_FUNCTION_NAME(t)
+ t();
+
+ const int BOOST_LOCAL_FUNCTION_PARAMS( (const bind a)
+ (const bind& b) (const bind& p) (const bind q)
+ (const bind this) ) {
+ return a;
+ } BOOST_LOCAL_FUNCTION_NAME(lt)
+ lt();
+ }
+ { // Only plain binds.
+ int c, d;
+
+ int& BOOST_LOCAL_FUNCTION_PARAMS( (bind c) (bind& d)
+ (bind& p) (bind& q) ) {
+ return d;
+ } BOOST_LOCAL_FUNCTION_NAME(l)
+ l();
+
+ s& BOOST_LOCAL_FUNCTION_PARAMS( (bind this) ) {
+ return *this_;
+ } BOOST_LOCAL_FUNCTION_NAME(t)
+ t();
+
+ int BOOST_LOCAL_FUNCTION_PARAMS( (bind c) (bind& d)
+ (bind& p) (bind& q) (bind this) ) {
+ return c;
+ } BOOST_LOCAL_FUNCTION_NAME(lt)
+ lt();
+ }
+
+ { // Both params and const binds.
+ int a, b;
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (int x) (int y)(default 0)
+ (const bind a) (const bind& b)
+ (const bind& p) (const bind q) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(l)
+ l(1);
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (int x) (int y)(default 0)
+ (const bind this) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(t)
+ t(1);
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (int x) (int y)(default 0)
+ (const bind a) (const bind this)
+ (const bind& b) (const bind& p) (const bind q) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(lt)
+ lt(1);
+ }
+ { // Both params and plain binds.
+ int c, d;
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (int x) (int y)(default 0)
+ (bind c) (bind& d) (bind& p) (bind q) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(l)
+ l(1);
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (int x) (int y)(default 0)
+ (bind this) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(t)
+ t(1);
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (int x) (int y)(default 0)
+ (bind c) (bind& d)
+ (bind& p) (bind this) (bind q) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(lt)
+ lt(1);
+ }
+ { // Both const and plain binds.
+ int a, b, c, d;
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (const bind a) (const bind& b)
+ (const bind p) (bind c) (bind& d) (bind q) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(l)
+ l();
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (const bind this)
+ (bind c) (bind& d) (bind q) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(ct)
+ ct();
+ void BOOST_LOCAL_FUNCTION_PARAMS( (const bind this)
+ (const bind a) (const bind& b) (const bind p)
+ (bind c) (bind& d) (bind q) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(lct)
+ lct();
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (const bind a) (const bind& b)
+ (const bind p) (bind this) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(pt)
+ pt();
+ void BOOST_LOCAL_FUNCTION_PARAMS( (const bind a) (const bind& b)
+ (const bind p) (bind c) (bind this) (bind& d) (bind q) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(lpt)
+ lpt();
+ }
+
+ { // All params, const binds, and plain binds.
+ int a, b, c, d;
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (int x) (int y)(default 0)
+ (const bind a) (const bind& b) (const bind& p)
+ (bind c) (bind& d) (bind& q) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(l)
+ l(1);
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (int x) (int y)(default 0)
+ (const bind this) (bind c) (bind& d) (bind& q) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(ct)
+ ct(1);
+ void BOOST_LOCAL_FUNCTION_PARAMS( (int x) (int y)(default 0)
+ (const bind a) (const bind& b) (const bind& p)
+ (bind this) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(pt)
+ pt(1);
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (int x) (int y)(default 0)
+ (const bind a) (const bind this) (const bind& b)
+ (const bind& p) (bind c) (bind& d) (bind& q) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(lct)
+ lct(1);
+ void BOOST_LOCAL_FUNCTION_PARAMS( (int x) (int y)(default 0)
+ (const bind a) (const bind& b) (const bind& p)
+ (bind c) (bind& d) (bind this) (bind& q) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(lpt)
+ lpt(1);
+ }
+ }
+};
+
+int main() {
+ s().f();
+ return 0;
+}
+//]
+

Added: sandbox/closure/libs/local_function/test/factorial.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/factorial.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,44 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestFactorial
+#include <boost/test/unit_test.hpp>
+#include <algorithm>
+#include <vector>
+
+//[test_factorial
+struct calculator {
+ std::vector<int> results;
+
+ void factorials(const std::vector<int>& nums) {
+ int BOOST_LOCAL_FUNCTION(bind this_, int num,
+ bool recursion, default false) {
+ int result = 0;
+
+ if (n < 2 ) result = 1;
+ else result = n * factorial(n - 1, true); // Recursive call.
+
+ if (!recursion) this_->results.push_back(result);
+ return result;
+ } BOOST_LOCAL_FUNCTION_NAME(recursive factorial) // Recursive.
+
+ std::for_each(nums.begin(), nums.end(), factorial);
+ }
+};
+//]
+
+BOOST_AUTO_TEST_CASE( test_factorial ) {
+ std::vector<int> v(3);
+ v[0] = 1; v[1] = 4; v[2] = 7;
+
+ calculator calc;
+ calc.factorials(v);
+ BOOST_CHECK( calc.results[0] == 1 );
+ BOOST_CHECK( calc.results[1] == 24 );
+ BOOST_CHECK( calc.results[2] == 64 );
+}
+

Added: sandbox/closure/libs/local_function/test/nesting.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/nesting.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,28 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestNesting
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_CASE( test_nesting ) {
+ //[test_nesting
+ int x = 0;
+
+ void BOOST_LOCAL_FUNCTION(bind& x) {
+ void BOOST_LOCAL_FUNCTION_PARAMS(bind& x) { // Nested.
+ x++;
+ } BOOST_LOCAL_FUNCTION_NAME(g)
+
+ x--;
+ g(); // Nested local function call.
+ } BOOST_LOCAL_FUNCTION_NAME(f)
+
+ f();
+ //]
+ BOOST_CHECK( x == 0 );
+}
+

Added: sandbox/closure/libs/local_function/test/overload.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/overload.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,43 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+//[overload_cpp
+#include <boost/local_function.hpp>
+#include <boost/functional/overloaded_function.hpp> // For overloading.
+#define BOOST_TEST_MODULE TestOverload
+#include <boost/test/unit_test.hpp>
+#include <string>
+#include <cmath>
+
+//[test_overload
+int add_i(int x, int y) { return x + y; }
+
+BOOST_AUTO_TEST_CASE( test_overload ) {
+ std::string s = "abc";
+ std::string BOOST_LOCAL_FUNCTION(
+ const bind& s, const std::string& x) {
+ return s + x;
+ } BOOST_LOCAL_FUNCTION_NAME(add_s)
+
+ double d = 1.23;
+ double BOOST_LOCAL_FUNCTION(const bind d, double x, double y, default 0) {
+ return d + x + y;
+ } BOOST_LOCAL_FUNCTION_NAME(add_d)
+
+ boost::overloaded_function<
+ std::string (const std::string&)
+ , double (double)
+ , double (double, double) // Overload giving default param.
+ , int (int, int)
+ > add(add_s, add_d, add_d, add_i); // Overloaded function object.
+
+ BOOST_CHECK( add("xyz") == "abcxyz" ); // Call `add_s`.
+ BOOST_CHECK( fabs(add(3.21) - 4.44) < 0.001 ); // Call `add_d` (no default).
+ BOOST_CHECK( fabs(add(3.21, 40.0) - 44.44) < 0.001); // Call `add_d`.
+ BOOST_CHECK( add(1, 2) == 3 ); // Call `add_i`.
+}
+//]
+

Added: sandbox/closure/libs/local_function/test/return_assign.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/return_assign.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,42 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include <boost/local_function.hpp>
+#include <boost/function.hpp>
+#define BOOST_TEST_MODULE TestReturnAssign
+#include <boost/test/unit_test.hpp>
+
+//[test_return_assign
+void call1(boost::function<int (int)> f) { BOOST_CHECK( f(1) == 5 ); }
+
+void call0(boost::function<int (void)> f) { BOOST_CHECK( f() == 5 ); }
+
+boost::function<int (int, int)> linear(int slope) {
+ boost::function< int (int, int) > l2;
+
+ int BOOST_LOCAL_FUNCTION(bind slope, int x, default 1, int y, default 2) {
+ return x + slope * y;
+ } BOOST_LOCAL_FUNCTION_NAME(lin)
+
+ boost::function<int (int, int)> f = lin; // Assign to local variable.
+ BOOST_CHECK( f(1, 2) == 5 );
+
+ call1(lin); // Pass to other functions.
+ call0(lin);
+
+ return lin; // Return.
+}
+
+void call(void) {
+ boost::function<int (int, int)> f = linear(2);
+ BOOST_CHECK( f(1, 2) == 5 );
+}
+//]
+
+BOOST_AUTO_TEST_CASE( test_return_assign ) {
+ call();
+}
+

Added: sandbox/closure/libs/local_function/test/ten.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/ten.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,16 @@
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestTen
+#include <boost/test/unit_test.hpp>
+#include <iostream>
+
+BOOST_AUTO_TEST_CASE( test_ten ) {
+ //[test_ten
+ int BOOST_LOCAL_FUNCTION(void) { // No parameter.
+ return 10;
+ } BOOST_LOCAL_FUNCTION_NAME(ten)
+
+ BOOST_CHECK( ten() == 10 );
+ //]
+}
+

Added: sandbox/closure/libs/local_function/test/transform.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/transform.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,39 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestTranform
+#include <boost/test/unit_test.hpp>
+#include <algorithm>
+#include <vector>
+
+BOOST_AUTO_TEST_CASE( test_transform ) {
+ //[test_transform
+ int offset = 5;
+ std::vector<int> v;
+ std::vector<int> w;
+
+ for(int i = 1; i <= 2; ++i) v.push_back(i * 10);
+ BOOST_CHECK( v[0] == 10 ); BOOST_CHECK( v[1] == 20 );
+ w.resize(v.size());
+
+ int BOOST_LOCAL_FUNCTION(const bind& offset, int i) {
+ return ++i + offset;
+ } BOOST_LOCAL_FUNCTION_NAME(inc)
+
+ std::transform(v.begin(), v.end(), w.begin(), inc);
+ BOOST_CHECK( w[0] == 16 ); BOOST_CHECK( w[1] == 26 );
+
+ int BOOST_LOCAL_FUNCTION(const bind& inc, int i, int j) {
+ return inc(i + j); // Call the other bound local function.
+ } BOOST_LOCAL_FUNCTION_NAME(inc_sum)
+
+ offset = 0;
+ std::transform(v.begin(), v.end(), w.begin(), v.begin(), inc_sum);
+ BOOST_CHECK( v[0] == 27 ); BOOST_CHECK( v[1] == 47 );
+ //]
+}
+

Added: sandbox/closure/libs/local_function/test/typeof.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/typeof.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,33 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include "addable.hpp"
+#include <boost/local_function.hpp>
+#include <boost/type_traits/remove_reference.hpp>
+#include <boost/concept_check.hpp>
+#define BOOST_TEST_MODULE TestTypeof
+#include <boost/test/unit_test.hpp>
+#include <algorithm>
+
+BOOST_AUTO_TEST_CASE( test_typeof ) {
+ //[test_typeof
+ int sum = 0, factor = 10;
+
+ void BOOST_LOCAL_FUNCTION(const bind factor, bind& sum, int num) {
+ // Typeof for concept checking.
+ BOOST_CONCEPT_ASSERT((Addable<boost::remove_reference<
+ BOOST_LOCAL_FUNCTION_TYPEOF(sum)>::type>));
+ // Typeof for declarations.
+ boost::remove_reference<BOOST_LOCAL_FUNCTION_TYPEOF(
+ factor)>::type mult = factor * num;
+ sum += mult;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ add(6);
+ //]
+ BOOST_CHECK( sum == 60 );
+}
+

Added: sandbox/closure/libs/local_function/test/typeof_template.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/local_function/test/typeof_template.cpp 2012-01-21 13:24:29 EST (Sat, 21 Jan 2012)
@@ -0,0 +1,38 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to 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).
+
+#include "addable.hpp"
+#include <boost/local_function.hpp>
+#include <boost/type_traits/remove_reference.hpp>
+#include <boost/concept_check.hpp>
+#define BOOST_TEST_MODULE TestTypeofTemplate
+#include <boost/test/unit_test.hpp>
+#include <algorithm>
+
+//[test_typeof_template
+template<typename T>
+T calculate(const T& factor) {
+ T sum = 0;
+
+ void BOOST_LOCAL_FUNCTION_TPL(const bind factor, bind& sum, T num) {
+ // Typeof for concept checking.
+ BOOST_CONCEPT_ASSERT((Addable<typename boost::remove_reference<
+ BOOST_LOCAL_FUNCTION_TYPEOF(sum)>::type>));
+ // Typeof for declarations.
+ boost::remove_reference<BOOST_LOCAL_FUNCTION_TYPEOF(
+ factor)>::type mult = factor * num;
+ sum += mult;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ add(6);
+ return sum;
+}
+//]
+
+BOOST_AUTO_TEST_CASE( test_typeof_template ) {
+ BOOST_CHECK( calculate(10) == 60 );
+}
+


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