2 /* Copyright 2002, The libsigc++ Development Team
3 * Assigned to public domain. Use as you wish without restriction.
6 #include "testutilities.h"
7 #include <sigc++/trackable.h>
8 #include <sigc++/signal.h>
9 #include <sigc++/functors/ptr_fun.h>
10 #include <sigc++/functors/mem_fun.h>
17 TestUtilities* util = nullptr;
18 std::ostringstream result_stream;
22 result_stream << "foo(int " << i << ") ";
26 struct A : public sigc::trackable
30 result_stream << "A::foo(int " << i << ") ";
34 void bar(std::string& str)
36 result_stream << "A::foo(string '" << str << "') ";
41 void test_empty_signal()
44 sigc::signal<int,int> sig;
48 util->check_result(result_stream, "");
53 sigc::signal<int, int> sig;
54 sig.connect(sigc::ptr_fun(&foo));
57 util->check_result(result_stream, "foo(int 1) ");
62 result_stream << "bar(float " << i << ") ";
66 void test_auto_disconnection()
69 sigc::signal<int,int> sig;
71 // connect some slots before emitting & test auto-disconnection
74 sig.connect(sigc::ptr_fun1(&foo));
75 sig.connect(sigc::mem_fun1(a, &A::foo));
76 sig.connect(sigc::ptr_fun1(&bar));
78 result_stream << sig.size();
79 util->check_result(result_stream, "foo(int 1) A::foo(int 1) bar(float 1) 3");
81 } // a dies => auto-disconnect
84 result_stream << sig.size();
85 util->check_result(result_stream, "foo(int 2) bar(float 2) 2");
92 std::string str("guest book");
93 sigc::signal<void,std::string&> sigstr;
94 sigstr.connect(sigc::mem_fun(a, &A::bar));
97 util->check_result(result_stream, "A::foo(string 'guest book') foo was here");
100 void test_make_slot()
103 sigc::signal<int,int> sig;
104 sig.connect(sigc::ptr_fun1(&foo));
105 sig.connect(sigc::ptr_fun(&bar));
106 sig.connect(sigc::ptr_fun(&foo));
108 sigc::signal<int,int> sig2;
109 sig2.connect(sig.make_slot());
111 util->check_result(result_stream, "foo(int 3) bar(float 3) foo(int 3) ");
114 void test_std_function_style_syntax()
116 sigc::signal<int(int)> sig;
117 sig.connect(sigc::ptr_fun(&foo));
120 util->check_result(result_stream, "foo(int 1) ");
123 void test_clear_called_in_signal_handler()
125 sigc::signal<void()> sig;
126 sig.connect([]() { result_stream << ", slot 1, "; });
127 sig.connect([&sig]() { sig.clear(); result_stream << "slot 2, "; });
128 sig.connect([]() { result_stream << "slot 3, "; });
129 result_stream << sig.size();
131 result_stream << sig.size();
133 util->check_result(result_stream, "3, slot 1, slot 2, 0");
136 void test_clear_called_outside_signal_handler()
138 sigc::signal<void()> sig;
139 sig.connect([]() { result_stream << ", slot 1, "; });
140 sig.connect([]() { result_stream << "slot 2, "; });
141 sig.connect([]() { result_stream << "slot 3, "; });
142 result_stream << sig.size();
145 result_stream << sig.size();
147 util->check_result(result_stream, "3, slot 1, slot 2, slot 3, 0");
150 } // end anonymous namespace
152 int main(int argc, char* argv[])
154 util = TestUtilities::get_instance();
156 if (!util->check_command_args(argc, argv))
157 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
161 test_auto_disconnection();
164 test_std_function_style_syntax();
165 test_clear_called_in_signal_handler();
166 test_clear_called_outside_signal_handler();
168 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;