tst-regex2.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #define _GNU_SOURCE 1
  2. #include <fcntl.h>
  3. #include <locale.h>
  4. #include <regex.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/stat.h>
  9. #include <sys/time.h>
  10. #include <time.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. static int
  14. do_test(void)
  15. {
  16. #if defined(__GLIBC__) || defined(__UCLIBC__)
  17. static const char *pat[] = {
  18. ".?.?.?.?.?.?.?Log\\.13",
  19. "(.?)(.?)(.?)(.?)(.?)(.?)(.?)Log\\.13",
  20. "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))"
  21. "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))"
  22. "((((((((((.?))))))))))Log\\.13"
  23. };
  24. char *buf, *string;
  25. const char *fname = "tst-regex2.dat";
  26. struct stat st;
  27. unsigned len;
  28. int testno;
  29. int exitcode = 0;
  30. int fd = open(fname, O_RDONLY);
  31. if (fd < 0) {
  32. printf("Couldn't open %s: %s\n", fname, strerror(errno));
  33. return 1;
  34. }
  35. if (fstat(fd, &st) < 0) {
  36. printf("Couldn't fstat %s: %s\n", fname, strerror(errno));
  37. return 1;
  38. }
  39. len = st.st_size;
  40. string = buf = malloc(len + 1);
  41. if (buf == NULL) {
  42. printf("Couldn't allocate %u bytes\n", len + 1);
  43. return 1;
  44. }
  45. if (read(fd, buf, st.st_size) != (ssize_t) st.st_size) {
  46. printf("Couldn't read %s\n", fname);
  47. return 1;
  48. }
  49. close(fd);
  50. buf[len] = '\0';
  51. #if defined __UCLIBC_HAS_XLOCALE__ || !defined __UCLIBC__
  52. setlocale(LC_ALL, "de_DE.UTF-8");
  53. #endif
  54. for (testno = 0; testno < 2; ++testno) {
  55. int i;
  56. for (i = 0; i < sizeof(pat) / sizeof(pat[0]); ++i) {
  57. struct timeval start, stop;
  58. regex_t rbuf;
  59. int err;
  60. printf("test %d pattern %d '%s'\n", testno, i, pat[i]);
  61. gettimeofday(&start, NULL);
  62. err = regcomp(&rbuf, pat[i],
  63. REG_EXTENDED | (testno ? REG_NOSUB : 0));
  64. if (err != 0) {
  65. char errstr[300];
  66. regerror(err, &rbuf, errstr, sizeof(errstr));
  67. puts(errstr);
  68. exitcode = 1;
  69. goto contin1;
  70. }
  71. regmatch_t pmatch[71];
  72. err = regexec(&rbuf, string, 71, pmatch, 0);
  73. if (err == REG_NOMATCH) {
  74. puts("regexec failed");
  75. exitcode = 1;
  76. goto contin1;
  77. }
  78. if (testno == 0) {
  79. if (pmatch[0].rm_eo != pmatch[0].rm_so + 13
  80. || pmatch[0].rm_eo > len
  81. || pmatch[0].rm_so < len - 100
  82. || strncmp(string + pmatch[0].rm_so,
  83. " ChangeLog.13 for earlier changes",
  84. sizeof " ChangeLog.13 for earlier changes" - 1
  85. ) != 0
  86. ) {
  87. puts("regexec without REG_NOSUB did not find the correct match");
  88. exitcode = 1;
  89. goto contin1;
  90. }
  91. if (i > 0) {
  92. int j, k, l;
  93. for (j = 0, l = 1; j < 7; ++j) {
  94. for (k = 0; k < (i == 1 ? 1 : 10); ++k, ++l) {
  95. if (pmatch[l].rm_so != pmatch[0].rm_so + j
  96. || pmatch[l].rm_eo != pmatch[l].rm_so + 1
  97. ) {
  98. printf("pmatch[%d] incorrect\n", l);
  99. exitcode = 1;
  100. goto contin1;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. gettimeofday(&stop, NULL);
  107. stop.tv_sec -= start.tv_sec;
  108. if (stop.tv_usec < start.tv_usec) {
  109. stop.tv_sec--;
  110. stop.tv_usec += 1000000;
  111. }
  112. stop.tv_usec -= start.tv_usec;
  113. printf(" %lu.%06lus\n", (unsigned long) stop.tv_sec,
  114. (unsigned long) stop.tv_usec);
  115. contin1:
  116. regfree(&rbuf);
  117. }
  118. }
  119. for (testno = 2; testno < 4; ++testno) {
  120. int i;
  121. for (i = 0; i < sizeof(pat) / sizeof(pat[0]); ++i) {
  122. struct timeval start, stop;
  123. struct re_pattern_buffer rpbuf;
  124. struct re_registers regs;
  125. const char *s;
  126. int match;
  127. printf("test %d pattern %d '%s'\n", testno, i, pat[i]);
  128. gettimeofday(&start, NULL);
  129. re_set_syntax(RE_SYNTAX_POSIX_EGREP
  130. | (testno == 3 ? RE_NO_SUB : 0));
  131. memset(&rpbuf, 0, sizeof(rpbuf));
  132. s = re_compile_pattern(pat[i], strlen(pat[i]), &rpbuf);
  133. if (s != NULL) {
  134. printf("%s\n", s);
  135. exitcode = 1;
  136. goto contin2;
  137. }
  138. memset(&regs, 0, sizeof(regs));
  139. match = re_search(&rpbuf, string, len, 0, len, &regs);
  140. if (match < 0) {
  141. printf("re_search failed (err:%d)\n", match);
  142. exitcode = 1;
  143. goto contin2;
  144. }
  145. if (match + 13 > len) {
  146. printf("re_search: match+13 > len (%d > %d)\n", match + 13, len);
  147. exitcode = 1;
  148. goto contin2;
  149. }
  150. if (match < len - 100) {
  151. printf("re_search: match < len-100 (%d < %d)\n", match, len - 100);
  152. exitcode = 1;
  153. goto contin2;
  154. }
  155. if (strncmp(string + match, " ChangeLog.13 for earlier changes",
  156. sizeof(" ChangeLog.13 for earlier changes") - 1
  157. ) != 0
  158. ) {
  159. printf("re_search did not find the correct match"
  160. "(found '%s' instead)\n", string + match);
  161. exitcode = 1;
  162. goto contin2;
  163. }
  164. if (testno == 2) {
  165. int expected = 72;
  166. if (i == 0)
  167. expected = 2;
  168. if (i == 1)
  169. expected = 9;
  170. if (regs.num_regs != expected) {
  171. printf("incorrect num_regs %d, expected %d\n", regs.num_regs, expected);
  172. exitcode = 1;
  173. goto contin2;
  174. }
  175. if (regs.start[0] != match || regs.end[0] != match + 13) {
  176. printf("incorrect regs.{start,end}[0] = { %d, %d },"
  177. " expected { %d, %d }\n",
  178. regs.start[0], regs.end[0],
  179. match, match + 13
  180. );
  181. exitcode = 1;
  182. goto contin2;
  183. }
  184. if (regs.start[regs.num_regs - 1] != -1
  185. || regs.end[regs.num_regs - 1] != -1
  186. ) {
  187. printf("incorrect regs.{start,end}[num_regs - 1] = { %d, %d },"
  188. " expected { -1, -1 }\n",
  189. regs.start[regs.num_regs - 1], regs.end[regs.num_regs - 1]
  190. );
  191. exitcode = 1;
  192. goto contin2;
  193. }
  194. if (i > 0) {
  195. int j, k, l;
  196. for (j = 0, l = 1; j < 7; ++j) {
  197. for (k = 0; k < (i == 1 ? 1 : 10); ++k, ++l) {
  198. if (regs.start[l] != match + j
  199. || regs.end[l] != match + j + 1
  200. ) {
  201. printf("incorrect regs.{start,end}[%d] = { %d, %d },"
  202. " expected { %d, %d }\n",
  203. l,
  204. regs.start[l], regs.end[l],
  205. match + j, match + j + 1
  206. );
  207. exitcode = 1;
  208. goto contin2;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. gettimeofday(&stop, NULL);
  215. stop.tv_sec -= start.tv_sec;
  216. if (stop.tv_usec < start.tv_usec) {
  217. stop.tv_sec--;
  218. stop.tv_usec += 1000000;
  219. }
  220. stop.tv_usec -= start.tv_usec;
  221. printf(" %lu.%06lus\n", (unsigned long) stop.tv_sec,
  222. (unsigned long) stop.tv_usec);
  223. contin2:
  224. regfree(&rpbuf);
  225. }
  226. }
  227. return exitcode;
  228. #else
  229. return 23;
  230. #endif
  231. }
  232. #define TIMEOUT 100
  233. #define TEST_FUNCTION do_test()
  234. #include "../test-skeleton.c"