tst-regex2.c 5.4 KB

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