]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - tests/test_slot.cc
Adjust the name of the library to match upstream
[ext/sigc++-2.0.git] / tests / test_slot.cc
1 /* Copyright 2002, The libsigc++ Development Team
2  *  Assigned to public domain.  Use as you wish without restriction.
3  */
4
5 #include "testutilities.h"
6 #include <sigc++/functors/slot.h>
7 #include <sstream>
8 #include <string>
9 #include <cstdlib>
10
11 //The Tru64 compiler seems to need this to avoid an unresolved symbol
12 //See bug #161503
13 #include <new>
14
15 namespace
16 {
17
18 TestUtilities* util = nullptr;
19 std::ostringstream result_stream;
20
21 class foo
22 {
23 public:
24   void operator()(int i)
25   {
26     result_stream << "foo(int " << i << ")";
27   }
28
29   void operator()(std::string& str)
30   {
31     result_stream << "foo(string '" << str << "') ";
32     str="foo was here";
33   }
34
35   void operator()(int, int)
36   {
37     result_stream << "foo(int, int)";
38   }
39 };
40
41 void test_simple()
42 {
43   // simple test
44   sigc::slot<void,int> s1 = foo();
45   s1(1);
46   util->check_result(result_stream, "foo(int 1)");
47
48   s1 = foo();
49   s1(2);
50   util->check_result(result_stream, "foo(int 2)");
51 }
52
53 void test_std_function_style_syntax()
54 {
55   // simple test
56   sigc::slot<void(int)> s1 = foo();
57   s1(1);
58   util->check_result(result_stream, "foo(int 1)");
59
60   s1 = foo();
61   s1(2);
62   util->check_result(result_stream, "foo(int 2)");
63 }
64
65 void test_implicit_conversion()
66 {
67   // test implicit conversion
68   sigc::slot<void,char> s2 = foo();
69   s2(3);
70   util->check_result(result_stream, "foo(int 3)");
71 }
72
73 void test_reference()
74 {
75   // test reference
76   sigc::slot<void,std::string&> sl1 = foo();
77   std::string str("guest book");
78   sl1(str);
79   result_stream << str;
80   util->check_result(result_stream, "foo(string 'guest book') foo was here");
81 }
82
83 void test_operator_equals()
84 {
85   // test operator=
86   std::string str = "guest book";
87   sigc::slot<void,std::string&> sl1 = foo();
88   sigc::slot<void,std::string&> sl2;
89   sl2 = sl1;
90   sl1 = sl2;
91   sl1(str);
92   result_stream << str;
93   util->check_result(result_stream, "foo(string 'guest book') foo was here");
94 }
95
96 void test_copy_ctor()
97 {
98   // test copy ctor
99   sigc::slot<void,int> s1 = foo();
100   sigc::slot<void,int> s1_clone(s1);
101   s1_clone(4);
102   util->check_result(result_stream, "foo(int 4)");
103 }
104
105 } // end anonymous namespace
106
107 int main(int argc, char* argv[])
108 {
109   util = TestUtilities::get_instance();
110
111   if (!util->check_command_args(argc, argv))
112     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
113
114   test_simple();
115   test_std_function_style_syntax();
116   test_implicit_conversion();
117   test_reference();
118   test_operator_equals();
119   test_copy_ctor();
120
121   return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
122 }