?? psx-interf.c
字號:
/* psx-interf.c: test POSIX interface. */#include <string.h>#include <assert.h>#include "test.h"#define ERROR_CODE_LENGTH 20#define TEST_ERRBUF_SIZE 15 void test_compile ();/* ANSWER should be at least ERROR_CODE_LENGTH long. */static char *get_error_string (error_code, answer) int error_code; char answer[];{ switch (error_code) { case 0: strcpy (answer, "No error"); break; case REG_NOMATCH: strcpy (answer, "REG_NOMATCH"); break; case REG_BADPAT: strcpy (answer, "REG_BADPAT"); break; case REG_EPAREN: strcpy (answer, "REG_EPAREN"); break; case REG_ESPACE: strcpy (answer, "REG_ESPACE"); break; case REG_ECOLLATE: strcpy (answer, "REG_ECOLLATE"); break; case REG_ECTYPE: strcpy (answer, "REG_ECTYPE"); break; case REG_EESCAPE: strcpy (answer, "REG_EESCAPE"); break; case REG_ESUBREG: strcpy (answer, "REG_ESUBREG"); break; case REG_EBRACK: strcpy (answer, "REG_EBRACK"); break; case REG_EBRACE: strcpy (answer, "REG_EBRACE"); break; case REG_BADBR: strcpy (answer, "REG_BADBR"); break; case REG_ERANGE: strcpy (answer, "REG_ERANGE"); break; case REG_BADRPT: strcpy (answer, "REG_BADRPT"); break; case REG_EEND: strcpy (answer, "REG_EEND"); break; default: strcpy (answer, "Bad error code"); } return answer;}/* I don't think we actually need to initialize all these things. --karl */voidinit_pattern_buffer (pattern_buffer_ptr) regex_t *pattern_buffer_ptr;{ pattern_buffer_ptr->buffer = NULL; pattern_buffer_ptr->allocated = 0; pattern_buffer_ptr->used = 0; pattern_buffer_ptr->fastmap = NULL; pattern_buffer_ptr->fastmap_accurate = 0; pattern_buffer_ptr->translate = NULL; pattern_buffer_ptr->can_be_null = 0; pattern_buffer_ptr->re_nsub = 0; pattern_buffer_ptr->no_sub = 0; pattern_buffer_ptr->not_bol = 0; pattern_buffer_ptr->not_eol = 0;}voidtest_compile (valid_pattern, error_code_expected, pattern, pattern_buffer_ptr, cflags) unsigned valid_pattern; int error_code_expected; const char *pattern; regex_t *pattern_buffer_ptr; int cflags;{ int error_code_returned; boolean error = false; char errbuf[TEST_ERRBUF_SIZE]; init_pattern_buffer (pattern_buffer_ptr); error_code_returned = regcomp (pattern_buffer_ptr, pattern, cflags); if (valid_pattern && error_code_returned) { printf ("\nShould have been a valid pattern but wasn't.\n"); regerror (error_code_returned, pattern_buffer_ptr, errbuf, TEST_ERRBUF_SIZE); printf ("%s", errbuf); error = true; } if (!valid_pattern && !error_code_returned) { printf ("\n\nInvalid pattern compiled as valid:\n"); error = true; } if (error_code_returned != error_code_expected) { char expected_error_string[ERROR_CODE_LENGTH]; char returned_error_string[ERROR_CODE_LENGTH]; get_error_string (error_code_expected, expected_error_string), get_error_string (error_code_returned, returned_error_string); printf (" Expected error code %s but got `%s'.\n", expected_error_string, returned_error_string); error = true; } if (error) print_pattern_info (pattern, pattern_buffer_ptr);}static voidtest_nsub (sub_count, pattern, cflags) unsigned sub_count; char *pattern; int cflags;{ regex_t pattern_buffer; test_compile (1, 0, pattern, &pattern_buffer, cflags); if (pattern_buffer.re_nsub != sub_count) { printf ("\nShould have counted %d subexpressions but counted %d \instead.\n", sub_count, pattern_buffer.re_nsub); } regfree (&pattern_buffer);} static voidtest_regcomp (){ regex_t pattern_buffer; int cflags = 0; printf ("\nStarting regcomp tests.\n"); cflags = 0; test_compile (0, REG_ESUBREG, "\\(a\\)\\2", &pattern_buffer, cflags); test_compile (0, REG_EBRACE, "a\\{", &pattern_buffer, cflags); test_compile (0, REG_BADBR, "a\\{-1\\}", &pattern_buffer, cflags); test_compile (0, REG_EBRACE, "a\\{", &pattern_buffer, cflags); test_compile (0, REG_EBRACE, "a\\{1", &pattern_buffer, cflags); cflags = REG_EXTENDED; test_compile (0, REG_ECTYPE, "[[:alpo:]]", &pattern_buffer, cflags); test_compile (0, REG_EESCAPE, "\\", &pattern_buffer, cflags); test_compile (0, REG_EBRACK, "[a", &pattern_buffer, cflags); test_compile (0, REG_EPAREN, "(", &pattern_buffer, cflags); test_compile (0, REG_ERANGE, "[z-a]", &pattern_buffer, cflags); test_nsub (1, "(a)", cflags); test_nsub (2, "((a))", cflags); test_nsub (2, "(a)(b)", cflags); cflags = REG_EXTENDED | REG_NOSUB; test_nsub (1, "(a)", cflags); regfree (&pattern_buffer); printf ("\nFinished regcomp tests.\n");}static voidfill_pmatch (pmatch, start0, end0, start1, end1, start2, end2) regmatch_t pmatch[]; regoff_t start0, end0, start1, end1, start2, end2;{ pmatch[0].rm_so = start0; pmatch[0].rm_eo = end0; pmatch[1].rm_so = start1; pmatch[1].rm_eo = end1; pmatch[2].rm_so = start2; pmatch[2].rm_eo = end2;}static voidtest_pmatch (pattern, string, nmatch, pmatch, correct_pmatch, cflags) char *pattern; char *string; unsigned nmatch; regmatch_t pmatch[]; regmatch_t correct_pmatch[]; int cflags;{ regex_t pattern_buffer; unsigned this_match; int error_code_returned; boolean found_nonmatch = false; test_compile (1, 0, pattern, &pattern_buffer, cflags); error_code_returned = regexec (&pattern_buffer, string, nmatch, pmatch, 0); if (error_code_returned == REG_NOMATCH) printf ("Matching failed in test_pmatch.\n"); else { for (this_match = 0; this_match < nmatch; this_match++) { if (pmatch[this_match].rm_so != correct_pmatch[this_match].rm_so) { if (found_nonmatch == false) printf ("\n"); printf ("Pmatch start %d wrong: was %d when should have \been %d.\n", this_match, pmatch[this_match].rm_so, correct_pmatch[this_match].rm_so); found_nonmatch = true; } if (pmatch[this_match].rm_eo != correct_pmatch[this_match].rm_eo) { if (found_nonmatch == false) printf ("\n"); printf ("Pmatch end %d wrong: was %d when should have been \%d.\n", this_match, pmatch[this_match].rm_eo, correct_pmatch[this_match].rm_eo); found_nonmatch = true; } } if (found_nonmatch) { printf (" The number of pmatches requested was: %d.\n", nmatch); printf (" The string to match was: `%s'.\n", string); print_pattern_info (pattern, &pattern_buffer); } } /* error_code_returned == REG_NOMATCH */ regfree (&pattern_buffer);}static voidtest_eflags (must_match_bol, must_match_eol, pattern, string, cflags, eflags) boolean must_match_bol; boolean must_match_eol; char *pattern; char *string; int cflags; int eflags;{ regex_t pattern_buffer; int error_code_returned; boolean was_error = false; test_compile (1, 0, pattern, &pattern_buffer, cflags); error_code_returned = regexec (&pattern_buffer, string, 0, 0, eflags); if (error_code_returned == REG_NOMATCH) { /* If wasn't true that both 1) the anchored part of the pattern had to match this string and 2) this string was a proper substring... */ if (!( (must_match_bol && (eflags & REG_NOTBOL)) || (must_match_eol && (eflags & REG_NOTEOL)) )) { printf ("\nEflags test failed: didn't match when should have.\n"); was_error = true; } } else /* We got a match. */ { /* If wasn't true that either 1) the anchored part of the pattern didn't have to match this string or 2) this string wasn't a proper substring... */ if ((must_match_bol == (eflags & REG_NOTBOL)) || (must_match_eol == (eflags & REG_NOTEOL))) { printf ("\nEflags test failed: matched when shouldn't have.\n"); was_error = true; } } if (was_error) { printf (" The string to match was: `%s'.\n", string); print_pattern_info (pattern, &pattern_buffer); if (eflags & REG_NOTBOL) printf (" The eflag REG_BOL was set.\n"); if (eflags & REG_NOTEOL) printf (" The eflag REG_EOL was set.\n"); } regfree (&pattern_buffer);}static voidtest_ignore_case (should_match, pattern, string, cflags) boolean should_match; char *pattern; char *string; int cflags;{ regex_t pattern_buffer; int error_code_returned; test_compile (1, 0, pattern, &pattern_buffer, cflags); error_code_returned = regexec (&pattern_buffer, string, 0, 0, 0);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -