Add hs_expression_ext_info() API function

This is a variant of hs_expression_info() that can also accept extended
parameters.
This commit is contained in:
Justin Viiret
2015-10-23 17:15:24 +11:00
committed by Matthew Barr
parent 67b9784dae
commit 12921b7c97
5 changed files with 239 additions and 56 deletions

View File

@@ -2154,6 +2154,37 @@ TEST(HyperscanArgChecks, ExprInfoNullErrPtr) {
EXPECT_TRUE(info == nullptr);
}
// hs_expression_ext_info: Compile a NULL pattern
TEST(HyperscanArgChecks, ExprExtInfoNullExpression) {
hs_expr_info_t *info = nullptr;
hs_compile_error_t *compile_err = nullptr;
hs_error_t err =
hs_expression_ext_info(nullptr, 0, nullptr, &info, &compile_err);
EXPECT_EQ(HS_COMPILER_ERROR, err);
EXPECT_TRUE(info == nullptr);
EXPECT_TRUE(compile_err != nullptr);
hs_free_compile_error(compile_err);
}
// hs_expression_ext_info: NULL info block ptr
TEST(HyperscanArgChecks, ExprExtInfoNullInfoPtr) {
hs_compile_error_t *compile_err = nullptr;
hs_error_t err =
hs_expression_ext_info("foobar", 0, nullptr, nullptr, &compile_err);
EXPECT_EQ(HS_COMPILER_ERROR, err);
EXPECT_TRUE(compile_err != nullptr);
hs_free_compile_error(compile_err);
}
// hs_expression_ext_info: No compiler error block
TEST(HyperscanArgChecks, ExprExtInfoNullErrPtr) {
hs_expr_info_t *info = nullptr;
hs_error_t err =
hs_expression_ext_info("foobar", 0, nullptr, &info, nullptr);
EXPECT_EQ(HS_COMPILER_ERROR, err);
EXPECT_TRUE(info == nullptr);
}
TEST(HyperscanArgChecks, hs_free_database_null) {
hs_error_t err = hs_free_database(nullptr);
ASSERT_EQ(HS_SUCCESS, err);

View File

@@ -42,6 +42,8 @@ namespace /* anonymous */ {
struct expected_info {
const char *pattern;
hs_expr_ext ext;
unsigned min;
unsigned max;
char unordered_matches;
@@ -52,10 +54,25 @@ struct expected_info {
class ExprInfop : public TestWithParam<expected_info> {
};
TEST_P(ExprInfop, width) {
static
void check_info(const expected_info &ei, const hs_expr_info_t *info) {
EXPECT_EQ(ei.min, info->min_width);
EXPECT_EQ(ei.max, info->max_width);
EXPECT_EQ(ei.unordered_matches, info->unordered_matches);
EXPECT_EQ(ei.matches_at_eod, info->matches_at_eod);
EXPECT_EQ(ei.matches_only_at_eod, info->matches_only_at_eod);
}
// Check with hs_expression_info function.
TEST_P(ExprInfop, check_no_ext) {
const expected_info &ei = GetParam();
SCOPED_TRACE(ei.pattern);
if (ei.ext.flags) {
// This is an extparam test, skip it.
return;
}
hs_expr_info_t *info = nullptr;
hs_compile_error_t *c_err = nullptr;
hs_error_t err = hs_expression_info(ei.pattern, 0, &info, &c_err);
@@ -63,54 +80,97 @@ TEST_P(ExprInfop, width) {
ASSERT_TRUE(info != nullptr);
ASSERT_TRUE(c_err == nullptr);
EXPECT_EQ(ei.min, info->min_width);
EXPECT_EQ(ei.max, info->max_width);
EXPECT_EQ(ei.unordered_matches, info->unordered_matches);
EXPECT_EQ(ei.matches_at_eod, info->matches_at_eod);
EXPECT_EQ(ei.matches_only_at_eod, info->matches_only_at_eod);
check_info(ei, info);
free(info);
}
// Check with hs_expression_ext_info function.
TEST_P(ExprInfop, check_ext) {
const expected_info &ei = GetParam();
SCOPED_TRACE(ei.pattern);
hs_expr_info_t *info = nullptr;
hs_compile_error_t *c_err = nullptr;
hs_error_t err =
hs_expression_ext_info(ei.pattern, 0, &ei.ext, &info, &c_err);
ASSERT_EQ(HS_SUCCESS, err);
ASSERT_TRUE(info != nullptr);
ASSERT_TRUE(c_err == nullptr);
check_info(ei, info);
free(info);
}
// Check with hs_expression_ext_info function and a nullptr ext param, for
// cases where ext.flags == 0. Functionally identical to check_no_ext above.
TEST_P(ExprInfop, check_ext_null) {
const expected_info &ei = GetParam();
SCOPED_TRACE(ei.pattern);
if (ei.ext.flags) {
// This is an extparam test, skip it.
return;
}
hs_expr_info_t *info = nullptr;
hs_compile_error_t *c_err = nullptr;
hs_error_t err =
hs_expression_ext_info(ei.pattern, 0, nullptr, &info, &c_err);
ASSERT_EQ(HS_SUCCESS, err);
ASSERT_TRUE(info != nullptr);
ASSERT_TRUE(c_err == nullptr);
check_info(ei, info);
free(info);
}
static const hs_expr_ext NO_EXT_PARAM = { 0, 0, 0, 0 };
static const expected_info ei_test[] = {
{"abc", 3, 3, 0, 0, 0},
{"abc.*def", 6, UINT_MAX, 0, 0, 0},
{"abc|defghi", 3, 6, 0, 0, 0},
{"abc(def)?", 3, 6, 0, 0, 0},
{"abc(def){0,3}", 3, 12, 0, 0, 0},
{"abc(def){1,4}", 6, 15, 0, 0, 0},
{"", 0, 0, 0, 0, 0},
{"^", 0, 0, 0, 0, 0},
{"^\\b", 0, 0, 1, 0, 0},
{"\\b$", 0, 0, 1, 1, 1},
{"(?m)\\b$", 0, 0, 1, 1, 0},
{"\\A", 0, 0, 0, 0, 0},
{"\\z", 0, 0, 0, 1, 1},
{"\\Z", 0, 0, 1, 1, 1},
{"$", 0, 0, 1, 1, 1},
{"(?m)$", 0, 0, 1, 1, 0},
{"^foo", 3, 3, 0, 0, 0},
{"^foo.*bar", 6, UINT_MAX, 0, 0, 0},
{"^foo.*bar?", 5, UINT_MAX, 0, 0, 0},
{"^foo.*bar$", 6, UINT_MAX, 1, 1, 1},
{"^foobar$", 6, 6, 1, 1, 1},
{"foobar$", 6, 6, 1, 1, 1},
{"^.*foo", 3, UINT_MAX, 0, 0, 0},
{"foo\\b", 3, 3, 1, 1, 0},
{"foo.{1,13}bar", 7, 19, 0, 0, 0},
{"foo.{10,}bar", 16, UINT_MAX, 0, 0, 0},
{"foo.{0,10}bar", 6, 16, 0, 0, 0},
{"foo.{,10}bar", 12, 12, 0, 0, 0},
{"foo.{10}bar", 16, 16, 0, 0, 0},
{"(^|\n)foo", 3, 4, 0, 0, 0},
{"(^\n|)foo", 3, 4, 0, 0, 0},
{"(?m)^foo", 3, 3, 0, 0, 0},
{"\\bfoo", 3, 3, 0, 0, 0},
{"^\\bfoo", 3, 3, 0, 0, 0},
{"(?m)^\\bfoo", 3, 3, 0, 0, 0},
{"\\Bfoo", 3, 3, 0, 0, 0},
{"(foo|bar\\z)", 3, 3, 0, 1, 0},
{"(foo|bar)\\z", 3, 3, 0, 1, 1},
{"abc", NO_EXT_PARAM, 3, 3, 0, 0, 0},
{"abc.*def", NO_EXT_PARAM, 6, UINT_MAX, 0, 0, 0},
{"abc|defghi", NO_EXT_PARAM, 3, 6, 0, 0, 0},
{"abc(def)?", NO_EXT_PARAM, 3, 6, 0, 0, 0},
{"abc(def){0,3}", NO_EXT_PARAM, 3, 12, 0, 0, 0},
{"abc(def){1,4}", NO_EXT_PARAM, 6, 15, 0, 0, 0},
{"", NO_EXT_PARAM, 0, 0, 0, 0, 0},
{"^", NO_EXT_PARAM, 0, 0, 0, 0, 0},
{"^\\b", NO_EXT_PARAM, 0, 0, 1, 0, 0},
{"\\b$", NO_EXT_PARAM, 0, 0, 1, 1, 1},
{"(?m)\\b$", NO_EXT_PARAM, 0, 0, 1, 1, 0},
{"\\A", NO_EXT_PARAM, 0, 0, 0, 0, 0},
{"\\z", NO_EXT_PARAM, 0, 0, 0, 1, 1},
{"\\Z", NO_EXT_PARAM, 0, 0, 1, 1, 1},
{"$", NO_EXT_PARAM, 0, 0, 1, 1, 1},
{"(?m)$", NO_EXT_PARAM, 0, 0, 1, 1, 0},
{"^foo", NO_EXT_PARAM, 3, 3, 0, 0, 0},
{"^foo.*bar", NO_EXT_PARAM, 6, UINT_MAX, 0, 0, 0},
{"^foo.*bar?", NO_EXT_PARAM, 5, UINT_MAX, 0, 0, 0},
{"^foo.*bar$", NO_EXT_PARAM, 6, UINT_MAX, 1, 1, 1},
{"^foobar$", NO_EXT_PARAM, 6, 6, 1, 1, 1},
{"foobar$", NO_EXT_PARAM, 6, 6, 1, 1, 1},
{"^.*foo", NO_EXT_PARAM, 3, UINT_MAX, 0, 0, 0},
{"foo\\b", NO_EXT_PARAM, 3, 3, 1, 1, 0},
{"foo.{1,13}bar", NO_EXT_PARAM, 7, 19, 0, 0, 0},
{"foo.{10,}bar", NO_EXT_PARAM, 16, UINT_MAX, 0, 0, 0},
{"foo.{0,10}bar", NO_EXT_PARAM, 6, 16, 0, 0, 0},
{"foo.{,10}bar", NO_EXT_PARAM, 12, 12, 0, 0, 0},
{"foo.{10}bar", NO_EXT_PARAM, 16, 16, 0, 0, 0},
{"(^|\n)foo", NO_EXT_PARAM, 3, 4, 0, 0, 0},
{"(^\n|)foo", NO_EXT_PARAM, 3, 4, 0, 0, 0},
{"(?m)^foo", NO_EXT_PARAM, 3, 3, 0, 0, 0},
{"\\bfoo", NO_EXT_PARAM, 3, 3, 0, 0, 0},
{"^\\bfoo", NO_EXT_PARAM, 3, 3, 0, 0, 0},
{"(?m)^\\bfoo", NO_EXT_PARAM, 3, 3, 0, 0, 0},
{"\\Bfoo", NO_EXT_PARAM, 3, 3, 0, 0, 0},
{"(foo|bar\\z)", NO_EXT_PARAM, 3, 3, 0, 1, 0},
{"(foo|bar)\\z", NO_EXT_PARAM, 3, 3, 0, 1, 1},
// Some cases with extended parameters.
{"^abc.*def", {HS_EXT_FLAG_MAX_OFFSET, 0, 10, 0}, 6, 10, 0, 0, 0},
{"abc.*def", {HS_EXT_FLAG_MAX_OFFSET, 0, 10, 0}, 6, 10, 0, 0, 0},
{"abc.*def", {HS_EXT_FLAG_MIN_LENGTH, 0, 0, 100}, 100, UINT_MAX, 0, 0, 0},
{"abc.*def", {HS_EXT_FLAG_MIN_LENGTH, 0, 0, 5}, 6, UINT_MAX, 0, 0, 0},
};
INSTANTIATE_TEST_CASE_P(ExprInfo, ExprInfop, ValuesIn(ei_test));