// 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 term_ref = boost::yap::expression_ref &>; template using term_cref = boost::yap::expression_ref const &>; namespace yap = boost::yap; namespace bh = boost::hana; struct void_callable { void operator()() { *called_ = (*call_count_)++; } int * call_count_; int * called_; }; struct int_callable { int operator()() { *called_ = (*call_count_)++; return 42; } int * call_count_; int * called_; }; struct double_callable { double operator()() { *called_ = (*call_count_)++; return 13.0; } int * call_count_; int * called_; }; TEST(comma, order_of_eval) { { int call_count = 0; int int_called = -1; int double_called = -1; auto int_double_expr = (term{{&call_count, &int_called}}(), term{{&call_count, &double_called}}()); EXPECT_EQ(evaluate(int_double_expr), 13.0); EXPECT_EQ(int_called, 0); EXPECT_EQ(double_called, 1); } { int call_count = 0; int int_called = -1; int double_called = -1; auto double_int_expr = (term{{&call_count, &double_called}}(), term{{&call_count, &int_called}}()); EXPECT_EQ(evaluate(double_int_expr), 42); EXPECT_EQ(int_called, 1); EXPECT_EQ(double_called, 0); } } TEST(comma, void_expressions) { { int call_count = 0; int void_called = -1; int int_called = -1; auto void_int_expr = (term{{&call_count, &void_called}}(), term{{&call_count, &int_called}}()); EXPECT_EQ(evaluate(void_int_expr), 42); EXPECT_EQ(void_called, 0); EXPECT_EQ(int_called, 1); } { int call_count = 0; int void_called = -1; int int_called = -1; auto int_void_expr = (term{{&call_count, &int_called}}(), term{{&call_count, &void_called}}()); BOOST_MPL_ASSERT( (std::is_same)); evaluate(int_void_expr); EXPECT_EQ(void_called, 1); EXPECT_EQ(int_called, 0); } }