tst-regex2.c 5.7 KB

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