Back to index

glibc  2.9
test-strlen.c
Go to the documentation of this file.
00001 /* Test and measure strlen 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 size_t (*proto_t) (const char *);
00025 size_t simple_strlen (const char *);
00026 size_t builtin_strlen (const char *);
00027 
00028 IMPL (simple_strlen, 0)
00029 IMPL (builtin_strlen, 0)
00030 IMPL (strlen, 1)
00031 
00032 size_t
00033 simple_strlen (const char *s)
00034 {
00035   const char *p;
00036 
00037   for (p = s; *p; ++p);
00038   return p - s;
00039 }
00040 
00041 size_t
00042 builtin_strlen (const char *p)
00043 {
00044   return __builtin_strlen (p);
00045 }
00046 
00047 static void
00048 do_one_test (impl_t *impl, const char *s, size_t exp_len)
00049 {
00050   size_t len = CALL (impl, s);
00051   if (len != exp_len)
00052     {
00053       error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
00054             len, exp_len);
00055       ret = 1;
00056       return;
00057     }
00058 
00059   if (HP_TIMING_AVAIL)
00060     {
00061       hp_timing_t start __attribute ((unused));
00062       hp_timing_t stop __attribute ((unused));
00063       hp_timing_t best_time = ~ (hp_timing_t) 0;
00064       size_t i;
00065 
00066       for (i = 0; i < 32; ++i)
00067        {
00068          HP_TIMING_NOW (start);
00069          CALL (impl, s);
00070          HP_TIMING_NOW (stop);
00071          HP_TIMING_BEST (best_time, start, stop);
00072        }
00073 
00074       printf ("\t%zd", (size_t) best_time);
00075     }
00076 }
00077 
00078 static void
00079 do_test (size_t align, size_t len, int max_char)
00080 {
00081   size_t i;
00082 
00083   align &= 7;
00084   if (align + len >= page_size)
00085     return;
00086 
00087   for (i = 0; i < len; ++i)
00088     buf1[align + i] = 1 + 7 * i % max_char;
00089   buf1[align + len] = 0;
00090 
00091   if (HP_TIMING_AVAIL)
00092     printf ("Length %4zd, alignment %2zd:", len, align);
00093 
00094   FOR_EACH_IMPL (impl, 0)
00095     do_one_test (impl, (char *) (buf1 + align), len);
00096 
00097   if (HP_TIMING_AVAIL)
00098     putchar ('\n');
00099 }
00100 
00101 static void
00102 do_random_tests (void)
00103 {
00104   size_t i, j, n, align, len;
00105   unsigned char *p = buf1 + page_size - 512;
00106 
00107   for (n = 0; n < ITERATIONS; n++)
00108     {
00109       align = random () & 15;
00110       len = random () & 511;
00111       if (len + align > 510)
00112        len = 511 - align - (random () & 7);
00113       j = len + align + 64;
00114       if (j > 512)
00115        j = 512;
00116 
00117       for (i = 0; i < j; i++)
00118        {
00119          if (i == len + align)
00120            p[i] = 0;
00121          else
00122            {
00123              p[i] = random () & 255;
00124              if (i >= align && i < len + align && !p[i])
00125               p[i] = (random () & 127) + 1;
00126            }
00127        }
00128 
00129       FOR_EACH_IMPL (impl, 1)
00130        if (CALL (impl, (char *) (p + align)) != len)
00131          {
00132            error (0, 0, "Iteration %zd - wrong result in function %s (%zd) %zd != %zd, p %p",
00133                  n, impl->name, align, CALL (impl, (char *) (p + align)),
00134                  len, p);
00135            ret = 1;
00136          }
00137     }
00138 }
00139 
00140 int
00141 test_main (void)
00142 {
00143   size_t i;
00144 
00145   test_init ();
00146 
00147   printf ("%20s", "");
00148   FOR_EACH_IMPL (impl, 0)
00149     printf ("\t%s", impl->name);
00150   putchar ('\n');
00151 
00152   for (i = 1; i < 8; ++i)
00153     do_test (0, i, 127);
00154 
00155   for (i = 1; i < 8; ++i)
00156     do_test (i, i, 127);
00157 
00158   for (i = 2; i <= 10; ++i)
00159     {
00160       do_test (0, 1 << i, 127);
00161       do_test (1, 1 << i, 127);
00162     }
00163 
00164   for (i = 1; i < 8; ++i)
00165     do_test (0, i, 255);
00166 
00167   for (i = 1; i < 8; ++i)
00168     do_test (i, i, 255);
00169 
00170   for (i = 2; i <= 10; ++i)
00171     {
00172       do_test (0, 1 << i, 255);
00173       do_test (1, 1 << i, 255);
00174     }
00175 
00176   do_random_tests ();
00177   return ret;
00178 }
00179 
00180 #include "../test-skeleton.c"