Testcases autogenerated from the Boost.Function tutorial

[SVN r16711]
This commit is contained in:
Douglas Gregor
2002-12-28 03:44:00 +00:00
parent 3b644dbfff
commit 951cb3acd4
10 changed files with 251 additions and 0 deletions

View File

@ -0,0 +1,38 @@
#include <boost/function.hpp>
#include <iostream>
float mul_ints(int x, int y) { return ((float)x) * y; }
int main()
{
boost::function<float (int x, int y)> f;
struct int_div {
float operator()(int x, int y) const { return ((float)x)/y; };
};
f = int_div();
std::cout << f(5, 3) >> std::endl;
if (f)
std::cout << f(5, 3) << std::endl;
else
std::cout << "f has no target, so it is unsafe to call" << std::endl;
f = 0;
f = &mul_ints;
}

View File

@ -0,0 +1,37 @@
#include <boost/function.hpp>
#include <iostream>
float mul_ints(int x, int y) { return ((float)x) * y; }
int main()
{
boost::function2<float, int, int> f;
struct int_div {
float operator()(int x, int y) const { return ((float)x)/y; };
};
f = int_div();
std::cout << f(5, 3) >> std::endl;
if (f)
std::cout << f(5, 3) << std::endl;
else
std::cout << "f has no target, so it is unsafe to call" << std::endl;
f = 0;
f = &mul_ints;
}

View File

@ -0,0 +1,19 @@
#include <boost/function.hpp>
#include <iostream>
struct stateful_type { int operator(int) const { return 0; } };
int main()
{
stateful_type a_function_object;
boost::function<int (int)> f;
f = boost::ref(a_function_object);
boost::function<int (int)> f2(f);
}

View File

@ -0,0 +1,19 @@
#include <boost/function.hpp>
#include <iostream>
struct stateful_type { int operator(int) const { return 0; } };
int main()
{
stateful_type a_function_object;
boost::function1<int, int> f;
f = boost::ref(a_function_object);
boost::function1<int, int> f2(f);
}

23
test/mem_fun_cxx98.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <boost/function.hpp>
#include <iostream>
#include <functional>
struct X {
int foo(int);
};
int main()
{
boost::function<int (X*, int)> f;
f = &X::foo;
X x;
f(&x, 5);
}

23
test/mem_fun_portable.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <boost/function.hpp>
#include <iostream>
#include <functional>
struct X {
int foo(int);
};
int main()
{
boost::function2<int, X*, int> f;
f = &X::foo;
X x;
f(&x, 5);
}

22
test/std_bind_cxx98.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <boost/function.hpp>
#include <iostream>
#include <functional>
struct X {
int foo(int);
};
int main()
{
boost::function<int (int)> f;
X x;
f = std::bind1st(std::mem_fun(&X::foo), &x);
f(5); // Call x.foo(5)
}

View File

@ -0,0 +1,22 @@
#include <boost/function.hpp>
#include <iostream>
#include <functional>
struct X {
int foo(int);
};
int main()
{
boost::function1<int, int> f;
X x;
f = std::bind1st(std::mem_fun(&X::foo), &x);
f(5); // Call x.foo(5)
}

24
test/sum_avg_cxx98.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <boost/function.hpp>
#include <iostream>
void do_sum_avg(int values[], int n, int& sum, float& avg)
{
sum = 0;
for (int i = 0; i < n; i++)
sum += values[i];
avg = (float)sum / n;
}
int main()
{
boost::function<void (int values[], int n, int& sum, float& avg)> sum_avg;
sum_avg = &do_sum_avg;
}

24
test/sum_avg_portable.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <boost/function.hpp>
#include <iostream>
void do_sum_avg(int values[], int n, int& sum, float& avg)
{
sum = 0;
for (int i = 0; i < n; i++)
sum += values[i];
avg = (float)sum / n;
}
int main()
{
boost::function4<void, int[], int, int&, float> sum_avg;
sum_avg = &do_sum_avg;
}