Better testing, more clarification

[SVN r1702]
This commit is contained in:
Dave Abrahams
2003-10-23 23:13:00 +00:00
parent a99e88d6d5
commit b1b6182a4d

View File

@@ -2,12 +2,13 @@
// subject to the Boost Software License, Version 1.0. (See accompanying // subject to the Boost Software License, Version 1.0. (See accompanying
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0) // file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
#include <iostream>
#include <boost/named_params.hpp> #include <boost/named_params.hpp>
#include <boost/type_traits/is_convertible.hpp> #include <boost/type_traits/is_convertible.hpp>
#include <cassert> #include <cassert>
#include <cstring>
namespace test { namespace test
{
using boost::keyword; using boost::keyword;
using boost::keywords; using boost::keywords;
@@ -29,9 +30,15 @@ struct index_t : keyword<index_t>
using keyword<index_t>::operator=; using keyword<index_t>::operator=;
} index; } index;
struct tester_t : keyword<tester_t>
{
using keyword<tester_t>::operator=;
} tester;
struct f_keywords // vc6 is happier with inheritance than with a typedef struct f_keywords // vc6 is happier with inheritance than with a typedef
: keywords< : keywords<
name_t tester_t
, name_t
, value_t , value_t
, index_t , index_t
> >
@@ -40,35 +47,75 @@ struct f_keywords // vc6 is happier with inheritance than with a typedef
template<class Params> template<class Params>
int f_impl(const Params& p) int f_impl(const Params& p)
{ {
std::cout << "-------- f --------" << std::endl; p[tester](
// name has no default p[name]
std::cout << "name = " << p[name] << std::endl; , p[value | 666.222]
std::cout << "value = " << p[value | 666.222] << std::endl; , p[index | 999]
std::cout << "index = " << p[index | 999] << std::endl; );
return 1; return 1;
} }
template<class Tester, class Name, class Value, class Index>
int f(Tester const& t, const Name& name_, const Value& value_, const Index& index_,
typename f_keywords::restrict<Tester, Name, Value, Index>::type x = f_keywords())
{
return f_impl(x(t, name_, value_, index_));
}
template<class Tester, class Name, class Value>
int f(Tester const& t, const Name& name_, const Value& value_,
typename f_keywords::restrict<Tester, Name, Value>::type x = f_keywords())
{
return f_impl(x(t, name_, value_));
}
template<class Tester, class Name>
int f(Tester const& t, const Name& name_,
typename f_keywords::restrict<Tester, Name>::type x = f_keywords())
{
return f_impl(x(t, name_));
}
template <class T>
bool equal(T const& x, T const& y)
{
return x == y;
}
bool equal(char const* s1, char const* s2)
{
return !std::strcmp(s1,s2);
}
template <class Name, class Value, class Index> template <class Name, class Value, class Index>
int f(const Name& name_, const Value& value_, const Index& index_, struct values_t
typename f_keywords::restrict<Name, Value, Index>::type x = f_keywords())
{ {
return f_impl(x(name_, value_, index_)); values_t(Name const& n, Value const& v, Index const& i)
: n(n), v(v), i(i)
{}
template <class Name_, class Value_, class Index_>
void operator()(Name_ const& n_, Value_ const& v_, Index_ const& i_) const
{
BOOST_STATIC_ASSERT((boost::is_same<Name,Name_>::value));
assert(equal(n, n_));
BOOST_STATIC_ASSERT((boost::is_same<Value,Value_>::value));
assert(equal(v, v_));
BOOST_STATIC_ASSERT((boost::is_same<Index,Index_>::value));
assert(equal(i, i_));
} }
template<class Name, class Value> Name const& n;
int f(const Name& name_, const Value& value_, Value const& v;
typename f_keywords::restrict<Name, Value>::type x = f_keywords()) Index const& i;
{ };
return f_impl(x(name_, value_));
}
template<class Name> template <class Name, class Value, class Index>
int f(const Name& name_, values_t<Name,Value,Index>
typename f_keywords::restrict<Name>::type x = f_keywords()) values(Name const& n, Value const& v, Index const& i)
{ {
return f_impl(x(name_)); return values_t<Name,Value,Index>(n,v,i);
} }
} }
struct Foo { operator const char*() const { return "foo"; } }; struct Foo { operator const char*() const { return "foo"; } };
@@ -78,13 +125,22 @@ int main()
using test::name; using test::name;
using test::value; using test::value;
using test::index; using test::index;
using test::tester;
f("foo", "bar", "baz"); f(
test::values("foo", "bar", "baz")
, "foo", "bar", "baz"
);
f(
test::values("foo", 666.222, 56)
#if BOOST_MSVC == 1200 // sadly templated operator= just doesn't work. #if BOOST_MSVC == 1200 // sadly templated operator= just doesn't work.
f(index(56), name("foo")); , index(56), name("foo")
#else #else
f(index = 56, name = Foo()); , index = 56, name = "foo"
#endif #endif
);
//f(index = 56, name = 55); // won't compile //f(index = 56, name = 55); // won't compile
return 0; return 0;
} }