File: contains_test.cpp

package info (click to toggle)
boost 1.32.0-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 93,952 kB
  • ctags: 128,458
  • sloc: cpp: 492,477; xml: 52,125; python: 13,519; ansic: 13,013; sh: 1,773; yacc: 853; makefile: 526; perl: 418; lex: 110; csh: 6
file content (174 lines) | stat: -rw-r--r-- 4,384 bytes parent folder | download
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
169
170
171
172
173
174
// Boost.Function library

//  Copyright Douglas Gregor 2004. Use, modification and
//  distribution is subject to the Boost Software License, Version
//  1.0. (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

#include <boost/test/minimal.hpp>
#include <boost/function.hpp>
#include <boost/ref.hpp>

static int forty_two() { return 42; }

struct Seventeen
{
  int operator()() const { return 17; }
};

struct ReturnInt
{
  explicit ReturnInt(int value) : value(value) {}

  int operator()() const { return value; }

  int value;
};

bool operator==(const ReturnInt& x, const ReturnInt& y)
{ return x.value == y.value; }

bool operator!=(const ReturnInt& x, const ReturnInt& y)
{ return x.value != y.value; }

static void target_test()
{
  boost::function0<int> f;

  f = &forty_two;
  BOOST_TEST(*f.target<int (*)()>() == &forty_two);
  BOOST_TEST(!f.target<Seventeen>());

  f = Seventeen();
  BOOST_TEST(!f.target<int (*)()>());
  BOOST_TEST(f.target<Seventeen>());

  Seventeen this_seventeen;
  f = boost::ref(this_seventeen);
  BOOST_TEST(!f.target<int (*)()>());
  BOOST_TEST(f.target<Seventeen>());
  BOOST_TEST(f.target<Seventeen>() == &this_seventeen);
}

static void equal_test()
{
  boost::function0<int> f;

  f = &forty_two;
  BOOST_TEST(f == &forty_two);
  BOOST_TEST(f != ReturnInt(17));
#if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  BOOST_TEST(&forty_two == f);
  BOOST_TEST(ReturnInt(17) != f);
#endif

  BOOST_TEST(f.contains(&forty_two));

  f = ReturnInt(17);
  BOOST_TEST(f != &forty_two);
  BOOST_TEST(f == ReturnInt(17));
  BOOST_TEST(f != ReturnInt(16));
#if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  BOOST_TEST(&forty_two != f);
  BOOST_TEST(ReturnInt(17) == f);
  BOOST_TEST(ReturnInt(16) != f);
#endif

  BOOST_TEST(f.contains(ReturnInt(17)));

#if !defined(BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX)
  boost::function<int(void)> g;

  g = &forty_two;
  BOOST_TEST(g == &forty_two);
  BOOST_TEST(g != ReturnInt(17));
#  if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  BOOST_TEST(&forty_two == g);
  BOOST_TEST(ReturnInt(17) != g);
#  endif

  g = ReturnInt(17);
  BOOST_TEST(g != &forty_two);
  BOOST_TEST(g == ReturnInt(17));
  BOOST_TEST(g != ReturnInt(16));
#  if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  BOOST_TEST(&forty_two != g);
  BOOST_TEST(ReturnInt(17) == g);
  BOOST_TEST(ReturnInt(16) != g);
#  endif
#endif
}

static void ref_equal_test()
{
  {
    ReturnInt ri(17);
    boost::function0<int> f = boost::ref(ri);

    // References and values are equal
    BOOST_TEST(f == boost::ref(ri));
    BOOST_TEST(f == ri);
    BOOST_TEST(boost::ref(ri) == f);
    BOOST_TEST(!(f != boost::ref(ri)));
    BOOST_TEST(!(f != ri));
    BOOST_TEST(!(boost::ref(ri) != f));
#if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
    BOOST_TEST(ri == f);
    BOOST_TEST(!(ri != f));
#endif

    // Values equal, references inequal
    ReturnInt ri2(17);
    BOOST_TEST(f == ri2);
    BOOST_TEST(f != boost::ref(ri2));
    BOOST_TEST(boost::ref(ri2) != f);
    BOOST_TEST(!(f != ri2));
    BOOST_TEST(!(f == boost::ref(ri2)));
    BOOST_TEST(!(boost::ref(ri2) == f));
#if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
    BOOST_TEST(ri2 == f);
    BOOST_TEST(!(ri2 != f));
#endif 
  }

#if !defined(BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX)
  {
    ReturnInt ri(17);
    boost::function<int(void)> f = boost::ref(ri);

    // References and values are equal
    BOOST_TEST(f == boost::ref(ri));
    BOOST_TEST(f == ri);
    BOOST_TEST(boost::ref(ri) == f);
    BOOST_TEST(!(f != boost::ref(ri)));
    BOOST_TEST(!(f != ri));
    BOOST_TEST(!(boost::ref(ri) != f));
#  if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
    BOOST_TEST(ri == f);
    BOOST_TEST(!(ri != f));
#  endif

    // Values equal, references inequal
    ReturnInt ri2(17);
    BOOST_TEST(f == ri2);
    BOOST_TEST(f != boost::ref(ri2));
    BOOST_TEST(boost::ref(ri2) != f);
    BOOST_TEST(!(f != ri2));
    BOOST_TEST(!(f == boost::ref(ri2)));
    BOOST_TEST(!(boost::ref(ri2) == f));
#  if !(defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3)
    BOOST_TEST(ri2 == f);
    BOOST_TEST(!(ri2 != f));
#  endif
  }
#endif
}

int test_main(int, char*[])
{
  target_test();
  equal_test();
  ref_equal_test();

  return 0;
}