Commit daa5a857 authored by Dominik Charousset's avatar Dominik Charousset

improved pattern matching unit test

parent 622f6303
......@@ -47,11 +47,7 @@ void foobar(index_token<Pos, Max>) {
foobar(index_token<Pos+1, Max>{});
}
int main() {
CPPA_TEST(test_match);
/* test guard evaluation */ {
// ascending values
void test_simple_guards() {
auto ascending = [](int a, int b, int c) -> bool {
return a < b && b < c;
};
......@@ -79,8 +75,9 @@ int main() {
CPPA_CHECK(ge_invoke(expr4, string("-h")));
CPPA_CHECK(ge_invoke(expr4, string("--help")));
CPPA_CHECK(!ge_invoke(expr4, string("-g")));
}
/* guard expressions on containers */ {
}
void test_container_guards() {
auto expr5 = _x1.starts_with("--");
CPPA_CHECK(ge_invoke(expr5, string("--help")));
CPPA_CHECK(!ge_invoke(expr5, string("-help")));
......@@ -114,8 +111,9 @@ int main() {
CPPA_CHECK(!ge_invoke(expr21, expr21_vec_b));
auto expr22 = _x1.empty() && _x2.not_empty();
CPPA_CHECK(ge_invoke(expr22, std::string(""), std::string("abc")));
}
/* user-defined expressions */ {
}
void test_user_defined_expressions() {
int ival = 5;
auto expr8 = _x1 == ival;
auto expr9 = _x1 == std::ref(ival);
......@@ -146,8 +144,9 @@ int main() {
CPPA_CHECK_EQUAL((ge_invoke(expr16_a, expr16_str)), expr16_str.size());
expr16_str.front() = '_';
CPPA_CHECK_EQUAL((ge_invoke(expr16_b, expr16_str)), false);
}
/* user-defined expressions using 'gref' */ {
}
void test_gref() {
int expr17_value = 42;
auto expr17 = gref(expr17_value) == 42;
CPPA_CHECK_EQUAL(ge_invoke(expr17), true);
......@@ -179,8 +178,9 @@ int main() {
};
CPPA_CHECK(expr21(make_any_tuple(atom("add"), 1, 2)) == make_any_tuple(3));
CPPA_CHECK(!expr21(make_any_tuple(atom("sub"), 1, 2)));
}
/* test 'match' function */ {
}
void test_match_function() {
auto res0 = match(5) (
on_arg_match.when(_x1 < 6) >> [&](int i) {
CPPA_CHECK_EQUAL(i, 5);
......@@ -219,8 +219,9 @@ int main() {
}
);
CPPA_CHECK_EQUAL(res5, "C");
}
/* test 'match_each' */ {
}
void test_match_each() {
string sum;
vector<string> sum_args = { "-h", "--version", "-wtf" };
auto iter1 = match_each(begin(sum_args), end(sum_args)) (
......@@ -246,8 +247,9 @@ int main() {
on("-h") >> [] { }
);
CPPA_CHECK(iter2 == (begin(sum_args) + 1));
}
/* test match_stream */ {
}
void test_match_stream() {
bool success = false;
istringstream iss("hello world");
success = match_stream<string>(iss) (
......@@ -279,15 +281,15 @@ int main() {
}
);
CPPA_CHECK_EQUAL(success, true);
}
// utility macro to identify invoked functors
}
void test_behavior() {
std::string last_invoked_fun;
# define bhvr_check(pf, tup, expected_result, str) { \
last_invoked_fun = ""; \
CPPA_CHECK_EQUAL(static_cast<bool>(pf(tup)), expected_result); \
CPPA_CHECK_EQUAL(last_invoked_fun, str); \
}
/* test if match hints are evaluated properly */ {
behavior bhvr1 {
on<int>() >> [&](int i) -> match_hint {
last_invoked_fun = "<int>@1";
......@@ -307,8 +309,6 @@ int main() {
bhvr_check(bhvr1, make_any_tuple(24), true, "<int>@1");
bhvr_check(bhvr1, make_any_tuple(2.f), true, "<float>@2");
bhvr_check(bhvr1, make_any_tuple(""), true, "<*>@4");
}
/* test or_else concatenation */ {
partial_function pf0 {
on<int, int>() >> [&] { last_invoked_fun = "<int, int>@1"; },
on<float>() >> [&] { last_invoked_fun = "<float>@2"; }
......@@ -331,8 +331,9 @@ int main() {
auto pf13 = pf11.or_else(pf12);
bhvr_check(pf13, make_any_tuple(42), true, "<int>@1");
bhvr_check(pf13, make_any_tuple(42.24f), true, "<float>@2");
}
/* check result of pattern matching */ {
}
void test_pattern_matching() {
auto res = match(make_any_tuple(42, 4.2f)) (
on(42, arg_match) >> [](double d) {
return d;
......@@ -351,6 +352,35 @@ int main() {
}
);
CPPA_CHECK(!res2);
}
void test_wildcards() {
auto expr = (
on(any_vals, arg_match) >> [](int a, int b) {
cout << "invoked: " << a << ", " << b << endl;
}
);
CPPA_CHECK(expr(make_any_tuple(1, 2, 3)));
CPPA_CHECK(expr(make_any_tuple(1, 2)));
partial_function pf = expr;
CPPA_CHECK(pf(make_any_tuple(1, 2)));
CPPA_CHECK(pf(make_any_tuple(1, 2, 3)));
behavior bhvr = expr;
CPPA_CHECK(bhvr(make_any_tuple(1, 2)));
CPPA_CHECK(bhvr(make_any_tuple(1, 2, 3)));
}
int main() {
CPPA_TEST(test_match);
test_wildcards();
test_simple_guards();
test_container_guards();
test_user_defined_expressions();
test_gref();
test_match_function();
test_match_each();
test_match_stream();
test_behavior();
test_pattern_matching();
return CPPA_TEST_RESULT();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment