Back to index

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