1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-07-30 19:23:07 +03:00

This commit adds pattern match feature using MPark's library (#2665)

This commit is contained in:
Roman Nozdrin
2022-12-20 19:00:32 +03:00
committed by GitHub
parent 4f5b31a122
commit 9746a2572b
3 changed files with 167 additions and 0 deletions

View File

@ -358,6 +358,13 @@ IF(NOT TARGET columnstore)
RETURN() RETURN()
ENDIF() 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(utils)
ADD_SUBDIRECTORY(oam/oamcpp) ADD_SUBDIRECTORY(oam/oamcpp)
ADD_SUBDIRECTORY(dbcon/execplan) ADD_SUBDIRECTORY(dbcon/execplan)

View File

@ -69,6 +69,12 @@ if (WITH_UNITTESTS)
target_link_libraries(comparators_tests ${ENGINE_LDFLAGS} ${ENGINE_WRITE_LIBS} ${CPPUNIT_LIBRARIES} cppunit) target_link_libraries(comparators_tests ${ENGINE_LDFLAGS} ${ENGINE_WRITE_LIBS} ${CPPUNIT_LIBRARIES} cppunit)
add_test(NAME columnstore:comparators_tests COMMAND comparators_tests) 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 # standalone EM routines test
add_executable(brm_em_standalone brm-em-standalone.cpp) add_executable(brm_em_standalone brm-em-standalone.cpp)
target_link_libraries(brm_em_standalone ${ENGINE_LDFLAGS} ${ENGINE_WRITE_LIBS} ${CPPUNIT_LIBRARIES} cppunit) target_link_libraries(brm_em_standalone ${ENGINE_LDFLAGS} ${ENGINE_WRITE_LIBS} ${CPPUNIT_LIBRARIES} cppunit)

154
tests/patterns_test.cpp Normal file
View File

@ -0,0 +1,154 @@
#include <mpark/patterns.hpp>
#include <string>
#include <utility>
#include <vector>
#include <gtest/gtest.h>
struct Aggregate
{
double d;
std::string s;
std::vector<int> 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);
}