]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - tests/test_signal.cc
Import libsigc++ 2.10.8 sources
[ext/sigc++-2.0.git] / tests / test_signal.cc
1 // -*- c++ -*-
2 /* Copyright 2002, The libsigc++ Development Team
3  *  Assigned to public domain.  Use as you wish without restriction.
4  */
5
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>
11 #include <string>
12 #include <cstdlib>
13
14 namespace
15 {
16
17 TestUtilities* util = nullptr;
18 std::ostringstream result_stream;
19
20 int foo(int i)
21 {
22   result_stream << "foo(int " << i << ") ";
23   return 1;
24 }
25
26 struct A : public sigc::trackable
27 {
28   int foo(int i)
29   {
30     result_stream << "A::foo(int " << i << ") ";
31     return 1;
32   }
33
34   void bar(std::string& str)
35   {
36     result_stream << "A::foo(string '" << str << "') ";
37     str = "foo was here";
38   }
39 };
40
41 void test_empty_signal()
42 {
43   // signal
44   sigc::signal<int,int> sig;
45
46   // emit empty signal
47   sig(0);
48   util->check_result(result_stream, "");
49 }
50
51 void test_simple()
52 {
53   sigc::signal<int, int> sig;
54   sig.connect(sigc::ptr_fun(&foo));
55
56   sig(1);
57   util->check_result(result_stream, "foo(int 1) ");
58 }
59
60 int bar(float i)
61 {
62   result_stream << "bar(float " << i << ") ";
63   return 1;
64 }
65
66 void test_auto_disconnection()
67 {
68   // signal
69   sigc::signal<int,int> sig;
70
71   // connect some slots before emitting & test auto-disconnection
72   {
73     A a;
74     sig.connect(sigc::ptr_fun1(&foo));
75     sig.connect(sigc::mem_fun1(a, &A::foo));
76     sig.connect(sigc::ptr_fun1(&bar));
77     sig(1);
78     result_stream << sig.size();
79     util->check_result(result_stream, "foo(int 1) A::foo(int 1) bar(float 1) 3");
80
81   } // a dies => auto-disconnect
82
83   sig(2);
84   result_stream << sig.size();
85   util->check_result(result_stream, "foo(int 2) bar(float 2) 2");
86 }
87
88 void test_reference()
89 {
90   // test reference
91   A a;
92   std::string str("guest book");
93   sigc::signal<void,std::string&> sigstr;
94   sigstr.connect(sigc::mem_fun(a, &A::bar));
95   sigstr(str);
96   result_stream << str;
97   util->check_result(result_stream, "A::foo(string 'guest book') foo was here");
98 }
99
100 void test_make_slot()
101 {
102   // 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));
107
108   sigc::signal<int,int> sig2;
109   sig2.connect(sig.make_slot());
110   sig2(3);
111   util->check_result(result_stream, "foo(int 3) bar(float 3) foo(int 3) ");
112 }
113
114 void test_std_function_style_syntax()
115 {
116   sigc::signal<int(int)> sig;
117   sig.connect(sigc::ptr_fun(&foo));
118
119   sig(1);
120   util->check_result(result_stream, "foo(int 1) ");
121 }
122
123 void test_clear_called_in_signal_handler()
124 {
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();
130   sig.emit();
131   result_stream << sig.size();
132   sig.emit();
133   util->check_result(result_stream, "3, slot 1, slot 2, 0");
134 }
135
136 void test_clear_called_outside_signal_handler()
137 {
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();
143   sig.emit();
144   sig.clear();
145   result_stream << sig.size();
146   sig.emit();
147   util->check_result(result_stream, "3, slot 1, slot 2, slot 3, 0");
148 }
149
150 } // end anonymous namespace
151
152 int main(int argc, char* argv[])
153 {
154   util = TestUtilities::get_instance();
155
156   if (!util->check_command_args(argc, argv))
157     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
158
159   test_empty_signal();
160   test_simple();
161   test_auto_disconnection();
162   test_reference();
163   test_make_slot();
164   test_std_function_style_syntax();
165   test_clear_called_in_signal_handler();
166   test_clear_called_outside_signal_handler();
167
168   return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
169 }