From c1fe4e78abd4bcd97af55ff75fb85c4f4012bb6e Mon Sep 17 00:00:00 2001 From: Kohei Takahashi Date: Tue, 3 Jul 2018 00:48:40 +0900 Subject: [PATCH] algo: Added tests for mutable functor --- test/algorithm/all.cpp | 27 +++++++++++++++++++++++++-- test/algorithm/any.cpp | 25 ++++++++++++++++++++++++- test/algorithm/count_if.cpp | 13 ++++++++++++- test/algorithm/fold.cpp | 22 ++++++++++------------ test/algorithm/none.cpp | 28 +++++++++++++++++++++++++++- test/algorithm/replace_if.cpp | 32 ++++++++++++++++++++++++-------- 6 files changed, 122 insertions(+), 25 deletions(-) diff --git a/test/algorithm/all.cpp b/test/algorithm/all.cpp index 7b555073..0fdafed9 100644 --- a/test/algorithm/all.cpp +++ b/test/algorithm/all.cpp @@ -1,11 +1,12 @@ /*============================================================================= Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2007 Dan Marsden + Copyright (c) 2018 Kohei Takahashi 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) ==============================================================================*/ -#include +#include #include #include #include @@ -28,6 +29,19 @@ namespace int search; }; + + struct mutable_search_for : search_for + { + explicit mutable_search_for(int in_search) + : search_for(in_search) + {} + + template + bool operator()(T const& v) + { + return search_for::operator()(v); + } + }; } int @@ -58,11 +72,20 @@ main() } { - typedef boost::mpl::vector_c mpl_vec; + boost::fusion::vector 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 mpl_vec; // We cannot use lambda here as mpl vec iterators return // rvalues, and lambda needs lvalues. 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(), mutable_search_for(1))); + BOOST_TEST(!boost::fusion::all(mpl_vec(), mutable_search_for(2))); } return boost::report_errors(); diff --git a/test/algorithm/any.cpp b/test/algorithm/any.cpp index da0aa793..906a7870 100644 --- a/test/algorithm/any.cpp +++ b/test/algorithm/any.cpp @@ -2,11 +2,12 @@ Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2005 Eric Niebler Copyright (c) Dan Marsden + Copyright (c) 2018 Kohei Takahashi 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) ==============================================================================*/ -#include +#include #include #include #include @@ -29,6 +30,19 @@ namespace int search; }; + + struct mutable_search_for : search_for + { + explicit mutable_search_for(int in_search) + : search_for(in_search) + {} + + template + bool operator()(T const& v) + { + return search_for::operator()(v); + } + }; } int @@ -44,12 +58,21 @@ main() BOOST_TEST(!boost::fusion::any(t, boost::lambda::_1 == 3)); } + { + boost::fusion::vector 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 mpl_vec; // We cannot use lambda here as mpl vec iterators return // rvalues, and lambda needs lvalues. 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(), mutable_search_for(2))); + BOOST_TEST(!boost::fusion::any(mpl_vec(), mutable_search_for(4))); } return boost::report_errors(); diff --git a/test/algorithm/count_if.cpp b/test/algorithm/count_if.cpp index 7555f6fc..35979baf 100644 --- a/test/algorithm/count_if.cpp +++ b/test/algorithm/count_if.cpp @@ -1,11 +1,12 @@ /*============================================================================= Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2005 Eric Niebler + Copyright (c) 2018 Kohei Takahashi 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) ==============================================================================*/ -#include +#include #include #include #include @@ -18,20 +19,29 @@ struct bind1st > : public F { T n; bind1st(T n) : n(n) { } + bool operator()(T v) { return F::operator()(n, v); } bool operator()(T v) const { return F::operator()(n, v); } }; +template +struct mutable_equal_to : std::equal_to +{ + bool operator()(T l, T r) { return std::equal_to::operator()(l, r); } +}; + int main() { { boost::fusion::vector t(1, 2, 3.3); BOOST_TEST(boost::fusion::count_if(t, bind1st >(2)) == 1); + BOOST_TEST(boost::fusion::count_if(t, bind1st >(2)) == 1); } { boost::fusion::vector t(1, 2, 3.3); BOOST_TEST(boost::fusion::count_if(t, bind1st >(3)) == 0); + BOOST_TEST(boost::fusion::count_if(t, bind1st >(3)) == 0); } { @@ -39,6 +49,7 @@ main() // Cannot use lambda here as mpl iterators return rvalues and lambda needs lvalues BOOST_TEST(boost::fusion::count_if(mpl_vec(), bind1st >(2)) == 2); BOOST_TEST(boost::fusion::count_if(mpl_vec(), bind1st >(2)) == 1); + BOOST_TEST(boost::fusion::count_if(mpl_vec(), bind1st >(2)) == 1); } return boost::report_errors(); diff --git a/test/algorithm/fold.cpp b/test/algorithm/fold.cpp index c441ceaf..e941b66f 100644 --- a/test/algorithm/fold.cpp +++ b/test/algorithm/fold.cpp @@ -1,27 +1,25 @@ /*============================================================================= Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2007 Dan Marsden + Copyright (c) 2018 Kohei Takahashi 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) ==============================================================================*/ -#include -#include +#include #include -#include -#include -#include -#include #include -#include +#include +#include +#include +#include #include -#include #include +#include #include - +#include #include #include -#include #include @@ -130,7 +128,7 @@ struct functor } }; -struct visitor +struct mutable_visitor { typedef int result_type; @@ -248,7 +246,7 @@ main() { boost::fusion::vector vec; - visitor v; + mutable_visitor v; boost::fusion::fold(vec, 0, v); } } diff --git a/test/algorithm/none.cpp b/test/algorithm/none.cpp index 83dfa752..5b1b9e81 100644 --- a/test/algorithm/none.cpp +++ b/test/algorithm/none.cpp @@ -1,11 +1,12 @@ /*============================================================================= Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2007 Dan Marsden + Copyright (c) 2018 Kohei Takahashi 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) ==============================================================================*/ -#include +#include #include #include #include @@ -28,6 +29,19 @@ namespace int search; }; + + struct mutable_search_for : search_for + { + explicit mutable_search_for(int in_search) + : search_for(in_search) + {} + + template + bool operator()(T const& v) + { + return search_for::operator()(v); + } + }; } int @@ -45,12 +59,24 @@ main() BOOST_TEST((!boost::fusion::none(t, boost::lambda::_1 < 3))); } + { + boost::fusion::vector 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 mpl_vec; // We cannot use lambda here as mpl vec iterators return // rvalues, and lambda needs lvalues. 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(), mutable_search_for(4))); + BOOST_TEST(!boost::fusion::none(mpl_vec(), mutable_search_for(3))); } return boost::report_errors(); diff --git a/test/algorithm/replace_if.cpp b/test/algorithm/replace_if.cpp index bad5905e..a13414fc 100644 --- a/test/algorithm/replace_if.cpp +++ b/test/algorithm/replace_if.cpp @@ -1,19 +1,16 @@ /*============================================================================= Copyright (c) 2001-2011 Joel de Guzman + Copyright (c) 2018 Kohei Takahashi 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) ==============================================================================*/ -#include +#include #include -#include -#include +#include #include #include -#include -#include #include -#include struct gt3 { @@ -24,6 +21,15 @@ struct gt3 } }; +struct mutable_gt3 : gt3 +{ + template + bool operator()(T x) + { + return gt3::operator()(x); + } +}; + int main() { @@ -42,8 +48,18 @@ main() { std::cout << replace_if(t1, gt3(), -456) << std::endl; - BOOST_TEST((replace_if(t1, gt3(), -456) - == make_vector(1, 2, -456, -456, s, -456))); + BOOST_TEST_EQ(replace_if(t1, gt3(), -456), make_vector(1, 2, -456, -456, s, -456)); + } + } + + { + char const* s = "Ruby"; + typedef vector 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)); } }