mirror of
https://github.com/boostorg/lambda.git
synced 2026-01-21 17:02:36 +00:00
Compare commits
94 Commits
svn-branch
...
svn-branch
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dcb6e93e10 | ||
|
|
886b65f3d5 | ||
|
|
9e45d49139 | ||
|
|
e9ed0829b2 | ||
|
|
17df366d20 | ||
|
|
485bcbdcd8 | ||
|
|
023996e106 | ||
|
|
3bd135028e | ||
|
|
b6b0b9a03f | ||
|
|
0f0c53b1f9 | ||
|
|
24ff929eb1 | ||
|
|
5031c63f73 | ||
|
|
a7ac9a8991 | ||
|
|
8a19479625 | ||
|
|
4add858581 | ||
|
|
14b8227ca6 | ||
|
|
5ecd79ed2e | ||
|
|
d718b75da3 | ||
|
|
f9689cc6a4 | ||
|
|
a80b8a59a0 | ||
|
|
b99048eab3 | ||
|
|
ec6cc563b1 | ||
|
|
0196c57a0a | ||
|
|
282825b784 | ||
|
|
b8d328c0fc | ||
|
|
c5704cd6e3 | ||
|
|
6f607b3eef | ||
|
|
8bb8820adb | ||
|
|
89fb84d751 | ||
|
|
be2b0df062 | ||
|
|
666a3eca2d | ||
|
|
95d865285e | ||
|
|
ec38046886 | ||
|
|
c713dc5e0c | ||
|
|
ff04032656 | ||
|
|
6f63b3770f | ||
|
|
22e7b9c779 | ||
|
|
68be6f6563 | ||
|
|
02314ab550 | ||
|
|
9f032a7301 | ||
|
|
da56b773a0 | ||
|
|
cd605ff48d | ||
|
|
641babedf8 | ||
|
|
f9a6b38845 | ||
|
|
846a406b7a | ||
|
|
29794ee654 | ||
|
|
f74aae1241 | ||
|
|
624a204462 | ||
|
|
b635d13d2c | ||
|
|
403d82da66 | ||
|
|
0f29970c8b | ||
|
|
05b9bd64c4 | ||
|
|
e1b36955b7 | ||
|
|
c6030ac58d | ||
|
|
e6edfdf997 | ||
|
|
aabd832db1 | ||
|
|
2aec95e806 | ||
|
|
95b82465e0 | ||
|
|
c5dafd3db8 | ||
|
|
fefd81ede1 | ||
|
|
a120795474 | ||
|
|
94292237b4 | ||
|
|
fbe72097b4 | ||
|
|
2f24d3f261 | ||
|
|
e057f63a54 | ||
|
|
6f32c54b5d | ||
|
|
3d659d2c3b | ||
|
|
e9721da0d0 | ||
|
|
f329a3bf68 | ||
|
|
a396be0bf6 | ||
|
|
eeb2c53b56 | ||
|
|
fcf0d30c4e | ||
|
|
d65bfa94ca | ||
|
|
2942562072 | ||
|
|
01031ccddd | ||
|
|
efdcc46d3a | ||
|
|
d60ae200a6 | ||
|
|
ff11fbb20e | ||
|
|
705c230963 | ||
|
|
cd444c824a | ||
|
|
50d9654200 | ||
|
|
937816e76e | ||
|
|
bc350d53bf | ||
|
|
34133ee940 | ||
|
|
38ffca82c9 | ||
|
|
1a2fb78541 | ||
|
|
b6d1367cec | ||
|
|
ec9ba2becf | ||
|
|
2bdf3738c1 | ||
|
|
c11fb1fe36 | ||
|
|
59ad507d3f | ||
|
|
10c4216ddc | ||
|
|
15baf78a58 | ||
|
|
f2b02864f9 |
5
doc/Jamfile.v2
Normal file
5
doc/Jamfile.v2
Normal file
@@ -0,0 +1,5 @@
|
||||
project boost/doc ;
|
||||
import boostbook : boostbook ;
|
||||
|
||||
boostbook lambda-doc : lambda.xml ;
|
||||
|
||||
7
doc/detail/README
Normal file
7
doc/detail/README
Normal file
@@ -0,0 +1,7 @@
|
||||
- lambda_doc.xml is a DocBook xml file from which the lambda docs are
|
||||
generated
|
||||
- lambda_doc_chunks.xsl loads the stylesheets that generate a separate
|
||||
html-file for each section
|
||||
- lambda_doc.xsl loads stylesheets that generate one big html-file
|
||||
(you need to edit the paths in these files to make them work)
|
||||
|
||||
3456
doc/detail/lambda_doc.xml
Executable file
3456
doc/detail/lambda_doc.xml
Executable file
File diff suppressed because it is too large
Load Diff
19
doc/detail/lambda_doc.xsl
Normal file
19
doc/detail/lambda_doc.xsl
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version='1.0'?>
|
||||
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
||||
version='1.0'
|
||||
xmlns="http://www.w3.org/TR/xhtml1/transitional"
|
||||
exclude-result-prefixes="#default">
|
||||
|
||||
<xsl:import href="/u/jajarvi/dtd/docbook-xsl/html/docbook.xsl"/>
|
||||
|
||||
|
||||
<!-- Add other variable definitions here -->
|
||||
|
||||
<xsl:variable name="shade.verbatim">0</xsl:variable>
|
||||
|
||||
<xsl:variable name="section.autolabel">1</xsl:variable>
|
||||
|
||||
<xsl:variable name="bibliography.collection">lambda_bib.xml</xsl:variable>
|
||||
|
||||
|
||||
</xsl:stylesheet>
|
||||
19
doc/detail/lambda_doc_chunks.xsl
Normal file
19
doc/detail/lambda_doc_chunks.xsl
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version='1.0'?>
|
||||
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
||||
version='1.0'
|
||||
xmlns="http://www.w3.org/TR/xhtml1/transitional"
|
||||
exclude-result-prefixes="#default">
|
||||
|
||||
<xsl:import href="/u/jajarvi/dtd/docbook-xsl/html/chunk.xsl"/>
|
||||
|
||||
|
||||
<!-- Add other variable definitions here -->
|
||||
|
||||
<xsl:variable name="shade.verbatim">0</xsl:variable>
|
||||
|
||||
<xsl:variable name="section.autolabel">1</xsl:variable>
|
||||
|
||||
<xsl:variable name="bibliography.collection">lambda_bib.xml</xsl:variable>
|
||||
|
||||
|
||||
</xsl:stylesheet>
|
||||
8
doc/index.html
Normal file
8
doc/index.html
Normal file
@@ -0,0 +1,8 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0; URL=../../../doc/html/lambda.html">
|
||||
</head>
|
||||
<body>
|
||||
Automatic redirection failed, please go to <a href="../../../doc/html/lambda.html">www.boost.org/doc/html/lambda.html</a>
|
||||
</body>
|
||||
</html>
|
||||
3451
doc/lambda.xml
Normal file
3451
doc/lambda.xml
Normal file
File diff suppressed because it is too large
Load Diff
8
index.html
Normal file
8
index.html
Normal file
@@ -0,0 +1,8 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0; URL=../../doc/html/lambda.html">
|
||||
</head>
|
||||
<body>
|
||||
Automatic redirection failed, please go to <a href="../../doc/html/lambda.html">www.boost.org/doc/html/lambda.html</a>
|
||||
</body>
|
||||
</html>
|
||||
59
test/Jamfile
Normal file
59
test/Jamfile
Normal file
@@ -0,0 +1,59 @@
|
||||
# Lambda library
|
||||
|
||||
# Copyright (C) 2001-2003 Jaakko Järvi
|
||||
|
||||
# Use, modification and distribution is subject to 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)
|
||||
|
||||
# For more information, see http://www.boost.org/
|
||||
|
||||
|
||||
subproject libs/lambda/test ;
|
||||
|
||||
# bring in rules for testing
|
||||
SEARCH on testing.jam = $(BOOST_BUILD_PATH) ;
|
||||
include testing.jam ;
|
||||
|
||||
# Make tests run by default.
|
||||
DEPENDS all : test ;
|
||||
|
||||
{
|
||||
# look in BOOST_ROOT for sources first, just in this Jamfile
|
||||
local SEARCH_SOURCE = $(BOOST_ROOT) $(SEARCH_SOURCE) ;
|
||||
|
||||
test-suite lambda
|
||||
:
|
||||
[ run libs/lambda/test/algorithm_test.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/bind_tests_simple.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/bind_tests_advanced.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/bind_tests_simple_f_refs.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/bll_and_function.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/cast_test.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/constructor_tests.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/control_structures.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/exception_test.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/extending_rt_traits.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/is_instance_of_test.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/member_pointer_test.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/operator_tests_simple.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/phoenix_control_structures.cpp : : : : ]
|
||||
|
||||
[ run libs/lambda/test/switch_construct.cpp : : : : ]
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
89
test/Makefile
Executable file
89
test/Makefile
Executable file
@@ -0,0 +1,89 @@
|
||||
BOOST = ../../..
|
||||
|
||||
CXX = gcc
|
||||
EXTRAFLAGS = -pedantic -Wno-long-long -ftemplate-depth-50
|
||||
LIBS = -lstdc++
|
||||
|
||||
#CXX = KCC
|
||||
#EXTRAFLAGS = --strict --display_error_number --diag_suppress 450 --max_pending_instantiations 50
|
||||
#LIBS =
|
||||
|
||||
INCLUDES = -I$(BOOST)
|
||||
|
||||
|
||||
|
||||
CXXFLAGS = $(INCLUDES) $(EXTRAFLAGS)
|
||||
|
||||
LIBFLAGS = $(LIBS)
|
||||
|
||||
|
||||
AR = ar
|
||||
|
||||
.SUFFIXES: .cpp .o
|
||||
|
||||
SOURCES = \
|
||||
is_instance_of_test.cpp \
|
||||
operator_tests_simple.cpp \
|
||||
member_pointer_test.cpp \
|
||||
control_structures.cpp \
|
||||
switch_construct.cpp \
|
||||
bind_tests_simple.cpp \
|
||||
bind_tests_advanced.cpp \
|
||||
bll_and_function.cpp \
|
||||
constructor_tests.cpp \
|
||||
extending_rt_traits.cpp \
|
||||
bind_tests_simple_f_refs.cpp \
|
||||
cast_test.cpp \
|
||||
phoenix_control_structures.cpp \
|
||||
exception_test.cpp \
|
||||
|
||||
|
||||
# Create lists of object files from the source file lists.
|
||||
|
||||
OBJECTS = ${SOURCES:.cpp=.o}
|
||||
|
||||
TARGETS = ${SOURCES:.cpp=.exe}
|
||||
|
||||
all: $(TARGETS)
|
||||
|
||||
%.exe: %.o
|
||||
$(CXX) $(LIBFLAGS) $(CXXFLAGS) -o $@ $<
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -o $@ -c $<
|
||||
|
||||
%.dep: %.cpp
|
||||
set -e; $(CXX) -M $(INCLUDES) -c $< \
|
||||
| sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \
|
||||
[ -s $@ ] || rm -f $@
|
||||
|
||||
DEP_FILES = $(SOURCES:.cpp=.dep)
|
||||
|
||||
include $(DEP_FILES)
|
||||
|
||||
clean:
|
||||
/bin/rm -rf $(TARGETS) $(OBJECTS) $(DEP_FILES)
|
||||
|
||||
run:
|
||||
./is_instance_of_test.exe
|
||||
./member_pointer_test.exe
|
||||
./operator_tests_simple.exe
|
||||
./control_structures.exe
|
||||
./switch_construct.exe
|
||||
./extending_rt_traits.exe
|
||||
./constructor_tests.exe
|
||||
./cast_test.exe
|
||||
./bind_tests_simple.exe
|
||||
./bind_tests_advanced.exe
|
||||
./bll_and_function.exe
|
||||
./bind_tests_simple_f_refs.exe
|
||||
./phoenix_control_structures.exe
|
||||
./exception_test.exe
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
6
test/README_gcc2.9x_users
Normal file
6
test/README_gcc2.9x_users
Normal file
@@ -0,0 +1,6 @@
|
||||
gcc 2.96
|
||||
cannot compile
|
||||
|
||||
exception_test.cpp (internal compiler error)
|
||||
|
||||
|
||||
60
test/algorithm_test.cpp
Normal file
60
test/algorithm_test.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
// bll_and_function.cpp - The Boost Lambda Library -----------------------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// test using BLL and boost::function
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
#include "boost/lambda/lambda.hpp"
|
||||
#include "boost/lambda/bind.hpp"
|
||||
#include "boost/lambda/algorithm.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
||||
|
||||
void test_foreach() {
|
||||
using namespace boost::lambda;
|
||||
|
||||
int a[10][20];
|
||||
int sum = 0;
|
||||
|
||||
std::for_each(a, a + 10,
|
||||
bind(ll::for_each(), _1, _1 + 20,
|
||||
protect((_1 = var(sum), ++var(sum)))));
|
||||
|
||||
sum = 0;
|
||||
std::for_each(a, a + 10,
|
||||
bind(ll::for_each(), _1, _1 + 20,
|
||||
protect((sum += _1))));
|
||||
|
||||
BOOST_TEST(sum == (199 + 1)/ 2 * 199);
|
||||
}
|
||||
|
||||
// More tests needed (for all algorithms)
|
||||
|
||||
int test_main(int, char *[]) {
|
||||
|
||||
test_foreach();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
376
test/bind_tests_advanced.cpp
Normal file
376
test/bind_tests_advanced.cpp
Normal file
@@ -0,0 +1,376 @@
|
||||
// bind_tests_advanced.cpp -- The Boost Lambda Library ------------------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
#include "boost/lambda/lambda.hpp"
|
||||
#include "boost/lambda/bind.hpp"
|
||||
|
||||
|
||||
#include "boost/any.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
using namespace boost::lambda;
|
||||
|
||||
int sum_0() { return 0; }
|
||||
int sum_1(int a) { return a; }
|
||||
int sum_2(int a, int b) { return a+b; }
|
||||
|
||||
int product_2(int a, int b) { return a*b; }
|
||||
|
||||
// unary function that returns a pointer to a binary function
|
||||
typedef int (*fptr_type)(int, int);
|
||||
fptr_type sum_or_product(bool x) {
|
||||
return x ? sum_2 : product_2;
|
||||
}
|
||||
|
||||
// a nullary functor that returns a pointer to a unary function that
|
||||
// returns a pointer to a binary function.
|
||||
struct which_one {
|
||||
typedef fptr_type (*result_type)(bool x);
|
||||
template <class T> struct sig { typedef result_type type; };
|
||||
|
||||
result_type operator()() const { return sum_or_product; }
|
||||
};
|
||||
|
||||
void test_nested_binds()
|
||||
{
|
||||
int j = 2; int k = 3;
|
||||
|
||||
// bind calls can be nested (the target function can be a lambda functor)
|
||||
// The interpretation is, that the innermost lambda functor returns something
|
||||
// that is bindable (another lambda functor, function pointer ...)
|
||||
bool condition;
|
||||
|
||||
condition = true;
|
||||
BOOST_TEST(bind(bind(&sum_or_product, _1), 1, 2)(condition)==3);
|
||||
BOOST_TEST(bind(bind(&sum_or_product, _1), _2, _3)(condition, j, k)==5);
|
||||
|
||||
condition = false;
|
||||
BOOST_TEST(bind(bind(&sum_or_product, _1), 1, 2)(condition)==2);
|
||||
BOOST_TEST(bind(bind(&sum_or_product, _1), _2, _3)(condition, j, k)==6);
|
||||
|
||||
|
||||
which_one wo;
|
||||
BOOST_TEST(bind(bind(bind(wo), _1), _2, _3)(condition, j, k)==6);
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// unlambda -------------------------------------------------
|
||||
|
||||
// Sometimes it may be necessary to prevent the argument substitution of
|
||||
// taking place. For example, we may end up with a nested bind expression
|
||||
// inadvertently when using the target function is received as a parameter
|
||||
|
||||
template<class F>
|
||||
int call_with_100(const F& f) {
|
||||
|
||||
|
||||
|
||||
// bind(f, _1)(make_const(100));
|
||||
// This would result in;
|
||||
// bind(_1 + 1, _1)(make_const(100)) , which would be a compile time error
|
||||
|
||||
return bind(unlambda(f), _1)(make_const(100));
|
||||
|
||||
// for other functors than lambda functors, unlambda has no effect
|
||||
// (except for making them const)
|
||||
}
|
||||
|
||||
template<class F>
|
||||
int call_with_101(const F& f) {
|
||||
|
||||
return bind(unlambda(f), _1)(make_const(101));
|
||||
|
||||
}
|
||||
|
||||
|
||||
void test_unlambda() {
|
||||
|
||||
int i = 1;
|
||||
|
||||
BOOST_TEST(unlambda(_1 + _2)(i, i) == 2);
|
||||
BOOST_TEST(unlambda(++var(i))() == 2);
|
||||
BOOST_TEST(call_with_100(_1 + 1) == 101);
|
||||
|
||||
|
||||
BOOST_TEST(call_with_101(_1 + 1) == 102);
|
||||
|
||||
BOOST_TEST(call_with_100(bind(std_functor(std::bind1st(std::plus<int>(), 1)), _1)) == 101);
|
||||
|
||||
// std_functor insturcts LL that the functor defines a result_type typedef
|
||||
// rather than a sig template.
|
||||
bind(std_functor(std::plus<int>()), _1, _2)(i, i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// protect ------------------------------------------------------------
|
||||
|
||||
// protect protects a lambda functor from argument substitution.
|
||||
// protect is useful e.g. with nested stl algorithm calls.
|
||||
|
||||
namespace ll {
|
||||
|
||||
struct for_each {
|
||||
|
||||
// note, std::for_each returns it's last argument
|
||||
// We want the same behaviour from our ll::for_each.
|
||||
// However, the functor can be called with any arguments, and
|
||||
// the return type thus depends on the argument types.
|
||||
|
||||
// 1. Provide a sig class member template:
|
||||
|
||||
// The return type deduction system instantiate this class as:
|
||||
// sig<Args>::type, where Args is a boost::tuples::cons-list
|
||||
// The head type is the function object type itself
|
||||
// cv-qualified (so it is possilbe to provide different return types
|
||||
// for differently cv-qualified operator()'s.
|
||||
|
||||
// The tail type is the list of the types of the actual arguments the
|
||||
// function was called with.
|
||||
// So sig should contain a typedef type, which defines a mapping from
|
||||
// the operator() arguments to its return type.
|
||||
// Note, that it is possible to provide different sigs for the same functor
|
||||
// if the functor has several operator()'s, even if they have different
|
||||
// number of arguments.
|
||||
|
||||
// Note, that the argument types in Args are guaranteed to be non-reference
|
||||
// types, but they can have cv-qualifiers.
|
||||
|
||||
template <class Args>
|
||||
struct sig {
|
||||
typedef typename boost::remove_const<
|
||||
typename boost::tuples::element<3, Args>::type
|
||||
>::type type;
|
||||
};
|
||||
|
||||
template <class A, class B, class C>
|
||||
C
|
||||
operator()(const A& a, const B& b, const C& c) const
|
||||
{ return std::for_each(a, b, c);}
|
||||
};
|
||||
|
||||
} // end of ll namespace
|
||||
|
||||
void test_protect()
|
||||
{
|
||||
int i = 0;
|
||||
int b[3][5];
|
||||
int* a[3];
|
||||
|
||||
for(int j=0; j<3; ++j) a[j] = b[j];
|
||||
|
||||
std::for_each(a, a+3,
|
||||
bind(ll::for_each(), _1, _1 + 5, protect(_1 = ++var(i))));
|
||||
|
||||
// This is how you could output the values (it is uncommented, no output
|
||||
// from a regression test file):
|
||||
// std::for_each(a, a+3,
|
||||
// bind(ll::for_each(), _1, _1 + 5,
|
||||
// std::cout << constant("\nLine ") << (&_1 - a) << " : "
|
||||
// << protect(_1)
|
||||
// )
|
||||
// );
|
||||
|
||||
int sum = 0;
|
||||
|
||||
std::for_each(a, a+3,
|
||||
bind(ll::for_each(), _1, _1 + 5,
|
||||
protect(sum += _1))
|
||||
);
|
||||
BOOST_TEST(sum == (1+15)*15/2);
|
||||
|
||||
sum = 0;
|
||||
|
||||
std::for_each(a, a+3,
|
||||
bind(ll::for_each(), _1, _1 + 5,
|
||||
sum += 1 + protect(_1)) // add element count
|
||||
);
|
||||
BOOST_TEST(sum == (1+15)*15/2 + 15);
|
||||
|
||||
(1 + protect(_1))(sum);
|
||||
|
||||
int k = 0;
|
||||
((k += constant(1)) += protect(constant(2)))();
|
||||
BOOST_TEST(k==1);
|
||||
|
||||
k = 0;
|
||||
((k += constant(1)) += protect(constant(2)))()();
|
||||
BOOST_TEST(k==3);
|
||||
|
||||
// note, the following doesn't work:
|
||||
|
||||
// ((var(k) = constant(1)) = protect(constant(2)))();
|
||||
|
||||
// (var(k) = constant(1))() returns int& and thus the
|
||||
// second assignment fails.
|
||||
|
||||
// We should have something like:
|
||||
// bind(var, var(k) = constant(1)) = protect(constant(2)))();
|
||||
// But currently var is not bindable.
|
||||
|
||||
// The same goes with ret. A bindable ret could be handy sometimes as well
|
||||
// (protect(std::cout << _1), std::cout << _1)(i)(j); does not work
|
||||
// because the comma operator tries to store the result of the evaluation
|
||||
// of std::cout << _1 as a copy (and you can't copy std::ostream).
|
||||
// something like this:
|
||||
// (protect(std::cout << _1), bind(ref, std::cout << _1))(i)(j);
|
||||
|
||||
|
||||
// the stuff below works, but we do not want extra output to
|
||||
// cout, must be changed to stringstreams but stringstreams do not
|
||||
// work due to a bug in the type deduction. Will be fixed...
|
||||
#if 0
|
||||
// But for now, ref is not bindable. There are other ways around this:
|
||||
|
||||
int x = 1, y = 2;
|
||||
(protect(std::cout << _1), (std::cout << _1, 0))(x)(y);
|
||||
|
||||
// added one dummy value to make the argument to comma an int
|
||||
// instead of ostream&
|
||||
|
||||
// Note, the same problem is more apparent without protect
|
||||
// (std::cout << 1, std::cout << constant(2))(); // does not work
|
||||
|
||||
(boost::ref(std::cout << 1), std::cout << constant(2))(); // this does
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
void test_lambda_functors_as_arguments_to_lambda_functors() {
|
||||
|
||||
// lambda functor is a function object, and can therefore be used
|
||||
// as an argument to another lambda functors function call object.
|
||||
|
||||
// Note however, that the argument/type substitution is not entered again.
|
||||
// This means, that something like this will not work:
|
||||
|
||||
(_1 + _2)(_1, make_const(7));
|
||||
(_1 + _2)(bind(&sum_0), make_const(7));
|
||||
|
||||
// or it does work, but the effect is not to call
|
||||
// sum_0() + 7, but rather
|
||||
// bind(sum_0) + 7, which results in another lambda functor
|
||||
// (lambda functor + int) and can be called again
|
||||
BOOST_TEST((_1 + _2)(bind(&sum_0), make_const(7))() == 7);
|
||||
|
||||
int i = 3, j = 12;
|
||||
BOOST_TEST((_1 - _2)(_2, _1)(i, j) == j - i);
|
||||
|
||||
// also, note that lambda functor are no special case for bind if received
|
||||
// as a parameter. In oder to be bindable, the functor must
|
||||
// defint the sig template, or then
|
||||
// the return type must be defined within the bind call. Lambda functors
|
||||
// do define the sig template, so if the return type deduction system
|
||||
// covers the case, there is no need to specify the return type
|
||||
// explicitly.
|
||||
|
||||
int a = 5, b = 6;
|
||||
|
||||
// Let type deduction find out the return type
|
||||
BOOST_TEST(bind(_1, _2, _3)(unlambda(_1 + _2), a, b) == 11);
|
||||
|
||||
//specify it yourself:
|
||||
BOOST_TEST(bind(_1, _2, _3)(ret<int>(_1 + _2), a, b) == 11);
|
||||
BOOST_TEST(ret<int>(bind(_1, _2, _3))(_1 + _2, a, b) == 11);
|
||||
BOOST_TEST(bind<int>(_1, _2, _3)(_1 + _2, a, b) == 11);
|
||||
|
||||
bind(_1,1.0)(_1+_1);
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void test_const_parameters() {
|
||||
|
||||
// (_1 + _2)(1, 2); // this would fail,
|
||||
|
||||
// Either make arguments const:
|
||||
BOOST_TEST((_1 + _2)(make_const(1), make_const(2)) == 3);
|
||||
|
||||
// Or use const_parameters:
|
||||
BOOST_TEST(const_parameters(_1 + _2)(1, 2) == 3);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void test_rvalue_arguments()
|
||||
{
|
||||
// Not quite working yet.
|
||||
// Problems with visual 7.1
|
||||
// BOOST_TEST((_1 + _2)(1, 2) == 3);
|
||||
}
|
||||
|
||||
void test_break_const()
|
||||
{
|
||||
|
||||
// break_const is currently unnecessary, as LL supports perfect forwarding
|
||||
// for up to there argument lambda functors, and LL does not support
|
||||
// lambda functors with more than 3 args.
|
||||
|
||||
// I'll keep the test case around anyway, if more arguments will be supported
|
||||
// in the future.
|
||||
|
||||
|
||||
|
||||
// break_const breaks constness! Be careful!
|
||||
// You need this only if you need to have side effects on some argument(s)
|
||||
// and some arguments are non-const rvalues and your lambda functors
|
||||
// take more than 3 arguments.
|
||||
|
||||
|
||||
int i = 1;
|
||||
// OLD COMMENT: (_1 += _2)(i, 2) // fails, 2 is a non-const rvalue
|
||||
// OLD COMMENT: const_parameters(_1 += _2)(i, 2) // fails, side-effect to i
|
||||
break_const(_1 += _2)(i, 2); // ok
|
||||
BOOST_TEST(i == 3);
|
||||
}
|
||||
|
||||
int test_main(int, char *[]) {
|
||||
|
||||
test_nested_binds();
|
||||
test_unlambda();
|
||||
test_protect();
|
||||
test_lambda_functors_as_arguments_to_lambda_functors();
|
||||
test_const_parameters();
|
||||
test_rvalue_arguments();
|
||||
test_break_const();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
172
test/bind_tests_simple.cpp
Normal file
172
test/bind_tests_simple.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
// bind_tests_simple.cpp -- The Boost Lambda Library ------------------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
#include "boost/lambda/bind.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace boost::lambda;
|
||||
|
||||
|
||||
int sum_of_args_0() { return 0; }
|
||||
int sum_of_args_1(int a) { return a; }
|
||||
int sum_of_args_2(int a, int b) { return a+b; }
|
||||
int sum_of_args_3(int a, int b, int c) { return a+b+c; }
|
||||
int sum_of_args_4(int a, int b, int c, int d) { return a+b+c+d; }
|
||||
int sum_of_args_5(int a, int b, int c, int d, int e) { return a+b+c+d+e; }
|
||||
int sum_of_args_6(int a, int b, int c, int d, int e, int f) { return a+b+c+d+e+f; }
|
||||
int sum_of_args_7(int a, int b, int c, int d, int e, int f, int g) { return a+b+c+d+e+f+g; }
|
||||
int sum_of_args_8(int a, int b, int c, int d, int e, int f, int g, int h) { return a+b+c+d+e+f+g+h; }
|
||||
int sum_of_args_9(int a, int b, int c, int d, int e, int f, int g, int h, int i) { return a+b+c+d+e+f+g+h+i; }
|
||||
|
||||
|
||||
// ----------------------------
|
||||
|
||||
class A {
|
||||
int i;
|
||||
public:
|
||||
A(int n) : i(n) {};
|
||||
int add(const int& j) { return i + j; }
|
||||
int add2(int a1, int a2) { return i + a1 + a2; }
|
||||
int add3(int a1, int a2, int a3) { return i + a1 + a2 + a3; }
|
||||
int add4(int a1, int a2, int a3, int a4) { return i + a1 + a2 + a3 + a4; }
|
||||
int add5(int a1, int a2, int a3, int a4, int a5)
|
||||
{ return i + a1 + a2 + a3 + a4 + a5; }
|
||||
int add6(int a1, int a2, int a3, int a4, int a5, int a6)
|
||||
{ return i + a1 + a2 + a3 + a4 + a5 + a6; }
|
||||
int add7(int a1, int a2, int a3, int a4, int a5, int a6, int a7)
|
||||
{ return i + a1 + a2 + a3 + a4 + a5 + a6 + a7; }
|
||||
int add8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
|
||||
{ return i + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8; }
|
||||
|
||||
};
|
||||
|
||||
void test_member_functions()
|
||||
{
|
||||
using boost::ref;
|
||||
A a(10);
|
||||
int i = 1;
|
||||
|
||||
|
||||
|
||||
|
||||
BOOST_TEST(bind(&A::add, ref(a), _1)(i) == 11);
|
||||
BOOST_TEST(bind(&A::add, &a, _1)(i) == 11);
|
||||
BOOST_TEST(bind(&A::add, _1, 1)(a) == 11);
|
||||
BOOST_TEST(bind(&A::add, _1, 1)(make_const(&a)) == 11);
|
||||
|
||||
BOOST_TEST(bind(&A::add2, _1, 1, 1)(a) == 12);
|
||||
BOOST_TEST(bind(&A::add3, _1, 1, 1, 1)(a) == 13);
|
||||
BOOST_TEST(bind(&A::add4, _1, 1, 1, 1, 1)(a) == 14);
|
||||
BOOST_TEST(bind(&A::add5, _1, 1, 1, 1, 1, 1)(a) == 15);
|
||||
BOOST_TEST(bind(&A::add6, _1, 1, 1, 1, 1, 1, 1)(a) == 16);
|
||||
BOOST_TEST(bind(&A::add7, _1, 1, 1, 1, 1, 1, 1, 1)(a) == 17);
|
||||
BOOST_TEST(bind(&A::add8, _1, 1, 1, 1, 1, 1, 1, 1, 1)(a) == 18);
|
||||
|
||||
// This should fail, as lambda functors store arguments as const
|
||||
// bind(&A::add, a, _1);
|
||||
}
|
||||
|
||||
int test_main(int, char *[]) {
|
||||
|
||||
int i = 1; int j = 2; int k = 3;
|
||||
int result;
|
||||
|
||||
// bind all parameters
|
||||
BOOST_TEST(bind(&sum_of_args_0)()==0);
|
||||
BOOST_TEST(bind(&sum_of_args_1, 1)()==1);
|
||||
BOOST_TEST(bind(&sum_of_args_2, 1, 2)()==3);
|
||||
BOOST_TEST(bind(&sum_of_args_3, 1, 2, 3)()==6);
|
||||
BOOST_TEST(bind(&sum_of_args_4, 1, 2, 3, 4)()==10);
|
||||
BOOST_TEST(bind(&sum_of_args_5, 1, 2, 3, 4, 5)()==15);
|
||||
BOOST_TEST(bind(&sum_of_args_6, 1, 2, 3, 4, 5, 6)()==21);
|
||||
BOOST_TEST(bind(&sum_of_args_7, 1, 2, 3, 4, 5, 6, 7)()==28);
|
||||
BOOST_TEST(bind(&sum_of_args_8, 1, 2, 3, 4, 5, 6, 7, 8)()==36);
|
||||
BOOST_TEST(bind(&sum_of_args_9, 1, 2, 3, 4, 5, 6, 7, 8, 9)()==45);
|
||||
|
||||
// first parameter open
|
||||
BOOST_TEST(bind(&sum_of_args_0)()==0);
|
||||
BOOST_TEST(bind(&sum_of_args_1, _1)(i)==1);
|
||||
BOOST_TEST(bind(&sum_of_args_2, _1, 2)(i)==3);
|
||||
BOOST_TEST(bind(&sum_of_args_3, _1, 2, 3)(i)==6);
|
||||
BOOST_TEST(bind(&sum_of_args_4, _1, 2, 3, 4)(i)==10);
|
||||
BOOST_TEST(bind(&sum_of_args_5, _1, 2, 3, 4, 5)(i)==15);
|
||||
BOOST_TEST(bind(&sum_of_args_6, _1, 2, 3, 4, 5, 6)(i)==21);
|
||||
BOOST_TEST(bind(&sum_of_args_7, _1, 2, 3, 4, 5, 6, 7)(i)==28);
|
||||
BOOST_TEST(bind(&sum_of_args_8, _1, 2, 3, 4, 5, 6, 7, 8)(i)==36);
|
||||
BOOST_TEST(bind(&sum_of_args_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i)==45);
|
||||
|
||||
// two open arguments
|
||||
BOOST_TEST(bind(&sum_of_args_0)()==0);
|
||||
BOOST_TEST(bind(&sum_of_args_1, _1)(i)==1);
|
||||
BOOST_TEST(bind(&sum_of_args_2, _1, _2)(i, j)==3);
|
||||
BOOST_TEST(bind(&sum_of_args_3, _1, _2, 3)(i, j)==6);
|
||||
BOOST_TEST(bind(&sum_of_args_4, _1, _2, 3, 4)(i, j)==10);
|
||||
BOOST_TEST(bind(&sum_of_args_5, _1, _2, 3, 4, 5)(i, j)==15);
|
||||
BOOST_TEST(bind(&sum_of_args_6, _1, _2, 3, 4, 5, 6)(i, j)==21);
|
||||
BOOST_TEST(bind(&sum_of_args_7, _1, _2, 3, 4, 5, 6, 7)(i, j)==28);
|
||||
BOOST_TEST(bind(&sum_of_args_8, _1, _2, 3, 4, 5, 6, 7, 8)(i, j)==36);
|
||||
BOOST_TEST(bind(&sum_of_args_9, _1, _2, 3, 4, 5, 6, 7, 8, 9)(i, j)==45);
|
||||
|
||||
// three open arguments
|
||||
BOOST_TEST(bind(&sum_of_args_0)()==0);
|
||||
BOOST_TEST(bind(&sum_of_args_1, _1)(i)==1);
|
||||
BOOST_TEST(bind(&sum_of_args_2, _1, _2)(i, j)==3);
|
||||
BOOST_TEST(bind(&sum_of_args_3, _1, _2, _3)(i, j, k)==6);
|
||||
BOOST_TEST(bind(&sum_of_args_4, _1, _2, _3, 4)(i, j, k)==10);
|
||||
BOOST_TEST(bind(&sum_of_args_5, _1, _2, _3, 4, 5)(i, j, k)==15);
|
||||
BOOST_TEST(bind(&sum_of_args_6, _1, _2, _3, 4, 5, 6)(i, j, k)==21);
|
||||
BOOST_TEST(bind(&sum_of_args_7, _1, _2, _3, 4, 5, 6, 7)(i, j, k)==28);
|
||||
BOOST_TEST(bind(&sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8)(i, j, k)==36);
|
||||
BOOST_TEST(bind(&sum_of_args_9, _1, _2, _3, 4, 5, 6, 7, 8, 9)(i, j, k)==45);
|
||||
|
||||
// function compositions with bind
|
||||
BOOST_TEST(bind(&sum_of_args_3, bind(&sum_of_args_2, _1, 2), 2, 3)(i)==8);
|
||||
BOOST_TEST(
|
||||
bind(&sum_of_args_9,
|
||||
bind(&sum_of_args_0), // 0
|
||||
bind(&sum_of_args_1, _1), // 1
|
||||
bind(&sum_of_args_2, _1, _2), // 3
|
||||
bind(&sum_of_args_3, _1, _2, _3), // 6
|
||||
bind(&sum_of_args_4, _1, _2, _3, 4), // 10
|
||||
bind(&sum_of_args_5, _1, _2, _3, 4, 5), // 15
|
||||
bind(&sum_of_args_6, _1, _2, _3, 4, 5, 6), // 21
|
||||
bind(&sum_of_args_7, _1, _2, _3, 4, 5, 6, 7), // 28
|
||||
bind(&sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8) // 36
|
||||
)(i, j, k) == 120);
|
||||
|
||||
// deeper nesting
|
||||
result =
|
||||
bind(&sum_of_args_1, // 12
|
||||
bind(&sum_of_args_4, // 12
|
||||
bind(&sum_of_args_2, // 3
|
||||
bind(&sum_of_args_1, // 1
|
||||
bind(&sum_of_args_1, _1) // 1
|
||||
),
|
||||
_2),
|
||||
_2,
|
||||
_3,
|
||||
4)
|
||||
)(i, j, k);
|
||||
BOOST_TEST(result == 12);
|
||||
|
||||
test_member_functions();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
150
test/bind_tests_simple_f_refs.cpp
Normal file
150
test/bind_tests_simple_f_refs.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
// bind_tests_simple.cpp -- The Boost Lambda Library ------------------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
#include "boost/lambda/bind.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace boost::lambda;
|
||||
|
||||
|
||||
int sum_of_args_0() { return 0; }
|
||||
int sum_of_args_1(int a) { return a; }
|
||||
int sum_of_args_2(int a, int b) { return a+b; }
|
||||
int sum_of_args_3(int a, int b, int c) { return a+b+c; }
|
||||
int sum_of_args_4(int a, int b, int c, int d) { return a+b+c+d; }
|
||||
int sum_of_args_5(int a, int b, int c, int d, int e) { return a+b+c+d+e; }
|
||||
int sum_of_args_6(int a, int b, int c, int d, int e, int f) { return a+b+c+d+e+f; }
|
||||
int sum_of_args_7(int a, int b, int c, int d, int e, int f, int g) { return a+b+c+d+e+f+g; }
|
||||
int sum_of_args_8(int a, int b, int c, int d, int e, int f, int g, int h) { return a+b+c+d+e+f+g+h; }
|
||||
int sum_of_args_9(int a, int b, int c, int d, int e, int f, int g, int h, int i) { return a+b+c+d+e+f+g+h+i; }
|
||||
|
||||
|
||||
// ----------------------------
|
||||
|
||||
class A {
|
||||
int i;
|
||||
public:
|
||||
A(int n) : i(n) {};
|
||||
int add(const int& j) { return i + j; }
|
||||
};
|
||||
|
||||
void test_member_functions()
|
||||
{
|
||||
using boost::ref;
|
||||
A a(10);
|
||||
int i = 1;
|
||||
|
||||
BOOST_TEST(bind(&A::add, ref(a), _1)(i) == 11);
|
||||
BOOST_TEST(bind(&A::add, &a, _1)(i) == 11);
|
||||
BOOST_TEST(bind(&A::add, _1, 1)(a) == 11);
|
||||
BOOST_TEST(bind(&A::add, _1, 1)(make_const(&a)) == 11);
|
||||
|
||||
// This should fail, as lambda functors store arguments as const
|
||||
// bind(&A::add, a, _1);
|
||||
}
|
||||
|
||||
int test_main(int, char *[]) {
|
||||
|
||||
int i = 1; int j = 2; int k = 3;
|
||||
int result;
|
||||
|
||||
|
||||
// bind all parameters
|
||||
BOOST_TEST(bind(sum_of_args_0)()==0);
|
||||
BOOST_TEST(bind(sum_of_args_1, 1)()==1);
|
||||
BOOST_TEST(bind(sum_of_args_2, 1, 2)()==3);
|
||||
BOOST_TEST(bind(sum_of_args_3, 1, 2, 3)()==6);
|
||||
BOOST_TEST(bind(sum_of_args_4, 1, 2, 3, 4)()==10);
|
||||
BOOST_TEST(bind(sum_of_args_5, 1, 2, 3, 4, 5)()==15);
|
||||
BOOST_TEST(bind(sum_of_args_6, 1, 2, 3, 4, 5, 6)()==21);
|
||||
BOOST_TEST(bind(sum_of_args_7, 1, 2, 3, 4, 5, 6, 7)()==28);
|
||||
BOOST_TEST(bind(sum_of_args_8, 1, 2, 3, 4, 5, 6, 7, 8)()==36);
|
||||
BOOST_TEST(bind(sum_of_args_9, 1, 2, 3, 4, 5, 6, 7, 8, 9)()==45);
|
||||
|
||||
// first parameter open
|
||||
BOOST_TEST(bind(sum_of_args_0)()==0);
|
||||
BOOST_TEST(bind(sum_of_args_1, _1)(i)==1);
|
||||
BOOST_TEST(bind(sum_of_args_2, _1, 2)(i)==3);
|
||||
BOOST_TEST(bind(sum_of_args_3, _1, 2, 3)(i)==6);
|
||||
BOOST_TEST(bind(sum_of_args_4, _1, 2, 3, 4)(i)==10);
|
||||
BOOST_TEST(bind(sum_of_args_5, _1, 2, 3, 4, 5)(i)==15);
|
||||
BOOST_TEST(bind(sum_of_args_6, _1, 2, 3, 4, 5, 6)(i)==21);
|
||||
BOOST_TEST(bind(sum_of_args_7, _1, 2, 3, 4, 5, 6, 7)(i)==28);
|
||||
BOOST_TEST(bind(sum_of_args_8, _1, 2, 3, 4, 5, 6, 7, 8)(i)==36);
|
||||
BOOST_TEST(bind(sum_of_args_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i)==45);
|
||||
|
||||
// two open arguments
|
||||
BOOST_TEST(bind(sum_of_args_0)()==0);
|
||||
BOOST_TEST(bind(sum_of_args_1, _1)(i)==1);
|
||||
BOOST_TEST(bind(sum_of_args_2, _1, _2)(i, j)==3);
|
||||
BOOST_TEST(bind(sum_of_args_3, _1, _2, 3)(i, j)==6);
|
||||
BOOST_TEST(bind(sum_of_args_4, _1, _2, 3, 4)(i, j)==10);
|
||||
BOOST_TEST(bind(sum_of_args_5, _1, _2, 3, 4, 5)(i, j)==15);
|
||||
BOOST_TEST(bind(sum_of_args_6, _1, _2, 3, 4, 5, 6)(i, j)==21);
|
||||
BOOST_TEST(bind(sum_of_args_7, _1, _2, 3, 4, 5, 6, 7)(i, j)==28);
|
||||
BOOST_TEST(bind(sum_of_args_8, _1, _2, 3, 4, 5, 6, 7, 8)(i, j)==36);
|
||||
BOOST_TEST(bind(sum_of_args_9, _1, _2, 3, 4, 5, 6, 7, 8, 9)(i, j)==45);
|
||||
|
||||
// three open arguments
|
||||
BOOST_TEST(bind(sum_of_args_0)()==0);
|
||||
BOOST_TEST(bind(sum_of_args_1, _1)(i)==1);
|
||||
BOOST_TEST(bind(sum_of_args_2, _1, _2)(i, j)==3);
|
||||
BOOST_TEST(bind(sum_of_args_3, _1, _2, _3)(i, j, k)==6);
|
||||
BOOST_TEST(bind(sum_of_args_4, _1, _2, _3, 4)(i, j, k)==10);
|
||||
BOOST_TEST(bind(sum_of_args_5, _1, _2, _3, 4, 5)(i, j, k)==15);
|
||||
BOOST_TEST(bind(sum_of_args_6, _1, _2, _3, 4, 5, 6)(i, j, k)==21);
|
||||
BOOST_TEST(bind(sum_of_args_7, _1, _2, _3, 4, 5, 6, 7)(i, j, k)==28);
|
||||
BOOST_TEST(bind(sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8)(i, j, k)==36);
|
||||
BOOST_TEST(bind(sum_of_args_9, _1, _2, _3, 4, 5, 6, 7, 8, 9)(i, j, k)==45);
|
||||
|
||||
// function compositions with bind
|
||||
BOOST_TEST(bind(sum_of_args_3, bind(sum_of_args_2, _1, 2), 2, 3)(i)==8);
|
||||
BOOST_TEST(
|
||||
bind(sum_of_args_9,
|
||||
bind(sum_of_args_0), // 0
|
||||
bind(sum_of_args_1, _1), // 1
|
||||
bind(sum_of_args_2, _1, _2), // 3
|
||||
bind(sum_of_args_3, _1, _2, _3), // 6
|
||||
bind(sum_of_args_4, _1, _2, _3, 4), // 10
|
||||
bind(sum_of_args_5, _1, _2, _3, 4, 5), // 15
|
||||
bind(sum_of_args_6, _1, _2, _3, 4, 5, 6), // 21
|
||||
bind(sum_of_args_7, _1, _2, _3, 4, 5, 6, 7), // 28
|
||||
bind(sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8) // 36
|
||||
)(i, j, k) == 120);
|
||||
|
||||
// deeper nesting
|
||||
result =
|
||||
bind(sum_of_args_1, // 12
|
||||
bind(sum_of_args_4, // 12
|
||||
bind(sum_of_args_2, // 3
|
||||
bind(sum_of_args_1, // 1
|
||||
bind(sum_of_args_1, _1) // 1
|
||||
),
|
||||
_2),
|
||||
_2,
|
||||
_3,
|
||||
4)
|
||||
)(i, j, k);
|
||||
BOOST_TEST(result == 12);
|
||||
|
||||
test_member_functions();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
68
test/bll_and_function.cpp
Normal file
68
test/bll_and_function.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
// bll_and_function.cpp - The Boost Lambda Library -----------------------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// test using BLL and boost::function
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
#include "boost/lambda/lambda.hpp"
|
||||
|
||||
#include "boost/function.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
|
||||
using namespace boost::lambda;
|
||||
|
||||
using namespace std;
|
||||
|
||||
void test_function() {
|
||||
|
||||
boost::function<int (int, int)> f;
|
||||
f = _1 + _2;
|
||||
|
||||
BOOST_TEST(f(1, 2)== 3);
|
||||
|
||||
int i=1; int j=2;
|
||||
boost::function<int& (int&, int)> g = _1 += _2;
|
||||
g(i, j);
|
||||
BOOST_TEST(i==3);
|
||||
|
||||
|
||||
|
||||
int* sum = new int();
|
||||
*sum = 0;
|
||||
boost::function<int& (int)> counter = *sum += _1;
|
||||
counter(5); // ok, sum* = 5;
|
||||
BOOST_TEST(*sum == 5);
|
||||
delete sum;
|
||||
|
||||
// The next statement would lead to a dangling reference
|
||||
// counter(3); // error, *sum does not exist anymore
|
||||
|
||||
}
|
||||
|
||||
|
||||
int test_main(int, char *[]) {
|
||||
|
||||
test_function();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
107
test/cast_test.cpp
Normal file
107
test/cast_test.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
// cast_tests.cpp -- The Boost Lambda Library ------------------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
|
||||
#include "boost/lambda/lambda.hpp"
|
||||
|
||||
#include "boost/lambda/casts.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace boost::lambda;
|
||||
using namespace std;
|
||||
|
||||
class base {
|
||||
int x;
|
||||
public:
|
||||
virtual std::string class_name() const { return "const base"; }
|
||||
virtual std::string class_name() { return "base"; }
|
||||
|
||||
};
|
||||
|
||||
class derived : public base {
|
||||
int y[100];
|
||||
public:
|
||||
virtual std::string class_name() const { return "const derived"; }
|
||||
virtual std::string class_name() { return "derived"; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
void do_test() {
|
||||
|
||||
derived *p_derived = new derived;
|
||||
base *p_base = new base;
|
||||
|
||||
base *b = 0;
|
||||
derived *d = 0;
|
||||
|
||||
(var(b) = ll_static_cast<base *>(p_derived))();
|
||||
(var(d) = ll_static_cast<derived *>(b))();
|
||||
|
||||
BOOST_TEST(b->class_name() == "derived");
|
||||
BOOST_TEST(d->class_name() == "derived");
|
||||
|
||||
(var(b) = ll_dynamic_cast<derived *>(b))();
|
||||
BOOST_TEST(b != 0);
|
||||
BOOST_TEST(b->class_name() == "derived");
|
||||
|
||||
(var(d) = ll_dynamic_cast<derived *>(p_base))();
|
||||
BOOST_TEST(d == 0);
|
||||
|
||||
|
||||
|
||||
const derived* p_const_derived = p_derived;
|
||||
|
||||
BOOST_TEST(p_const_derived->class_name() == "const derived");
|
||||
(var(d) = ll_const_cast<derived *>(p_const_derived))();
|
||||
BOOST_TEST(d->class_name() == "derived");
|
||||
|
||||
int i = 10;
|
||||
char* cp = reinterpret_cast<char*>(&i);
|
||||
|
||||
int* ip;
|
||||
(var(ip) = ll_reinterpret_cast<int *>(cp))();
|
||||
BOOST_TEST(*ip == 10);
|
||||
|
||||
|
||||
// typeid
|
||||
|
||||
BOOST_TEST(string(ll_typeid(d)().name()) == string(typeid(d).name()));
|
||||
|
||||
|
||||
// sizeof
|
||||
|
||||
BOOST_TEST(ll_sizeof(_1)(p_derived) == sizeof(p_derived));
|
||||
BOOST_TEST(ll_sizeof(_1)(*p_derived) == sizeof(*p_derived));
|
||||
BOOST_TEST(ll_sizeof(_1)(p_base) == sizeof(p_base));
|
||||
BOOST_TEST(ll_sizeof(_1)(*p_base) == sizeof(*p_base));
|
||||
|
||||
int an_array[100];
|
||||
BOOST_TEST(ll_sizeof(_1)(an_array) == 100 * sizeof(int));
|
||||
|
||||
delete p_derived;
|
||||
delete p_base;
|
||||
|
||||
|
||||
}
|
||||
|
||||
int test_main(int, char *[]) {
|
||||
|
||||
do_test();
|
||||
return 0;
|
||||
}
|
||||
261
test/constructor_tests.cpp
Normal file
261
test/constructor_tests.cpp
Normal file
@@ -0,0 +1,261 @@
|
||||
// constructor_tests.cpp -- The Boost Lambda Library ------------------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
|
||||
#include "boost/lambda/lambda.hpp"
|
||||
#include "boost/lambda/bind.hpp"
|
||||
|
||||
#include "boost/lambda/construct.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
using namespace boost::lambda;
|
||||
using namespace std;
|
||||
|
||||
template<class T>
|
||||
bool check_tuple(int n, const T& t)
|
||||
{
|
||||
return (t.get_head() == n) && check_tuple(n+1, t.get_tail());
|
||||
}
|
||||
|
||||
template <>
|
||||
bool check_tuple(int n, const null_type& ) { return true; }
|
||||
|
||||
|
||||
void constructor_all_lengths()
|
||||
{
|
||||
bool ok;
|
||||
ok = check_tuple(
|
||||
1,
|
||||
bind(constructor<tuple<int> >(),
|
||||
1)()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
bind(constructor<tuple<int, int> >(),
|
||||
1, 2)()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
bind(constructor<tuple<int, int, int> >(),
|
||||
1, 2, 3)()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
bind(constructor<tuple<int, int, int, int> >(),
|
||||
1, 2, 3, 4)()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
bind(constructor<tuple<int, int, int, int, int> >(),
|
||||
1, 2, 3, 4, 5)()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
bind(constructor<tuple<int, int, int, int, int, int> >(),
|
||||
1, 2, 3, 4, 5, 6)()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
bind(constructor<tuple<int, int, int, int, int, int, int> >(),
|
||||
1, 2, 3, 4, 5, 6, 7)()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
bind(constructor<tuple<int, int, int, int, int, int, int, int> >(),
|
||||
1, 2, 3, 4, 5, 6, 7, 8)()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
bind(constructor<tuple<int, int, int, int, int, int, int, int, int> >(),
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9)()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
}
|
||||
|
||||
void new_ptr_all_lengths()
|
||||
{
|
||||
bool ok;
|
||||
ok = check_tuple(
|
||||
1,
|
||||
*(bind(new_ptr<tuple<int> >(),
|
||||
1))()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
*(bind(new_ptr<tuple<int, int> >(),
|
||||
1, 2))()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
*(bind(new_ptr<tuple<int, int, int> >(),
|
||||
1, 2, 3))()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
*(bind(new_ptr<tuple<int, int, int, int> >(),
|
||||
1, 2, 3, 4))()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
*(bind(new_ptr<tuple<int, int, int, int, int> >(),
|
||||
1, 2, 3, 4, 5))()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
*(bind(new_ptr<tuple<int, int, int, int, int, int> >(),
|
||||
1, 2, 3, 4, 5, 6))()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
*(bind(new_ptr<tuple<int, int, int, int, int, int, int> >(),
|
||||
1, 2, 3, 4, 5, 6, 7))()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
*(bind(new_ptr<tuple<int, int, int, int, int, int, int, int> >(),
|
||||
1, 2, 3, 4, 5, 6, 7, 8))()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
ok = check_tuple(
|
||||
1,
|
||||
*(bind(new_ptr<tuple<int, int, int, int, int, int, int, int, int> >(),
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9))()
|
||||
);
|
||||
BOOST_TEST(ok);
|
||||
|
||||
}
|
||||
|
||||
class is_destructor_called {
|
||||
bool& b;
|
||||
public:
|
||||
is_destructor_called(bool& bb) : b(bb) { b = false; }
|
||||
~is_destructor_called() { b = true; }
|
||||
};
|
||||
|
||||
void test_destructor ()
|
||||
{
|
||||
char space[sizeof(is_destructor_called)];
|
||||
bool flag;
|
||||
|
||||
is_destructor_called* idc = new(space) is_destructor_called(flag);
|
||||
BOOST_TEST(flag == false);
|
||||
bind(destructor(), _1)(idc);
|
||||
BOOST_TEST(flag == true);
|
||||
|
||||
idc = new(space) is_destructor_called(flag);
|
||||
BOOST_TEST(flag == false);
|
||||
bind(destructor(), _1)(*idc);
|
||||
BOOST_TEST(flag == true);
|
||||
}
|
||||
|
||||
|
||||
class count_deletes {
|
||||
public:
|
||||
static int count;
|
||||
~count_deletes() { ++count; }
|
||||
};
|
||||
|
||||
int count_deletes::count = 0;
|
||||
|
||||
void test_news_and_deletes ()
|
||||
{
|
||||
int* i[10];
|
||||
for_each(i, i+10, _1 = bind(new_ptr<int>(), 2));
|
||||
int count_errors = 0;
|
||||
|
||||
for_each(i, i+10, (*_1 == 2) || ++var(count_errors));
|
||||
BOOST_TEST(count_errors == 0);
|
||||
|
||||
|
||||
count_deletes* ct[10];
|
||||
for_each(ct, ct+10, _1 = bind(new_ptr<count_deletes>()));
|
||||
count_deletes::count = 0;
|
||||
for_each(ct, ct+10, bind(delete_ptr(), _1));
|
||||
BOOST_TEST(count_deletes::count == 10);
|
||||
|
||||
}
|
||||
|
||||
void test_array_new_and_delete()
|
||||
{
|
||||
count_deletes* c;
|
||||
(_1 = bind(new_array<count_deletes>(), 5))(c);
|
||||
count_deletes::count = 0;
|
||||
|
||||
bind(delete_array(), _1)(c);
|
||||
BOOST_TEST(count_deletes::count == 5);
|
||||
}
|
||||
|
||||
|
||||
void delayed_construction()
|
||||
{
|
||||
vector<int> x(3);
|
||||
vector<int> y(3);
|
||||
|
||||
fill(x.begin(), x.end(), 0);
|
||||
fill(y.begin(), y.end(), 1);
|
||||
|
||||
vector<pair<int, int> > v;
|
||||
|
||||
transform(x.begin(), x.end(), y.begin(), back_inserter(v),
|
||||
bind(constructor<pair<int, int> >(), _1, _2) );
|
||||
}
|
||||
|
||||
int test_main(int, char *[]) {
|
||||
|
||||
constructor_all_lengths();
|
||||
new_ptr_all_lengths();
|
||||
delayed_construction();
|
||||
test_destructor();
|
||||
test_news_and_deletes();
|
||||
test_array_new_and_delete();
|
||||
|
||||
return 0;
|
||||
}
|
||||
123
test/control_structures.cpp
Normal file
123
test/control_structures.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
// -- control_structures.cpp -- The Boost Lambda Library ------------------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
#include "boost/lambda/lambda.hpp"
|
||||
#include "boost/lambda/if.hpp"
|
||||
#include "boost/lambda/loops.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
using boost::lambda::constant;
|
||||
using boost::lambda::_1;
|
||||
using boost::lambda::_2;
|
||||
using boost::lambda::_3;
|
||||
using boost::lambda::make_const;
|
||||
using boost::lambda::for_loop;
|
||||
using boost::lambda::while_loop;
|
||||
using boost::lambda::do_while_loop;
|
||||
using boost::lambda::if_then;
|
||||
using boost::lambda::if_then_else;
|
||||
using boost::lambda::if_then_else_return;
|
||||
|
||||
// 2 container for_each
|
||||
template <class InputIter1, class InputIter2, class Function>
|
||||
Function for_each(InputIter1 first, InputIter1 last,
|
||||
InputIter2 first2, Function f) {
|
||||
for ( ; first != last; ++first, ++first2)
|
||||
f(*first, *first2);
|
||||
return f;
|
||||
}
|
||||
|
||||
void simple_loops() {
|
||||
|
||||
// for loops ---------------------------------------------------------
|
||||
int i;
|
||||
int arithmetic_series = 0;
|
||||
for_loop(_1 = 0, _1 < 10, _1++, arithmetic_series += _1)(i);
|
||||
BOOST_TEST(arithmetic_series == 45);
|
||||
|
||||
// no body case
|
||||
for_loop(boost::lambda::var(i) = 0, boost::lambda::var(i) < 100, ++boost::lambda::var(i))();
|
||||
BOOST_TEST(i == 100);
|
||||
|
||||
// while loops -------------------------------------------------------
|
||||
int a = 0, b = 0, c = 0;
|
||||
|
||||
while_loop((_1 + _2) >= (_1 * _2), (++_1, ++_2, ++_3))(a, b, c);
|
||||
BOOST_TEST(c == 3);
|
||||
|
||||
int count;
|
||||
count = 0; i = 0;
|
||||
while_loop(_1++ < 10, ++boost::lambda::var(count))(i);
|
||||
BOOST_TEST(count == 10);
|
||||
|
||||
// note that the first parameter of do_while_loop is the condition
|
||||
count = 0; i = 0;
|
||||
do_while_loop(_1++ < 10, ++boost::lambda::var(count))(i);
|
||||
BOOST_TEST(count == 11);
|
||||
|
||||
a = 0;
|
||||
do_while_loop(constant(false), _1++)(a);
|
||||
BOOST_TEST(a == 1);
|
||||
|
||||
// no body cases
|
||||
a = 40; b = 30;
|
||||
while_loop(--_1 > _2)(a, b);
|
||||
BOOST_TEST(a == b);
|
||||
|
||||
// (the no body case for do_while_loop is pretty redundant)
|
||||
a = 40; b = 30;
|
||||
do_while_loop(--_1 > _2)(a, b);
|
||||
BOOST_TEST(a == b);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void simple_ifs () {
|
||||
|
||||
int value = 42;
|
||||
if_then(_1 < 0, _1 = 0)(value);
|
||||
BOOST_TEST(value == 42);
|
||||
|
||||
value = -42;
|
||||
if_then(_1 < 0, _1 = -_1)(value);
|
||||
BOOST_TEST(value == 42);
|
||||
|
||||
int min;
|
||||
if_then_else(_1 < _2, boost::lambda::var(min) = _1, boost::lambda::var(min) = _2)
|
||||
(make_const(1), make_const(2));
|
||||
BOOST_TEST(min == 1);
|
||||
|
||||
if_then_else(_1 < _2, boost::lambda::var(min) = _1, boost::lambda::var(min) = _2)
|
||||
(make_const(5), make_const(3));
|
||||
BOOST_TEST(min == 3);
|
||||
|
||||
int x, y;
|
||||
x = -1; y = 1;
|
||||
BOOST_TEST(if_then_else_return(_1 < _2, _2, _1)(x, y) == (std::max)(x ,y));
|
||||
BOOST_TEST(if_then_else_return(_1 < _2, _2, _1)(y, x) == (std::max)(x ,y));
|
||||
}
|
||||
|
||||
|
||||
int test_main(int, char *[])
|
||||
{
|
||||
simple_loops();
|
||||
simple_ifs();
|
||||
return 0;
|
||||
}
|
||||
621
test/exception_test.cpp
Normal file
621
test/exception_test.cpp
Normal file
@@ -0,0 +1,621 @@
|
||||
// -- exception_test.cpp -- The Boost Lambda Library ------------------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
#include "boost/lambda/lambda.hpp"
|
||||
|
||||
#include "boost/lambda/exceptions.hpp"
|
||||
|
||||
#include "boost/lambda/bind.hpp"
|
||||
|
||||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace boost::lambda;
|
||||
using namespace std;
|
||||
|
||||
// to prevent unused variables warnings
|
||||
template <class T> void dummy(const T& t) {}
|
||||
|
||||
void erroneous_exception_related_lambda_expressions() {
|
||||
|
||||
int i = 0;
|
||||
dummy(i);
|
||||
|
||||
// Uncommenting any of the below code lines should result in a compile
|
||||
// time error
|
||||
|
||||
// this should fail (a rethrow binder outside of catch
|
||||
// rethrow()();
|
||||
|
||||
// this should fail too for the same reason
|
||||
// try_catch(rethrow(), catch_all(cout << constant("Howdy")))();
|
||||
|
||||
// this fails too (_e outside of catch_exception)
|
||||
// (_1 + _2 + _e)(i, i, i);
|
||||
|
||||
// and this (_e outside of catch_exception)
|
||||
// try_catch( throw_exception(1), catch_all(cout << _e));
|
||||
|
||||
// and this (_3 in catch_exception
|
||||
// try_catch( throw_exception(1), catch_exception<int>(cout << _3));
|
||||
}
|
||||
|
||||
|
||||
class A1 {};
|
||||
class A2 {};
|
||||
class A3 {};
|
||||
class A4 {};
|
||||
class A5 {};
|
||||
class A6 {};
|
||||
class A7 {};
|
||||
class A8 {};
|
||||
class A9 {};
|
||||
|
||||
void throw_AX(int j) {
|
||||
int i = j;
|
||||
switch(i) {
|
||||
case 1: throw A1();
|
||||
case 2: throw A2();
|
||||
case 3: throw A3();
|
||||
case 4: throw A4();
|
||||
case 5: throw A5();
|
||||
case 6: throw A6();
|
||||
case 7: throw A7();
|
||||
case 8: throw A8();
|
||||
case 9: throw A9();
|
||||
}
|
||||
}
|
||||
|
||||
void test_different_number_of_catch_blocks() {
|
||||
|
||||
int ecount;
|
||||
|
||||
// no catch(...) cases
|
||||
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=1; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 1);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=2; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 2);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=3; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A3>(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 3);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=4; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A3>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A4>(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 4);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=5; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A3>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A4>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A5>(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 5);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=6; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A3>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A4>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A5>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A6>(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 6);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=7; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A3>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A4>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A5>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A6>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A7>(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 7);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=8; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A3>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A4>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A5>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A6>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A7>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A8>(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 8);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=9; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A3>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A4>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A5>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A6>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A7>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A8>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A9>(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 9);
|
||||
|
||||
|
||||
// with catch(...) blocks
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=1; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_all(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 1);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=2; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_all(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 2);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=3; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_all(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 3);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=4; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A3>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_all(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 4);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=5; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A3>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A4>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_all(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 5);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=6; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A3>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A4>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A5>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_all(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 6);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=7; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A3>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A4>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A5>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A6>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_all(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 7);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=8; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A3>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A4>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A5>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A6>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A7>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_all(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 8);
|
||||
|
||||
ecount = 0;
|
||||
for(int i=1; i<=9; i++)
|
||||
{
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A2>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A3>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A4>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A5>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A6>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A7>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_exception<A8>(
|
||||
var(ecount)++
|
||||
),
|
||||
catch_all(
|
||||
var(ecount)++
|
||||
)
|
||||
)(i);
|
||||
}
|
||||
BOOST_TEST(ecount == 9);
|
||||
}
|
||||
|
||||
void test_empty_catch_blocks() {
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_exception<A1>()
|
||||
)(make_const(1));
|
||||
|
||||
try_catch(
|
||||
bind(throw_AX, _1),
|
||||
catch_all()
|
||||
)(make_const(1));
|
||||
|
||||
}
|
||||
|
||||
|
||||
void return_type_matching() {
|
||||
|
||||
// Rules for return types of the lambda functors in try and catch parts:
|
||||
// 1. The try part dictates the return type of the whole
|
||||
// try_catch lambda functor
|
||||
// 2. If return type of try part is void, catch parts can return anything,
|
||||
// but the return types are ignored
|
||||
// 3. If the return type of the try part is A, then each catch return type
|
||||
// must be implicitly convertible to A, or then it must throw for sure
|
||||
|
||||
|
||||
int i = 1;
|
||||
|
||||
BOOST_TEST(
|
||||
|
||||
try_catch(
|
||||
_1 + 1,
|
||||
catch_exception<int>((&_1, rethrow())), // no match, but ok since throws
|
||||
catch_exception<char>(_e) // ok, char convertible to int
|
||||
)(i)
|
||||
|
||||
== 2
|
||||
);
|
||||
|
||||
// note that while e.g. char is convertible to int, it is not convertible
|
||||
// to int&, (some lambda functors return references)
|
||||
|
||||
// try_catch(
|
||||
// _1 += 1,
|
||||
// catch_exception<char>(_e) // NOT ok, char not convertible to int&
|
||||
// )(i);
|
||||
|
||||
// if you don't care about the return type, you can use make_void
|
||||
try_catch(
|
||||
make_void(_1 += 1),
|
||||
catch_exception<char>(_e) // since try is void, catch can return anything
|
||||
)(i);
|
||||
BOOST_TEST(i == 2);
|
||||
|
||||
try_catch(
|
||||
(_1 += 1, throw_exception('a')),
|
||||
catch_exception<char>(_e) // since try throws, it is void,
|
||||
// so catch can return anything
|
||||
)(i);
|
||||
BOOST_TEST(i == 3);
|
||||
|
||||
char a = 'a';
|
||||
try_catch(
|
||||
try_catch(
|
||||
throw_exception(1),
|
||||
catch_exception<int>(throw_exception('b'))
|
||||
),
|
||||
catch_exception<char>( _1 = _e )
|
||||
)(a);
|
||||
BOOST_TEST(a == 'b');
|
||||
}
|
||||
|
||||
int test_main(int, char *[]) {
|
||||
|
||||
try
|
||||
{
|
||||
test_different_number_of_catch_blocks();
|
||||
return_type_matching();
|
||||
test_empty_catch_blocks();
|
||||
}
|
||||
catch (int x)
|
||||
{
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
384
test/extending_rt_traits.cpp
Normal file
384
test/extending_rt_traits.cpp
Normal file
@@ -0,0 +1,384 @@
|
||||
// extending_return_type_traits.cpp -- The Boost Lambda Library --------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
#include "boost/lambda/bind.hpp"
|
||||
#include "boost/lambda/lambda.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
class A {};
|
||||
class B {};
|
||||
|
||||
using namespace boost::lambda;
|
||||
|
||||
|
||||
B operator--(const A&, int) { return B(); }
|
||||
B operator--(A&) { return B(); }
|
||||
B operator++(const A&, int) { return B(); }
|
||||
B operator++(A&) { return B(); }
|
||||
B operator-(const A&) { return B(); }
|
||||
B operator+(const A&) { return B(); }
|
||||
|
||||
B operator!(const A&) { return B(); }
|
||||
|
||||
B operator&(const A&) { return B(); }
|
||||
B operator*(const A&) { return B(); }
|
||||
|
||||
namespace boost {
|
||||
namespace lambda {
|
||||
|
||||
// unary + and -
|
||||
template<class Act>
|
||||
struct plain_return_type_1<unary_arithmetic_action<Act>, A > {
|
||||
typedef B type;
|
||||
};
|
||||
|
||||
// post incr/decr
|
||||
template<class Act>
|
||||
struct plain_return_type_1<post_increment_decrement_action<Act>, A > {
|
||||
typedef B type;
|
||||
};
|
||||
|
||||
// pre incr/decr
|
||||
template<class Act>
|
||||
struct plain_return_type_1<pre_increment_decrement_action<Act>, A > {
|
||||
typedef B type;
|
||||
};
|
||||
// !
|
||||
template<>
|
||||
struct plain_return_type_1<logical_action<not_action>, A> {
|
||||
typedef B type;
|
||||
};
|
||||
// &
|
||||
template<>
|
||||
struct plain_return_type_1<other_action<addressof_action>, A> {
|
||||
typedef B type;
|
||||
};
|
||||
// *
|
||||
template<>
|
||||
struct plain_return_type_1<other_action<contentsof_action>, A> {
|
||||
typedef B type;
|
||||
};
|
||||
|
||||
|
||||
} // lambda
|
||||
} // boost
|
||||
|
||||
void ok(B b) {}
|
||||
|
||||
void test_unary_operators()
|
||||
{
|
||||
A a; int i = 1;
|
||||
ok((++_1)(a));
|
||||
ok((--_1)(a));
|
||||
ok((_1++)(a));
|
||||
ok((_1--)(a));
|
||||
ok((+_1)(a));
|
||||
ok((-_1)(a));
|
||||
ok((!_1)(a));
|
||||
ok((&_1)(a));
|
||||
ok((*_1)(a));
|
||||
|
||||
BOOST_TEST((*_1)(make_const(&i)) == 1);
|
||||
}
|
||||
|
||||
class X {};
|
||||
class Y {};
|
||||
class Z {};
|
||||
|
||||
Z operator+(const X&, const Y&) { return Z(); }
|
||||
Z operator-(const X&, const Y&) { return Z(); }
|
||||
X operator*(const X&, const Y&) { return X(); }
|
||||
|
||||
Z operator/(const X&, const Y&) { return Z(); }
|
||||
Z operator%(const X&, const Y&) { return Z(); }
|
||||
|
||||
class XX {};
|
||||
class YY {};
|
||||
class ZZ {};
|
||||
class VV {};
|
||||
|
||||
// it is possible to support differently cv-qualified versions
|
||||
YY operator*(XX&, YY&) { return YY(); }
|
||||
ZZ operator*(const XX&, const YY&) { return ZZ(); }
|
||||
XX operator*(volatile XX&, volatile YY&) { return XX(); }
|
||||
VV operator*(const volatile XX&, const volatile YY&) { return VV(); }
|
||||
|
||||
// the traits can be more complex:
|
||||
template <class T>
|
||||
class my_vector {};
|
||||
|
||||
template<class A, class B>
|
||||
my_vector<typename return_type_2<arithmetic_action<plus_action>, A&, B&>::type>
|
||||
operator+(const my_vector<A>& a, const my_vector<B>& b)
|
||||
{
|
||||
typedef typename
|
||||
return_type_2<arithmetic_action<plus_action>, A&, B&>::type res_type;
|
||||
return my_vector<res_type>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// bitwise ops:
|
||||
X operator<<(const X&, const Y&) { return X(); }
|
||||
Z operator>>(const X&, const Y&) { return Z(); }
|
||||
Z operator&(const X&, const Y&) { return Z(); }
|
||||
Z operator|(const X&, const Y&) { return Z(); }
|
||||
Z operator^(const X&, const Y&) { return Z(); }
|
||||
|
||||
// comparison ops:
|
||||
|
||||
X operator<(const X&, const Y&) { return X(); }
|
||||
Z operator>(const X&, const Y&) { return Z(); }
|
||||
Z operator<=(const X&, const Y&) { return Z(); }
|
||||
Z operator>=(const X&, const Y&) { return Z(); }
|
||||
Z operator==(const X&, const Y&) { return Z(); }
|
||||
Z operator!=(const X&, const Y&) { return Z(); }
|
||||
|
||||
// logical
|
||||
|
||||
X operator&&(const X&, const Y&) { return X(); }
|
||||
Z operator||(const X&, const Y&) { return Z(); }
|
||||
|
||||
// arithh assignment
|
||||
|
||||
Z operator+=( X&, const Y&) { return Z(); }
|
||||
Z operator-=( X&, const Y&) { return Z(); }
|
||||
Y operator*=( X&, const Y&) { return Y(); }
|
||||
Z operator/=( X&, const Y&) { return Z(); }
|
||||
Z operator%=( X&, const Y&) { return Z(); }
|
||||
|
||||
// bitwise assignment
|
||||
Z operator<<=( X&, const Y&) { return Z(); }
|
||||
Z operator>>=( X&, const Y&) { return Z(); }
|
||||
Y operator&=( X&, const Y&) { return Y(); }
|
||||
Z operator|=( X&, const Y&) { return Z(); }
|
||||
Z operator^=( X&, const Y&) { return Z(); }
|
||||
|
||||
// assignment
|
||||
class Assign {
|
||||
public:
|
||||
void operator=(const Assign& a) {}
|
||||
X operator[](const int& i) { return X(); }
|
||||
};
|
||||
|
||||
|
||||
|
||||
namespace boost {
|
||||
namespace lambda {
|
||||
|
||||
// you can do action groups
|
||||
template<class Act>
|
||||
struct plain_return_type_2<arithmetic_action<Act>, X, Y> {
|
||||
typedef Z type;
|
||||
};
|
||||
|
||||
// or specialize the exact action
|
||||
template<>
|
||||
struct plain_return_type_2<arithmetic_action<multiply_action>, X, Y> {
|
||||
typedef X type;
|
||||
};
|
||||
|
||||
// if you want to make a distinction between differently cv-qualified
|
||||
// types, you need to specialize on a different level:
|
||||
template<>
|
||||
struct return_type_2<arithmetic_action<multiply_action>, XX, YY> {
|
||||
typedef YY type;
|
||||
};
|
||||
template<>
|
||||
struct return_type_2<arithmetic_action<multiply_action>, const XX, const YY> {
|
||||
typedef ZZ type;
|
||||
};
|
||||
template<>
|
||||
struct return_type_2<arithmetic_action<multiply_action>, volatile XX, volatile YY> {
|
||||
typedef XX type;
|
||||
};
|
||||
template<>
|
||||
struct return_type_2<arithmetic_action<multiply_action>, volatile const XX, const volatile YY> {
|
||||
typedef VV type;
|
||||
};
|
||||
|
||||
// the mapping can be more complex:
|
||||
template<class A, class B>
|
||||
struct plain_return_type_2<arithmetic_action<plus_action>, my_vector<A>, my_vector<B> > {
|
||||
typedef typename
|
||||
return_type_2<arithmetic_action<plus_action>, A&, B&>::type res_type;
|
||||
typedef my_vector<res_type> type;
|
||||
};
|
||||
|
||||
// bitwise binary:
|
||||
// you can do action groups
|
||||
template<class Act>
|
||||
struct plain_return_type_2<bitwise_action<Act>, X, Y> {
|
||||
typedef Z type;
|
||||
};
|
||||
|
||||
// or specialize the exact action
|
||||
template<>
|
||||
struct plain_return_type_2<bitwise_action<leftshift_action>, X, Y> {
|
||||
typedef X type;
|
||||
};
|
||||
|
||||
// comparison binary:
|
||||
// you can do action groups
|
||||
template<class Act>
|
||||
struct plain_return_type_2<relational_action<Act>, X, Y> {
|
||||
typedef Z type;
|
||||
};
|
||||
|
||||
// or specialize the exact action
|
||||
template<>
|
||||
struct plain_return_type_2<relational_action<less_action>, X, Y> {
|
||||
typedef X type;
|
||||
};
|
||||
|
||||
// logical binary:
|
||||
// you can do action groups
|
||||
template<class Act>
|
||||
struct plain_return_type_2<logical_action<Act>, X, Y> {
|
||||
typedef Z type;
|
||||
};
|
||||
|
||||
// or specialize the exact action
|
||||
template<>
|
||||
struct plain_return_type_2<logical_action<and_action>, X, Y> {
|
||||
typedef X type;
|
||||
};
|
||||
|
||||
// arithmetic assignment :
|
||||
// you can do action groups
|
||||
template<class Act>
|
||||
struct plain_return_type_2<arithmetic_assignment_action<Act>, X, Y> {
|
||||
typedef Z type;
|
||||
};
|
||||
|
||||
// or specialize the exact action
|
||||
template<>
|
||||
struct plain_return_type_2<arithmetic_assignment_action<multiply_action>, X, Y> {
|
||||
typedef Y type;
|
||||
};
|
||||
|
||||
// arithmetic assignment :
|
||||
// you can do action groups
|
||||
template<class Act>
|
||||
struct plain_return_type_2<bitwise_assignment_action<Act>, X, Y> {
|
||||
typedef Z type;
|
||||
};
|
||||
|
||||
// or specialize the exact action
|
||||
template<>
|
||||
struct plain_return_type_2<bitwise_assignment_action<and_action>, X, Y> {
|
||||
typedef Y type;
|
||||
};
|
||||
|
||||
// assignment
|
||||
template<>
|
||||
struct plain_return_type_2<other_action<assignment_action>, Assign, Assign> {
|
||||
typedef void type;
|
||||
};
|
||||
// subscript
|
||||
template<>
|
||||
struct plain_return_type_2<other_action<subscript_action>, Assign, int> {
|
||||
typedef X type;
|
||||
};
|
||||
|
||||
|
||||
} // end lambda
|
||||
} // end boost
|
||||
|
||||
|
||||
|
||||
void test_binary_operators() {
|
||||
|
||||
X x; Y y;
|
||||
(_1 + _2)(x, y);
|
||||
(_1 - _2)(x, y);
|
||||
(_1 * _2)(x, y);
|
||||
(_1 / _2)(x, y);
|
||||
(_1 % _2)(x, y);
|
||||
|
||||
|
||||
// make a distinction between differently cv-qualified operators
|
||||
XX xx; YY yy;
|
||||
const XX& cxx = xx;
|
||||
const YY& cyy = yy;
|
||||
volatile XX& vxx = xx;
|
||||
volatile YY& vyy = yy;
|
||||
const volatile XX& cvxx = xx;
|
||||
const volatile YY& cvyy = yy;
|
||||
|
||||
ZZ dummy1 = (_1 * _2)(cxx, cyy);
|
||||
YY dummy2 = (_1 * _2)(xx, yy);
|
||||
XX dummy3 = (_1 * _2)(vxx, vyy);
|
||||
VV dummy4 = (_1 * _2)(cvxx, cvyy);
|
||||
|
||||
my_vector<int> v1; my_vector<double> v2;
|
||||
my_vector<double> d = (_1 + _2)(v1, v2);
|
||||
|
||||
// bitwise
|
||||
|
||||
(_1 << _2)(x, y);
|
||||
(_1 >> _2)(x, y);
|
||||
(_1 | _2)(x, y);
|
||||
(_1 & _2)(x, y);
|
||||
(_1 ^ _2)(x, y);
|
||||
|
||||
// comparison
|
||||
|
||||
(_1 < _2)(x, y);
|
||||
(_1 > _2)(x, y);
|
||||
(_1 <= _2)(x, y);
|
||||
(_1 >= _2)(x, y);
|
||||
(_1 == _2)(x, y);
|
||||
(_1 != _2)(x, y);
|
||||
|
||||
// logical
|
||||
|
||||
(_1 || _2)(x, y);
|
||||
(_1 && _2)(x, y);
|
||||
|
||||
// arithmetic assignment
|
||||
(_1 += _2)(x, y);
|
||||
(_1 -= _2)(x, y);
|
||||
(_1 *= _2)(x, y);
|
||||
(_1 /= _2)(x, y);
|
||||
(_1 %= _2)(x, y);
|
||||
|
||||
// bitwise assignment
|
||||
(_1 <<= _2)(x, y);
|
||||
(_1 >>= _2)(x, y);
|
||||
(_1 |= _2)(x, y);
|
||||
(_1 &= _2)(x, y);
|
||||
(_1 ^= _2)(x, y);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int test_main(int, char *[]) {
|
||||
test_unary_operators();
|
||||
test_binary_operators();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
79
test/is_instance_of_test.cpp
Normal file
79
test/is_instance_of_test.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
// is_instance_of_test.cpp -- The Boost Lambda Library ------------------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
|
||||
#include "boost/lambda/detail/is_instance_of.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
template <class T1> struct A1 {};
|
||||
template <class T1, class T2> struct A2 {};
|
||||
template <class T1, class T2, class T3> struct A3 {};
|
||||
template <class T1, class T2, class T3, class T4> struct A4 {};
|
||||
|
||||
class B1 : public A1<int> {};
|
||||
class B2 : public A2<int,int> {};
|
||||
class B3 : public A3<int,int,int> {};
|
||||
class B4 : public A4<int,int,int,int> {};
|
||||
|
||||
// classes that are convertible to classes that derive from A instances
|
||||
// This is not enough to make the test succeed
|
||||
|
||||
class C1 { public: operator A1<int>() { return A1<int>(); } };
|
||||
class C2 { public: operator B2() { return B2(); } };
|
||||
class C3 { public: operator B3() { return B3(); } };
|
||||
class C4 { public: operator B4() { return B4(); } };
|
||||
|
||||
// test that the result is really a constant
|
||||
// (in an alternative implementation, gcc 3.0.2. claimed that it was
|
||||
// a non-constant)
|
||||
template <bool b> class X {};
|
||||
// this should compile
|
||||
X<boost::lambda::is_instance_of_2<int, A2>::value> x;
|
||||
|
||||
|
||||
int test_main(int, char *[]) {
|
||||
|
||||
using boost::lambda::is_instance_of_1;
|
||||
using boost::lambda::is_instance_of_2;
|
||||
using boost::lambda::is_instance_of_3;
|
||||
using boost::lambda::is_instance_of_4;
|
||||
|
||||
|
||||
BOOST_TEST((is_instance_of_1<B1, A1>::value == true));
|
||||
BOOST_TEST((is_instance_of_1<A1<float>, A1>::value == true));
|
||||
BOOST_TEST((is_instance_of_1<int, A1>::value == false));
|
||||
BOOST_TEST((is_instance_of_1<C1, A1>::value == false));
|
||||
|
||||
BOOST_TEST((is_instance_of_2<B2, A2>::value == true));
|
||||
BOOST_TEST((is_instance_of_2<A2<int, float>, A2>::value == true));
|
||||
BOOST_TEST((is_instance_of_2<int, A2>::value == false));
|
||||
BOOST_TEST((is_instance_of_2<C2, A2>::value == false));
|
||||
|
||||
BOOST_TEST((is_instance_of_3<B3, A3>::value == true));
|
||||
BOOST_TEST((is_instance_of_3<A3<int, float, char>, A3>::value == true));
|
||||
BOOST_TEST((is_instance_of_3<int, A3>::value == false));
|
||||
BOOST_TEST((is_instance_of_3<C3, A3>::value == false));
|
||||
|
||||
BOOST_TEST((is_instance_of_4<B4, A4>::value == true));
|
||||
BOOST_TEST((is_instance_of_4<A4<int, float, char, double>, A4>::value == true));
|
||||
BOOST_TEST((is_instance_of_4<int, A4>::value == false));
|
||||
BOOST_TEST((is_instance_of_4<C4, A4>::value == false));
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
192
test/member_pointer_test.cpp
Normal file
192
test/member_pointer_test.cpp
Normal file
@@ -0,0 +1,192 @@
|
||||
// member_pointer_test.cpp -- The Boost Lambda Library ------------------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
|
||||
#include "boost/lambda/lambda.hpp"
|
||||
#include "boost/lambda/bind.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace boost::lambda;
|
||||
using namespace std;
|
||||
|
||||
|
||||
struct my_struct {
|
||||
my_struct(int x) : mem(x) {};
|
||||
|
||||
int mem;
|
||||
|
||||
int fooc() const { return mem; }
|
||||
int foo() { return mem; }
|
||||
int foo1c(int y) const { return y + mem; }
|
||||
int foo1(int y) { return y + mem; }
|
||||
int foo2c(int y, int x) const { return y + x + mem; }
|
||||
int foo2(int y, int x) { return y + x + mem; }
|
||||
int foo3c(int y, int x, int z) const { return y + x + z + mem; }
|
||||
int foo3(int y, int x, int z ){ return y + x + z + mem; }
|
||||
int foo4c(int a1, int a2, int a3, int a4) const { return a1+a2+a3+a4+mem; }
|
||||
int foo4(int a1, int a2, int a3, int a4){ return a1+a2+a3+a4+mem; }
|
||||
|
||||
int foo3default(int y = 1, int x = 2, int z = 3) { return y + x + z + mem; }
|
||||
};
|
||||
|
||||
my_struct x(3);
|
||||
|
||||
void pointer_to_data_member_tests() {
|
||||
|
||||
// int i = 0;
|
||||
my_struct *y = &x;
|
||||
|
||||
BOOST_TEST((_1 ->* &my_struct::mem)(y) == 3);
|
||||
|
||||
(_1 ->* &my_struct::mem)(y) = 4;
|
||||
BOOST_TEST(x.mem == 4);
|
||||
|
||||
((_1 ->* &my_struct::mem) = 5)(y);
|
||||
BOOST_TEST(x.mem == 5);
|
||||
|
||||
// &my_struct::mem is a temporary, must be constified
|
||||
((y ->* _1) = 6)(make_const(&my_struct::mem));
|
||||
BOOST_TEST(x.mem == 6);
|
||||
|
||||
((_1 ->* _2) = 7)(y, make_const(&my_struct::mem));
|
||||
BOOST_TEST(x.mem == 7);
|
||||
|
||||
}
|
||||
|
||||
void pointer_to_member_function_tests() {
|
||||
|
||||
my_struct *y = new my_struct(1);
|
||||
BOOST_TEST( (_1 ->* &my_struct::foo)(y)() == (y->mem));
|
||||
BOOST_TEST( (_1 ->* &my_struct::fooc)(y)() == (y->mem));
|
||||
BOOST_TEST( (y ->* _1)(make_const(&my_struct::foo))() == (y->mem));
|
||||
BOOST_TEST( (y ->* _1)(make_const(&my_struct::fooc))() == (y->mem));
|
||||
BOOST_TEST( (_1 ->* _2)(y, make_const(&my_struct::foo))() == (y->mem));
|
||||
BOOST_TEST( (_1 ->* _2)(y, make_const(&my_struct::fooc))() == (y->mem));
|
||||
|
||||
BOOST_TEST( (_1 ->* &my_struct::foo1)(y)(1) == (y->mem+1));
|
||||
BOOST_TEST( (_1 ->* &my_struct::foo1c)(y)(1) == (y->mem+1));
|
||||
BOOST_TEST( (y ->* _1)(make_const(&my_struct::foo1))(1) == (y->mem+1));
|
||||
BOOST_TEST( (y ->* _1)(make_const(&my_struct::foo1c))(1) == (y->mem+1));
|
||||
BOOST_TEST( (_1 ->* _2)(y, make_const(&my_struct::foo1))(1) == (y->mem+1));
|
||||
BOOST_TEST( (_1 ->* _2)(y, make_const(&my_struct::foo1c))(1) == (y->mem+1));
|
||||
|
||||
BOOST_TEST( (_1 ->* &my_struct::foo2)(y)(1,2) == (y->mem+1+2));
|
||||
BOOST_TEST( (_1 ->* &my_struct::foo2c)(y)(1,2) == (y->mem+1+2));
|
||||
BOOST_TEST( (y ->* _1)(make_const(&my_struct::foo2))(1,2) == (y->mem+1+2));
|
||||
BOOST_TEST( (y ->* _1)(make_const(&my_struct::foo2c))(1,2) == (y->mem+1+2));
|
||||
BOOST_TEST( (_1 ->* _2)(y, make_const(&my_struct::foo2))(1,2) == (y->mem+1+2));
|
||||
BOOST_TEST( (_1 ->* _2)(y, make_const(&my_struct::foo2c))(1,2) == (y->mem+1+2));
|
||||
|
||||
BOOST_TEST( (_1 ->* &my_struct::foo3)(y)(1,2,3) == (y->mem+1+2+3));
|
||||
BOOST_TEST( (_1 ->* &my_struct::foo3c)(y)(1,2,3) == (y->mem+1+2+3));
|
||||
BOOST_TEST( (y ->* _1)(make_const(&my_struct::foo3))(1,2,3) == (y->mem+1+2+3));
|
||||
BOOST_TEST( (y ->* _1)(make_const(&my_struct::foo3c))(1,2,3) == (y->mem+1+2+3));
|
||||
BOOST_TEST( (_1 ->* _2)(y, make_const(&my_struct::foo3))(1,2,3) == (y->mem+1+2+3));
|
||||
BOOST_TEST( (_1 ->* _2)(y, make_const(&my_struct::foo3c))(1,2,3) == (y->mem+1+2+3));
|
||||
|
||||
BOOST_TEST( (_1 ->* &my_struct::foo4)(y)(1,2,3,4) == (y->mem+1+2+3+4));
|
||||
BOOST_TEST( (_1 ->* &my_struct::foo4c)(y)(1,2,3,4) == (y->mem+1+2+3+4));
|
||||
BOOST_TEST( (y ->* _1)(make_const(&my_struct::foo4))(1,2,3,4) == (y->mem+1+2+3+4));
|
||||
BOOST_TEST( (y ->* _1)(make_const(&my_struct::foo4c))(1,2,3,4) == (y->mem+1+2+3+4));
|
||||
BOOST_TEST( (_1 ->* _2)(y, make_const(&my_struct::foo4))(1,2,3,4) == (y->mem+1+2+3+4));
|
||||
BOOST_TEST( (_1 ->* _2)(y, make_const(&my_struct::foo4c))(1,2,3,4) == (y->mem+1+2+3+4));
|
||||
|
||||
|
||||
|
||||
// member functions with default values do not work (inherent language issue)
|
||||
// BOOST_TEST( (_1 ->* &my_struct::foo3default)(y)() == (y->mem+1+2+3));
|
||||
|
||||
}
|
||||
|
||||
class A {};
|
||||
class B {};
|
||||
class C {};
|
||||
class D {};
|
||||
|
||||
// ->* can be overloaded to do anything
|
||||
bool operator->*(A a, B b) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool operator->*(B b, A a) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// let's provide specializations to take care of the return type deduction.
|
||||
// Note, that you need to provide all four cases for non-const and const
|
||||
// or use the plain_return_type_2 template.
|
||||
namespace boost {
|
||||
namespace lambda {
|
||||
|
||||
template <>
|
||||
struct return_type_2<other_action<member_pointer_action>, B, A> {
|
||||
typedef bool type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct return_type_2<other_action<member_pointer_action>, const B, A> {
|
||||
typedef bool type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct return_type_2<other_action<member_pointer_action>, B, const A> {
|
||||
typedef bool type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct return_type_2<other_action<member_pointer_action>, const B, const A> {
|
||||
typedef bool type;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // lambda
|
||||
} // boost
|
||||
|
||||
void test_overloaded_pointer_to_member()
|
||||
{
|
||||
A a; B b;
|
||||
|
||||
// this won't work, can't deduce the return type
|
||||
// BOOST_TEST((_1->*_2)(a, b) == false);
|
||||
|
||||
// ret<bool> gives the return type
|
||||
BOOST_TEST(ret<bool>(_1->*_2)(a, b) == false);
|
||||
BOOST_TEST(ret<bool>(a->*_1)(b) == false);
|
||||
BOOST_TEST(ret<bool>(_1->*b)(a) == false);
|
||||
BOOST_TEST((ret<bool>((var(a))->*b))() == false);
|
||||
BOOST_TEST((ret<bool>((var(a))->*var(b)))() == false);
|
||||
|
||||
|
||||
// this is ok without ret<bool> due to the return_type_2 spcialization above
|
||||
BOOST_TEST((_1->*_2)(b, a) == true);
|
||||
BOOST_TEST((b->*_1)(a) == true);
|
||||
BOOST_TEST((_1->*a)(b) == true);
|
||||
BOOST_TEST((var(b)->*a)() == true);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int test_main(int, char *[]) {
|
||||
|
||||
pointer_to_data_member_tests();
|
||||
pointer_to_member_function_tests();
|
||||
test_overloaded_pointer_to_member();
|
||||
return 0;
|
||||
}
|
||||
|
||||
399
test/operator_tests_simple.cpp
Normal file
399
test/operator_tests_simple.cpp
Normal file
@@ -0,0 +1,399 @@
|
||||
// operator_tests_simple.cpp -- The Boost Lambda Library ---------------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
#include "boost/lambda/lambda.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#ifndef BOOST_NO_STRINGSTREAM
|
||||
#include <sstream>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
using namespace boost::lambda;
|
||||
|
||||
|
||||
class unary_plus_tester {};
|
||||
unary_plus_tester operator+(const unary_plus_tester& a) { return a; }
|
||||
|
||||
void cout_tests()
|
||||
{
|
||||
#ifndef BOOST_NO_STRINGSTREAM
|
||||
using std::cout;
|
||||
ostringstream os;
|
||||
int i = 10;
|
||||
(os << _1)(i);
|
||||
|
||||
(os << constant("FOO"))();
|
||||
|
||||
BOOST_TEST(os.str() == std::string("10FOO"));
|
||||
|
||||
|
||||
istringstream is("ABC 1");
|
||||
std::string s;
|
||||
int k;
|
||||
|
||||
is >> s;
|
||||
is >> k;
|
||||
|
||||
BOOST_TEST(s == std::string("ABC"));
|
||||
BOOST_TEST(k == 1);
|
||||
// test for constant, constant_ref and var
|
||||
i = 5;
|
||||
constant_type<int>::type ci(constant(i));
|
||||
var_type<int>::type vi(var(i));
|
||||
|
||||
(vi = _1)(make_const(100));
|
||||
BOOST_TEST((ci)() == 5);
|
||||
BOOST_TEST(i == 100);
|
||||
|
||||
int a;
|
||||
constant_ref_type<int>::type cr(constant_ref(i));
|
||||
(++vi, var(a) = cr)();
|
||||
BOOST_TEST(i == 101);
|
||||
#endif
|
||||
}
|
||||
|
||||
void arithmetic_operators() {
|
||||
int i = 1; int j = 2; int k = 3;
|
||||
|
||||
using namespace std;
|
||||
using namespace boost::lambda;
|
||||
|
||||
BOOST_TEST((_1 + 1)(i)==2);
|
||||
BOOST_TEST(((_1 + 1) * _2)(i, j)==4);
|
||||
BOOST_TEST((_1 - 1)(i)==0);
|
||||
|
||||
BOOST_TEST((_1 * 2)(j)==4);
|
||||
BOOST_TEST((_1 / 2)(j)==1);
|
||||
|
||||
BOOST_TEST((_1 % 2)(k)==1);
|
||||
|
||||
BOOST_TEST((-_1)(i) == -1);
|
||||
BOOST_TEST((+_1)(i) == 1);
|
||||
|
||||
// test that unary plus really does something
|
||||
unary_plus_tester u;
|
||||
unary_plus_tester up = (+_1)(u);
|
||||
}
|
||||
|
||||
void bitwise_operators() {
|
||||
unsigned int ui = 2;
|
||||
|
||||
BOOST_TEST((_1 << 1)(ui)==(2 << 1));
|
||||
BOOST_TEST((_1 >> 1)(ui)==(2 >> 1));
|
||||
|
||||
BOOST_TEST((_1 & 1)(ui)==(2 & 1));
|
||||
BOOST_TEST((_1 | 1)(ui)==(2 | 1));
|
||||
BOOST_TEST((_1 ^ 1)(ui)==(2 ^ 1));
|
||||
BOOST_TEST((~_1)(ui)==~2u);
|
||||
}
|
||||
|
||||
void comparison_operators() {
|
||||
int i = 0, j = 1;
|
||||
|
||||
BOOST_TEST((_1 < _2)(i, j) == true);
|
||||
BOOST_TEST((_1 <= _2)(i, j) == true);
|
||||
BOOST_TEST((_1 == _2)(i, j) == false);
|
||||
BOOST_TEST((_1 != _2)(i, j) == true);
|
||||
BOOST_TEST((_1 > _2)(i, j) == false);
|
||||
BOOST_TEST((_1 >= _2)(i, j) == false);
|
||||
|
||||
BOOST_TEST((!(_1 < _2))(i, j) == false);
|
||||
BOOST_TEST((!(_1 <= _2))(i, j) == false);
|
||||
BOOST_TEST((!(_1 == _2))(i, j) == true);
|
||||
BOOST_TEST((!(_1 != _2))(i, j) == false);
|
||||
BOOST_TEST((!(_1 > _2))(i, j) == true);
|
||||
BOOST_TEST((!(_1 >= _2))(i, j) == true);
|
||||
}
|
||||
|
||||
void logical_operators() {
|
||||
|
||||
bool t = true, f = false;
|
||||
BOOST_TEST((_1 && _2)(t, t) == true);
|
||||
BOOST_TEST((_1 && _2)(t, f) == false);
|
||||
BOOST_TEST((_1 && _2)(f, t) == false);
|
||||
BOOST_TEST((_1 && _2)(f, f) == false);
|
||||
|
||||
BOOST_TEST((_1 || _2)(t, t) == true);
|
||||
BOOST_TEST((_1 || _2)(t, f) == true);
|
||||
BOOST_TEST((_1 || _2)(f, t) == true);
|
||||
BOOST_TEST((_1 || _2)(f, f) == false);
|
||||
|
||||
BOOST_TEST((!_1)(t) == false);
|
||||
BOOST_TEST((!_1)(f) == true);
|
||||
|
||||
// test short circuiting
|
||||
int i=0;
|
||||
|
||||
(false && ++_1)(i);
|
||||
BOOST_TEST(i==0);
|
||||
i = 0;
|
||||
|
||||
(true && ++_1)(i);
|
||||
BOOST_TEST(i==1);
|
||||
i = 0;
|
||||
|
||||
(false || ++_1)(i);
|
||||
BOOST_TEST(i==1);
|
||||
i = 0;
|
||||
|
||||
(true || ++_1)(i);
|
||||
BOOST_TEST(i==0);
|
||||
i = 0;
|
||||
}
|
||||
|
||||
void unary_incs_and_decs() {
|
||||
int i = 0;
|
||||
|
||||
BOOST_TEST(_1++(i) == 0);
|
||||
BOOST_TEST(i == 1);
|
||||
i = 0;
|
||||
|
||||
BOOST_TEST(_1--(i) == 0);
|
||||
BOOST_TEST(i == -1);
|
||||
i = 0;
|
||||
|
||||
BOOST_TEST((++_1)(i) == 1);
|
||||
BOOST_TEST(i == 1);
|
||||
i = 0;
|
||||
|
||||
BOOST_TEST((--_1)(i) == -1);
|
||||
BOOST_TEST(i == -1);
|
||||
i = 0;
|
||||
|
||||
// the result of prefix -- and ++ are lvalues
|
||||
(++_1)(i) = 10;
|
||||
BOOST_TEST(i==10);
|
||||
i = 0;
|
||||
|
||||
(--_1)(i) = 10;
|
||||
BOOST_TEST(i==10);
|
||||
i = 0;
|
||||
}
|
||||
|
||||
void compound_operators() {
|
||||
|
||||
int i = 1;
|
||||
|
||||
// normal variable as the left operand
|
||||
(i += _1)(make_const(1));
|
||||
BOOST_TEST(i == 2);
|
||||
|
||||
(i -= _1)(make_const(1));
|
||||
BOOST_TEST(i == 1);
|
||||
|
||||
(i *= _1)(make_const(10));
|
||||
BOOST_TEST(i == 10);
|
||||
|
||||
(i /= _1)(make_const(2));
|
||||
BOOST_TEST(i == 5);
|
||||
|
||||
(i %= _1)(make_const(2));
|
||||
BOOST_TEST(i == 1);
|
||||
|
||||
// lambda expression as a left operand
|
||||
(_1 += 1)(i);
|
||||
BOOST_TEST(i == 2);
|
||||
|
||||
(_1 -= 1)(i);
|
||||
BOOST_TEST(i == 1);
|
||||
|
||||
(_1 *= 10)(i);
|
||||
BOOST_TEST(i == 10);
|
||||
|
||||
(_1 /= 2)(i);
|
||||
BOOST_TEST(i == 5);
|
||||
|
||||
(_1 %= 2)(i);
|
||||
BOOST_TEST(i == 1);
|
||||
|
||||
// shifts
|
||||
unsigned int ui = 2;
|
||||
(_1 <<= 1)(ui);
|
||||
BOOST_TEST(ui==(2 << 1));
|
||||
|
||||
ui = 2;
|
||||
(_1 >>= 1)(ui);
|
||||
BOOST_TEST(ui==(2 >> 1));
|
||||
|
||||
ui = 2;
|
||||
(ui <<= _1)(make_const(1));
|
||||
BOOST_TEST(ui==(2 << 1));
|
||||
|
||||
ui = 2;
|
||||
(ui >>= _1)(make_const(1));
|
||||
BOOST_TEST(ui==(2 >> 1));
|
||||
|
||||
// and, or, xor
|
||||
ui = 2;
|
||||
(_1 &= 1)(ui);
|
||||
BOOST_TEST(ui==(2 & 1));
|
||||
|
||||
ui = 2;
|
||||
(_1 |= 1)(ui);
|
||||
BOOST_TEST(ui==(2 | 1));
|
||||
|
||||
ui = 2;
|
||||
(_1 ^= 1)(ui);
|
||||
BOOST_TEST(ui==(2 ^ 1));
|
||||
|
||||
ui = 2;
|
||||
(ui &= _1)(make_const(1));
|
||||
BOOST_TEST(ui==(2 & 1));
|
||||
|
||||
ui = 2;
|
||||
(ui |= _1)(make_const(1));
|
||||
BOOST_TEST(ui==(2 | 1));
|
||||
|
||||
ui = 2;
|
||||
(ui ^= _1)(make_const(1));
|
||||
BOOST_TEST(ui==(2 ^ 1));
|
||||
|
||||
}
|
||||
|
||||
void assignment_and_subscript() {
|
||||
|
||||
// assignment and subscript need to be defined as member functions.
|
||||
// Hence, if you wish to use a normal variable as the left hand argument,
|
||||
// you must wrap it with var to turn it into a lambda expression
|
||||
|
||||
using std::string;
|
||||
string s;
|
||||
|
||||
(_1 = "one")(s);
|
||||
BOOST_TEST(s == string("one"));
|
||||
|
||||
(var(s) = "two")();
|
||||
BOOST_TEST(s == string("two"));
|
||||
|
||||
BOOST_TEST((var(s)[_1])(make_const(2)) == 'o');
|
||||
BOOST_TEST((_1[2])(s) == 'o');
|
||||
BOOST_TEST((_1[_2])(s, make_const(2)) == 'o');
|
||||
|
||||
// subscript returns lvalue
|
||||
(var(s)[_1])(make_const(1)) = 'o';
|
||||
BOOST_TEST(s == "too");
|
||||
|
||||
(_1[1])(s) = 'a';
|
||||
BOOST_TEST(s == "tao");
|
||||
|
||||
(_1[_2])(s, make_const(0)) = 'm';
|
||||
BOOST_TEST(s == "mao");
|
||||
|
||||
// TODO: tests for vector, set, map, multimap
|
||||
}
|
||||
|
||||
class A {};
|
||||
|
||||
void address_of_and_dereference() {
|
||||
|
||||
A a; int i = 42;
|
||||
|
||||
BOOST_TEST((&_1)(a) == &a);
|
||||
BOOST_TEST((*&_1)(i) == 42);
|
||||
|
||||
std::vector<int> vi; vi.push_back(1);
|
||||
std::vector<int>::iterator it = vi.begin();
|
||||
|
||||
(*_1 = 7)(it);
|
||||
BOOST_TEST(vi[0] == 7);
|
||||
|
||||
// TODO: Add tests for more complex iterator types
|
||||
}
|
||||
|
||||
|
||||
|
||||
void comma() {
|
||||
|
||||
int i = 100;
|
||||
BOOST_TEST((_1 = 10, 2 * _1)(i) == 20);
|
||||
|
||||
// TODO: that the return type is the exact type of the right argument
|
||||
// (that r/l valueness is preserved)
|
||||
|
||||
}
|
||||
|
||||
void pointer_arithmetic() {
|
||||
|
||||
int ia[4] = { 1, 2, 3, 4 };
|
||||
int* ip = ia;
|
||||
int* ia_last = &ia[3];
|
||||
|
||||
const int cia[4] = { 1, 2, 3, 4 };
|
||||
const int* cip = cia;
|
||||
const int* cia_last = &cia[3];
|
||||
|
||||
|
||||
// non-const array
|
||||
BOOST_TEST((*(_1 + 1))(ia) == 2);
|
||||
|
||||
// non-const pointer
|
||||
BOOST_TEST((*(_1 + 1))(ip) == 2);
|
||||
|
||||
BOOST_TEST((*(_1 - 1))(ia_last) == 3);
|
||||
|
||||
// const array
|
||||
BOOST_TEST((*(_1 + 1))(cia) == 2);
|
||||
// const pointer
|
||||
BOOST_TEST((*(_1 + 1))(cip) == 2);
|
||||
BOOST_TEST((*(_1 - 1))(cia_last) == 3);
|
||||
|
||||
// pointer arithmetic should not make non-consts const
|
||||
(*(_1 + 2))(ia) = 0;
|
||||
(*(_1 + 3))(ip) = 0;
|
||||
|
||||
BOOST_TEST(ia[2] == 0);
|
||||
BOOST_TEST(ia[3] == 0);
|
||||
|
||||
// pointer - pointer
|
||||
BOOST_TEST((_1 - _2)(ia_last, ia) == 3);
|
||||
BOOST_TEST((_1 - _2)(cia_last, cia) == 3);
|
||||
BOOST_TEST((ia_last - _1)(ia) == 3);
|
||||
BOOST_TEST((cia_last - _1)(cia) == 3);
|
||||
BOOST_TEST((cia_last - _1)(cip) == 3);
|
||||
|
||||
}
|
||||
|
||||
int test_main(int, char *[]) {
|
||||
|
||||
arithmetic_operators();
|
||||
bitwise_operators();
|
||||
comparison_operators();
|
||||
logical_operators();
|
||||
unary_incs_and_decs();
|
||||
compound_operators();
|
||||
assignment_and_subscript();
|
||||
address_of_and_dereference();
|
||||
comma();
|
||||
pointer_arithmetic();
|
||||
cout_tests();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
148
test/phoenix_control_structures.cpp
Normal file
148
test/phoenix_control_structures.cpp
Normal file
@@ -0,0 +1,148 @@
|
||||
// phoenix_style_control_structures.cpp -- The Boost Lambda Library ------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
#include "boost/lambda/lambda.hpp"
|
||||
#include "boost/lambda/if.hpp"
|
||||
#include "boost/lambda/loops.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
|
||||
|
||||
|
||||
using namespace boost::lambda;
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
// If-else, while, do-while, for statements
|
||||
|
||||
|
||||
int test_main(int, char *[]) {
|
||||
|
||||
vector<int> v;
|
||||
v.clear();
|
||||
v.push_back(1);
|
||||
v.push_back(2);
|
||||
v.push_back(3);
|
||||
v.push_back(4);
|
||||
v.push_back(5);
|
||||
v.push_back(6);
|
||||
v.push_back(7);
|
||||
v.push_back(8);
|
||||
v.push_back(9);
|
||||
v.push_back(10);
|
||||
|
||||
int sum = 0;
|
||||
//////////////////////////////////
|
||||
for_each(v.begin(), v.end(),
|
||||
if_(_1 > 3 && _1 <= 8)
|
||||
[
|
||||
sum += _1
|
||||
]
|
||||
);
|
||||
|
||||
BOOST_TEST(sum == 4+5+6+7+8);
|
||||
|
||||
int gt = 0, eq = 0, lt = 0;
|
||||
//////////////////////////////////
|
||||
for_each(v.begin(), v.end(),
|
||||
if_(_1 > 5)
|
||||
[
|
||||
++var(gt)
|
||||
]
|
||||
.else_
|
||||
[
|
||||
if_(_1 == 5)
|
||||
[
|
||||
++var(eq)
|
||||
]
|
||||
.else_
|
||||
[
|
||||
++var(lt)
|
||||
]
|
||||
]
|
||||
);
|
||||
|
||||
BOOST_TEST(lt==4);
|
||||
BOOST_TEST(eq==1);
|
||||
BOOST_TEST(gt==5);
|
||||
|
||||
vector<int> t = v;
|
||||
|
||||
int counta = 0;
|
||||
int countb = 0;
|
||||
//////////////////////////////////
|
||||
for_each(v.begin(), v.end(),
|
||||
(
|
||||
while_(_1--)
|
||||
[
|
||||
++var(counta)
|
||||
],
|
||||
++var(countb)
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(counta == 55);
|
||||
BOOST_TEST(countb == 10);
|
||||
|
||||
|
||||
v = t;
|
||||
|
||||
counta = 0; countb = 0;
|
||||
//////////////////////////////////
|
||||
for_each(v.begin(), v.end(),
|
||||
(
|
||||
do_
|
||||
[
|
||||
++var(counta)
|
||||
]
|
||||
.while_(_1--),
|
||||
++var(countb)
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(counta == (2+11)*10/2);
|
||||
BOOST_TEST(countb == 10);
|
||||
|
||||
|
||||
v = t;
|
||||
counta = 0; countb = 0;
|
||||
//////////////////////////////////
|
||||
int iii;
|
||||
for_each(v.begin(), v.end(),
|
||||
(
|
||||
for_(var(iii) = 0, var(iii) < _1, ++var(iii))
|
||||
[
|
||||
++var(counta)
|
||||
],
|
||||
++var(countb)
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(counta == (1+10)*10/2);
|
||||
BOOST_TEST(countb == 10);
|
||||
|
||||
v = t;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
392
test/switch_construct.cpp
Normal file
392
test/switch_construct.cpp
Normal file
@@ -0,0 +1,392 @@
|
||||
// switch_test.cpp -- The Boost Lambda Library --------------------------
|
||||
//
|
||||
// Copyright (C) 2000-2003 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
|
||||
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// For more information, see www.boost.org
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
#include <boost/test/minimal.hpp> // see "Header Implementation Option"
|
||||
|
||||
|
||||
#include "boost/lambda/lambda.hpp"
|
||||
#include "boost/lambda/if.hpp"
|
||||
#include "boost/lambda/switch.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
|
||||
|
||||
// Check that elements 0 -- index are 1, and the rest are 0
|
||||
bool check(const std::vector<int>& v, int index) {
|
||||
using namespace boost::lambda;
|
||||
int counter = 0;
|
||||
std::vector<int>::const_iterator
|
||||
result = std::find_if(v.begin(), v.end(),
|
||||
! if_then_else_return(
|
||||
var(counter)++ <= index,
|
||||
_1 == 1,
|
||||
_1 == 0)
|
||||
);
|
||||
return result == v.end();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void do_switch_no_defaults_tests() {
|
||||
|
||||
using namespace boost::lambda;
|
||||
|
||||
int i = 0;
|
||||
std::vector<int> v,w;
|
||||
|
||||
// elements from 0 to 9
|
||||
std::generate_n(std::back_inserter(v),
|
||||
10,
|
||||
var(i)++);
|
||||
std::fill_n(std::back_inserter(w), 10, 0);
|
||||
|
||||
// ---
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0]))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 0));
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1]))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 1));
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
case_statement<2>(++var(w[2]))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 2));
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
case_statement<2>(++var(w[2])),
|
||||
case_statement<3>(++var(w[3]))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 3));
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
case_statement<2>(++var(w[2])),
|
||||
case_statement<3>(++var(w[3])),
|
||||
case_statement<4>(++var(w[4]))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 4));
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
case_statement<2>(++var(w[2])),
|
||||
case_statement<3>(++var(w[3])),
|
||||
case_statement<4>(++var(w[4])),
|
||||
case_statement<5>(++var(w[5]))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 5));
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
case_statement<2>(++var(w[2])),
|
||||
case_statement<3>(++var(w[3])),
|
||||
case_statement<4>(++var(w[4])),
|
||||
case_statement<5>(++var(w[5])),
|
||||
case_statement<6>(++var(w[6]))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 6));
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
case_statement<2>(++var(w[2])),
|
||||
case_statement<3>(++var(w[3])),
|
||||
case_statement<4>(++var(w[4])),
|
||||
case_statement<5>(++var(w[5])),
|
||||
case_statement<6>(++var(w[6])),
|
||||
case_statement<7>(++var(w[7]))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 7));
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
case_statement<2>(++var(w[2])),
|
||||
case_statement<3>(++var(w[3])),
|
||||
case_statement<4>(++var(w[4])),
|
||||
case_statement<5>(++var(w[5])),
|
||||
case_statement<6>(++var(w[6])),
|
||||
case_statement<7>(++var(w[7])),
|
||||
case_statement<8>(++var(w[8]))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 8));
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void do_switch_yes_defaults_tests() {
|
||||
|
||||
using namespace boost::lambda;
|
||||
|
||||
int i = 0;
|
||||
std::vector<int> v,w;
|
||||
|
||||
// elements from 0 to 9
|
||||
std::generate_n(std::back_inserter(v),
|
||||
10,
|
||||
var(i)++);
|
||||
std::fill_n(std::back_inserter(w), 10, 0);
|
||||
|
||||
int default_count;
|
||||
// ---
|
||||
default_count = 0;
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
default_statement(++var(default_count))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, -1));
|
||||
BOOST_TEST(default_count == 10);
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
default_count = 0;
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
default_statement(++var(default_count))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 0));
|
||||
BOOST_TEST(default_count == 9);
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
default_count = 0;
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
default_statement(++var(default_count))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 1));
|
||||
BOOST_TEST(default_count == 8);
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
default_count = 0;
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
case_statement<2>(++var(w[2])),
|
||||
default_statement(++var(default_count))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 2));
|
||||
BOOST_TEST(default_count == 7);
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
default_count = 0;
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
case_statement<2>(++var(w[2])),
|
||||
case_statement<3>(++var(w[3])),
|
||||
default_statement(++var(default_count))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 3));
|
||||
BOOST_TEST(default_count == 6);
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
default_count = 0;
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
case_statement<2>(++var(w[2])),
|
||||
case_statement<3>(++var(w[3])),
|
||||
case_statement<4>(++var(w[4])),
|
||||
default_statement(++var(default_count))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 4));
|
||||
BOOST_TEST(default_count == 5);
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
default_count = 0;
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
case_statement<2>(++var(w[2])),
|
||||
case_statement<3>(++var(w[3])),
|
||||
case_statement<4>(++var(w[4])),
|
||||
case_statement<5>(++var(w[5])),
|
||||
default_statement(++var(default_count))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 5));
|
||||
BOOST_TEST(default_count == 4);
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
default_count = 0;
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
case_statement<2>(++var(w[2])),
|
||||
case_statement<3>(++var(w[3])),
|
||||
case_statement<4>(++var(w[4])),
|
||||
case_statement<5>(++var(w[5])),
|
||||
case_statement<6>(++var(w[6])),
|
||||
default_statement(++var(default_count))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 6));
|
||||
BOOST_TEST(default_count == 3);
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
// ---
|
||||
default_count = 0;
|
||||
std::for_each(v.begin(), v.end(),
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<0>(++var(w[0])),
|
||||
case_statement<1>(++var(w[1])),
|
||||
case_statement<2>(++var(w[2])),
|
||||
case_statement<3>(++var(w[3])),
|
||||
case_statement<4>(++var(w[4])),
|
||||
case_statement<5>(++var(w[5])),
|
||||
case_statement<6>(++var(w[6])),
|
||||
case_statement<7>(++var(w[7])),
|
||||
default_statement(++var(default_count))
|
||||
)
|
||||
);
|
||||
|
||||
BOOST_TEST(check(w, 7));
|
||||
BOOST_TEST(default_count == 2);
|
||||
std::fill_n(w.begin(), 10, 0);
|
||||
|
||||
}
|
||||
|
||||
void test_empty_cases() {
|
||||
|
||||
using namespace boost::lambda;
|
||||
|
||||
// ---
|
||||
switch_statement(
|
||||
_1,
|
||||
default_statement()
|
||||
)(make_const(1));
|
||||
|
||||
switch_statement(
|
||||
_1,
|
||||
case_statement<1>()
|
||||
)(make_const(1));
|
||||
|
||||
}
|
||||
|
||||
int test_main(int, char* []) {
|
||||
|
||||
do_switch_no_defaults_tests();
|
||||
do_switch_yes_defaults_tests();
|
||||
|
||||
test_empty_cases();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user