File: test_compose.cc

package info (click to toggle)
libsigc%2B%2B-2.0 2.12.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,432 kB
  • sloc: cpp: 4,132; xml: 339; python: 196; makefile: 192; sh: 5
file content (112 lines) | stat: -rw-r--r-- 2,378 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// -*- c++ -*-
/* Copyright 2002, The libsigc++ Development Team
 *  Assigned to public domain.  Use as you wish without restriction.
 */

#include "testutilities.h"
#include <sigc++/adaptors/compose.h>
#include <sstream>
#include <cstdlib>

// assume existance of T_functor::result_type for unknown functor types:
namespace sigc { SIGC_FUNCTORS_HAVE_RESULT_TYPE }

namespace
{
std::ostringstream result_stream;

struct set
{
  // choose a type that can hold all return values
  typedef double result_type;

  double operator()(int i)
  {
    result_stream << "set(int " << i << ") ";
    return i*i;
  }

  double operator()(double i)
  {
    result_stream << "set(double " << i << ") ";
    return i*5;
  }
};

struct set_void
{
  typedef void result_type;

  void operator()(double i)
  {
    result_stream << "set_void(double " << i << ")";
  }
};

struct get
{
#ifdef SIGC_CXX_TYPEOF
  bool operator()()
  {
    result_stream << "get() ";
    return true;
  }

  int operator()(int i)
  {
    result_stream << "get(" << i << ") ";
    return i*2;
  }

  double operator()(int i, int j)
  {
    result_stream << "get(" << i << ", " << j << ") ";
    return double(i)/double(j);
  }
#else
  // choose a type that can hold all return values
  typedef double result_type;

  double operator()()
  {
    result_stream << "get() ";
    return true;
  }

  double operator()(int i)
  {
    result_stream << "get(" << i << ") ";
    return i*2;
  }

  double operator()(int i, int j)
  {
    result_stream << "get(" << i << ", " << j << ") ";
    return double(i)/double(j);
  }
#endif
};

} // end anonymous namespace

int main(int argc, char* argv[])
{
  auto util = TestUtilities::get_instance();

  if (!util->check_command_args(argc, argv))
    return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;

  result_stream << sigc::compose(set(), get())();
  util->check_result(result_stream, "get() set(double 1) 5");

  result_stream << sigc::compose(set(), get())(1);
  util->check_result(result_stream, "get(1) set(double 2) 10");

  result_stream << sigc::compose(set(), get())(1, 2);
  util->check_result(result_stream, "get(1, 2) set(double 0.5) 2.5");

  sigc::compose(set_void(), get())(3); //void test
  util->check_result(result_stream, "get(3) set_void(double 6)");

  return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
}