diff --git a/tests/test-str-source.c b/tests/test-str-source.c index adf965b..f507e8d 100644 --- a/tests/test-str-source.c +++ b/tests/test-str-source.c @@ -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. @@ -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; } @@ -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]; @@ -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); @@ -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; }