2015-02-18 02:34:10 +11:00
|
|
|
#include "signal.hpp"
|
2015-04-13 16:45:56 +10:00
|
|
|
|
2019-01-03 15:48:34 +11:00
|
|
|
#include "except.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
|
2018-08-05 14:42:02 +10:00
|
|
|
test_null (cruft::TAP::logger &tap)
|
2015-02-18 02:34:10 +11:00
|
|
|
{
|
2016-01-19 18:31:49 +11:00
|
|
|
tap.expect_nothrow ([] {
|
2019-03-13 12:08:57 +11:00
|
|
|
cruft::signal<void(*)(void)> void_signal;
|
2016-01-19 18:31:49 +11:00
|
|
|
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
|
2018-08-05 14:42:02 +10:00
|
|
|
test_single (cruft::TAP::logger &tap)
|
2015-02-18 02:34:10 +11:00
|
|
|
{
|
2011-06-27 15:31:41 +10:00
|
|
|
unsigned int val = 0;
|
2019-03-13 12:08:57 +11:00
|
|
|
cruft::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
|
2018-08-05 14:42:02 +10:00
|
|
|
test_double (cruft::TAP::logger &tap)
|
2015-02-18 02:34:10 +11:00
|
|
|
{
|
2011-06-27 15:31:41 +10:00
|
|
|
unsigned int val = 0;
|
2019-03-13 12:08:57 +11:00
|
|
|
cruft::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
|
2018-08-05 14:42:02 +10:00
|
|
|
test_value_signal (cruft::TAP::logger &tap)
|
2015-02-18 02:34:10 +11:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::value_signal<unsigned> val;
|
2016-01-19 18:31:49 +11:00
|
|
|
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
|
2020-07-23 15:16:15 +10:00
|
|
|
test_parallel_release (cruft::TAP::logger &tap)
|
2015-03-10 22:52:38 +11:00
|
|
|
{
|
2016-01-19 18:31:49 +11:00
|
|
|
tap.expect_nothrow ([] {
|
2019-03-13 12:08:57 +11:00
|
|
|
using function_t = std::function<void(void)>;
|
|
|
|
cruft::signal<function_t> sig;
|
2015-03-10 22:52:38 +11:00
|
|
|
|
2020-07-23 15:16:15 +10:00
|
|
|
cruft::signal<function_t>::cookie a = sig.connect ([&] (void) { a.release (); });
|
|
|
|
cruft::signal<function_t>::cookie b = sig.connect ([&] (void) { b.release (); });
|
|
|
|
cruft::signal<function_t>::cookie c = sig.connect ([&] (void) { c.release (); });
|
|
|
|
cruft::signal<function_t>::cookie d = sig.connect ([&] (void) { d.release (); });
|
2015-03-10 22:52:38 +11:00
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
sig ();
|
2020-07-23 15:16:15 +10:00
|
|
|
}, "parallel release in invocation");
|
2015-03-10 22:52:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2020-07-23 15:16:15 +10:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
2011-06-27 15:31:41 +10:00
|
|
|
int
|
2015-02-18 02:34:10 +11:00
|
|
|
main (int, char **)
|
|
|
|
{
|
2019-01-03 15:48:34 +11:00
|
|
|
return cruft::TAP::logger::run ([] (auto &tap) {
|
|
|
|
test_null (tap);
|
|
|
|
test_single (tap);
|
|
|
|
test_double (tap);
|
|
|
|
test_value_signal (tap);
|
2020-07-23 15:16:15 +10:00
|
|
|
test_parallel_release (tap);
|
2019-01-03 15:48:34 +11:00
|
|
|
});
|
2011-06-27 15:31:41 +10:00
|
|
|
}
|