Back to index

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