ref_ct_test.cpp

[SVN r12524]
This commit is contained in:
Aleksey Gurtovoy 2002-01-27 13:32:37 +00:00
parent 413265f497
commit 99e7406bd9
2 changed files with 9 additions and 187 deletions

View File

@ -1,128 +0,0 @@
//-----------------------------------------------------------------------------
// ref_ct_test.cpp - compile-time test for "boost/ref.hpp" header content
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
//
// Copyright (c) 2002
// Aleksey Gurtovoy
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appears in all copies and
// that both the copyright notice and this permission notice appear in
// supporting documentation. No representations are made about the
// suitability of this software for any purpose. It is provided "as is"
// without express or implied warranty.
#include <boost/ref.hpp>
#include <boost/type_traits/same_traits.hpp>
#include <boost/static_assert.hpp>
namespace {
template< typename T, typename U >
void ref_test(boost::reference_wrapper<U>)
{
typedef typename boost::reference_wrapper<U>::type type;
BOOST_STATIC_ASSERT((boost::is_same<U,type>::value));
BOOST_STATIC_ASSERT((boost::is_same<T,type>::value));
}
template< typename T >
void assignable_test(T x)
{
x = x;
}
template< bool R, typename T >
void is_reference_wrapper_test(T)
{
BOOST_STATIC_ASSERT(boost::is_reference_wrapper<T>::value == R);
}
template< typename R, typename Ref >
void cxx_reference_test(Ref)
{
BOOST_STATIC_ASSERT((boost::is_same<R,Ref>::value));
}
template< typename R, typename Ref >
void unwrap_reference_test(Ref)
{
typedef typename boost::unwrap_reference<Ref>::type type;
BOOST_STATIC_ASSERT((boost::is_same<R,type>::value));
}
} // namespace
int main()
{
int i = 0;
int& ri = i;
int const ci = 0;
int const& rci = ci;
// 'ref/cref' functions test
ref_test<int>(boost::ref(i));
ref_test<int>(boost::ref(ri));
ref_test<int const>(boost::ref(ci));
ref_test<int const>(boost::ref(rci));
ref_test<int const>(boost::cref(i));
ref_test<int const>(boost::cref(ri));
ref_test<int const>(boost::cref(ci));
ref_test<int const>(boost::cref(rci));
// test 'assignable' requirement
assignable_test(boost::ref(i));
assignable_test(boost::ref(ri));
assignable_test(boost::cref(i));
assignable_test(boost::cref(ci));
assignable_test(boost::cref(rci));
// 'is_reference_wrapper' test
is_reference_wrapper_test<true>(boost::ref(i));
is_reference_wrapper_test<true>(boost::ref(ri));
is_reference_wrapper_test<true>(boost::cref(i));
is_reference_wrapper_test<true>(boost::cref(ci));
is_reference_wrapper_test<true>(boost::cref(rci));
is_reference_wrapper_test<false>(i);
is_reference_wrapper_test<false, int&>(ri);
is_reference_wrapper_test<false>(ci);
is_reference_wrapper_test<false, int const&>(rci);
// ordinary references/function template arguments deduction test
cxx_reference_test<int>(i);
cxx_reference_test<int>(ri);
cxx_reference_test<int>(ci);
cxx_reference_test<int>(rci);
cxx_reference_test<int&, int&>(i);
cxx_reference_test<int&, int&>(ri);
cxx_reference_test<int const&, int const&>(i);
cxx_reference_test<int const&, int const&>(ri);
cxx_reference_test<int const&, int const&>(ci);
cxx_reference_test<int const&, int const&>(rci);
// 'unwrap_reference' test
unwrap_reference_test<int>(boost::ref(i));
unwrap_reference_test<int>(boost::ref(ri));
unwrap_reference_test<int const>(boost::cref(i));
unwrap_reference_test<int const>(boost::cref(ci));
unwrap_reference_test<int const>(boost::cref(rci));
unwrap_reference_test<int>(i);
unwrap_reference_test<int>(ri);
unwrap_reference_test<int>(ci);
unwrap_reference_test<int>(rci);
unwrap_reference_test<int&, int&>(i);
unwrap_reference_test<int&, int&>(ri);
unwrap_reference_test<int const&, int const&>(i);
unwrap_reference_test<int const&, int const&>(ri);
unwrap_reference_test<int const&, int const&>(ci);
unwrap_reference_test<int const&, int const&>(rci);
return 0;
}

View File

@ -1,3 +1,7 @@
// run-time test for "boost/ref.hpp" header content
// see 'ref_ct_test.cpp' for compile-time part
#if defined(_MSC_VER) && !defined(__ICL)
# pragma warning(disable: 4786) // identifier truncated in debug info
# pragma warning(disable: 4710) // function not inlined
@ -6,8 +10,6 @@
#endif
#include <boost/ref.hpp>
#include <boost/type_traits.hpp>
#include <boost/pending/ct_if.hpp>
#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
# pragma warning(push, 3)
@ -27,7 +29,7 @@ namespace {
using namespace boost;
template <class T>
struct wrapper
struct ref_wrapper
{
// Used to verify implicit conversion
static T* get_pointer(T& x)
@ -40,73 +42,25 @@ struct wrapper
return &x;
}
static void check_type_typedef(...)
{
BOOST_ERROR("expected a reference_wrapper argument");
}
template <class U>
static void check_type_typedef(boost::reference_wrapper<U>)
{
typedef typename boost::reference_wrapper<U>::type type_typedef;
BOOST_TEST((boost::is_same<type_typedef,U>::value));
}
template <class Arg>
static T* passthru(Arg x)
{
check_type_typedef(x);
return get_pointer(x);
}
template <class Arg>
static T const* cref_passthru(Arg x)
{
check_type_typedef(x);
return get_const_pointer(x);
}
template <class Arg>
static void test_unwrapped(Arg x)
{
typedef typename unwrap_reference<Arg>::type unwrapped;
BOOST_TEST((is_same<Arg,unwrapped>::value));
}
template <bool is_constant> struct select {};
typedef select<true> constant;
typedef select<false> non_constant;
static void cref_test(T x, constant) {}
static void cref_test(T x, non_constant)
{
BOOST_TEST(is_reference_wrapper<reference_wrapper<T const> >::value);
BOOST_TEST(cref_passthru(cref(x)) == &x);
BOOST_TEST(&cref(x).get() == &x);
}
BOOST_STATIC_CONSTANT(
bool, t_is_constant = boost::is_const<T>::value);
static void test(T x)
{
BOOST_TEST(passthru(ref(x)) == &x);
BOOST_TEST((is_same<reference_wrapper<T>::type,T>::value));
BOOST_TEST(&ref(x).get() == &x);
typedef reference_wrapper<T> wrapped;
BOOST_TEST(is_reference_wrapper<wrapped>::value);
typedef typename unwrap_reference<wrapped>::type unwrapped_wrapper;
BOOST_TEST((is_same<T,unwrapped_wrapper>::value));
typedef typename unwrap_reference<T>::type unwrapped_self;
BOOST_TEST((is_same<T,unwrapped_self>::value));
cref_test(x, select<t_is_constant>());
BOOST_TEST(cref_passthru(cref(x)) == &x);
BOOST_TEST(&cref(x).get() == &x);
}
};
@ -114,11 +68,7 @@ struct wrapper
int test_main(int, char * [])
{
wrapper<int>::test(1);
wrapper<int const>::test(1);
BOOST_TEST(!is_reference_wrapper<int>::value);
BOOST_TEST(!is_reference_wrapper<int&>::value);
BOOST_TEST(!is_reference_wrapper<int const>::value);
BOOST_TEST(!is_reference_wrapper<int const&>::value);
ref_wrapper<int>::test(1);
ref_wrapper<int const>::test(1);
return 0;
}