tst-regex2.c 5.5 KB

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