Imported Upstream version 2.99.2
[platform/upstream/libsigc++.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_fun(&foo));
75     sig.connect(sigc::mem_fun(a, &A::foo));
76     sig.connect(sigc::ptr_fun(&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_fun(&foo));
105   sig.connect(sigc::ptr_fun(&bar));
106   sig.connect(sigc::ptr_fun(&foo));
107   sigc::signal<int(int)> sig2;
108   sig2.connect(sig.make_slot());
109   sig2(3);
110   util->check_result(result_stream, "foo(int 3) bar(float 3) foo(int 3) ");
111 }
112
113 } // end anonymous namespace
114
115 int main(int argc, char* argv[])
116 {
117   util = TestUtilities::get_instance();
118
119   if (!util->check_command_args(argc, argv))
120     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
121
122   test_empty_signal();
123   test_simple();
124   test_auto_disconnection();
125   test_reference();
126   test_make_slot();
127
128   return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
129 }