Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r77022 - trunk/libs/local_function/test
From: lorcaminiti_at_[hidden]
Date: 2012-02-14 19:36:56


Author: lcaminiti
Date: 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
New Revision: 77022
URL: http://svn.boost.org/trac/boost/changeset/77022

Log:
Added LocalFunction tests.
Added:
   trunk/libs/local_function/test/Jamfile.v2 (contents, props changed)
   trunk/libs/local_function/test/add.cpp (contents, props changed)
   trunk/libs/local_function/test/add_classifiers.cpp (contents, props changed)
   trunk/libs/local_function/test/add_default.cpp (contents, props changed)
   trunk/libs/local_function/test/add_except.cpp (contents, props changed)
   trunk/libs/local_function/test/add_inline.cpp (contents, props changed)
   trunk/libs/local_function/test/add_lambda.cpp (contents, props changed)
   trunk/libs/local_function/test/add_params.cpp (contents, props changed)
   trunk/libs/local_function/test/add_seq.cpp (contents, props changed)
   trunk/libs/local_function/test/add_template.cpp (contents, props changed)
   trunk/libs/local_function/test/add_this.cpp (contents, props changed)
   trunk/libs/local_function/test/add_typed.cpp (contents, props changed)
   trunk/libs/local_function/test/add_with_default.cpp (contents, props changed)
   trunk/libs/local_function/test/addable.hpp (contents, props changed)
   trunk/libs/local_function/test/factorial.cpp (contents, props changed)
   trunk/libs/local_function/test/goto.cpp (contents, props changed)
   trunk/libs/local_function/test/goto_err.cpp (contents, props changed)
   trunk/libs/local_function/test/macro_commas.cpp (contents, props changed)
   trunk/libs/local_function/test/nesting.cpp (contents, props changed)
   trunk/libs/local_function/test/operator.cpp (contents, props changed)
   trunk/libs/local_function/test/operator_err.cpp (contents, props changed)
   trunk/libs/local_function/test/overload.cpp (contents, props changed)
   trunk/libs/local_function/test/return_assign.cpp (contents, props changed)
   trunk/libs/local_function/test/return_derivative.cpp (contents, props changed)
   trunk/libs/local_function/test/return_inc.cpp (contents, props changed)
   trunk/libs/local_function/test/return_setget.cpp (contents, props changed)
   trunk/libs/local_function/test/return_this.cpp (contents, props changed)
   trunk/libs/local_function/test/seq.cpp (contents, props changed)
   trunk/libs/local_function/test/ten_void.cpp (contents, props changed)
   trunk/libs/local_function/test/transform.cpp (contents, props changed)
   trunk/libs/local_function/test/typeof.cpp (contents, props changed)
   trunk/libs/local_function/test/typeof_template.cpp (contents, props changed)

Added: trunk/libs/local_function/test/Jamfile.v2
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/Jamfile.v2 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,45 @@
+
+# Copyright (C) 2009-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/local_function
+
+import testing ;
+
+project : requirements <library>/boost//unit_test_framework ;
+
+run add.cpp ;
+run add_classifiers.cpp ;
+run add_default.cpp ;
+run add_except.cpp ;
+run add_inline.cpp ;
+run add_lambda.cpp ;
+run add_params.cpp ;
+run add_template.cpp ;
+run add_this.cpp ;
+run add_typed.cpp ;
+run add_with_default.cpp ;
+
+run factorial.cpp ;
+run goto.cpp ;
+compile-fail goto_err.cpp ;
+run macro_commas.cpp ;
+run nesting.cpp ;
+run operator.cpp ;
+compile-fail operator_err.cpp ;
+run overload.cpp ;
+run ten_void.cpp ;
+run transform.cpp ;
+run typeof.cpp ;
+run typeof_template.cpp ;
+
+run return_assign.cpp ;
+run return_derivative.cpp ;
+run return_inc.cpp ;
+run return_setget.cpp ;
+run return_this.cpp ;
+
+run add_seq.cpp ;
+run seq.cpp ;
+

Added: trunk/libs/local_function/test/add.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/add.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,29 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAdd
+#include <boost/test/unit_test.hpp>
+#include <algorithm>
+
+BOOST_AUTO_TEST_CASE( test_add )
+//[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.
+ 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: trunk/libs/local_function/test/add_classifiers.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/add_classifiers.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,30 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/config.hpp>
+#ifdef BOOST_NO_AUTO_DECLARATIONS // No C++11 auto declarations.
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddClassifiers
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_CASE( test_add_classifiers ) {
+ //[add_classifiers
+ int BOOST_LOCAL_FUNCTION(auto int x, register int y) { // Classifiers.
+ return x + y;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+ //]
+
+ BOOST_CHECK( add(1, 2) == 3 );
+}
+
+#else // C++11 auto declarations.
+
+int main(void) { return 0; } // Trivial program.
+
+#endif
+

Added: trunk/libs/local_function/test/add_default.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/add_default.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,22 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddDefault
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_CASE( test_add_default ) {
+ //[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: trunk/libs/local_function/test/add_except.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/add_except.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,27 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddExcept
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_CASE( test_except ) {
+ //[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: trunk/libs/local_function/test/add_inline.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/add_inline.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,31 @@
+
+// Copyright (C) 2009-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/local_function
+
+//[ 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 ) {
+ //[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: trunk/libs/local_function/test/add_lambda.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/add_lambda.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,37 @@
+
+// Copyright (C) 2009-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/local_function
+
+#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 )
+//[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.
+ 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 // NO_LAMBDAS
+
+int main(void) { return 0; } // Trivial program.
+
+#endif // NO_LAMBDAS
+

Added: trunk/libs/local_function/test/add_params.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/add_params.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,22 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddParams
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_CASE( test_add_params ) {
+ //[add_params
+ 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: trunk/libs/local_function/test/add_seq.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/add_seq.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,29 @@
+
+// Copyright (C) 2009-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/local_function
+
+#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 )
+//[add_seq
+{ // 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; // Parameters as sequence.
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ add(1); // Call the local function.
+ 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: trunk/libs/local_function/test/add_template.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/add_template.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,34 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddTemplate
+#include <boost/test/unit_test.hpp>
+#include <algorithm>
+
+//[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: trunk/libs/local_function/test/add_this.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/add_this.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,38 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddThis
+#include <boost/test/unit_test.hpp>
+#include <vector>
+#include <algorithm>
+
+//[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: trunk/libs/local_function/test/add_typed.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/add_typed.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,39 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddTyped
+#include <boost/test/unit_test.hpp>
+#include <vector>
+#include <algorithm>
+
+//[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: trunk/libs/local_function/test/add_with_default.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/add_with_default.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,26 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestAddWithDefault
+#include <boost/test/unit_test.hpp>
+
+//[add_with_default_macro
+#define WITH_DEFAULT , default
+//]
+
+BOOST_AUTO_TEST_CASE( test_add_with_default ) {
+ //[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: trunk/libs/local_function/test/addable.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/addable.hpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,25 @@
+
+// Copyright (C) 2009-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/local_function
+
+#ifndef ADDABLE_HPP_
+#define ADDABLE_HPP_
+
+#include <boost/concept_check.hpp>
+
+template<typename T>
+struct Addable { // User-defined concept.
+ BOOST_CONCEPT_USAGE(Addable) {
+ return_type(x + y); // Check addition `T operator+(T x, T y)`.
+ }
+private:
+ T x;
+ T y;
+ void return_type(T const&); // Used to check addition returns type `T`.
+};
+
+#endif // #include guard
+

Added: trunk/libs/local_function/test/factorial.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/factorial.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,45 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestFactorial
+#include <boost/test/unit_test.hpp>
+#include <algorithm>
+#include <vector>
+
+//[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(num <= 0) result = 1;
+ else result = num * factorial(num - 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] = 3; v[2] = 4;
+
+ calculator calc;
+ calc.factorials(v);
+ BOOST_CHECK( calc.results[0] == 1 );
+ BOOST_CHECK( calc.results[1] == 6 );
+ BOOST_CHECK( calc.results[2] == 24 );
+}
+

Added: trunk/libs/local_function/test/goto.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/goto.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,28 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestGoto
+#include <boost/test/unit_test.hpp>
+
+//[goto
+int error(int x, int y) {
+ int BOOST_LOCAL_FUNCTION(int x) {
+ if(x > 0) goto success; // OK: Can jump within local function.
+ return -1;
+ success:
+ return 0;
+ } BOOST_LOCAL_FUNCTION_NAME(validate)
+
+ return validate(x + y);
+}
+//]
+
+BOOST_AUTO_TEST_CASE( test_goto ) {
+ error(1, 2);
+}
+

Added: trunk/libs/local_function/test/goto_err.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/goto_err.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,31 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestGotoErr
+#include <boost/test/unit_test.hpp>
+
+//[goto_err
+int error(int x, int y) {
+ int BOOST_LOCAL_FUNCTION(int x) {
+ if(x <= 0) goto failure; // Error: Cannot jump to enclosing scope.
+ else goto success; // OK: Can jump within local function.
+
+ success:
+ return 0;
+ } BOOST_LOCAL_FUNCTION_NAME(validate)
+
+ return validate(x + y);
+faliure:
+ return -1;
+}
+//]
+
+BOOST_AUTO_TEST_CASE( test_goto_err ) {
+ error(1, 2);
+}
+

Added: trunk/libs/local_function/test/macro_commas.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/macro_commas.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,42 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#include <boost/utility/identity_type.hpp>
+#include <boost/config.hpp>
+#define BOOST_TEST_MODULE TestMacroCommas
+#include <boost/test/unit_test.hpp>
+#include <map>
+#include <string>
+
+std::string cat(const std::string& x, const std::string& y) { return x + y; }
+
+template<typename V, typename K>
+struct key_sizeof {
+ BOOST_STATIC_CONSTANT(int, value = sizeof(K));
+};
+
+typedef int sign_t;
+
+BOOST_AUTO_TEST_CASE( test_macro_commas ) {
+ //[macro_commas
+ void BOOST_LOCAL_FUNCTION(
+ BOOST_IDENTITY_TYPE((const std::map<std::string, size_t>&)) m,
+ BOOST_IDENTITY_TYPE((::sign_t)) sign,
+ const size_t& factor,
+ default (key_sizeof<std::string, size_t>::value),
+ const std::string& separator, default cat(":", " ")
+ ) {
+ // Do something...
+ } BOOST_LOCAL_FUNCTION_NAME(f)
+ //]
+
+ std::map<std::string, size_t> m;
+ ::sign_t sign = -1;
+ f(m, sign);
+}
+

Added: trunk/libs/local_function/test/nesting.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/nesting.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,29 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestNesting
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_CASE( test_nesting ) {
+ //[nesting
+ int x = 0;
+
+ void BOOST_LOCAL_FUNCTION(bind& x) {
+ void BOOST_LOCAL_FUNCTION(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: trunk/libs/local_function/test/operator.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/operator.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,28 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestOperator
+#include <boost/test/unit_test.hpp>
+
+//[operator
+struct point {
+ int x;
+ int y;
+};
+
+BOOST_AUTO_TEST_CASE( test_operator ) {
+ bool BOOST_LOCAL_FUNCTION(const point& p, const point& q) {
+ return p.x == q.x && p.y == q.y;
+ } BOOST_LOCAL_FUNCTION_NAME(equal) // OK: not using `operator...`.
+
+ point a; a.x = 1; a.y = 2;
+ point b = a;
+ BOOST_CHECK( equal(a, b) );
+}
+//]
+

Added: trunk/libs/local_function/test/operator_err.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/operator_err.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,28 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestOperatorErr
+#include <boost/test/unit_test.hpp>
+
+//[operator_err
+struct point {
+ int x;
+ int y;
+};
+
+BOOST_AUTO_TEST_CASE( test_operator_err ) {
+ bool BOOST_LOCAL_FUNCTION(const point& p, const point& q) {
+ return p.x == q.x && p.y == q.y;
+ } BOOST_LOCAL_FUNCTION_NAME(operator==) // Error: Cannot use `operator...`.
+
+ point a; a.x = 1; a.y = 2;
+ point b = a;
+ BOOST_CHECK( a == b );
+}
+//]
+

Added: trunk/libs/local_function/test/overload.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/overload.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,43 @@
+
+// Copyright (C) 2009-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/local_function
+
+#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>
+
+//[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: trunk/libs/local_function/test/return_assign.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/return_assign.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,42 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#include <boost/function.hpp>
+#define BOOST_TEST_MODULE TestReturnAssign
+#include <boost/test/unit_test.hpp>
+#include <iostream>
+
+//[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(const int& slope) {
+ int BOOST_LOCAL_FUNCTION(const 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: trunk/libs/local_function/test/return_derivative.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/return_derivative.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,29 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#include <boost/function.hpp>
+#define BOOST_TEST_MODULE TestReturnDerivative
+#include <boost/test/unit_test.hpp>
+
+boost::function<int (int)> derivative(boost::function<int (int)>& f, int dx) {
+ int BOOST_LOCAL_FUNCTION(bind& f, const bind dx, int x) {
+ return (f(x + dx) - f(x)) / dx;
+ } BOOST_LOCAL_FUNCTION_NAME(deriv)
+ return deriv;
+}
+
+BOOST_AUTO_TEST_CASE( test_return_derivative ) {
+ int BOOST_LOCAL_FUNCTION(int x) {
+ return x + 4;
+ } BOOST_LOCAL_FUNCTION_NAME(add2)
+ boost::function<int (int)> a2 = add2; // Reference valid where closure used.
+
+ boost::function<int (int)> d2 = derivative(a2, 2);
+ BOOST_CHECK( d2(6) == 1 );
+}
+

Added: trunk/libs/local_function/test/return_inc.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/return_inc.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,31 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#include <boost/function.hpp>
+#define BOOST_TEST_MODULE TestReturnInc
+#include <boost/test/unit_test.hpp>
+
+boost::function<int (void)> inc(int& value) {
+ int BOOST_LOCAL_FUNCTION(bind& value) {
+ return ++value;
+ } BOOST_LOCAL_FUNCTION_NAME(i)
+ return i;
+}
+
+BOOST_AUTO_TEST_CASE( test_return_inc ) {
+ int value1 = 0; // Reference valid in scope where closure is used.
+ boost::function<int (void)> inc1 = inc(value1);
+ int value2 = 0;
+ boost::function<int (void)> inc2 = inc(value2);
+
+ BOOST_CHECK( inc1() == 1 );
+ BOOST_CHECK( inc1() == 2 );
+ BOOST_CHECK( inc2() == 1 );
+ BOOST_CHECK( inc1() == 3 );
+}
+

Added: trunk/libs/local_function/test/return_setget.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/return_setget.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,39 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#include <boost/function.hpp>
+#define BOOST_TEST_MODULE TestReturnSetGet
+#include <boost/test/unit_test.hpp>
+#include <string>
+
+boost::function<void (const std::string&)> set;
+boost::function<const std::string& (void)> get;
+
+void action(void) {
+ // State `message` hidden behind access functions from here.
+ BOOST_CHECK( get() == "abc" );
+ set("xyz");
+ BOOST_CHECK( get() == "xyz" );
+}
+
+BOOST_AUTO_TEST_CASE( test_return_setget ) {
+ std::string message = "abc"; // Reference valid where closure used.
+
+ void BOOST_LOCAL_FUNCTION(bind& message, const std::string& text) {
+ message = text;
+ } BOOST_LOCAL_FUNCTION_NAME(s)
+ set = s;
+
+ const std::string& BOOST_LOCAL_FUNCTION(const bind& message) {
+ return message;
+ } BOOST_LOCAL_FUNCTION_NAME(g)
+ get = g;
+
+ action();
+}
+

Added: trunk/libs/local_function/test/return_this.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/return_this.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,38 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#include <boost/function.hpp>
+#define BOOST_TEST_MODULE TestReturnThis
+#include <boost/test/unit_test.hpp>
+
+struct number {
+ number(int value) : value_(value) {}
+
+ boost::function<int (void)> inc(void) {
+ int BOOST_LOCAL_FUNCTION(bind this_) {
+ return ++this_->value_;
+ } BOOST_LOCAL_FUNCTION_NAME(i)
+ return i;
+ }
+
+private:
+ int value_;
+};
+
+BOOST_AUTO_TEST_CASE( test_return_this ) {
+ number n1 = 0; // Object valid in scope where closure is used.
+ boost::function<int (void)> inc1 = n1.inc();
+ number n2 = 0;
+ boost::function<int (void)> inc2 = n2.inc();
+
+ BOOST_CHECK( inc1() == 1 );
+ BOOST_CHECK( inc1() == 2 );
+ BOOST_CHECK( inc2() == 1 );
+ BOOST_CHECK( inc1() == 3 );
+}
+

Added: trunk/libs/local_function/test/seq.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/seq.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,166 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+
+//[seq
+struct s {
+ void f(double p = 1.23, double q = -1.23) {
+ { // Only params.
+ void BOOST_LOCAL_FUNCTION( (int x) (int y)(default 0) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(l)
+ l(1);
+ }
+ { // Only const binds.
+ int a, b;
+
+ const int& BOOST_LOCAL_FUNCTION( (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( (const bind this_) ) {
+ return *this_;
+ } BOOST_LOCAL_FUNCTION_NAME(t)
+ t();
+
+ const int BOOST_LOCAL_FUNCTION( (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( (bind c) (bind& d)
+ (bind& p) (bind& q) ) {
+ return d;
+ } BOOST_LOCAL_FUNCTION_NAME(l)
+ l();
+
+ s& BOOST_LOCAL_FUNCTION( (bind this_) ) {
+ return *this_;
+ } BOOST_LOCAL_FUNCTION_NAME(t)
+ t();
+
+ int BOOST_LOCAL_FUNCTION( (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( (const bind a) (const bind& b)
+ (const bind& p) (const bind q)
+ (int x) (int y)(default 0) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(l)
+ l(1);
+
+ void BOOST_LOCAL_FUNCTION( (const bind this_)
+ (int x) (int y)(default 0) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(t)
+ t(1);
+
+ void BOOST_LOCAL_FUNCTION( (const bind a) (const bind this_)
+ (const bind& b) (const bind& p) (const bind q)
+ (int x) (int y)(default 0) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(lt)
+ lt(1);
+ }
+ { // Both params and plain binds.
+ int c, d;
+
+ void BOOST_LOCAL_FUNCTION( (bind c) (bind& d) (bind& p) (bind q)
+ (int x) (int y)(default 0) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(l)
+ l(1);
+
+ void BOOST_LOCAL_FUNCTION( (bind this_)
+ (int x) (int y)(default 0) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(t)
+ t(1);
+
+ void BOOST_LOCAL_FUNCTION( (bind c) (bind& d)
+ (bind& p) (bind this_) (bind q)
+ (int x) (int y)(default 0) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(lt)
+ lt(1);
+ }
+ { // Both const and plain binds.
+ int a, b, c, d;
+
+ void BOOST_LOCAL_FUNCTION( (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( (const bind this_)
+ (bind c) (bind& d) (bind q) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(ct)
+ ct();
+ void BOOST_LOCAL_FUNCTION( (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( (const bind a) (const bind& b)
+ (const bind p) (bind this_) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(pt)
+ pt();
+ void BOOST_LOCAL_FUNCTION( (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(
+ (const bind a) (const bind& b) (const bind& p)
+ (bind c) (bind& d) (bind& q) (int x) (int y)(default 0) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(l)
+ l(1);
+
+ void BOOST_LOCAL_FUNCTION( (const bind this_)
+ (bind c) (bind& d) (bind& q)
+ (int x) (int y)(default 0) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(ct)
+ ct(1);
+ void BOOST_LOCAL_FUNCTION(
+ (const bind a) (const bind& b) (const bind& p)
+ (bind this_) (int x) (int y)(default 0) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(pt)
+ pt(1);
+
+ void BOOST_LOCAL_FUNCTION( (const bind a) (const bind this_)
+ (const bind& b) (const bind& p) (bind c) (bind& d)
+ (bind& q) (int x) (int y)(default 0) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(lct)
+ lct(1);
+ void BOOST_LOCAL_FUNCTION( (const bind a) (const bind& b)
+ (const bind& p) (bind c) (bind& d) (bind this_) (bind& q)
+ (int x) (int y)(default 0) ) {
+ } BOOST_LOCAL_FUNCTION_NAME(lpt)
+ lpt(1);
+ }
+ }
+};
+//]
+
+int main(void) {
+ s().f();
+ return 0;
+}
+

Added: trunk/libs/local_function/test/ten_void.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/ten_void.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,22 @@
+
+// Copyright (C) 2009-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/local_function
+
+#include <boost/local_function.hpp>
+#define BOOST_TEST_MODULE TestTenVoid
+#include <boost/test/unit_test.hpp>
+#include <iostream>
+
+BOOST_AUTO_TEST_CASE( test_ten_void ) {
+ //[ten_void
+ int BOOST_LOCAL_FUNCTION(void) { // No parameter.
+ return 10;
+ } BOOST_LOCAL_FUNCTION_NAME(ten)
+
+ BOOST_CHECK( ten() == 10 );
+ //]
+}
+

Added: trunk/libs/local_function/test/transform.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/transform.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,40 @@
+
+// Copyright (C) 2009-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/local_function
+
+#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 ) {
+ //[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(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: trunk/libs/local_function/test/typeof.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/typeof.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,34 @@
+
+// Copyright (C) 2009-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/local_function
+
+#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 ) {
+ //[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: trunk/libs/local_function/test/typeof_template.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/local_function/test/typeof_template.cpp 2012-02-14 19:36:53 EST (Tue, 14 Feb 2012)
@@ -0,0 +1,36 @@
+
+// Copyright (C) 2009-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/local_function
+
+#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>
+
+//[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) {
+ // Local function `TYPEOF` does not need `typename`.
+ BOOST_CONCEPT_ASSERT((Addable<typename boost::remove_reference<
+ BOOST_LOCAL_FUNCTION_TYPEOF(sum)>::type>));
+ sum += factor * num;
+ } 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