tst-regex2.c 6.0 KB

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