test_regex.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /*! \file
3  * \brief Test of \c posix_regcomp() and \c posix_regexec() implementation
4  *
5  * Copyright (c) 2015-2020 by the developers. See the LICENSE file for details.
6  */
7 
8 
9 /* ========================================================================== */
10 /* Include headers */
11 
12 #include "posix.h" /* Include this first because of feature test macros */
13 
14 #include <stdio.h>
15 #include <string.h>
16 
17 #include "config.h"
18 
19 #include "test.h"
20 #include "test_regex.h"
21 
22 
23 /* ========================================================================== */
24 /*! \addtogroup TEST */
25 /*! @{ */
26 
27 
28 /* ========================================================================== */
29 /* Print error message if system failed to compile a regular expression
30  *
31  * \param[in] code Error code
32  * \param[in] ere Pointer to compiled ERE
33  */
34 
35 #if CFG_USE_CLB || CFG_USE_XSI
36 static void print_ere_error(int code, posix_regex_t* ere)
37 {
38  size_t len;
39  char* buf = NULL;
40 
41 # if CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI
42  /* Don't use NLS for error messages on stderr */
43  posix_setlocale(POSIX_LC_MESSAGES, "POSIX");
44 # endif /* CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI */
45  len = posix_regerror(code, ere, buf, 0);
46  buf = (char*) posix_malloc(len);
47  if(NULL == buf)
48  {
49  fprintf(stderr, TEST_TAB "Out of memory\n");
50  }
51  else
52  {
53  posix_regerror(code, ere, buf, len);
54  fprintf(stderr, TEST_TAB "%s\n", buf);
55  posix_free((void*) buf);
56  }
57 # if CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI
58  posix_setlocale(POSIX_LC_MESSAGES, "");
59 # endif /* CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI */
60 }
61 #endif /* CFG_USE_CLB || CFG_USE_XSI */
62 
63 
64 /* ========================================================================== */
65 /*! \brief Test \c posix_regcomp() and \c posix_regexec() implementation
66  *
67  * The following cases are tested:
68  * - Single field with arbitrary characters
69  * - Literal dot character
70  * - Bracket expression (first element)
71  * - Bracket expression (second element)
72  * - Start and end of line
73  *
74  * \return
75  * - \c EXIT_SUCCESS on success
76  * - \c EXIT_FAILURE on error
77  */
78 
79 int test_regex(void)
80 {
81  int res = POSIX_EXIT_SUCCESS;
82 #if CFG_USE_CLB || CFG_USE_XSI
83 # define TS_NUM (size_t) 5 /* Number of patterns and test strings */
84  static const char* pat[TS_NUM] = {
85  "This.*string",
86  "This \\. is a dot",
87  "Bracket expression: [Xa]",
88  "Bracket expression: [Xa]",
89  "^Test$"
90  };
91  static const char* ts[TS_NUM] =
92  {
93  "This is an ASCII string",
94  "This . is a dot",
95  "Bracket expression: X",
96  "Bracket expression: a",
97  "Test"
98  };
99  size_t i;
100  posix_regex_t ere[TS_NUM];
101  int rv;
102 
103  /* Compile extended regular expression */
104  for(i = 0; i < TS_NUM; ++i)
105  {
106  /* Compile regular expression if required */
107  rv = posix_regcomp(&ere[i], pat[i], POSIX_REG_EXTENDED | POSIX_REG_NOSUB);
108  if(rv)
109  {
110  print_error("'posix_regcomp()' reported error");
111  fprintf(stderr, TEST_TAB "Pattern: \"%s\"\n", pat[i]);
112  print_ere_error(rv, &ere[i]);
113  res = POSIX_EXIT_FAILURE;
114  break;
115  }
116  }
117 
118  if(POSIX_EXIT_SUCCESS == res)
119  {
120  /* Check extended regular expression matching */
121  for(i = 0; i < TS_NUM; ++i)
122  {
123  rv = posix_regexec(&ere[i], ts[i], 0, NULL, 0);
124  if(rv)
125  {
126  print_error("'posix_regexec()' reported error");
127  fprintf(stderr, TEST_TAB "Pattern: \"%s\"\n", pat[i]);
128  fprintf(stderr, TEST_TAB "String : \"%s\"\n", ts[i]);
129  print_ere_error(rv, &ere[i]);
130  res = POSIX_EXIT_FAILURE;
131  break;
132  }
133  }
134  /* Release ressourced for regular expressions */
135  for(i = 0; i < TS_NUM; ++i) { posix_regfree(&ere[i]); }
136  }
137 #else /* CFG_USE_CLB || CFG_USE_XSI */
138  /* Not supported */
139  res = POSIX_EXIT_FAILURE;
140 #endif /* CFG_USE_CLB || CFG_USE_XSI */
141 
142  return(res);
143 }
144 
145 
146 /*! @} */
147 
148 /* EOF */
TEST_TAB
#define TEST_TAB
Tabulator to indent messages from test programs.
Definition: test.h:13
test_regex
int test_regex(void)
Test posix_regcomp() and posix_regexec() implementation.
Definition: test_regex.c:79
print_error
void print_error(const char *)
Print error message.
Definition: main.cxx:276

Generated at 2024-04-27 using  doxygen