Back to index

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