algo: Added tests for mutable functor

This commit is contained in:
Kohei Takahashi
2018-07-03 00:48:40 +09:00
parent 0e4c5127f5
commit c1fe4e78ab
6 changed files with 122 additions and 25 deletions

View File

@ -1,11 +1,12 @@
/*============================================================================= /*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2007 Dan Marsden Copyright (c) 2007 Dan Marsden
Copyright (c) 2018 Kohei Takahashi
Distributed under the Boost Software License, Version 1.0. (See accompanying Distributed under 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) file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/ ==============================================================================*/
#include <boost/detail/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
#include <boost/fusion/container/vector/vector.hpp> #include <boost/fusion/container/vector/vector.hpp>
#include <boost/fusion/adapted/mpl.hpp> #include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/algorithm/query/all.hpp> #include <boost/fusion/algorithm/query/all.hpp>
@ -28,6 +29,19 @@ namespace
int search; int search;
}; };
struct mutable_search_for : search_for
{
explicit mutable_search_for(int in_search)
: search_for(in_search)
{}
template <typename T>
bool operator()(T const& v)
{
return search_for::operator()(v);
}
};
} }
int int
@ -58,11 +72,20 @@ main()
} }
{ {
typedef boost::mpl::vector_c<int, 1> mpl_vec; boost::fusion::vector<int, short, double> t(1, 2, 3.3);
BOOST_TEST(!boost::fusion::all(t, search_for(1)));
BOOST_TEST(!boost::fusion::all(t, mutable_search_for(1)));
}
{
typedef boost::mpl::vector_c<int, 1, 1, 1> mpl_vec;
// We cannot use lambda here as mpl vec iterators return // We cannot use lambda here as mpl vec iterators return
// rvalues, and lambda needs lvalues. // rvalues, and lambda needs lvalues.
BOOST_TEST(boost::fusion::all(mpl_vec(), search_for(1))); BOOST_TEST(boost::fusion::all(mpl_vec(), search_for(1)));
BOOST_TEST(!boost::fusion::all(mpl_vec(), search_for(2))); BOOST_TEST(!boost::fusion::all(mpl_vec(), search_for(2)));
BOOST_TEST(boost::fusion::all(mpl_vec(), mutable_search_for(1)));
BOOST_TEST(!boost::fusion::all(mpl_vec(), mutable_search_for(2)));
} }
return boost::report_errors(); return boost::report_errors();

View File

@ -2,11 +2,12 @@
Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2005 Eric Niebler Copyright (c) 2005 Eric Niebler
Copyright (c) Dan Marsden Copyright (c) Dan Marsden
Copyright (c) 2018 Kohei Takahashi
Distributed under the Boost Software License, Version 1.0. (See accompanying Distributed under 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) file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/ ==============================================================================*/
#include <boost/detail/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
#include <boost/fusion/container/vector/vector.hpp> #include <boost/fusion/container/vector/vector.hpp>
#include <boost/fusion/adapted/mpl.hpp> #include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/algorithm/query/any.hpp> #include <boost/fusion/algorithm/query/any.hpp>
@ -29,6 +30,19 @@ namespace
int search; int search;
}; };
struct mutable_search_for : search_for
{
explicit mutable_search_for(int in_search)
: search_for(in_search)
{}
template <typename T>
bool operator()(T const& v)
{
return search_for::operator()(v);
}
};
} }
int int
@ -44,12 +58,21 @@ main()
BOOST_TEST(!boost::fusion::any(t, boost::lambda::_1 == 3)); BOOST_TEST(!boost::fusion::any(t, boost::lambda::_1 == 3));
} }
{
boost::fusion::vector<int, short, double> t(1, 2, 3.3);
BOOST_TEST(boost::fusion::any(t, search_for(1)));
BOOST_TEST(boost::fusion::any(t, mutable_search_for(1)));
}
{ {
typedef boost::mpl::vector_c<int, 1, 2, 3> mpl_vec; typedef boost::mpl::vector_c<int, 1, 2, 3> mpl_vec;
// We cannot use lambda here as mpl vec iterators return // We cannot use lambda here as mpl vec iterators return
// rvalues, and lambda needs lvalues. // rvalues, and lambda needs lvalues.
BOOST_TEST(boost::fusion::any(mpl_vec(), search_for(2))); BOOST_TEST(boost::fusion::any(mpl_vec(), search_for(2)));
BOOST_TEST(!boost::fusion::any(mpl_vec(), search_for(4))); BOOST_TEST(!boost::fusion::any(mpl_vec(), search_for(4)));
BOOST_TEST(boost::fusion::any(mpl_vec(), mutable_search_for(2)));
BOOST_TEST(!boost::fusion::any(mpl_vec(), mutable_search_for(4)));
} }
return boost::report_errors(); return boost::report_errors();

View File

@ -1,11 +1,12 @@
/*============================================================================= /*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2005 Eric Niebler Copyright (c) 2005 Eric Niebler
Copyright (c) 2018 Kohei Takahashi
Distributed under the Boost Software License, Version 1.0. (See accompanying Distributed under 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) file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/ ==============================================================================*/
#include <boost/detail/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
#include <boost/fusion/container/vector/vector.hpp> #include <boost/fusion/container/vector/vector.hpp>
#include <boost/fusion/adapted/mpl.hpp> #include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/algorithm/query/count_if.hpp> #include <boost/fusion/algorithm/query/count_if.hpp>
@ -18,20 +19,29 @@ struct bind1st<F<T> > : public F<T>
{ {
T n; T n;
bind1st(T n) : n(n) { } bind1st(T n) : n(n) { }
bool operator()(T v) { return F<T>::operator()(n, v); }
bool operator()(T v) const { return F<T>::operator()(n, v); } bool operator()(T v) const { return F<T>::operator()(n, v); }
}; };
template <typename T>
struct mutable_equal_to : std::equal_to<T>
{
bool operator()(T l, T r) { return std::equal_to<T>::operator()(l, r); }
};
int int
main() main()
{ {
{ {
boost::fusion::vector<int, short, double> t(1, 2, 3.3); boost::fusion::vector<int, short, double> t(1, 2, 3.3);
BOOST_TEST(boost::fusion::count_if(t, bind1st<std::equal_to<double> >(2)) == 1); BOOST_TEST(boost::fusion::count_if(t, bind1st<std::equal_to<double> >(2)) == 1);
BOOST_TEST(boost::fusion::count_if(t, bind1st<mutable_equal_to<double> >(2)) == 1);
} }
{ {
boost::fusion::vector<int, short, double> t(1, 2, 3.3); boost::fusion::vector<int, short, double> t(1, 2, 3.3);
BOOST_TEST(boost::fusion::count_if(t, bind1st<std::equal_to<double> >(3)) == 0); BOOST_TEST(boost::fusion::count_if(t, bind1st<std::equal_to<double> >(3)) == 0);
BOOST_TEST(boost::fusion::count_if(t, bind1st<mutable_equal_to<double> >(3)) == 0);
} }
{ {
@ -39,6 +49,7 @@ main()
// Cannot use lambda here as mpl iterators return rvalues and lambda needs lvalues // Cannot use lambda here as mpl iterators return rvalues and lambda needs lvalues
BOOST_TEST(boost::fusion::count_if(mpl_vec(), bind1st<std::greater_equal<int> >(2)) == 2); BOOST_TEST(boost::fusion::count_if(mpl_vec(), bind1st<std::greater_equal<int> >(2)) == 2);
BOOST_TEST(boost::fusion::count_if(mpl_vec(), bind1st<std::less<int> >(2)) == 1); BOOST_TEST(boost::fusion::count_if(mpl_vec(), bind1st<std::less<int> >(2)) == 1);
BOOST_TEST(boost::fusion::count_if(mpl_vec(), bind1st<mutable_equal_to<int> >(2)) == 1);
} }
return boost::report_errors(); return boost::report_errors();

View File

@ -1,27 +1,25 @@
/*============================================================================= /*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2007 Dan Marsden Copyright (c) 2007 Dan Marsden
Copyright (c) 2018 Kohei Takahashi
Distributed under the Boost Software License, Version 1.0. (See accompanying Distributed under 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) file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/ ==============================================================================*/
#include <boost/detail/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
#include <boost/fusion/container/vector/vector.hpp>
#include <boost/fusion/adapted/mpl.hpp> #include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/sequence/io/out.hpp>
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/container/generation/make_vector.hpp>
#include <boost/fusion/algorithm/iteration/fold.hpp>
#include <boost/fusion/algorithm/iteration/accumulate.hpp> #include <boost/fusion/algorithm/iteration/accumulate.hpp>
#include <boost/type_traits/is_same.hpp> #include <boost/fusion/algorithm/iteration/fold.hpp>
#include <boost/fusion/container/generation/make_vector.hpp>
#include <boost/fusion/container/vector/vector.hpp>
#include <boost/fusion/sequence/io/out.hpp>
#include <boost/mpl/if.hpp> #include <boost/mpl/if.hpp>
#include <boost/mpl/next.hpp>
#include <boost/mpl/int.hpp> #include <boost/mpl/int.hpp>
#include <boost/mpl/next.hpp>
#include <boost/mpl/vector.hpp> #include <boost/mpl/vector.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/remove_const.hpp> #include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/remove_reference.hpp> #include <boost/type_traits/remove_reference.hpp>
#include <boost/type_traits/is_reference.hpp>
#include <string> #include <string>
@ -130,7 +128,7 @@ struct functor
} }
}; };
struct visitor struct mutable_visitor
{ {
typedef int result_type; typedef int result_type;
@ -248,7 +246,7 @@ main()
{ {
boost::fusion::vector<long> vec; boost::fusion::vector<long> vec;
visitor v; mutable_visitor v;
boost::fusion::fold(vec, 0, v); boost::fusion::fold(vec, 0, v);
} }
} }

View File

@ -1,11 +1,12 @@
/*============================================================================= /*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2007 Dan Marsden Copyright (c) 2007 Dan Marsden
Copyright (c) 2018 Kohei Takahashi
Distributed under the Boost Software License, Version 1.0. (See accompanying Distributed under 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) file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/ ==============================================================================*/
#include <boost/detail/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
#include <boost/fusion/container/vector/vector.hpp> #include <boost/fusion/container/vector/vector.hpp>
#include <boost/fusion/adapted/mpl.hpp> #include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/algorithm/query/none.hpp> #include <boost/fusion/algorithm/query/none.hpp>
@ -28,6 +29,19 @@ namespace
int search; int search;
}; };
struct mutable_search_for : search_for
{
explicit mutable_search_for(int in_search)
: search_for(in_search)
{}
template <typename T>
bool operator()(T const& v)
{
return search_for::operator()(v);
}
};
} }
int int
@ -45,12 +59,24 @@ main()
BOOST_TEST((!boost::fusion::none(t, boost::lambda::_1 < 3))); BOOST_TEST((!boost::fusion::none(t, boost::lambda::_1 < 3)));
} }
{
boost::fusion::vector<int, short, double> t(1, 2, 3.3);
BOOST_TEST(!boost::fusion::none(t, search_for(1)));
BOOST_TEST(boost::fusion::none(t, search_for(3)));
BOOST_TEST(!boost::fusion::none(t, mutable_search_for(1)));
BOOST_TEST(boost::fusion::none(t, mutable_search_for(3)));
}
{ {
typedef boost::mpl::vector_c<int, 1, 2, 3> mpl_vec; typedef boost::mpl::vector_c<int, 1, 2, 3> mpl_vec;
// We cannot use lambda here as mpl vec iterators return // We cannot use lambda here as mpl vec iterators return
// rvalues, and lambda needs lvalues. // rvalues, and lambda needs lvalues.
BOOST_TEST(boost::fusion::none(mpl_vec(), search_for(4))); BOOST_TEST(boost::fusion::none(mpl_vec(), search_for(4)));
BOOST_TEST(!boost::fusion::none(mpl_vec(), search_for(3))); BOOST_TEST(!boost::fusion::none(mpl_vec(), search_for(3)));
BOOST_TEST(boost::fusion::none(mpl_vec(), mutable_search_for(4)));
BOOST_TEST(!boost::fusion::none(mpl_vec(), mutable_search_for(3)));
} }
return boost::report_errors(); return boost::report_errors();

View File

@ -1,19 +1,16 @@
/*============================================================================= /*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2018 Kohei Takahashi
Distributed under the Boost Software License, Version 1.0. (See accompanying Distributed under 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) file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/ ==============================================================================*/
#include <boost/detail/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
#include <boost/fusion/container/vector/vector.hpp> #include <boost/fusion/container/vector/vector.hpp>
#include <boost/fusion/adapted/mpl.hpp> #include <boost/fusion/container/generation/make_vector.hpp>
#include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/sequence/io/out.hpp> #include <boost/fusion/sequence/io/out.hpp>
#include <boost/fusion/sequence/comparison/equal_to.hpp> #include <boost/fusion/sequence/comparison/equal_to.hpp>
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/container/generation/make_vector.hpp>
#include <boost/fusion/algorithm/transformation/replace_if.hpp> #include <boost/fusion/algorithm/transformation/replace_if.hpp>
#include <string>
struct gt3 struct gt3
{ {
@ -24,6 +21,15 @@ struct gt3
} }
}; };
struct mutable_gt3 : gt3
{
template <typename T>
bool operator()(T x)
{
return gt3::operator()(x);
}
};
int int
main() main()
{ {
@ -42,8 +48,18 @@ main()
{ {
std::cout << replace_if(t1, gt3(), -456) << std::endl; std::cout << replace_if(t1, gt3(), -456) << std::endl;
BOOST_TEST((replace_if(t1, gt3(), -456) BOOST_TEST_EQ(replace_if(t1, gt3(), -456), make_vector(1, 2, -456, -456, s, -456));
== make_vector(1, 2, -456, -456, s, -456))); }
}
{
char const* s = "Ruby";
typedef vector<int, short, double, long, char const*, float> vector_type;
vector_type t1(1, 2, 3.3, 4, s, 5.5f);
{
std::cout << replace_if(t1, mutable_gt3(), -456) << std::endl;
BOOST_TEST_EQ(replace_if(t1, mutable_gt3(), -456), make_vector(1, 2, -456, -456, s, -456));
} }
} }