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++/sigc++.h>
11 // TODO: put something like #ifndef FORTE (some older version, I think) or AIX xlC... #else ...
13 #define ENABLE_TEST_OF_OVERLOADED_FUNCTIONS 0
18 TestUtilities* util = nullptr;
19 std::ostringstream result_stream;
23 void foo(short i1) { result_stream << "test::foo(short " << i1 << ')'; }
25 void foo_const(int i1) const { result_stream << "test::foo_const(int " << i1 << ')'; }
27 void foo_volatile(float i1) volatile
29 result_stream << "test::foo_volatile(float " << i1 << ')';
32 void foo_const_volatile(double i1) const volatile
34 result_stream << "test::foo_const_volatile(double " << i1 << ')';
37 void foo_overloaded(char i1) { result_stream << "test::foo_overloaded(char " << int(i1) << ')'; }
39 #if ENABLE_TEST_OF_OVERLOADED_FUNCTIONS
40 void foo_overloaded(short i1)
42 result_stream << "test::foo_overloaded(short " << (int)i1 << ')';
46 double foo_overloaded(int i1, int i2)
48 result_stream << "test::foo_overloaded(int " << i1 << ", int " << i2 << ')';
53 } // end anonymous namespace
58 sigc::mem_fun (&test::foo)(t, 1);
59 util->check_result(result_stream, "test::foo(short 1)");
65 sigc::mem_fun (&test::foo_const)(t, 2);
66 util->check_result(result_stream, "test::foo_const(int 2)");
69 void test_const_with_const_object()
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)");
76 void test_non_const_volatile()
79 sigc::mem_fun (&test::foo_volatile)(t, 4);
80 util->check_result(result_stream, "test::foo_volatile(float 4)");
83 void test_const_volatile()
86 sigc::mem_fun (&test::foo_const_volatile)(t, 5);
87 util->check_result(result_stream, "test::foo_const_volatile(double 5)");
90 void test_const_volatile_with_const_object()
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)");
97 #if ENABLE_TEST_OF_OVERLOADED_FUNCTIONS
98 void test_overloaded()
101 sigc::mem_fun<char> (&test::foo_overloaded)(t, 7);
102 util->check_result(result_stream, "test::foo_overloaded(char 7)");
104 sigc::mem_fun<short> (&test::foo_overloaded)(t, 7);
105 util->check_result(result_stream, "test::foo_overloaded(short 7)");
107 // sigc::mem_fun(&test::foo_overloaded)(t, 7);
108 // util->check_result(result_stream, "test::foo_overloaded(short 7)");
110 sigc::mem_fun (&test::foo_overloaded)(t, 7, 8);
111 util->check_result(result_stream, "test::foo_overloaded(int 7, int 8)");
118 sigc::mem_fun(t, &test::foo)(9);
119 util->check_result(result_stream, "test::foo(short 9)");
121 sigc::mem_fun(t, &test::foo)(9);
122 util->check_result(result_stream, "test::foo(short 9)");
124 sigc::mem_fun(t, &test::foo_const)(9);
125 util->check_result(result_stream, "test::foo_const(int 9)");
127 sigc::mem_fun(t, &test::foo_const)(9);
128 util->check_result(result_stream, "test::foo_const(int 9)");
130 sigc::mem_fun(t, &test::foo_volatile)(9);
131 util->check_result(result_stream, "test::foo_volatile(float 9)");
133 sigc::mem_fun(t, &test::foo_volatile)(9);
134 util->check_result(result_stream, "test::foo_volatile(float 9)");
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)");
140 sigc::mem_fun(t, &test::foo_overloaded)(9, 10);
141 util->check_result(result_stream, "test::foo_overloaded(int 9, int 10)");
145 class TestAutoDisconnect : public sigc::trackable
150 result_stream << "TestAutoDisconnect::foo() called.";
154 void test_auto_disconnect()
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;
160 TestAutoDisconnect t;
161 slot_of_member_method = sigc::mem_fun(t, &TestAutoDisconnect::foo);
163 //The method should be called:
164 slot_of_member_method();
165 util->check_result(result_stream, "TestAutoDisconnect::foo() called.");
168 //The method should not be called:
169 slot_of_member_method();
170 util->check_result(result_stream, "");
174 main(int argc, char* argv[])
176 util = TestUtilities::get_instance();
178 if (!util->check_command_args(argc, argv))
179 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
183 test_const_with_const_object();
184 test_non_const_volatile();
185 test_const_volatile();
186 test_const_volatile_with_const_object();
188 #if ENABLE_TEST_OF_OVERLOADED_FUNCTIONS
194 test_auto_disconnect();
196 return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;