]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - tests/test_compose.cc
Adjust the name of the library to match upstream
[ext/sigc++-2.0.git] / tests / test_compose.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++/adaptors/compose.h>
8 #include <sstream>
9 #include <cstdlib>
10
11 // assume existance of T_functor::result_type for unknown functor types:
12 namespace sigc { SIGC_FUNCTORS_HAVE_RESULT_TYPE }
13
14 namespace
15 {
16 std::ostringstream result_stream;
17
18 struct set
19 {
20   // choose a type that can hold all return values
21   typedef double result_type;
22
23   double operator()(int i)
24   {
25     result_stream << "set(int " << i << ") ";
26     return i*i;
27   }
28
29   double operator()(double i)
30   {
31     result_stream << "set(double " << i << ") ";
32     return i*5;
33   }
34 };
35
36 struct set_void
37 {
38   typedef void result_type;
39
40   void operator()(double i)
41   {
42     result_stream << "set_void(double " << i << ")";
43   }
44 };
45
46 struct get
47 {
48 #ifdef SIGC_CXX_TYPEOF
49   bool operator()()
50   {
51     result_stream << "get() ";
52     return true;
53   }
54
55   int operator()(int i)
56   {
57     result_stream << "get(" << i << ") ";
58     return i*2;
59   }
60
61   double operator()(int i, int j)
62   {
63     result_stream << "get(" << i << ", " << j << ") ";
64     return double(i)/double(j);
65   }
66 #else
67   // choose a type that can hold all return values
68   typedef double result_type;
69
70   double operator()()
71   {
72     result_stream << "get() ";
73     return true;
74   }
75
76   double operator()(int i)
77   {
78     result_stream << "get(" << i << ") ";
79     return i*2;
80   }
81
82   double operator()(int i, int j)
83   {
84     result_stream << "get(" << i << ", " << j << ") ";
85     return double(i)/double(j);
86   }
87 #endif
88 };
89
90 } // end anonymous namespace
91
92 int main(int argc, char* argv[])
93 {
94   auto util = TestUtilities::get_instance();
95
96   if (!util->check_command_args(argc, argv))
97     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
98
99   result_stream << sigc::compose(set(), get())();
100   util->check_result(result_stream, "get() set(double 1) 5");
101
102   result_stream << sigc::compose(set(), get())(1);
103   util->check_result(result_stream, "get(1) set(double 2) 10");
104
105   result_stream << sigc::compose(set(), get())(1, 2);
106   util->check_result(result_stream, "get(1, 2) set(double 0.5) 2.5");
107
108   sigc::compose(set_void(), get())(3); //void test
109   util->check_result(result_stream, "get(3) set_void(double 6)");
110
111   return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
112 }