Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/signals/test/signal_n_test.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 4.2 KB
Line 
1// Boost.Signals library
2
3// Copyright Douglas Gregor 2001-2003. Use, modification and
4// distribution is subject to the Boost Software License, Version
5// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6// http://www.boost.org/LICENSE_1_0.txt)
7
8// For more information, see http://www.boost.org
9
10#include <boost/test/minimal.hpp>
11#include <boost/signal.hpp>
12#include <functional>
13
14template<typename T>
15struct max_or_default {
16  typedef T result_type;
17  template<typename InputIterator>
18  typename InputIterator::value_type
19  operator()(InputIterator first, InputIterator last) const
20  {
21    if (first == last)
22      return T();
23
24    T max = *first++;
25    for (; first != last; ++first)
26      max = (*first > max)? *first : max;
27
28    return max;
29  }
30};
31
32struct make_int {
33  make_int(int n, int cn) : N(n), CN(n) {}
34  int operator()() { return N; }
35  int operator()() const { return CN; }
36
37  int N;
38  int CN;
39};
40
41template<int N>
42struct make_increasing_int {
43  make_increasing_int() : n(N) {}
44
45  int operator()() const { return n++; }
46
47  mutable int n;
48};
49
50int get_37() { return 37; }
51
52static void
53test_zero_args()
54{
55  make_int i42(42, 41);
56  make_int i2(2, 1);
57  make_int i72(72, 71);
58  make_int i63(63, 63);
59  make_int i62(62, 61);
60
61  {
62    boost::signal0<int, max_or_default<int>, std::string> s0;
63    boost::BOOST_SIGNALS_NAMESPACE::connection c2 = s0.connect(i2);
64    boost::BOOST_SIGNALS_NAMESPACE::connection c72 = s0.connect("72", i72);
65    boost::BOOST_SIGNALS_NAMESPACE::connection c62 = s0.connect("6x", i62);
66    boost::BOOST_SIGNALS_NAMESPACE::connection c42 = s0.connect(i42);
67    boost::BOOST_SIGNALS_NAMESPACE::connection c37 = s0.connect(&get_37);
68
69    BOOST_CHECK(s0() == 72);
70
71    s0.disconnect("72");
72    BOOST_CHECK(s0() == 62);
73
74    c72.disconnect(); // Double-disconnect should be safe
75    BOOST_CHECK(s0() == 62);
76
77    s0.disconnect("72"); // Triple-disconect should be safe
78    BOOST_CHECK(s0() == 62);
79
80    // Also connect 63 in the same group as 62
81    s0.connect("6x", i63);
82    BOOST_CHECK(s0() == 63);
83
84    // Disconnect all of the 60's
85    s0.disconnect("6x");
86    BOOST_CHECK(s0() == 42);
87
88    c42.disconnect();
89    BOOST_CHECK(s0() == 37);
90
91    c37.disconnect();
92    BOOST_CHECK(s0() == 2);
93
94    c2.disconnect();
95    BOOST_CHECK(s0() == 0);
96  }
97
98  {
99    boost::signal0<int, max_or_default<int> > s0;
100    boost::BOOST_SIGNALS_NAMESPACE::connection c2 = s0.connect(i2);
101    boost::BOOST_SIGNALS_NAMESPACE::connection c72 = s0.connect(i72);
102    boost::BOOST_SIGNALS_NAMESPACE::connection c62 = s0.connect(i62);
103    boost::BOOST_SIGNALS_NAMESPACE::connection c42 = s0.connect(i42);
104
105    const boost::signal0<int, max_or_default<int> >& cs0 = s0;
106    BOOST_CHECK(cs0() == 72);
107  }
108
109  {
110    make_increasing_int<7> i7;
111    make_increasing_int<10> i10;
112
113    boost::signal0<int, max_or_default<int> > s0;
114    boost::BOOST_SIGNALS_NAMESPACE::connection c7 = s0.connect(i7);
115    boost::BOOST_SIGNALS_NAMESPACE::connection c10 = s0.connect(i10);
116
117    BOOST_CHECK(s0() == 10);
118    BOOST_CHECK(s0() == 11);
119  }
120}
121
122static void
123test_one_arg()
124{
125  boost::signal1<int, int, max_or_default<int> > s1;
126
127  s1.connect(std::negate<int>());
128  s1.connect(std::bind1st(std::multiplies<int>(), 2));
129
130  BOOST_CHECK(s1(1) == 2);
131  BOOST_CHECK(s1(-1) == 1);
132}
133
134static void
135test_signal_signal_connect()
136{
137  boost::signal1<int, int, max_or_default<int> > s1;
138
139  s1.connect(std::negate<int>());
140
141  BOOST_CHECK(s1(3) == -3);
142
143  {
144    boost::signal1<int, int, max_or_default<int> > s2;
145    s1.connect(s2);
146    s2.connect(std::bind1st(std::multiplies<int>(), 2));
147    s2.connect(std::bind1st(std::multiplies<int>(), -3));
148
149    BOOST_CHECK(s2(-3) == 9);
150    BOOST_CHECK(s1(3) == 6);
151  } // s2 goes out of scope and disconnects
152
153  BOOST_CHECK(s1(3) == -3);
154}
155
156struct EventCounter
157{
158  EventCounter() : count(0) {}
159
160  void operator()()
161  {
162    ++count;
163  }
164
165  int count;
166};
167
168static void
169test_ref()
170{
171  EventCounter ec;
172  boost::signal0<void> s;
173
174  {
175    boost::BOOST_SIGNALS_NAMESPACE::scoped_connection c = s.connect(boost::ref(ec));
176    BOOST_CHECK(ec.count == 0);
177    s();
178    BOOST_CHECK(ec.count == 1);
179  }
180  s();
181  BOOST_CHECK(ec.count == 1);
182}
183
184int
185test_main(int, char* [])
186{
187  test_zero_args();
188  test_one_arg();
189  test_signal_signal_connect();
190  test_ref();
191  return 0;
192}
Note: See TracBrowser for help on using the repository browser.