1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
// -*- c++ -*-
/* Copyright 2002, The libsigc++ Development Team
* Assigned to public domain. Use as you wish without restriction.
*/
#include "testutilities.h"
#include <sigc++/adaptors/retype.h>
#include <sstream>
#include <cstdlib>
namespace
{
TestUtilities* util = nullptr;
std::ostringstream result_stream;
struct foo : public sigc::trackable
{
float test_int(int i)
{
result_stream << "foo::test_int(int " << i << ") ";
return i * 1.5f;
}
float test_float(float f)
{
result_stream << "foo::test_float(float " << f << ") ";
return f * 5;
}
};
void bar(short s)
{
result_stream << "bar(short " << s << ")";
}
void test_member_int()
{
foo foo_;
result_stream << sigc::retype(sigc::mem_fun(foo_, &foo::test_int))(1.234f);
util->check_result(result_stream, "foo::test_int(int 1) 1.5");
}
void test_member_float()
{
foo foo_;
result_stream << sigc::retype(sigc::mem_fun(foo_, &foo::test_float))(5);
util->check_result(result_stream, "foo::test_float(float 5) 25");
}
void test_ptr_fun()
{
sigc::retype(sigc::ptr_fun(&bar))(6.789f);
util->check_result(result_stream, "bar(short 6)");
}
void test_member_int_with_slot()
{
foo foo_;
sigc::slot<float,float> s1 = sigc::retype(sigc::mem_fun(foo_, &foo::test_int));
result_stream << s1(1.234f);
util->check_result(result_stream, "foo::test_int(int 1) 1.5");
}
void test_member_float_with_slot()
{
foo foo_;
sigc::slot<float,int> s2 = sigc::retype(sigc::mem_fun(foo_, &foo::test_float));
result_stream << s2(5);
util->check_result(result_stream, "foo::test_float(float 5) 25");
}
void test_ptr_fun_with_slot()
{
sigc::slot<void,double> s3 = sigc::retype(sigc::ptr_fun(&bar));
s3(6.789);
util->check_result(result_stream, "bar(short 6)");
}
void test_retype_slot()
{
foo foo_;
sigc::slot<float,float> s1 = sigc::retype(sigc::mem_fun(foo_, &foo::test_int));
sigc::slot<float,int> s2 = sigc::retype(s1);
result_stream << s2(5);
util->check_result(result_stream, "foo::test_int(int 5) 7.5");
}
void test_std_function_style_syntax()
{
foo foo_;
sigc::slot<float(float)> s1 = sigc::retype(sigc::mem_fun(foo_, &foo::test_int));
result_stream << s1(1.234f);
util->check_result(result_stream, "foo::test_int(int 1) 1.5");
}
} // end anonymous namespace
int main(int argc, char* argv[])
{
util = TestUtilities::get_instance();
if (!util->check_command_args(argc, argv))
return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
test_member_int();
test_member_float();
test_ptr_fun();
test_member_int_with_slot();
test_member_float_with_slot();
test_ptr_fun_with_slot();
test_retype_slot();
test_std_function_style_syntax();
return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
}
|