]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - tests/test_mem_fun.cc
Adjust the name of the library to match upstream
[ext/sigc++-2.0.git] / tests / test_mem_fun.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++/sigc++.h>
8 #include <sstream>
9 #include <cstdlib>
10
11 // TODO: put something like #ifndef FORTE (some older version, I think) or AIX xlC... #else ...
12 // #endif around:
13 #define ENABLE_TEST_OF_OVERLOADED_FUNCTIONS 0
14
15 namespace
16 {
17
18 TestUtilities* util = nullptr;
19 std::ostringstream result_stream;
20
21 struct test
22 {
23   void foo(short i1) { result_stream << "test::foo(short " << i1 << ')'; }
24
25   void foo_const(int i1) const { result_stream << "test::foo_const(int " << i1 << ')'; }
26
27   void foo_volatile(float i1) volatile
28   {
29     result_stream << "test::foo_volatile(float " << i1 << ')';
30   }
31
32   void foo_const_volatile(double i1) const volatile
33   {
34     result_stream << "test::foo_const_volatile(double " << i1 << ')';
35   }
36
37   void foo_overloaded(char i1) { result_stream << "test::foo_overloaded(char " << int(i1) << ')'; }
38
39 #if ENABLE_TEST_OF_OVERLOADED_FUNCTIONS
40   void foo_overloaded(short i1)
41   {
42     result_stream << "test::foo_overloaded(short " << (int)i1 << ')';
43   }
44 #endif
45
46   double foo_overloaded(int i1, int i2)
47   {
48     result_stream << "test::foo_overloaded(int " << i1 << ", int " << i2 << ')';
49     return 1.0;
50   }
51 };
52
53 } // end anonymous namespace
54
55 void test_non_const()
56 {
57   test t;
58   sigc::mem_fun (&test::foo)(t, 1);
59   util->check_result(result_stream, "test::foo(short 1)");
60 }
61
62 void test_const()
63 {
64   test t;
65   sigc::mem_fun (&test::foo_const)(t, 2);
66   util->check_result(result_stream, "test::foo_const(int 2)");
67 }
68
69 void test_const_with_const_object()
70 {
71   const auto t = test();
72   sigc::mem_fun (&test::foo_const)(t, 3);
73   util->check_result(result_stream, "test::foo_const(int 3)");
74 }
75
76 void test_non_const_volatile()
77 {
78   test t;
79   sigc::mem_fun (&test::foo_volatile)(t, 4);
80   util->check_result(result_stream, "test::foo_volatile(float 4)");
81 }
82
83 void test_const_volatile()
84 {
85   test t;
86   sigc::mem_fun (&test::foo_const_volatile)(t, 5);
87   util->check_result(result_stream, "test::foo_const_volatile(double 5)");
88 }
89
90 void test_const_volatile_with_const_object()
91 {
92   const auto t = test();
93   sigc::mem_fun (&test::foo_const_volatile)(t, 6);
94   util->check_result(result_stream, "test::foo_const_volatile(double 6)");
95 }
96
97 #if ENABLE_TEST_OF_OVERLOADED_FUNCTIONS
98 void test_overloaded()
99 {
100   test t;
101   sigc::mem_fun<char> (&test::foo_overloaded)(t, 7);
102   util->check_result(result_stream, "test::foo_overloaded(char 7)");
103
104   sigc::mem_fun<short> (&test::foo_overloaded)(t, 7);
105   util->check_result(result_stream, "test::foo_overloaded(short 7)");
106
107   // sigc::mem_fun(&test::foo_overloaded)(t, 7);
108   // util->check_result(result_stream, "test::foo_overloaded(short 7)");
109
110   sigc::mem_fun (&test::foo_overloaded)(t, 7, 8);
111   util->check_result(result_stream, "test::foo_overloaded(int 7, int 8)");
112 }
113 #endif
114
115 void test_bound()
116 {
117   test t;
118   sigc::mem_fun(t, &test::foo)(9);
119   util->check_result(result_stream, "test::foo(short 9)");
120
121   sigc::mem_fun(t, &test::foo)(9);
122   util->check_result(result_stream, "test::foo(short 9)");
123
124   sigc::mem_fun(t, &test::foo_const)(9);
125   util->check_result(result_stream, "test::foo_const(int 9)");
126
127   sigc::mem_fun(t, &test::foo_const)(9);
128   util->check_result(result_stream, "test::foo_const(int 9)");
129
130   sigc::mem_fun(t, &test::foo_volatile)(9);
131   util->check_result(result_stream, "test::foo_volatile(float 9)");
132
133   sigc::mem_fun(t, &test::foo_volatile)(9);
134   util->check_result(result_stream, "test::foo_volatile(float 9)");
135
136 #if ENABLE_TEST_OF_OVERLOADED_FUNCTIONS
137   sigc::mem_fun(t, &test::foo_overloaded)(9, 10);
138   util->check_result(result_stream, "test::foo_overloaded(int 9, int 10)");
139
140   sigc::mem_fun(t, &test::foo_overloaded)(9, 10);
141   util->check_result(result_stream, "test::foo_overloaded(int 9, int 10)");
142 #endif
143 }
144
145 class TestAutoDisconnect : public sigc::trackable
146 {
147 public:
148   void foo()
149   {
150     result_stream << "TestAutoDisconnect::foo() called.";
151   }
152 };
153
154 void test_auto_disconnect()
155 {
156   //Check that slot doesn't try to call a method on a destroyed instance,
157   //when the instance's class derives from trackable.
158   sigc::slot<void()> slot_of_member_method;
159   {
160     TestAutoDisconnect t;
161     slot_of_member_method = sigc::mem_fun(t, &TestAutoDisconnect::foo);
162
163     //The method should be called:
164     slot_of_member_method();
165     util->check_result(result_stream, "TestAutoDisconnect::foo() called.");
166   }
167
168   //The method should not be called:
169   slot_of_member_method();
170   util->check_result(result_stream, "");
171 }
172
173 int
174 main(int argc, char* argv[])
175 {
176   util = TestUtilities::get_instance();
177
178   if (!util->check_command_args(argc, argv))
179     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
180
181   test_non_const();
182   test_const();
183   test_const_with_const_object();
184   test_non_const_volatile();
185   test_const_volatile();
186   test_const_volatile_with_const_object();
187
188 #if ENABLE_TEST_OF_OVERLOADED_FUNCTIONS
189   test_overload();
190 #endif
191
192   test_bound();
193
194   test_auto_disconnect();
195
196   return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
197 }