diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..597fe23 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.6) + +add_subdirectory(googletest-release-1.8.0) + +find_package(Boost 1.62.0 REQUIRED) +include_directories(${Boost_INCLUDE_DIRS}) + +if (NOT CMAKE_CXX_COMPILER_ID STREQUAL Clang) + message(FATAL_ERROR "Only Clang with -std=c++1z will work") +endif() + +add_definitions(-std=c++1z) + +add_subdirectory(test) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..789de68 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,18 @@ +include(CTest) + +enable_testing() + +add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} -VV -C ${CMAKE_CFG_INTDIR}) + +include_directories( + ${CMAKE_HOME_DIRECTORY} + ${CMAKE_HOME_DIRECTORY}/googletest-release-1.8.0/googletest/include +) + +macro(add_test_executable name) + add_executable(${name} ${name}.cpp) + target_link_libraries(${name} gtest gtest_main) + add_test(${name} ${CMAKE_CURRENT_BINARY_DIR}/${name} --gtest_catch_exceptions=1) +endmacro() + +add_test_executable(print) diff --git a/test/print.cpp b/test/print.cpp new file mode 100644 index 0000000..f0a8bcc --- /dev/null +++ b/test/print.cpp @@ -0,0 +1,138 @@ +#include "expression.hpp" + +#define BOOST_PROTO17_STREAM_OPERATORS +#include "print.hpp" + +#include + +#include + + +template +using term = boost::proto17::terminal; + +namespace bp17 = boost::proto17; + + +TEST(print, test_print) +{ + term unity{1.0}; + int i_ = 42; + term i{std::move(i_)}; + bp17::expression< + bp17::expr_kind::plus, + term, + term + > expr = unity + std::move(i); + bp17::expression< + bp17::expr_kind::plus, + term, + bp17::expression< + bp17::expr_kind::plus, + term, + term + > + > unevaluated_expr = unity + std::move(expr); + + { + std::ostringstream oss; + bp17::print(oss, unity); + EXPECT_EQ(oss.str(), R"(term[=1] +)"); + } + + { + std::ostringstream oss; + bp17::print(oss, expr); + EXPECT_EQ(oss.str(), R"(expr<+> + term[=1] + term[=42] +)"); + } + + { + std::ostringstream oss; + bp17::print(oss, unevaluated_expr); + EXPECT_EQ(oss.str(), R"(expr<+> + term[=1] + expr<+> + term[=1] + term[=42] +)"); + } + + struct thing {}; + term a_thing(thing{}); + + { + std::ostringstream oss; + bp17::print(oss, a_thing); + EXPECT_EQ(oss.str(), R"(term[=<>] +)"); + } + + { + using namespace boost::proto17::literals; + + { + std::ostringstream oss; + oss << (0_p + unity); + EXPECT_EQ(oss.str(), R"(expr<+> + placeholder<0> + term[=1] +)"); + } + + { + std::ostringstream oss; + oss << (2_p + 3_p); + EXPECT_EQ(oss.str(), R"(expr<+> + placeholder<2> + placeholder<3> +)"); + } + + { + std::ostringstream oss; + oss << (unity + 1_p); + EXPECT_EQ(oss.str(), R"(expr<+> + term[=1] + placeholder<1> +)"); + } + } + + { + std::ostringstream oss; + oss << unity; + EXPECT_EQ(oss.str(), R"(term[=1] +)"); + } + + { + std::ostringstream oss; + oss << expr; + EXPECT_EQ(oss.str(), R"(expr<+> + term[=1] + term[=42] +)"); + } + + { + std::ostringstream oss; + oss << unevaluated_expr; + EXPECT_EQ(oss.str(), R"(expr<+> + term[=1] + expr<+> + term[=1] + term[=42] +)"); + } + + { + std::ostringstream oss; + oss << a_thing; + EXPECT_EQ(oss.str(), R"(term[=<>] +)"); + } +}