Back to index

glibc  2.9
test-strpbrk.c
Go to the documentation of this file.
00001 /* Test and measure strpbrk functions.
00002    Copyright (C) 1999, 2002, 2003, 2005 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Written by Jakub Jelinek <jakub@redhat.com>, 1999.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #ifndef STRPBRK_RESULT
00022 # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL)
00023 # define RES_TYPE char *
00024 # define TEST_MAIN
00025 # include "test-string.h"
00026 
00027 typedef char *(*proto_t) (const char *, const char *);
00028 char *simple_strpbrk (const char *, const char *);
00029 char *stupid_strpbrk (const char *, const char *);
00030 
00031 IMPL (stupid_strpbrk, 0)
00032 IMPL (simple_strpbrk, 0)
00033 IMPL (strpbrk, 1)
00034 
00035 char *
00036 simple_strpbrk (const char *s, const char *rej)
00037 {
00038   const char *r;
00039   char c;
00040 
00041   while ((c = *s++) != '\0')
00042     for (r = rej; *r != '\0'; ++r)
00043       if (*r == c)
00044        return (char *) s - 1;
00045   return NULL;
00046 }
00047 
00048 char *
00049 stupid_strpbrk (const char *s, const char *rej)
00050 {
00051   size_t ns = strlen (s), nrej = strlen (rej);
00052   size_t i, j;
00053 
00054   for (i = 0; i < ns; ++i)
00055     for (j = 0; j < nrej; ++j)
00056       if (s[i] == rej[j])
00057        return (char *) s + i;
00058   return NULL;
00059 }
00060 #endif
00061 
00062 static void
00063 do_one_test (impl_t *impl, const char *s, const char *rej, RES_TYPE exp_res)
00064 {
00065   RES_TYPE res = CALL (impl, s, rej);
00066   if (res != exp_res)
00067     {
00068       error (0, 0, "Wrong result in function %s %p %p", impl->name,
00069             (void *) res, (void *) exp_res);
00070       ret = 1;
00071       return;
00072     }
00073 
00074   if (HP_TIMING_AVAIL)
00075     {
00076       hp_timing_t start __attribute ((unused));
00077       hp_timing_t stop __attribute ((unused));
00078       hp_timing_t best_time = ~ (hp_timing_t) 0;
00079       size_t i;
00080 
00081       for (i = 0; i < 32; ++i)
00082        {
00083          HP_TIMING_NOW (start);
00084          CALL (impl, s, rej);
00085          HP_TIMING_NOW (stop);
00086          HP_TIMING_BEST (best_time, start, stop);
00087        }
00088 
00089       printf ("\t%zd", (size_t) best_time);
00090     }
00091 }
00092 
00093 static void
00094 do_test (size_t align, size_t pos, size_t len)
00095 {
00096   size_t i;
00097   int c;
00098   RES_TYPE result;
00099   char *rej, *s;
00100 
00101   align &= 7;
00102   if (align + pos + 10 >= page_size || len > 240)
00103     return;
00104 
00105   rej = (char *) (buf2 + (random () & 255));
00106   s = (char *) (buf1 + align);
00107 
00108   for (i = 0; i < len; ++i)
00109     {
00110       rej[i] = random () & 255;
00111       if (!rej[i])
00112        rej[i] = random () & 255;
00113       if (!rej[i])
00114        rej[i] = 1 + (random () & 127);
00115     }
00116   rej[len] = '\0';
00117   for (c = 1; c <= 255; ++c)
00118     if (strchr (rej, c) == NULL)
00119       break;
00120 
00121   for (i = 0; i < pos; ++i)
00122     {
00123       s[i] = random () & 255;
00124       if (strchr (rej, s[i]))
00125        {
00126          s[i] = random () & 255;
00127          if (strchr (rej, s[i]))
00128            s[i] = c;
00129        }
00130     }
00131   s[pos] = rej[random () % (len + 1)];
00132   if (s[pos])
00133     {
00134       for (i = pos + 1; i < pos + 10; ++i)
00135        s[i] = random () & 255;
00136       s[i] = '\0';
00137     }
00138   result = STRPBRK_RESULT (s, pos);
00139 
00140   if (HP_TIMING_AVAIL)
00141     printf ("Length %4zd, alignment %2zd, rej len %2zd:", pos, align, len);
00142 
00143   FOR_EACH_IMPL (impl, 0)
00144     do_one_test (impl, s, rej, result);
00145 
00146   if (HP_TIMING_AVAIL)
00147     putchar ('\n');
00148 }
00149 
00150 static void
00151 do_random_tests (void)
00152 {
00153   size_t i, j, n, align, pos, len, rlen;
00154   RES_TYPE result;
00155   int c;
00156   unsigned char *p = buf1 + page_size - 512;
00157   unsigned char *rej;
00158 
00159   for (n = 0; n < ITERATIONS; n++)
00160     {
00161       align = random () & 15;
00162       pos = random () & 511;
00163       if (pos + align >= 511)
00164        pos = 510 - align - (random () & 7);
00165       len = random () & 511;
00166       if (pos >= len && (random () & 1))
00167        len = pos + 1 + (random () & 7);
00168       if (len + align >= 512)
00169        len = 511 - align - (random () & 7);
00170       if (random () & 1)
00171        rlen = random () & 63;
00172       else
00173        rlen = random () & 15;
00174       rej = buf2 + page_size - rlen - 1 - (random () & 7);
00175       for (i = 0; i < rlen; ++i)
00176        {
00177          rej[i] = random () & 255;
00178          if (!rej[i])
00179            rej[i] = random () & 255;
00180          if (!rej[i])
00181            rej[i] = 1 + (random () & 127);
00182        }
00183       rej[i] = '\0';
00184       for (c = 1; c <= 255; ++c)
00185        if (strchr ((char *) rej, c) == NULL)
00186          break;
00187       j = (pos > len ? pos : len) + align + 64;
00188       if (j > 512)
00189        j = 512;
00190 
00191       for (i = 0; i < j; i++)
00192        {
00193          if (i == len + align)
00194            p[i] = '\0';
00195          else if (i == pos + align)
00196            p[i] = rej[random () % (rlen + 1)];
00197          else if (i < align || i > pos + align)
00198            p[i] = random () & 255;
00199          else
00200            {
00201              p[i] = random () & 255;
00202              if (strchr ((char *) rej, p[i]))
00203               {
00204                 p[i] = random () & 255;
00205                 if (strchr ((char *) rej, p[i]))
00206                   p[i] = c;
00207               }
00208            }
00209        }
00210 
00211       result = STRPBRK_RESULT ((char *) (p + align), pos < len ? pos : len);
00212 
00213       FOR_EACH_IMPL (impl, 1)
00214        if (CALL (impl, (char *) (p + align), (char *) rej) != result)
00215          {
00216            error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %p != %p",
00217                  n, impl->name, align, rej, rlen, pos, len,
00218                  (void *) CALL (impl, (char *) (p + align), (char *) rej),
00219                  (void *) result);
00220            ret = 1;
00221          }
00222     }
00223 }
00224 
00225 int
00226 test_main (void)
00227 {
00228   size_t i;
00229 
00230   test_init ();
00231 
00232   printf ("%32s", "");
00233   FOR_EACH_IMPL (impl, 0)
00234     printf ("\t%s", impl->name);
00235   putchar ('\n');
00236 
00237   for (i = 0; i < 32; ++i)
00238     {
00239       do_test (0, 512, i);
00240       do_test (i, 512, i);
00241     }
00242 
00243   for (i = 1; i < 8; ++i)
00244     {
00245       do_test (0, 16 << i, 4);
00246       do_test (i, 16 << i, 4);
00247     }
00248 
00249   for (i = 1; i < 8; ++i)
00250     do_test (i, 64, 10);
00251 
00252   for (i = 0; i < 64; ++i)
00253     do_test (0, i, 6);
00254 
00255   do_random_tests ();
00256   return ret;
00257 }
00258 
00259 #include "../test-skeleton.c"