2011-06-27 15:31:41 +10:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
#include "signal.hpp"
|
|
|
|
#include "debug.hpp"
|
|
|
|
#include "raii.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
|
2015-02-18 02:34:10 +11:00
|
|
|
test_null (void)
|
|
|
|
{
|
|
|
|
util::signal<void(void)> void_signal;
|
|
|
|
void_signal ();
|
|
|
|
}
|
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
|
2015-02-18 02:34:10 +11:00
|
|
|
test_single (void)
|
|
|
|
{
|
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
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
auto raii = void_signal.connect (increment_uint);
|
2011-06-27 15:31:41 +10:00
|
|
|
void_signal (val);
|
|
|
|
|
2012-05-11 12:34:21 +10:00
|
|
|
CHECK_EQ (val, 1);
|
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
|
|
|
test_double (void)
|
|
|
|
{
|
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
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
auto raii = void_signal.connect (increment_uint);
|
|
|
|
auto raii = void_signal.connect (increment_uint);
|
2011-06-27 15:31:41 +10:00
|
|
|
void_signal (val);
|
|
|
|
|
2012-05-11 12:34:21 +10:00
|
|
|
CHECK_EQ (val, 2);
|
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
|
2015-02-18 02:34:10 +11:00
|
|
|
test_linking_pointers (void)
|
|
|
|
{
|
|
|
|
util::signal<void(const char*)> ptr_signal;
|
|
|
|
ptr_signal (nullptr);
|
2011-07-03 16:04:49 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-19 13:27:47 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
|
|
|
test_value_signal (void)
|
|
|
|
{
|
|
|
|
util::value_signal<unsigned> val;
|
|
|
|
auto raii = val.connect ([] (unsigned v) { CHECK_EQ (v, 42); });
|
|
|
|
val = 42u;
|
|
|
|
|
|
|
|
unsigned check = val;
|
|
|
|
CHECK_EQ (check, 42);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-10 22:52:38 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
|
|
|
test_combiner (void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
util::signal<bool(void), util::combine::logical_and> sig;
|
|
|
|
|
|
|
|
unsigned count = 0;
|
|
|
|
auto raii = sig.connect ([&] (void) { ++count; return true; });
|
|
|
|
auto raii = sig.connect ([&] (void) { ++count; return true; });
|
|
|
|
auto raii = sig.connect ([&] (void) { ++count; return true; });
|
|
|
|
|
|
|
|
CHECK (sig ());
|
|
|
|
CHECK_EQ (count, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
util::signal<bool(void), util::combine::logical_and> sig;
|
|
|
|
|
|
|
|
unsigned count = 0;
|
|
|
|
auto raii = sig.connect ([&] (void) { ++count; return true; });
|
|
|
|
auto raii = sig.connect ([&] (void) { ++count; return false; });
|
|
|
|
auto raii = sig.connect ([&] (void) { ++count; return true; });
|
|
|
|
|
|
|
|
CHECK (!sig ());
|
|
|
|
CHECK_EQ (count, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
|
|
|
test_disconnect (void)
|
|
|
|
{
|
|
|
|
util::signal<void(void)> sig;
|
|
|
|
|
|
|
|
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); });
|
|
|
|
|
|
|
|
sig ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 **)
|
|
|
|
{
|
2011-06-27 15:31:41 +10:00
|
|
|
test_null ();
|
|
|
|
test_single ();
|
|
|
|
test_double ();
|
2015-02-19 13:27:47 +11:00
|
|
|
test_value_signal ();
|
2015-03-10 22:52:38 +11:00
|
|
|
test_combiner ();
|
|
|
|
test_disconnect ();
|
2011-06-27 15:31:41 +10:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|