// Copyright (C) 2016-2018 T. Zachary Laine // // 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 template using term = boost::yap::terminal; template using ref = boost::yap::expression_ref; namespace yap = boost::yap; namespace bh = boost::hana; template struct user_expr { static boost::yap::expr_kind const kind = Kind; Tuple elements; BOOST_YAP_USER_BINARY_OPERATOR_MEMBER(plus, ::user_expr) }; template using user_term = boost::yap::terminal; template using user_ref = boost::yap::expression_ref; struct thing {}; TEST(expression, test_print) { term unity{1.0}; int i_ = 42; term i{std::move(i_)}; yap::expression< yap::expr_kind::plus, bh::tuple &>, term>> expr = unity + std::move(i); yap::expression< yap::expr_kind::plus, bh::tuple< ref &>, yap::expression< yap::expr_kind::plus, bh::tuple &>, term>>>> unevaluated_expr = unity + std::move(expr); { std::ostringstream oss; yap::print(oss, unity); EXPECT_EQ(oss.str(), R"(term[=1] )"); } { std::ostringstream oss; yap::print(oss, expr); EXPECT_EQ(oss.str(), R"(expr<+> term[=1] & term[=42] )"); } { std::ostringstream oss; yap::print(oss, unevaluated_expr); EXPECT_EQ(oss.str(), R"(expr<+> term[=1] & expr<+> term[=1] & term[=42] )"); } term a_thing(thing{}); { std::ostringstream oss; yap::print(oss, a_thing); EXPECT_EQ(oss.str(), R"(term[=<>] )"); } term const const_unity{1.0}; yap::expression< yap::expr_kind::plus, bh::tuple &>, ref const &>>> nonconst_plus_const = unity + const_unity; { std::ostringstream oss; yap::print(oss, nonconst_plus_const); EXPECT_EQ(oss.str(), R"(expr<+> term[=1] & term[=1] const & )"); } { using namespace yap::literals; std::ostringstream oss; yap::print(oss, 1_p); EXPECT_EQ(oss.str(), R"(term>[=1] )"); } } TEST(user_expr, test_print) { user_term unity{1.0}; int i_ = 42; user_term i{std::move(i_)}; user_expr< yap::expr_kind::plus, bh::tuple &>, user_term>> expr = unity + std::move(i); user_expr< yap::expr_kind::plus, bh::tuple< user_ref &>, user_expr< yap::expr_kind::plus, bh::tuple &>, user_term>>>> unevaluated_expr = unity + std::move(expr); { std::ostringstream oss; yap::print(oss, unity); EXPECT_EQ(oss.str(), R"(term[=1] )"); } { std::ostringstream oss; yap::print(oss, expr); EXPECT_EQ(oss.str(), R"(expr<+> term[=1] & term[=42] )"); } { std::ostringstream oss; yap::print(oss, unevaluated_expr); EXPECT_EQ(oss.str(), R"(expr<+> term[=1] & expr<+> term[=1] & term[=42] )"); } user_term a_thing{bh::make_tuple(thing{})}; { std::ostringstream oss; yap::print(oss, a_thing); EXPECT_EQ(oss.str(), R"(term[=<>] )"); } user_term const const_unity{1.0}; user_expr< yap::expr_kind::plus, bh::tuple< user_ref &>, user_ref const &>>> nonconst_plus_const = unity + const_unity; { std::ostringstream oss; yap::print(oss, nonconst_plus_const); EXPECT_EQ(oss.str(), R"(expr<+> term[=1] & term[=1] const & )"); } }