2015-02-18 02:34:10 +11:00
|
|
|
#include "signal.hpp"
|
2015-04-13 16:45:56 +10:00
|
|
|
|
|
|
|
#include "tap.hpp"
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-06-27 15:31:41 +10:00
|
|
|
void
|
2016-01-19 18:31:49 +11:00
|
|
|
test_null (util::TAP::logger &tap)
|
2015-02-18 02:34:10 +11:00
|
|
|
{
|
2016-01-19 18:31:49 +11:00
|
|
|
tap.expect_nothrow ([] {
|
|
|
|
util::signal<void(void)> void_signal;
|
|
|
|
void_signal ();
|
|
|
|
}, "void signal");
|
2015-02-18 02:34:10 +11:00
|
|
|
}
|
2011-06-27 15:31:41 +10:00
|
|
|
|
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-06-27 15:31:41 +10:00
|
|
|
void
|
2015-02-18 02:34:10 +11:00
|
|
|
increment_uint (unsigned int &val)
|
|
|
|
{
|
|
|
|
++val;
|
2011-06-27 15:31:41 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-06-27 15:31:41 +10:00
|
|
|
void
|
2016-01-19 18:31:49 +11:00
|
|
|
test_single (util::TAP::logger &tap)
|
2015-02-18 02:34:10 +11:00
|
|
|
{
|
2011-06-27 15:31:41 +10:00
|
|
|
unsigned int val = 0;
|
2015-02-18 02:34:10 +11:00
|
|
|
util::signal<void(unsigned int&)> void_signal;
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
auto cookie = void_signal.connect (increment_uint);
|
2011-06-27 15:31:41 +10:00
|
|
|
void_signal (val);
|
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
tap.expect_eq (val, 1u, "single listener");
|
2011-06-27 15:31:41 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-06-27 15:31:41 +10:00
|
|
|
void
|
2016-01-19 18:31:49 +11:00
|
|
|
test_double (util::TAP::logger &tap)
|
2015-02-18 02:34:10 +11:00
|
|
|
{
|
2011-06-27 15:31:41 +10:00
|
|
|
unsigned int val = 0;
|
2015-02-18 02:34:10 +11:00
|
|
|
util::signal<void(unsigned int&)> void_signal;
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
auto cookie0 = void_signal.connect (increment_uint);
|
|
|
|
auto cookie1 = void_signal.connect (increment_uint);
|
2011-06-27 15:31:41 +10:00
|
|
|
void_signal (val);
|
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
tap.expect_eq (val, 2u, "double listener");
|
2011-06-27 15:31:41 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-07-03 16:04:49 +10:00
|
|
|
void
|
2016-01-19 18:31:49 +11:00
|
|
|
test_value_signal (util::TAP::logger &tap)
|
2015-02-18 02:34:10 +11:00
|
|
|
{
|
2016-01-19 18:31:49 +11:00
|
|
|
util::value_signal<unsigned> val;
|
|
|
|
unsigned passed = 0;
|
2011-07-03 16:04:49 +10:00
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
auto cookie = val.connect ([&] (unsigned v) { passed = v; });
|
2011-07-03 16:04:49 +10:00
|
|
|
|
2015-02-19 13:27:47 +11:00
|
|
|
val = 42u;
|
2016-01-19 18:31:49 +11:00
|
|
|
|
|
|
|
tap.expect_eq (passed, 42u, "value signal, passed value");
|
2015-02-19 13:27:47 +11:00
|
|
|
|
|
|
|
unsigned check = val;
|
2016-01-19 18:31:49 +11:00
|
|
|
tap.expect_eq (check, 42u, "value signal, read value");
|
2015-02-19 13:27:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-10 22:52:38 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
2016-01-19 18:31:49 +11:00
|
|
|
test_combiner (util::TAP::logger &tap)
|
2015-03-10 22:52:38 +11:00
|
|
|
{
|
|
|
|
{
|
|
|
|
util::signal<bool(void), util::combine::logical_and> sig;
|
|
|
|
|
|
|
|
unsigned count = 0;
|
2016-01-19 18:31:49 +11:00
|
|
|
auto cookie0 = sig.connect ([&] (void) { ++count; return true; });
|
|
|
|
auto cookie1 = sig.connect ([&] (void) { ++count; return true; });
|
|
|
|
auto cookie2 = sig.connect ([&] (void) { ++count; return true; });
|
2015-03-10 22:52:38 +11:00
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
tap.expect (sig (), "bool signal, success");
|
|
|
|
tap.expect_eq (count, 3u, "bool signal, success, count");
|
2015-03-10 22:52:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
util::signal<bool(void), util::combine::logical_and> sig;
|
|
|
|
|
|
|
|
unsigned count = 0;
|
2016-01-19 18:31:49 +11:00
|
|
|
auto cookie0 = sig.connect ([&] (void) { ++count; return true; });
|
|
|
|
auto cookie1 = sig.connect ([&] (void) { ++count; return false; });
|
|
|
|
auto cookie2 = sig.connect ([&] (void) { ++count; return true; });
|
|
|
|
|
|
|
|
tap.expect (!sig (), "bool signal, failure");
|
2015-03-10 22:52:38 +11:00
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
// ordering of signals is not guaranteed so we can't say for sure how
|
|
|
|
// many callbacks will be triggered; it will _probably_ be in order
|
|
|
|
// though.
|
|
|
|
tap.expect_le (count, 3u, "bool signal, failure, count");
|
2015-03-10 22:52:38 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
2016-01-19 18:31:49 +11:00
|
|
|
test_disconnect (util::TAP::logger &tap)
|
2015-03-10 22:52:38 +11:00
|
|
|
{
|
2016-01-19 18:31:49 +11:00
|
|
|
tap.expect_nothrow ([] {
|
|
|
|
util::signal<void(void)> sig;
|
2015-03-10 22:52:38 +11:00
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
util::signal<void(void)>::cookie a = sig.connect ([&] (void) { sig.disconnect (a); });
|
|
|
|
util::signal<void(void)>::cookie b = sig.connect ([&] (void) { sig.disconnect (b); });
|
|
|
|
util::signal<void(void)>::cookie c = sig.connect ([&] (void) { sig.disconnect (c); });
|
|
|
|
util::signal<void(void)>::cookie d = sig.connect ([&] (void) { sig.disconnect (d); });
|
2015-03-10 22:52:38 +11:00
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
sig ();
|
|
|
|
}, "parallel disconnect in invocation");
|
2015-03-10 22:52:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-06-27 15:31:41 +10:00
|
|
|
int
|
2015-02-18 02:34:10 +11:00
|
|
|
main (int, char **)
|
|
|
|
{
|
2015-04-13 16:45:56 +10:00
|
|
|
util::TAP::logger tap;
|
2016-01-19 18:31:49 +11:00
|
|
|
|
|
|
|
test_null (tap);
|
|
|
|
test_single (tap);
|
|
|
|
test_double (tap);
|
|
|
|
test_value_signal (tap);
|
|
|
|
test_combiner (tap);
|
|
|
|
test_disconnect (tap);
|
|
|
|
|
|
|
|
return tap.status ();
|
2011-06-27 15:31:41 +10:00
|
|
|
}
|