Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/signals/test/signal_test.cpp @ 13

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

added boost

File size: 3.7 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#include <iostream>
14
15template<typename T>
16struct max_or_default {
17  typedef T result_type;
18  template<typename InputIterator>
19  typename InputIterator::value_type
20  operator()(InputIterator first, InputIterator last) const
21  {
22    if (first == last)
23      return T();
24
25    T max = *first++;
26    for (; first != last; ++first)
27      max = (*first > max)? *first : max;
28
29    return max;
30  }
31};
32
33struct make_int {
34  make_int(int n, int cn) : N(n), CN(cn) {}
35
36  int operator()() { return N; }
37  int operator()() const { return CN; }
38
39  int N;
40  int CN;
41};
42
43template<int N>
44struct make_increasing_int {
45  make_increasing_int() : n(N) {}
46
47  int operator()() const { return n++; }
48
49  mutable int n;
50};
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::signal<int (), max_or_default<int> > s0;
63
64    std::cout << "sizeof(signal) = " << sizeof(s0) << std::endl;
65    boost::BOOST_SIGNALS_NAMESPACE::connection c2 = s0.connect(i2);
66    boost::BOOST_SIGNALS_NAMESPACE::connection c72 = s0.connect(72, i72);
67    boost::BOOST_SIGNALS_NAMESPACE::connection c62 = s0.connect(60, i62);
68    boost::BOOST_SIGNALS_NAMESPACE::connection c42 = s0.connect(i42);
69
70    BOOST_CHECK(s0() == 72);
71
72    s0.disconnect(72);
73    BOOST_CHECK(s0() == 62);
74
75    c72.disconnect(); // Double-disconnect should be safe
76    BOOST_CHECK(s0() == 62);
77
78    s0.disconnect(72); // Triple-disconect should be safe
79    BOOST_CHECK(s0() == 62);
80
81    // Also connect 63 in the same group as 62
82    s0.connect(60, i63);
83    BOOST_CHECK(s0() == 63);
84
85    // Disconnect all of the 60's
86    s0.disconnect(60);
87    BOOST_CHECK(s0() == 42);
88
89    c42.disconnect();
90    BOOST_CHECK(s0() == 2);
91
92    c2.disconnect();
93    BOOST_CHECK(s0() == 0);
94  }
95
96  {
97    boost::signal<int (), max_or_default<int> > s0;
98    boost::BOOST_SIGNALS_NAMESPACE::connection c2 = s0.connect(i2);
99    boost::BOOST_SIGNALS_NAMESPACE::connection c72 = s0.connect(i72);
100    boost::BOOST_SIGNALS_NAMESPACE::connection c62 = s0.connect(i62);
101    boost::BOOST_SIGNALS_NAMESPACE::connection c42 = s0.connect(i42);
102
103    const boost::signal<int (), max_or_default<int> >& cs0 = s0;
104    BOOST_CHECK(cs0() == 72);
105  }
106
107  {
108    make_increasing_int<7> i7;
109    make_increasing_int<10> i10;
110
111    boost::signal<int (), max_or_default<int> > s0;
112    boost::BOOST_SIGNALS_NAMESPACE::connection c7 = s0.connect(i7);
113    boost::BOOST_SIGNALS_NAMESPACE::connection c10 = s0.connect(i10);
114
115    BOOST_CHECK(s0() == 10);
116    BOOST_CHECK(s0() == 11);
117  }
118}
119
120static void
121test_one_arg()
122{
123  boost::signal<int (int value), max_or_default<int> > s1;
124
125  s1.connect(std::negate<int>());
126  s1.connect(std::bind1st(std::multiplies<int>(), 2));
127
128  BOOST_CHECK(s1(1) == 2);
129  BOOST_CHECK(s1(-1) == 1);
130}
131
132static void
133test_signal_signal_connect()
134{
135  boost::signal<int (int value), max_or_default<int> > s1;
136
137  s1.connect(std::negate<int>());
138
139  BOOST_CHECK(s1(3) == -3);
140
141  {
142    boost::signal<int (int value), max_or_default<int> > s2;
143    s1.connect(s2);
144    s2.connect(std::bind1st(std::multiplies<int>(), 2));
145    s2.connect(std::bind1st(std::multiplies<int>(), -3));
146
147    BOOST_CHECK(s2(-3) == 9);
148    BOOST_CHECK(s1(3) == 6);
149  } // s2 goes out of scope and disconnects
150
151  BOOST_CHECK(s1(3) == -3);
152}
153
154int
155test_main(int, char* [])
156{
157  test_zero_args();
158  test_one_arg();
159  test_signal_signal_connect();
160  return 0;
161}
Note: See TracBrowser for help on using the repository browser.