summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/optional/test/optional_test_common.hpp
blob: 943e6460370c482a596afbc22aca5c1a2ac4d9da (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
// Copyright (C) 2014, Andrzej Krzemienski.
//
// 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)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
//  fernando_cacciola@hotmail.com
//
#include <boost/core/ignore_unused.hpp>

#ifdef ENABLE_TRACE
#define TRACE(msg) std::cout << msg << std::endl ;
#else
#define TRACE(msg)
#endif

namespace boost {

void assertion_failed (char const * expr, char const * func, char const * file, long )
{
  using std::string ;
  string msg =  string("Boost assertion failure for \"")
               + string(expr)
               + string("\" at file \"")
               + string(file)
               + string("\" function \"")
               + string(func)
               + string("\"") ;

  TRACE(msg);

  throw std::logic_error(msg);
}

}

using boost::optional ;


#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
using boost::swap ;
using boost::get ;
using boost::get_pointer ;
#endif

// MSVC6.0 does not support comparisons of optional against a literal null pointer value (0)
// via the safe_bool operator.
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1300) ) // 1300 == VC++ 7.1
#define BOOST_OPTIONAL_NO_NULL_COMPARE
#else
#define BOOST_OPTIONAL_NO_NULL_COMPARE  // Andrzej: I also disable 0 comparison everywhere
#endif

#define ARG(T) (static_cast< T const* >(0))

//
// Helper class used to verify the lifetime managment of the values held by optional
//
class X
{
  public :

    X ( int av ) : v(av)
    {
      ++ count ;

      TRACE ( "X::X(" << av << "). this=" << this ) ;
    }

    X ( X const& rhs ) : v(rhs.v)
    {
       pending_copy = false ;

       TRACE ( "X::X( X const& rhs). this=" << this << " rhs.v=" << rhs.v ) ;

       if ( throw_on_copy )
       {
         TRACE ( "throwing exception in X's copy ctor" ) ;
         throw 0 ;
       }

       ++ count ;
    }

    ~X()
    {
      pending_dtor = false ;

      -- count ;

      TRACE ( "X::~X(). v=" << v << "  this=" << this );
    }

    X& operator= ( X const& rhs )
      {
        pending_assign = false ;

        if ( throw_on_assign )
        {
          TRACE ( "throwing exception in X's assignment" ) ;

          v = -1 ;

          throw 0 ;
        }
        else
        {
          v = rhs.v ;

          TRACE ( "X::operator =( X const& rhs). this=" << this << " rhs.v=" << rhs.v ) ;
        }
        return *this ;
      }

    friend bool operator == ( X const& a, X const& b )
      { return a.v == b.v ; }

    friend bool operator != ( X const& a, X const& b )
      { return a.v != b.v ; }

    friend bool operator < ( X const& a, X const& b )
      { return a.v < b.v ; }

    int  V() const { return v ; }
    int& V()       { return v ; }

    static int  count ;
    static bool pending_copy   ;
    static bool pending_dtor   ;
    static bool pending_assign ;
    static bool throw_on_copy ;
    static bool throw_on_assign ;

  private :

    int  v ;

  private :

    X() ;
} ;


int  X::count           = 0 ;
bool X::pending_copy    = false ;
bool X::pending_dtor    = false ;
bool X::pending_assign  = false ;
bool X::throw_on_copy   = false ;
bool X::throw_on_assign = false ;

inline void set_pending_copy           ( X const* ) { X::pending_copy  = true  ; }
inline void set_pending_dtor           ( X const* ) { X::pending_dtor  = true  ; }
inline void set_pending_assign         ( X const* ) { X::pending_assign = true  ; }
inline void set_throw_on_copy          ( X const* ) { X::throw_on_copy = true  ; }
inline void set_throw_on_assign        ( X const* ) { X::throw_on_assign = true  ; }
inline void reset_throw_on_copy        ( X const* ) { X::throw_on_copy = false ; }
inline void reset_throw_on_assign      ( X const* ) { X::throw_on_assign = false ; }
inline void check_is_pending_copy      ( X const* ) { BOOST_TEST( X::pending_copy ) ; }
inline void check_is_pending_dtor      ( X const* ) { BOOST_TEST( X::pending_dtor ) ; }
inline void check_is_pending_assign    ( X const* ) { BOOST_TEST( X::pending_assign ) ; }
inline void check_is_not_pending_copy  ( X const* ) { BOOST_TEST( !X::pending_copy ) ; }
inline void check_is_not_pending_dtor  ( X const* ) { BOOST_TEST( !X::pending_dtor ) ; }
inline void check_is_not_pending_assign( X const* ) { BOOST_TEST( !X::pending_assign ) ; }
inline void check_instance_count       ( int c, X const* ) { BOOST_TEST( X::count == c ) ; }
inline int  get_instance_count         ( X const* ) { return X::count ; }

inline void set_pending_copy           (...) {}
inline void set_pending_dtor           (...) {}
inline void set_pending_assign         (...) {}
inline void set_throw_on_copy          (...) {}
inline void set_throw_on_assign        (...) {}
inline void reset_throw_on_copy        (...) {}
inline void reset_throw_on_assign      (...) {}
inline void check_is_pending_copy      (...) {}
inline void check_is_pending_dtor      (...) {}
inline void check_is_pending_assign    (...) {}
inline void check_is_not_pending_copy  (...) {}
inline void check_is_not_pending_dtor  (...) {}
inline void check_is_not_pending_assign(...) {}
inline void check_instance_count       (...) {}
inline int  get_instance_count         (...) { return 0 ; }


template<class T>
inline void check_uninitialized_const ( optional<T> const& opt )
{
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
  BOOST_TEST( opt == 0 ) ;
#endif
  BOOST_TEST( !opt ) ;
  BOOST_TEST( !get_pointer(opt) ) ;
  BOOST_TEST( !opt.get_ptr() ) ;
  BOOST_TEST( !opt.has_value() ) ;
  BOOST_TEST( !opt.is_initialized() ) ;
  BOOST_TEST( opt == boost::none ) ;
}
template<class T>
inline void check_uninitialized ( optional<T>& opt )
{
#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
  BOOST_TEST( opt == 0 ) ;
#endif
  BOOST_TEST( !opt ) ;
  BOOST_TEST( !get_pointer(opt) ) ;
  BOOST_TEST( !opt.get_ptr() ) ;
  BOOST_TEST( !opt.has_value() ) ;
  BOOST_TEST( !opt.is_initialized() ) ;
  BOOST_TEST( opt == boost::none ) ;

  check_uninitialized_const(opt);
}

template<class T>
inline void check_initialized_const ( optional<T> const& opt )
{
  BOOST_TEST( opt ) ;

#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
  BOOST_TEST( opt != 0 ) ;
#endif

  BOOST_TEST ( !!opt ) ;
  BOOST_TEST ( get_pointer(opt) ) ;
  BOOST_TEST ( opt.get_ptr() ) ;
  BOOST_TEST ( opt.has_value() ) ;
  BOOST_TEST ( opt.is_initialized() ) ;
  BOOST_TEST ( opt != boost::none ) ;
}

template<class T>
inline void check_initialized ( optional<T>& opt )
{
  BOOST_TEST( opt ) ;

#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE
  BOOST_TEST( opt != 0 ) ;
#endif

  BOOST_TEST ( !!opt ) ;
  BOOST_TEST ( get_pointer(opt) ) ;
  BOOST_TEST ( opt.get_ptr() ) ;
  BOOST_TEST ( opt.has_value() ) ;
  BOOST_TEST ( opt.is_initialized() ) ;
  BOOST_TEST ( opt != boost::none ) ;

  check_initialized_const(opt);
}

template<class T>
inline void check_value_const ( optional<T> const& opt, T const& v, T const& z )
{
  BOOST_TEST( *opt == v ) ;
  BOOST_TEST( *opt != z ) ;
  BOOST_TEST( opt.get() == v ) ;
  BOOST_TEST( opt.get() != z ) ;
  BOOST_TEST( (*(opt.operator->()) == v) ) ;
  BOOST_TEST( *get_pointer(opt) == v ) ;
}

template<class T>
inline void check_value ( optional<T>& opt, T const& v, T const& z )
{
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1200) // 1200 == VC++ 6.0
  // For some reason, VC6.0 is creating a temporary while evaluating (*opt == v),
  // so we need to turn throw on copy off first.
  reset_throw_on_copy( ARG(T) ) ;
#endif

  BOOST_TEST( *opt == v ) ;
  BOOST_TEST( *opt != z ) ;
  BOOST_TEST( opt.get() == v ) ;
  BOOST_TEST( opt.get() != z ) ;
  BOOST_TEST( (*(opt.operator->()) == v) ) ;
  BOOST_TEST( *get_pointer(opt) == v ) ;

  check_value_const(opt,v,z);
}