Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error messages to some silent failure cases in test-str-source.c #113

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 115 additions & 23 deletions tests/test-str-source.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/*
test-str-source.c - Sample program for using tre_reguexec()
It also serves as a sanity check for correct
linkage when multiple variants of the TRE library
are present.

This software is released under a BSD-style license.
See the file LICENSE for details and copyright.
Expand Down Expand Up @@ -78,11 +81,15 @@ make_str_source(const char *str)

s = calloc(1, sizeof(*s));
if (!s)
return NULL;
{
fprintf(stderr,"Could not calloc(1,sizeof(tre_str_source)\n");
return NULL;
}

ctx = malloc(sizeof(str_handler_ctx));
if (!ctx)
{
fprintf(stderr,"Could not malloc(sizeof(str_handler_ctx)\n");
free(s);
return NULL;
}
Expand All @@ -109,6 +116,7 @@ free_str_source(tre_str_source *s)
static void
test_reguexec(const char *str, const char *regex)
{
reg_errcode_t ec;
regex_t preg;
tre_str_source *source;
regmatch_t pmatch[5];
Expand All @@ -117,9 +125,17 @@ test_reguexec(const char *str, const char *regex)
if (!source)
return;

tre_regcomp(&preg, regex, REG_EXTENDED);
if (tre_reguexec(&preg, source, elementsof(pmatch), pmatch, 0) == 0)
ec = tre_regcomp(&preg, regex, REG_EXTENDED);
if (ec != REG_OK)
{
fprintf(output_fd,"Pattern {%s} failed to compile, err code 0x%08x\n", regex, (unsigned)ec);
}
else if ((ec = tre_reguexec(&preg, source, elementsof(pmatch), pmatch, 0)) == 0)
fprintf(output_fd,"Match: %d - %d\n", (int)pmatch[0].rm_so, (int)pmatch[0].rm_eo);
else
{
fprintf(output_fd,"Match pattern {%s} against string {%s} failed, err code 0x%08x\n", regex, str, (unsigned)ec);
}

free_str_source(source);
tre_regfree(&preg);
Expand All @@ -129,33 +145,109 @@ int
main(int argc, char **argv)
{
int ch;
int rc;
int query;
int int_res;
int desired_config;
int config_mismatch = 0;
char *char_res;
output_fd = stdout;
while (EOF != (ch = getopt(argc,argv,"o:"))) {
switch (ch) {
case 'o':
if (NULL == (output_fd = fopen(optarg,"w"))) {
fprintf(stderr,"Could not open {%s} for output, quitting\n",optarg);
exit(1);
}
break;
default:
fprintf(stderr,"Invalid command line option '-%c', quitting\n",ch);
if (NULL != output_fd && stdout != output_fd) {
fclose(output_fd);
output_fd = NULL;
}
exit(1);
while ((ch = getopt(argc,argv,"o:") != EOF))
{
switch (ch)
{
case 'o':
if ((output_fd = fopen(optarg,"w")) == NULL)
{
fprintf(stderr,"Could not open {%s} for output, quitting\n", optarg);
exit(1);
}
break;
default:
fprintf(stderr,"Invalid command line option '-%c', quitting\n", ch);
if (output_fd != NULL && stdout != output_fd)
{
fclose(output_fd);
output_fd = NULL;
}
exit(1);
}
}

/* Display the version and configuation of the TRE library we are using. */
fprintf(output_fd,"Using TRE library version: %s\n", tre_version());
fprintf(output_fd," test compiled for version: %s\n", TRE_VERSION);

query = (int) TRE_CONFIG_APPROX;
int_res = -1;
rc = tre_config(query,&int_res);
#ifdef TRE_APPROX
desired_config = 1;
#else
desired_config = 0;
#endif
fprintf(output_fd," TRE_CONFIG_APPROX: library %d (rc=%d) test compiled for %d\n", int_res, rc, desired_config);
config_mismatch |= (int_res != desired_config);

query = (int) TRE_CONFIG_WCHAR;
int_res = -1;
rc = tre_config(query,&int_res);
#ifdef TRE_WCHAR
desired_config = 1;
#else
desired_config = 0;
#endif
fprintf(output_fd," TRE_CONFIG_WCHAR: library %d (rc=%d) test compiled for %d\n", int_res, rc, desired_config);
config_mismatch |= (int_res != desired_config);

query = (int) TRE_CONFIG_MULTIBYTE;
int_res = -1;
rc = tre_config(query,&int_res);
#ifdef TRE_MULTIBYTE
desired_config = 1;
#else
desired_config = 0;
#endif
fprintf(output_fd," TRE_CONFIG_MULTIBYTE: library %d (rc=%d) test compiled for %d\n", int_res, rc, desired_config);
config_mismatch |= (int_res != desired_config);

query = (int) TRE_CONFIG_SYSTEM_ABI;
int_res = -1;
rc = tre_config(query,&int_res);
#ifdef TRE_CONFIG_SYSTEM_ABI
desired_config = 1;
#else
desired_config = 0;
#endif
fprintf(output_fd," TRE_CONFIG_SYSTEM_ABI: library %d (rc=%d) test compiled for %d\n", int_res, rc, desired_config);
config_mismatch |= (int_res != desired_config);

query = (int) TRE_CONFIG_VERSION;
char_res = "---";
rc = tre_config(query,&char_res);
fprintf(output_fd," TRE_CONFIG_VERSION: {%s} (rc=%d)\n", char_res, rc);

if (config_mismatch)
{
fprintf(output_fd,"WARNING: The configuration this test program was compiled with is not the same");
fprintf(output_fd,"as the configuration of the libtre being used, some tests may be incorrect.\n");
}
}

fprintf(output_fd,"------------------ Test 1: should Match: 6 - 12\n");
test_reguexec("xfoofofoofoo","(foo)\\1");
fprintf(output_fd,"------------------ Test 2: should Match: 0 - 6\n");
test_reguexec("catcat","(cat|dog)\\1");
fprintf(output_fd,"------------------ Test 3: should not match\n");
test_reguexec("catdog","(cat|dog)\\1");
fprintf(output_fd,"------------------ Test 4: should Match: 0 - 6\n");
test_reguexec("dogdog","(cat|dog)\\1");
fprintf(output_fd,"------------------ Test 5: should not match\n");
test_reguexec("dogcat","(cat|dog)\\1");

if (NULL != output_fd && stdout != output_fd) {
fclose(output_fd);
output_fd = NULL;
}
if (NULL != output_fd && stdout != output_fd)
{
fclose(output_fd);
output_fd = NULL;
}
return 0;
}
Loading