blob: 585d94e1112e3d4ff4346e92e1bc8650f5a54303 [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// (C) Copyright Gennadiy Rozental 2005-2008.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5
6// See http://www.boost.org/libs/test for the library home page.
7//
8// File : $RCSfile$
9//
10// Version : $Revision: 54633 $
11//
12// Description : Facilities to perform exception safety_tests
13// ***************************************************************************
14
Jeff Thompson3d613fd2013-10-15 15:39:04 -070015#ifndef NDNBOOST_TEST_MOCK_OBJECT_HPP_112205GER
16#define NDNBOOST_TEST_MOCK_OBJECT_HPP_112205GER
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070017
18// Boost.Test
19#include <ndnboost/test/detail/config.hpp>
20#include <ndnboost/test/interaction_based.hpp>
21
22// Boost
23#include <ndnboost/preprocessor/punctuation/comma.hpp>
24
25#include <ndnboost/test/detail/suppress_warnings.hpp>
26
27//____________________________________________________________________________//
28
29namespace ndnboost {
30
31namespace itest {
32
33// ************************************************************************** //
34// ************** mock_object_base ************** //
35// ************************************************************************** //
36
37class mock_object_base {
38public:
39 mock_object_base() {}
40
41 template<typename T1>
42 mock_object_base( T1 const& ) {}
43
44 template<typename T1, typename T2>
45 mock_object_base( T1 const&, T2 const& ) {}
46
47 template<typename T1, typename T2, typename T3>
48 mock_object_base( T1 const&, T2 const&, T3 const& ) {}
49
50 template<typename T1, typename T2, typename T3, typename T4>
51 mock_object_base( T1 const&, T2 const&, T3 const&, T4 const& ) {}
52
53 template<typename T1, typename T2, typename T3, typename T4, typename T5>
54 mock_object_base( T1 const&, T2 const&, T3 const&, T4 const&, T5 const& ) {}
55};
56
57// ************************************************************************** //
58// ************** mock_object implementation helpers ************** //
59// ************************************************************************** //
60
61#define MO_OP_IMPL( op, descr, ret ) \
Jeff Thompson3d613fd2013-10-15 15:39:04 -070062 NDNBOOST_ITEST_SCOPE( mock_object::operator op ); \
63 NDNBOOST_ITEST_EPOINT( descr ); \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070064 return ret \
65/**/
66
67#define MO_UNARY_OP( op, descr ) \
68self_type const& operator op() const \
69{ \
70 MO_OP_IMPL( op, descr, prototype() ); \
71} \
72/**/
73
74#define MO_UNARY_BOOL_OP( op, descr ) \
75bool operator op() const \
76{ \
Jeff Thompson3d613fd2013-10-15 15:39:04 -070077 MO_OP_IMPL( op, descr, (!!NDNBOOST_ITEST_DPOINT()) ); \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070078} \
79/**/
80
81#define MO_BINARY_OP( op, descr ) \
82template<int i1, typename Base1,int i2, typename Base2> \
83inline mock_object<i1,Base1> const& \
84operator op( mock_object<i1,Base1> const& mo, \
85 mock_object<i2,Base2> const& ) \
86{ \
87 MO_OP_IMPL( op, descr, mo ); \
88} \
89 \
90template<int i, typename Base, typename T> \
91inline mock_object<i,Base> const& \
92operator op( mock_object<i,Base> const& mo, T const& ) \
93{ \
94 MO_OP_IMPL( op, descr, mo ); \
95} \
96 \
97template<int i, typename Base, typename T> \
98inline mock_object<i,Base> const& \
99operator op( T const&, mock_object<i,Base> const& mo ) \
100{ \
101 MO_OP_IMPL( op, descr, mo ); \
102} \
103/**/
104
105#define MO_BINARY_BOOL_OP( op, descr ) \
106template<int i1, typename Base1,int i2, typename Base2> \
107inline bool \
108operator op( mock_object<i1,Base1> const&, \
109 mock_object<i2,Base2> const& ) \
110{ \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700111 MO_OP_IMPL( op, descr, NDNBOOST_ITEST_DPOINT() ); \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700112} \
113 \
114template<int i, typename Base, typename T> \
115inline bool \
116operator op( mock_object<i,Base> const&, T const& ) \
117{ \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700118 MO_OP_IMPL( op, descr, NDNBOOST_ITEST_DPOINT() ); \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700119} \
120 \
121template<int i, typename Base, typename T> \
122inline bool \
123operator op( T const&, mock_object<i,Base> const& ) \
124{ \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700125 MO_OP_IMPL( op, descr, NDNBOOST_ITEST_DPOINT() ); \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700126} \
127/**/
128
129// ************************************************************************** //
130// ************** mock_object ************** //
131// ************************************************************************** //
132
133template<int i = 0, typename Base=mock_object_base>
134class mock_object;
135
136template<int i, typename Base>
137class mock_object : public Base {
138 // Private typeefs
139 typedef mock_object<i,Base> self_type;
140 struct dummy { void nonnull() {}; };
141 typedef void (dummy::*safe_bool)();
142
143 // prototype constructor
144 mock_object( dummy* ) {}
145
146public:
147 static mock_object& prototype()
148 {
149 static mock_object p( reinterpret_cast<dummy*>(0) );
150 return p;
151 }
152
153 // Constructors
154 mock_object()
155 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700156 NDNBOOST_ITEST_SCOPE( mock_object::mock_object );
157 NDNBOOST_ITEST_EPOINT( "Mock object default constructor" );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700158 }
159
160 template<typename T1>
161 mock_object( T1 const& arg1 )
162 : mock_object_base( arg1 )
163 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700164 NDNBOOST_ITEST_SCOPE( mock_object::mock_object );
165 NDNBOOST_ITEST_EPOINT( "Mock object constructor" );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700166 }
167
168 template<typename T1, typename T2>
169 mock_object( T1 const& arg1, T2 const& arg2 )
170 : mock_object_base( arg1, arg2 )
171 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700172 NDNBOOST_ITEST_SCOPE( mock_object::mock_object );
173 NDNBOOST_ITEST_EPOINT( "Mock object constructor" );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700174 }
175
176 template<typename T1, typename T2, typename T3>
177 mock_object( T1 const& arg1, T2 const& arg2, T3 const& arg3 )
178 : mock_object_base( arg1, arg2, arg3 )
179 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700180 NDNBOOST_ITEST_SCOPE( mock_object::mock_object );
181 NDNBOOST_ITEST_EPOINT( "Mock object constructor" );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700182 }
183
184 template<typename T1, typename T2, typename T3, typename T4>
185 mock_object( T1 const& arg1, T2 const& arg2, T3 const& arg3, T4 const& arg4 )
186 : mock_object_base( arg1, arg2, arg3, arg4 )
187 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700188 NDNBOOST_ITEST_SCOPE( mock_object::mock_object );
189 NDNBOOST_ITEST_EPOINT( "Mock object constructor" );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700190 }
191
192 template<typename T1, typename T2, typename T3, typename T4, typename T5>
193 mock_object( T1 const& arg1, T2 const& arg2, T3 const& arg3, T4 const& arg4, T5 const& arg5 )
194 : mock_object_base( arg1, arg2, arg3, arg4, arg5 )
195 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700196 NDNBOOST_ITEST_SCOPE( mock_object::mock_object );
197 NDNBOOST_ITEST_EPOINT( "Mock object constructor" );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700198 }
199
200 mock_object( mock_object const& )
201 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700202 NDNBOOST_ITEST_SCOPE( mock_object::mock_object );
203 NDNBOOST_ITEST_EPOINT( "Mock object copy constructor" );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700204 }
205
206 // assignment
207 self_type const& operator =( mock_object const& ) const
208 {
209 MO_OP_IMPL( =, "Copy assignment", prototype() );
210 }
211
212 template <typename T>
213 self_type const& operator =( T const& ) const
214 {
215 MO_OP_IMPL( =, "Copy assignment", prototype() );
216 }
217
218 // Unary operators
219 MO_UNARY_BOOL_OP( !, "Logical NOT operator" )
220 MO_UNARY_OP( &, "Address-of operator" )
221 MO_UNARY_OP( ~, "One's complement operator" )
222 MO_UNARY_OP( *, "Pointer dereference" )
223 MO_UNARY_OP( +, "Unary plus" )
224
225 // Increment and Decrement
226 MO_UNARY_OP( ++, "Prefix increment" )
227 MO_UNARY_OP( --, "Prefix decrement" )
228 self_type const& operator ++(int) const
229 {
230 MO_OP_IMPL( ++, "Postfix increment", prototype() );
231 }
232 self_type const& operator --(int) const
233 {
234 MO_OP_IMPL( --, "Postfix decrement", prototype() );
235 }
236
237 // Bool context convertion
238 operator safe_bool() const
239 {
240 MO_OP_IMPL( safe_bool, "Bool context conversion",
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700241 (NDNBOOST_ITEST_DPOINT() ? 0 : &dummy::nonnull) );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700242 }
243
244 // Function-call operators
245 self_type const& operator ()() const
246 {
247 MO_OP_IMPL( (), "0-arity function-call", prototype() );
248 }
249 template<typename T1>
250 self_type const& operator ()( T1 const& arg1 ) const
251 {
252 MO_OP_IMPL( (), "1-arity function-call", prototype() );
253 }
254 template<typename T1, typename T2>
255 self_type const& operator ()( T1 const&, T2 const& ) const
256 {
257 MO_OP_IMPL( (), "2-arity function-call", prototype() );
258 }
259 template<typename T1, typename T2, typename T3>
260 self_type const& operator ()( T1 const&, T2 const&, T3 const& ) const
261 {
262 MO_OP_IMPL( (), "3-arity function-call", prototype() );
263 }
264 template<typename T1, typename T2, typename T3, typename T4>
265 self_type const& operator ()( T1 const&, T2 const&, T3 const&, T4 const& ) const
266 {
267 MO_OP_IMPL( (), "4-arity function-call", prototype() );
268 }
269 template<typename T1, typename T2, typename T3, typename T4, typename T5>
270 self_type const& operator ()( T1 const&, T2 const&, T3 const&, T4 const&, T5 const& ) const
271 {
272 MO_OP_IMPL( (), "5-arity function-call", prototype() );
273 }
274
275 // Substripting
276 template<typename T>
277 self_type const& operator []( T const& ) const
278 {
279 MO_OP_IMPL( [], "Substripting", prototype() );
280 }
281
282 // Class member access
283 self_type const* operator->() const
284 {
285 MO_OP_IMPL( ->, "Class member access", this );
286 }
287};
288
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700289// !! MO_BINARY_OP( NDNBOOST_PP_COMMA(), "Comma operator" )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700290
291MO_BINARY_BOOL_OP( !=, "Inequality" )
292MO_BINARY_OP( %, "Modulus" )
293MO_BINARY_OP( %=, "Modulus/assignment" )
294MO_BINARY_OP( &, "Bitwise AND" )
295MO_BINARY_BOOL_OP( &&, "Logical AND" )
296MO_BINARY_OP( &=, "Bitwise AND/assignment" )
297MO_BINARY_OP( *, "Multiplication" )
298MO_BINARY_OP( *=, "Multiplication/assignment" )
299MO_BINARY_OP( +, "Addition" )
300MO_BINARY_OP( +=, "Addition/assignment" )
301//MO_BINARY_OP( -, "Subtraction" )
302MO_BINARY_OP( -=, "Subtraction/assignment" )
303MO_BINARY_OP( ->*, "Pointer-to-member selection" )
304MO_BINARY_OP( /, "Division" )
305MO_BINARY_OP( /=, "Division/assignment" )
306MO_BINARY_BOOL_OP( <, "Less than" )
307MO_BINARY_OP( <<=, "Left shift/assignment" )
308MO_BINARY_BOOL_OP( <=, "Less than or equal to" )
309MO_BINARY_BOOL_OP( ==, "Equality" )
310MO_BINARY_BOOL_OP( >, "Greater than" )
311MO_BINARY_BOOL_OP( >=, "Greater than or equal to" )
312MO_BINARY_OP( >>=, "Right shift/assignment" )
313MO_BINARY_OP( ^, "Exclusive OR" )
314MO_BINARY_OP( ^=, "Exclusive OR/assignment" )
315MO_BINARY_OP( |, "Bitwise inclusive OR" )
316MO_BINARY_OP( |=, "Bitwise inclusive OR/assignment" )
317MO_BINARY_BOOL_OP( ||, "Logical OR" )
318
319MO_BINARY_OP( <<, "Left shift" )
320MO_BINARY_OP( >>, "Right shift" )
321
322} // namespace itest
323
324} // namespace ndnboost
325
326#include <ndnboost/test/detail/enable_warnings.hpp>
327
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700328#endif // NDNBOOST_TEST_MOCK_OBJECT_HPP_112205GER