• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  (C) Copyright Gennadiy Rozental 2011-2015.
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$
11 //
12 //  Description : datasets test helpers
13 // ***************************************************************************
14 
15 #ifndef BOOST_TEST_TEST_DATASETS_HPP
16 #define BOOST_TEST_TEST_DATASETS_HPP
17 
18 // Boost
19 #include <boost/type_traits/is_same.hpp>
20 #include <boost/type_traits/is_convertible.hpp>
21 
22 #include <iostream>
23 #include <vector>
24 #include <list>
25 
26 //____________________________________________________________________________//
27 
28 class copy_count {
29 public:
copy_count()30     copy_count() {}
copy_count(copy_count const &)31     copy_count( copy_count const& ) {
32       value()++;
33     }
copy_count(copy_count &&)34     copy_count( copy_count&& ) BOOST_NOEXCEPT_OR_NOTHROW {} // noexcept is important indeed
copy_count(copy_count const &&)35     copy_count( copy_count const&& ) {}
36 //    ~copy_count() { std::cout << "~copy_count" << std::endl; }
37 
value()38     static int& value()         { static int s_value; return s_value; };
39 
make()40     static copy_count make()    { return copy_count(); }
make_const()41     static copy_count const make_const() { return copy_count(); }
42 };
43 
44 //____________________________________________________________________________//
45 
46 template<typename T>
47 struct check_arg_type {
48     template<typename S>
operator ()check_arg_type49     void    operator()( S const& ) const
50     {
51         BOOST_CHECK_MESSAGE( (boost::is_same<S,T>::value), "Sample type does not match expected" );
52     }
53 };
54 
55 //____________________________________________________________________________//
56 
57 template<typename T1, typename T2>
58 struct check_arg_type<std::tuple<T1,T2>> {
59     template<typename S1, typename S2>
operator ()check_arg_type60     void    operator()( S1 const&, S2 const& ) const
61     {
62         BOOST_CHECK_MESSAGE( (boost::is_same<S1,T1>::value), "S1 type does not match expected" );
63         BOOST_CHECK_MESSAGE( (boost::is_same<S2,T2>::value), "S2 type does not match expected" );
64     }
65 };
66 
67 //____________________________________________________________________________//
68 
69 template<typename T1, typename T2, typename T3>
70 struct check_arg_type<std::tuple<T1,T2,T3>> {
71     template<typename S1, typename S2, typename S3>
operator ()check_arg_type72     void    operator()( S1 const&, S2 const&, S3 const& ) const
73     {
74         BOOST_CHECK_MESSAGE( (boost::is_same<S1,T1>::value), "S1 type does not match expected" );
75         BOOST_CHECK_MESSAGE( (boost::is_same<S2,T2>::value), "S2 type does not match expected" );
76         BOOST_CHECK_MESSAGE( (boost::is_same<S3,T3>::value), "S3 type does not match expected" );
77     }
78 };
79 
80 //____________________________________________________________________________//
81 
82 template<typename T>
83 struct check_arg_type_like {
84     template<typename S>
operator ()check_arg_type_like85     void    operator()( S const& ) const
86     {
87         BOOST_CHECK_MESSAGE( (boost::is_convertible<S,T>::value), "Sample type does not match expected" );
88     }
89 };
90 
91 //____________________________________________________________________________//
92 
93 template<typename T1, typename T2>
94 struct check_arg_type_like<std::tuple<T1,T2>> {
95     template<typename S1, typename S2>
operator ()check_arg_type_like96     void    operator()( S1 const&, S2 const& ) const
97     {
98         BOOST_CHECK_MESSAGE( (boost::is_convertible<S1,T1>::value), "S1 type does not match expected" );
99         BOOST_CHECK_MESSAGE( (boost::is_convertible<S2,T2>::value), "S2 type does not match expected" );
100     }
101 };
102 
103 //____________________________________________________________________________//
104 
105 template<typename T1, typename T2, typename T3>
106 struct check_arg_type_like<std::tuple<T1,T2,T3>> {
107     template<typename S1, typename S2, typename S3>
operator ()check_arg_type_like108     void    operator()( S1 const&, S2 const&, S3 const& ) const
109     {
110         BOOST_CHECK_MESSAGE( (boost::is_convertible<S1,T1>::value), "S1 type does not match expected" );
111         BOOST_CHECK_MESSAGE( (boost::is_convertible<S2,T2>::value), "S2 type does not match expected" );
112         BOOST_CHECK_MESSAGE( (boost::is_convertible<S3,T3>::value), "S3 type does not match expected" );
113     }
114 };
115 
116 //____________________________________________________________________________//
117 
118 struct invocation_count {
invocation_countinvocation_count119     invocation_count() : m_value( 0 ) {}
120 
121     template<typename S>
operator ()invocation_count122     void    operator()( S const& ) const
123     {
124         m_value++;
125     }
126     template<typename S1,typename S2>
operator ()invocation_count127     void    operator()( S1 const&, S2 const& ) const
128     {
129         m_value++;
130     }
131     template<typename S1,typename S2,typename S3>
operator ()invocation_count132     void    operator()( S1 const&, S2 const&, S3 const& ) const
133     {
134         m_value++;
135     }
136 
137     mutable int    m_value;
138 
139 private:
140     invocation_count(invocation_count const&);
141 };
142 
143 //____________________________________________________________________________//
144 
145 struct expected_call_count {
expected_call_countexpected_call_count146     explicit expected_call_count( int exp_count )
147     : m_call_count( 0 )
148     , m_exp_count( exp_count )
149     {}
150     expected_call_count(expected_call_count const&) = delete;
151     void operator=(expected_call_count const&) = delete;
152 
~expected_call_countexpected_call_count153     ~expected_call_count()
154     {
155         BOOST_TEST( m_exp_count == m_call_count );
156     }
157 
158     template<typename S>
operator ()expected_call_count159     void    operator()( S const& ) const
160     {
161         m_call_count++;
162     }
163     template<typename S1,typename S2>
operator ()expected_call_count164     void    operator()( S1 const&, S2 const& ) const
165     {
166         m_call_count++;
167     }
168     template<typename S1,typename S2,typename S3>
operator ()expected_call_count169     void    operator()( S1 const&, S2 const&, S3 const& ) const
170     {
171         m_call_count++;
172     }
173 
174     mutable int    m_call_count;
175     int            m_exp_count;
176 
177 };
178 
179 //____________________________________________________________________________//
180 
181 struct print_sample {
182     template<typename T>
operator ()print_sample183     void    operator()( T const& sample ) const
184     {
185         std::cout << "S has type: " << typeid(T).name() << " and value " << sample << std::endl;
186     }
187 };
188 
189 //____________________________________________________________________________//
190 
191 inline std::vector<copy_count>
make_copy_count_collection()192 make_copy_count_collection()
193 {
194     return std::vector<copy_count>( 3 );
195 }
196 
197 //____________________________________________________________________________//
198 
199 inline std::list<copy_count> const
make_copy_count_const_collection()200 make_copy_count_const_collection()
201 {
202     return std::list<copy_count>( 3 );
203 }
204 
205 //____________________________________________________________________________//
206 
207 #endif // BOOST_TEST_TEST_DATASETS_HPP
208