Back to index

glibc  2.9
test-strcat.c
Go to the documentation of this file.
00001 /* Test and measure strcat 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 #define TEST_MAIN
00022 #include "test-string.h"
00023 
00024 typedef char *(*proto_t) (char *, const char *);
00025 char *simple_strcat (char *, const char *);
00026 
00027 IMPL (simple_strcat, 0)
00028 IMPL (strcat, 1)
00029 
00030 char *
00031 simple_strcat (char *dst, const char *src)
00032 {
00033   char *ret = dst;
00034   while (*dst++ != '\0');
00035   --dst;
00036   while ((*dst++ = *src++) != '\0');
00037   return ret;
00038 }
00039 
00040 static void
00041 do_one_test (impl_t *impl, char *dst, const char *src)
00042 {
00043   size_t k = strlen (dst);
00044   if (CALL (impl, dst, src) != dst)
00045     {
00046       error (0, 0, "Wrong result in function %s %p %p", impl->name,
00047             CALL (impl, dst, src), dst);
00048       ret = 1;
00049       return;
00050     }
00051 
00052   if (strcmp (dst + k, src) != 0)
00053     {
00054       error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
00055             impl->name, dst, src);
00056       ret = 1;
00057       return;
00058     }
00059 
00060   if (HP_TIMING_AVAIL)
00061     {
00062       hp_timing_t start __attribute ((unused));
00063       hp_timing_t stop __attribute ((unused));
00064       hp_timing_t best_time = ~ (hp_timing_t) 0;
00065       size_t i;
00066 
00067       for (i = 0; i < 32; ++i)
00068        {
00069          dst[k] = '\0';
00070          HP_TIMING_NOW (start);
00071          CALL (impl, dst, src);
00072          HP_TIMING_NOW (stop);
00073          HP_TIMING_BEST (best_time, start, stop);
00074        }
00075 
00076       printf ("\t%zd", (size_t) best_time);
00077     }
00078 }
00079 
00080 static void
00081 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int max_char)
00082 {
00083   size_t i;
00084   char *s1, *s2;
00085 
00086   align1 &= 7;
00087   if (align1 + len1 >= page_size)
00088     return;
00089 
00090   align2 &= 7;
00091   if (align2 + len1 + len2 >= page_size)
00092     return;
00093 
00094   s1 = (char *) (buf1 + align1);
00095   s2 = (char *) (buf2 + align2);
00096 
00097   for (i = 0; i < len1; ++i)
00098     s1[i] = 32 + 23 * i % (max_char - 32);
00099   s1[len1] = '\0';
00100 
00101   for (i = 0; i < len2; i++)
00102     s2[i] = 32 + 23 * i % (max_char - 32);
00103 
00104   if (HP_TIMING_AVAIL)
00105     printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len1, len2, align1, align2);
00106 
00107   FOR_EACH_IMPL (impl, 0)
00108     {
00109       s2[len2] = '\0';
00110       do_one_test (impl, s2, s1);
00111     }
00112 
00113   if (HP_TIMING_AVAIL)
00114     putchar ('\n');
00115 }
00116 
00117 static void
00118 do_random_tests (void)
00119 {
00120   size_t i, j, n, align1, align2, len1, len2;
00121   unsigned char *p1 = buf1 + page_size - 512;
00122   unsigned char *p2 = buf2 + page_size - 512;
00123   unsigned char *res;
00124 
00125   for (n = 0; n < ITERATIONS; n++)
00126     {
00127       align1 = random () & 31;
00128       if (random () & 1)
00129        align2 = random () & 31;
00130       else
00131        align2 = align1 + (random () & 24);
00132       len1 = random () & 511;
00133       if (len1 + align2 > 512)
00134        len2 = random () & 7;
00135       else
00136        len2 = (512 - len1 - align2) * (random () & (1024 * 1024 - 1))
00137               / (1024 * 1024);
00138       j = align1;
00139       if (align2 + len2 > j)
00140        j = align2 + len2;
00141       if (len1 + j >= 511)
00142        len1 = 510 - j - (random () & 7);
00143       if (len1 >= 512)
00144        len1 = 0;
00145       if (align1 + len1 < 512 - 8)
00146        {
00147          j = 510 - align1 - len1 - (random () & 31);
00148          if (j > 0 && j < 512)
00149            align1 += j;
00150        }
00151       j = len1 + align1 + 64;
00152       if (j > 512)
00153        j = 512;
00154       for (i = 0; i < j; i++)
00155        {
00156          if (i == len1 + align1)
00157            p1[i] = 0;
00158          else
00159            {
00160              p1[i] = random () & 255;
00161              if (i >= align1 && i < len1 + align1 && !p1[i])
00162               p1[i] = (random () & 127) + 3;
00163            }
00164        }
00165       for (i = 0; i < len2; i++)
00166        {
00167          buf1[i] = random () & 255;
00168          if (!buf1[i])
00169            buf1[i] = (random () & 127) + 3;
00170        }
00171       buf1[len2] = 0;
00172 
00173       FOR_EACH_IMPL (impl, 1)
00174        {
00175          memset (p2 - 64, '\1', align2 + 64);
00176          memset (p2 + align2 + len2 + 1, '\1', 512 - align2 - len2 - 1);
00177          memcpy (p2 + align2, buf1, len2 + 1);
00178          res = (unsigned char *) CALL (impl, (char *) (p2 + align2),
00179                                    (char *) (p1 + align1));
00180          if (res != p2 + align2)
00181            {
00182              error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd %zd) %p != %p",
00183                    n, impl->name, align1, align2, len1, len2, res,
00184                    p2 + align2);
00185              ret = 1;
00186            }
00187          for (j = 0; j < align2 + 64; ++j)
00188            {
00189              if (p2[j - 64] != '\1')
00190               {
00191                 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd, %zd)",
00192                       n, impl->name, align1, align2, len1, len2);
00193                 ret = 1;
00194                 break;
00195               }
00196            }
00197          if (memcmp (p2 + align2, buf1, len2))
00198            {
00199              error (0, 0, "Iteration %zd - garbage in string before, %s (%zd, %zd, %zd, %zd)",
00200                    n, impl->name, align1, align2, len1, len2);
00201              ret = 1;
00202            }
00203          for (j = align2 + len1 + len2 + 1; j < 512; ++j)
00204            {
00205              if (p2[j] != '\1')
00206               {
00207                 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd, %zd)",
00208                       n, impl->name, align1, align2, len1, len2);
00209                 ret = 1;
00210                 break;
00211               }
00212            }
00213          if (memcmp (p1 + align1, p2 + align2 + len2, len1 + 1))
00214            {
00215              error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd, %zd)",
00216                    n, impl->name, align1, align2, len1, len2);
00217              ret = 1;
00218            }
00219        }
00220     }
00221 }
00222 
00223 int
00224 test_main (void)
00225 {
00226   size_t i;
00227 
00228   test_init ();
00229 
00230   printf ("%28s", "");
00231   FOR_EACH_IMPL (impl, 0)
00232     printf ("\t%s", impl->name);
00233   putchar ('\n');
00234 
00235   for (i = 0; i < 16; ++i)
00236     {
00237       do_test (0, 0, i, i, 127);
00238       do_test (0, 0, i, i, 255);
00239       do_test (0, i, i, i, 127);
00240       do_test (i, 0, i, i, 255);
00241     }
00242 
00243   for (i = 1; i < 8; ++i)
00244     {
00245       do_test (0, 0, 8 << i, 8 << i, 127);
00246       do_test (8 - i, 2 * i, 8 << i, 8 << i, 127);
00247       do_test (0, 0, 8 << i, 2 << i, 127);
00248       do_test (8 - i, 2 * i, 8 << i, 2 << i, 127);
00249     }
00250 
00251   for (i = 1; i < 8; ++i)
00252     {
00253       do_test (i, 2 * i, 8 << i, 1, 127);
00254       do_test (2 * i, i, 8 << i, 1, 255);
00255       do_test (i, i, 8 << i, 10, 127);
00256       do_test (i, i, 8 << i, 10, 255);
00257     }
00258 
00259   do_random_tests ();
00260   return ret;
00261 }
00262 
00263 #include "../test-skeleton.c"