diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e1aa6069..2fb495dc6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -358,6 +358,13 @@ IF(NOT TARGET columnstore) RETURN() ENDIF() +set(EXTERNAL_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/external) +ExternalProject_Add(mpark_patterns + GIT_REPOSITORY https://github.com/mpark/patterns + GIT_TAG v0.3.0 + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} +) + ADD_SUBDIRECTORY(utils) ADD_SUBDIRECTORY(oam/oamcpp) ADD_SUBDIRECTORY(dbcon/execplan) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2d2e86514..647b4aeb1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -69,6 +69,12 @@ if (WITH_UNITTESTS) target_link_libraries(comparators_tests ${ENGINE_LDFLAGS} ${ENGINE_WRITE_LIBS} ${CPPUNIT_LIBRARIES} cppunit) add_test(NAME columnstore:comparators_tests COMMAND comparators_tests) + add_executable(patterns_test patterns_test.cpp) + add_dependencies(patterns_test googletest mpark_patterns) + target_include_directories(patterns_test PUBLIC ${ENGINE_COMMON_INCLUDES} ${ENGINE_BLOCKCACHE_INCLUDE} ${ENGINE_PRIMPROC_INCLUDE} ) + target_link_libraries(patterns_test ${GTEST_LIBRARIES}) + gtest_add_tests(TARGET patterns_test TEST_PREFIX mpark_patterns:) + # standalone EM routines test add_executable(brm_em_standalone brm-em-standalone.cpp) target_link_libraries(brm_em_standalone ${ENGINE_LDFLAGS} ${ENGINE_WRITE_LIBS} ${CPPUNIT_LIBRARIES} cppunit) diff --git a/tests/patterns_test.cpp b/tests/patterns_test.cpp new file mode 100644 index 000000000..e3c7dbbe6 --- /dev/null +++ b/tests/patterns_test.cpp @@ -0,0 +1,154 @@ +#include + +#include +#include +#include + +#include + +struct Aggregate +{ + double d; + std::string s; + std::vector v; +}; + +TEST(Aggregate, LRef) +{ + Aggregate aggregate{4.2, "hello", {101, 202}}; + + using namespace mpark::patterns; + double& result = match(aggregate)( + pattern(ds(arg, "world", _)) = [](double& d) -> double& { return d; }, + pattern(ds(arg, "hello", _)) = [](double& d) -> double& { return d; }); + + EXPECT_EQ(aggregate.d, result); + EXPECT_EQ(&aggregate.d, &result); +} + +TEST(Aggregate, ConstLRef) +{ + const Aggregate aggregate{4.2, "hello", {101, 202}}; + + using namespace mpark::patterns; + const double& result = match(aggregate)( + pattern(ds(arg, "world", _)) = [](const double& d) -> const double& { return d; }, + pattern(ds(arg, "hello", _)) = [](const double& d) -> const double& { return d; }); + + EXPECT_EQ(aggregate.d, result); + EXPECT_EQ(&aggregate.d, &result); +} + +TEST(Aggregate, RRef) +{ + auto aggregate = [] { return Aggregate{4.2, "hello", {101, 202}}; }; + + using namespace mpark::patterns; + double result = match(aggregate())( + pattern(ds(arg, "world", _)) = [](double&& d) -> double { return std::move(d); }, + pattern(ds(arg, "hello", _)) = [](double&& d) -> double { return std::move(d); }); + + EXPECT_EQ(4.2, result); +} + +TEST(Aggregate, Empty) +{ + struct + { + } empty; + + using namespace mpark::patterns; + match(empty)(pattern(ds()) = [] {}); +} + +TEST(Aggregate, OneChar) +{ + struct + { + char x; + } one{'x'}; + + using namespace mpark::patterns; + int result = match(one)( + pattern(ds('a')) = [] { return false; }, pattern(ds('x')) = [] { return true; }); + + EXPECT_TRUE(result); +} + +TEST(Aggregate, OneInt) +{ + struct + { + int x; + } one{101}; + + using namespace mpark::patterns; + int result = match(one)( + pattern(ds(101)) = [] { return true; }, pattern(ds(1)) = [] { return false; }); + + EXPECT_TRUE(result); +} + +TEST(Aggregate, TwoChars) +{ + struct + { + char x; + char y; + } two{'x', 'y'}; + + using namespace mpark::patterns; + bool result = match(two)( + pattern(ds('a', 'b')) = [] { return false; }, pattern(ds('x', 'y')) = [] { return true; }); + + EXPECT_TRUE(result); +} + +TEST(Aggregate, TwoInts) +{ + struct + { + int x; + int y; + } two{101, 202}; + + using namespace mpark::patterns; + bool result = match(two)( + pattern(ds(1, 2)) = [] { return false; }, pattern(ds(101, 202)) = [] { return true; }); + + EXPECT_TRUE(result); +} + +TEST(Aggregate, ThreeChars) +{ + struct + { + char x; + char y; + char z; + } three{'x', 'y', 'z'}; + + using namespace mpark::patterns; + bool result = match(three)( + pattern(ds('x', 'y', 'z')) = [] { return true; }, pattern(ds('a', 'b', 'c')) = [] { return false; }); + + EXPECT_TRUE(result); +} + +TEST(Aggregate, FourChars) +{ + struct + { + char a; + char b; + char c; + char d; + } four{'a', 'b', 'c', 'd'}; + + using namespace mpark::patterns; + bool result = match(four)( + pattern(ds('p', 'q', 'r', 's')) = [] { return false; }, + pattern(ds('a', 'b', 'c', 'd')) = [] { return true; }); + + EXPECT_TRUE(result); +} \ No newline at end of file