Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r76350 - sandbox/closure/libs/closure/test
From: lorcaminiti_at_[hidden]
Date: 2012-01-07 18:36:49


Author: lcaminiti
Date: 2012-01-07 18:36:48 EST (Sat, 07 Jan 2012)
New Revision: 76350
URL: http://svn.boost.org/trac/boost/changeset/76350

Log:
Adding return tests.
Added:
   sandbox/closure/libs/closure/test/Jamfile.v2 (contents, props changed)
   sandbox/closure/libs/closure/test/return_derivative.cpp (contents, props changed)
   sandbox/closure/libs/closure/test/return_factorial.cpp (contents, props changed)
   sandbox/closure/libs/closure/test/return_inc.cpp (contents, props changed)
   sandbox/closure/libs/closure/test/return_setget.cpp (contents, props changed)
   sandbox/closure/libs/closure/test/return_this.cpp (contents, props changed)
Properties modified:
   sandbox/closure/libs/closure/test/ (props changed)

Added: sandbox/closure/libs/closure/test/Jamfile.v2
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/closure/test/Jamfile.v2 2012-01-07 18:36:48 EST (Sat, 07 Jan 2012)
@@ -0,0 +1,11 @@
+
+import testing ;
+
+test-suite boost_closure_test :
+ [ run return_inc.cpp /boost//unit_test_framework ]
+ [ run return_this.cpp /boost//unit_test_framework ]
+ [ run return_derivative.cpp /boost//unit_test_framework ]
+ [ run return_setget.cpp /boost//unit_test_framework ]
+ [ run return_factorial.cpp /boost//unit_test_framework ]
+;
+

Added: sandbox/closure/libs/closure/test/return_derivative.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/closure/test/return_derivative.cpp 2012-01-07 18:36:48 EST (Sat, 07 Jan 2012)
@@ -0,0 +1,25 @@
+
+//[test_return_derivative_cpp
+#include <boost/closure.hpp>
+#include <boost/function.hpp>
+#define BOOST_TEST_MODULE ReturnDerivative
+#include <boost/test/unit_test.hpp>
+
+boost::function<int (int)> derivative(boost::function<int (int)>& f, int dx) {
+ int BOOST_CLOSURE(bind& f, const bind dx, int x) {
+ return (f(x + dx) - f(x)) / dx;
+ } BOOST_CLOSURE_END(deriv)
+ return deriv;
+}
+
+BOOST_AUTO_TEST_CASE( return_derivative ) {
+ int BOOST_CLOSURE(int x) {
+ return x + 4;
+ } BOOST_CLOSURE_END(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: sandbox/closure/libs/closure/test/return_factorial.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/closure/test/return_factorial.cpp 2012-01-07 18:36:48 EST (Sat, 07 Jan 2012)
@@ -0,0 +1,32 @@
+
+//[test_return_factorial_cpp
+#include <boost/closure.hpp>
+#include <boost/function.hpp>
+#define BOOST_TEST_MODULE ReturnFactorial
+#include <boost/test/unit_test.hpp>
+#include <vector>
+#include <iostream>
+
+boost::function<int (int)> factorial(std::vector<int>& steps) {
+ int BOOST_CLOSURE(bind& steps, int n) {
+ int result;
+ if(n < 2) result = 1;
+ else result = n * (*this)(n - 1); // Recursive call.
+ steps.push_back(result);
+ return result;
+ } BOOST_CLOSURE_END(fact)
+ return fact;
+}
+
+BOOST_AUTO_TEST_CASE( return_factorial ) {
+ std::vector<int> steps;
+ boost::function<int (int)> fact = factorial(steps);
+
+ int i = fact(4); BOOST_CHECK( i == 24 );
+ i = steps.at(0); BOOST_CHECK( i == 1 );
+ i = steps.at(1); BOOST_CHECK( i == 2 );
+ i = steps.at(2); BOOST_CHECK( i == 6 );
+ i = steps.at(3); BOOST_CHECK( i == 24 );
+}
+//]
+

Added: sandbox/closure/libs/closure/test/return_inc.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/closure/test/return_inc.cpp 2012-01-07 18:36:48 EST (Sat, 07 Jan 2012)
@@ -0,0 +1,27 @@
+
+//[test_return_inc_cpp
+#include <boost/closure.hpp>
+#include <boost/function.hpp>
+#define BOOST_TEST_MODULE ReturnInc
+#include <boost/test/unit_test.hpp>
+
+boost::function<int (void)> inc(int& value) {
+ int BOOST_CLOSURE(bind& value) {
+ return ++value;
+ } BOOST_CLOSURE_END(i)
+ return i;
+}
+
+BOOST_AUTO_TEST_CASE( 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: sandbox/closure/libs/closure/test/return_setget.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/closure/test/return_setget.cpp 2012-01-07 18:36:48 EST (Sat, 07 Jan 2012)
@@ -0,0 +1,35 @@
+
+//[test_return_setget_cpp
+#include <boost/closure.hpp>
+#include <boost/function.hpp>
+#define BOOST_TEST_MODULE ReturnSetGet
+#include <boost/test/unit_test.hpp>
+#include <string>
+
+boost::function<void (std::string const&)> set;
+boost::function<std::string const& (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( return_setget ) {
+ std::string message = "abc"; // Reference valid where closure used.
+
+ void BOOST_CLOSURE(bind& message, std::string const& text) {
+ message = text;
+ } BOOST_CLOSURE_END(s)
+ set = s;
+
+ std::string const& BOOST_CLOSURE(const bind& message) {
+ return message;
+ } BOOST_CLOSURE_END(g)
+ get = g;
+
+ action();
+}
+//]
+

Added: sandbox/closure/libs/closure/test/return_this.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/closure/test/return_this.cpp 2012-01-07 18:36:48 EST (Sat, 07 Jan 2012)
@@ -0,0 +1,34 @@
+
+//[test_return_this_cpp
+#include <boost/closure.hpp>
+#include <boost/function.hpp>
+#define BOOST_TEST_MODULE ReturnThis
+#include <boost/test/unit_test.hpp>
+
+struct number {
+ number(int value) : value_(value) {}
+
+ boost::function<int (void)> inc(void) {
+ int BOOST_CLOSURE(bind this_) {
+ return ++this_->value_;
+ } BOOST_CLOSURE_END(i)
+ return i;
+ }
+
+private:
+ int value_;
+};
+
+BOOST_AUTO_TEST_CASE( 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 );
+}
+//]
+


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