File: test_slot.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 (122 lines) | stat: -rw-r--r-- 2,526 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
113
114
115
116
117
118
119
120
121
122
/* Copyright 2002, The libsigc++ Development Team
 *  Assigned to public domain.  Use as you wish without restriction.
 */

#include "testutilities.h"
#include <sigc++/functors/slot.h>
#include <sstream>
#include <string>
#include <cstdlib>

//The Tru64 compiler seems to need this to avoid an unresolved symbol
//See bug #161503
#include <new>

namespace
{

TestUtilities* util = nullptr;
std::ostringstream result_stream;

class foo
{
public:
  void operator()(int i)
  {
    result_stream << "foo(int " << i << ")";
  }

  void operator()(std::string& str)
  {
    result_stream << "foo(string '" << str << "') ";
    str="foo was here";
  }

  void operator()(int, int)
  {
    result_stream << "foo(int, int)";
  }
};

void test_simple()
{
  // simple test
  sigc::slot<void,int> s1 = foo();
  s1(1);
  util->check_result(result_stream, "foo(int 1)");

  s1 = foo();
  s1(2);
  util->check_result(result_stream, "foo(int 2)");
}

void test_std_function_style_syntax()
{
  // simple test
  sigc::slot<void(int)> s1 = foo();
  s1(1);
  util->check_result(result_stream, "foo(int 1)");

  s1 = foo();
  s1(2);
  util->check_result(result_stream, "foo(int 2)");
}

void test_implicit_conversion()
{
  // test implicit conversion
  sigc::slot<void,char> s2 = foo();
  s2(3);
  util->check_result(result_stream, "foo(int 3)");
}

void test_reference()
{
  // test reference
  sigc::slot<void,std::string&> sl1 = foo();
  std::string str("guest book");
  sl1(str);
  result_stream << str;
  util->check_result(result_stream, "foo(string 'guest book') foo was here");
}

void test_operator_equals()
{
  // test operator=
  std::string str = "guest book";
  sigc::slot<void,std::string&> sl1 = foo();
  sigc::slot<void,std::string&> sl2;
  sl2 = sl1;
  sl1 = sl2;
  sl1(str);
  result_stream << str;
  util->check_result(result_stream, "foo(string 'guest book') foo was here");
}

void test_copy_ctor()
{
  // test copy ctor
  sigc::slot<void,int> s1 = foo();
  sigc::slot<void,int> s1_clone(s1);
  s1_clone(4);
  util->check_result(result_stream, "foo(int 4)");
}

} // end anonymous namespace

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

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

  test_simple();
  test_std_function_style_syntax();
  test_implicit_conversion();
  test_reference();
  test_operator_equals();
  test_copy_ctor();

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