mirror of
https://github.com/boostorg/yap.git
synced 2026-02-22 03:52:18 +00:00
197 lines
4.3 KiB
C++
197 lines
4.3 KiB
C++
#include <boost/yap/expression.hpp>
|
|
#include <boost/yap/print.hpp>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
template <typename T>
|
|
using term = boost::yap::terminal<T>;
|
|
|
|
namespace yap = boost::yap;
|
|
namespace bh = boost::hana;
|
|
|
|
|
|
template <boost::yap::expr_kind Kind, typename Tuple>
|
|
struct user_expr
|
|
{
|
|
using this_type = user_expr<Kind, Tuple>;
|
|
|
|
static boost::yap::expr_kind const kind = Kind;
|
|
|
|
Tuple elements;
|
|
|
|
BOOST_YAP_USER_BINARY_OPERATOR_MEMBER(plus, this_type, ::user_expr)
|
|
};
|
|
|
|
template <typename T>
|
|
using user_term = boost::yap::terminal<T, user_expr>;
|
|
|
|
template <typename T>
|
|
using user_ref = boost::yap::expression_ref<T, user_expr>;
|
|
|
|
struct thing {};
|
|
|
|
TEST(expression, test_print)
|
|
{
|
|
term<double> unity{1.0};
|
|
int i_ = 42;
|
|
term<int &&> i{std::move(i_)};
|
|
yap::expression<
|
|
yap::expr_kind::plus,
|
|
bh::tuple<
|
|
yap::expression_ref<term<double> &>,
|
|
term<int &&>
|
|
>
|
|
> expr = unity + std::move(i);
|
|
yap::expression<
|
|
yap::expr_kind::plus,
|
|
bh::tuple<
|
|
yap::expression_ref<term<double> &>,
|
|
yap::expression<
|
|
yap::expr_kind::plus,
|
|
bh::tuple<
|
|
yap::expression_ref<term<double> &>,
|
|
term<int &&>
|
|
>
|
|
>
|
|
>
|
|
> unevaluated_expr = unity + std::move(expr);
|
|
|
|
{
|
|
std::ostringstream oss;
|
|
yap::print(oss, unity);
|
|
EXPECT_EQ(oss.str(), R"(term<double>[=1]
|
|
)");
|
|
}
|
|
|
|
{
|
|
std::ostringstream oss;
|
|
yap::print(oss, expr);
|
|
EXPECT_EQ(oss.str(), R"(expr<+>
|
|
term<double>[=1] &
|
|
term<int &&>[=42]
|
|
)");
|
|
}
|
|
|
|
{
|
|
std::ostringstream oss;
|
|
yap::print(oss, unevaluated_expr);
|
|
EXPECT_EQ(oss.str(), R"(expr<+>
|
|
term<double>[=1] &
|
|
expr<+>
|
|
term<double>[=1] &
|
|
term<int &&>[=42]
|
|
)");
|
|
}
|
|
|
|
term<thing> a_thing(thing{});
|
|
|
|
{
|
|
std::ostringstream oss;
|
|
yap::print(oss, a_thing);
|
|
EXPECT_EQ(oss.str(), R"(term<thing>[=<<unprintable-value>>]
|
|
)");
|
|
}
|
|
|
|
term<double> const const_unity{1.0};
|
|
yap::expression<
|
|
yap::expr_kind::plus,
|
|
bh::tuple<
|
|
yap::expression_ref<term<double> &>,
|
|
yap::expression_ref<term<double> const &>
|
|
>
|
|
> nonconst_plus_const = unity + const_unity;
|
|
|
|
{
|
|
std::ostringstream oss;
|
|
yap::print(oss, nonconst_plus_const);
|
|
EXPECT_EQ(oss.str(), R"(expr<+>
|
|
term<double>[=1] &
|
|
term<double>[=1] const &
|
|
)");
|
|
}
|
|
}
|
|
|
|
TEST(user_expr, test_print)
|
|
{
|
|
user_term<double> unity{1.0};
|
|
int i_ = 42;
|
|
user_term<int &&> i{std::move(i_)};
|
|
user_expr<
|
|
yap::expr_kind::plus,
|
|
bh::tuple<
|
|
user_ref<user_term<double> &>,
|
|
user_term<int &&>
|
|
>
|
|
> expr = unity + std::move(i);
|
|
user_expr<
|
|
yap::expr_kind::plus,
|
|
bh::tuple<
|
|
user_ref<user_term<double> &>,
|
|
user_expr<
|
|
yap::expr_kind::plus,
|
|
bh::tuple<
|
|
user_ref<user_term<double> &>,
|
|
user_term<int &&>
|
|
>
|
|
>
|
|
>
|
|
> unevaluated_expr = unity + std::move(expr);
|
|
|
|
{
|
|
std::ostringstream oss;
|
|
yap::print(oss, unity);
|
|
EXPECT_EQ(oss.str(), R"(term<double>[=1]
|
|
)");
|
|
}
|
|
|
|
{
|
|
std::ostringstream oss;
|
|
yap::print(oss, expr);
|
|
EXPECT_EQ(oss.str(), R"(expr<+>
|
|
term<double>[=1] &
|
|
term<int &&>[=42]
|
|
)");
|
|
}
|
|
|
|
{
|
|
std::ostringstream oss;
|
|
yap::print(oss, unevaluated_expr);
|
|
EXPECT_EQ(oss.str(), R"(expr<+>
|
|
term<double>[=1] &
|
|
expr<+>
|
|
term<double>[=1] &
|
|
term<int &&>[=42]
|
|
)");
|
|
}
|
|
|
|
user_term<thing> a_thing{bh::make_tuple(thing{})};
|
|
|
|
{
|
|
std::ostringstream oss;
|
|
yap::print(oss, a_thing);
|
|
EXPECT_EQ(oss.str(), R"(term<thing>[=<<unprintable-value>>]
|
|
)");
|
|
}
|
|
|
|
user_term<double> const const_unity{1.0};
|
|
user_expr<
|
|
yap::expr_kind::plus,
|
|
bh::tuple<
|
|
user_ref<user_term<double> &>,
|
|
user_ref<user_term<double> const &>
|
|
>
|
|
> nonconst_plus_const = unity + const_unity;
|
|
|
|
{
|
|
std::ostringstream oss;
|
|
yap::print(oss, nonconst_plus_const);
|
|
EXPECT_EQ(oss.str(), R"(expr<+>
|
|
term<double>[=1] &
|
|
term<double>[=1] const &
|
|
)");
|
|
}
|
|
}
|