summaryrefslogtreecommitdiff
path: root/tests/test_signal.cc
blob: d050558b98850f5ddaf549a8973033ea6edd17f6 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* Copyright 2002 - 2016, The libsigc++ Development Team
 *  Assigned to public domain.  Use as you wish without restriction.
 */

#include "testutilities.h"
#include <sigc++/trackable.h>
#include <sigc++/signal.h>

namespace
{

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

int
foo(int i)
{
  result_stream << "foo(int " << i << ") ";
  return 1;
}

struct A : public sigc::trackable
{
  int foo(int i)
  {
    result_stream << "A::foo(int " << i << ") ";
    return 1;
  }

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

void
test_empty_signal()
{
  // signal
  sigc::signal<int(int)> sig;

  // emit empty signal
  sig(0);
  util->check_result(result_stream, "");
}

void
test_simple()
{
  sigc::signal<int(int)> sig;
  sig.connect(sigc::ptr_fun(&foo));

  sig(1);
  util->check_result(result_stream, "foo(int 1) ");
}

int
bar(float i)
{
  result_stream << "bar(float " << i << ") ";
  return 1;
}

void
test_auto_disconnection()
{
  // signal
  sigc::signal<int(int)> sig;

  // connect some slots before emitting & test auto-disconnection
  {
    A a;
    sig.connect(sigc::ptr_fun(&foo));
    sig.connect(sigc::mem_fun(a, &A::foo));
    sig.connect(sigc::ptr_fun(&bar));
    sig(1);
    result_stream << sig.size();
    util->check_result(result_stream, "foo(int 1) A::foo(int 1) bar(float 1) 3");

  } // a dies => auto-disconnect

  sig(2);
  result_stream << sig.size();
  util->check_result(result_stream, "foo(int 2) bar(float 2) 2");
}

void
test_reference()
{
  // test reference
  A a;
  std::string str("guest book");
  sigc::signal<void(std::string&)> sigstr;
  sigstr.connect(sigc::mem_fun(a, &A::bar));
  sigstr(str);
  result_stream << str;
  util->check_result(result_stream, "A::foo(string 'guest book') foo was here");
}

void
test_make_slot()
{
  // test make_slot()
  sigc::signal<int(int)> sig;
  sig.connect(sigc::ptr_fun(&foo));
  sig.connect(sigc::ptr_fun(&bar));
  sig.connect(sigc::ptr_fun(&foo));
  sigc::signal<int(int)> sig2;
  sig2.connect(sig.make_slot());
  sig2(3);
  util->check_result(result_stream, "foo(int 3) bar(float 3) foo(int 3) ");
}

void
test_clear_called_in_signal_handler()
{
  sigc::signal<void()> sig;
  sig.connect([]() { result_stream << ", slot 1, "; });
  sig.connect(
    [&sig]()
    {
      sig.clear();
      result_stream << "slot 2, ";
    });
  sig.connect([]() { result_stream << "slot 3, "; });
  result_stream << sig.size();
  sig.emit();
  result_stream << sig.size();
  sig.emit();
  util->check_result(result_stream, "3, slot 1, slot 2, 0");
}

void
test_clear_called_outside_signal_handler()
{
  sigc::signal<void()> sig;
  sig.connect([]() { result_stream << ", slot 1, "; });
  sig.connect([]() { result_stream << "slot 2, "; });
  sig.connect([]() { result_stream << "slot 3, "; });
  result_stream << sig.size();
  sig.emit();
  sig.clear();
  result_stream << sig.size();
  sig.emit();
  util->check_result(result_stream, "3, slot 1, slot 2, slot 3, 0");
}

} // 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_empty_signal();
  test_simple();
  test_auto_disconnection();
  test_reference();
  test_make_slot();
  test_clear_called_in_signal_handler();
  test_clear_called_outside_signal_handler();

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