Back to index

glibc  2.9
test-strncmp.c
Go to the documentation of this file.
00001 /* Test and measure strncmp functions.
00002    Copyright (C) 1999, 2002, 2003 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_strncmp (const char *, const char *, size_t);
00026 int stupid_strncmp (const char *, const char *, size_t);
00027 
00028 IMPL (stupid_strncmp, 0)
00029 IMPL (simple_strncmp, 0)
00030 IMPL (strncmp, 1)
00031 
00032 int
00033 simple_strncmp (const char *s1, const char *s2, size_t n)
00034 {
00035   int ret = 0;
00036 
00037   while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
00038         && *s1++);
00039   return ret;
00040 }
00041 
00042 int
00043 stupid_strncmp (const char *s1, const char *s2, size_t n)
00044 {
00045   size_t ns1 = strnlen (s1, n) + 1, ns2 = strnlen (s2, n) + 1;
00046   int ret = 0;
00047 
00048   n = ns1 < n ? ns1 : n;
00049   n = ns2 < n ? ns2 : n;
00050   while (n-- && (ret = *(unsigned char *) s1++ - * (unsigned char *) s2++) == 0);
00051   return ret;
00052 }
00053 
00054 static void
00055 do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n,
00056             int exp_result)
00057 {
00058   int result = CALL (impl, s1, s2, n);
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, n);
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_limit (size_t align1, size_t align2, size_t len, size_t n, int max_char,
00090         int exp_result)
00091 {
00092   size_t i, align_n;
00093   char *s1, *s2;
00094 
00095   if (n == 0)
00096     {
00097       s1 = (char*)(buf1 + page_size);
00098       s2 = (char*)(buf2 + page_size);
00099       if (HP_TIMING_AVAIL)
00100        printf ("Length %4zd/%4zd:", len, n);
00101        
00102       FOR_EACH_IMPL (impl, 0)
00103        do_one_test (impl, s1, s2, n, 0);
00104 
00105       if (HP_TIMING_AVAIL)
00106        putchar ('\n');
00107        
00108       return;
00109     }
00110 
00111   align1 &= 15;
00112   align2 &= 15;
00113   align_n = (page_size - n) & 15;
00114 
00115   s1 = (char*)(buf1 + page_size - n);
00116   s2 = (char*)(buf2 + page_size - n);
00117   
00118   if (align1 < align_n)
00119     s1 -= (align_n - align1);
00120     
00121   if (align2 < align_n)
00122     s2 -= (align_n - align2);
00123     
00124   for (i = 0; i < n; i++)
00125     s1[i] = s2[i] = 1 + 23 * i % max_char;
00126 
00127   if (len < n)
00128     {
00129       s1[len] = 0;
00130       s2[len] = 0;
00131       if (exp_result < 0)
00132        s2[len] = 32;
00133       else if (exp_result > 0)
00134        s1[len] = 64;
00135     }
00136 
00137   if (HP_TIMING_AVAIL)
00138     printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len, n, align1, align2);
00139 
00140   FOR_EACH_IMPL (impl, 0)
00141     do_one_test (impl, s1, s2, n, exp_result);
00142 
00143   if (HP_TIMING_AVAIL)
00144     putchar ('\n');
00145 }
00146 
00147 static void
00148 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char,
00149         int exp_result)
00150 {
00151   size_t i;
00152   char *s1, *s2;
00153 
00154   if (n == 0)
00155     return;
00156 
00157   align1 &= 7;
00158   if (align1 + n + 1 >= page_size)
00159     return;
00160 
00161   align2 &= 7;
00162   if (align2 + n + 1 >= page_size)
00163     return;
00164 
00165   s1 = (char*)(buf1 + align1);
00166   s2 = (char*)(buf2 + align2);
00167 
00168   for (i = 0; i < n; i++)
00169     s1[i] = s2[i] = 1 + 23 * i % max_char;
00170 
00171   s1[n] = 24 + exp_result;
00172   s2[n] = 23;
00173   s1[len] = 0;
00174   s2[len] = 0;
00175   if (exp_result < 0)
00176     s2[len] = 32;
00177   else if (exp_result > 0)
00178     s1[len] = 64;
00179   if (len >= n)
00180     s2[n - 1] -= exp_result;
00181 
00182   if (HP_TIMING_AVAIL)
00183     printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len, n, align1, align2);
00184 
00185   FOR_EACH_IMPL (impl, 0)
00186     do_one_test (impl, (char*)s1, (char*)s2, n, exp_result);
00187 
00188   if (HP_TIMING_AVAIL)
00189     putchar ('\n');
00190 }
00191 
00192 static void
00193 do_random_tests (void)
00194 {
00195   size_t i, j, n, align1, align2, pos, len1, len2, size;
00196   int result;
00197   long r;
00198   unsigned char *p1 = buf1 + page_size - 512;
00199   unsigned char *p2 = buf2 + page_size - 512;
00200 
00201   for (n = 0; n < ITERATIONS; n++)
00202     {
00203       align1 = random () & 31;
00204       if (random () & 1)
00205        align2 = random () & 31;
00206       else
00207        align2 = align1 + (random () & 24);
00208       pos = random () & 511;
00209       size = random () & 511;
00210       j = align1 > align2 ? align1 : align2;
00211       if (pos + j >= 511)
00212        pos = 510 - j - (random () & 7);
00213       len1 = random () & 511;
00214       if (pos >= len1 && (random () & 1))
00215        len1 = pos + (random () & 7);
00216       if (len1 + j >= 512)
00217        len1 = 511 - j - (random () & 7);
00218       if (pos >= len1)
00219        len2 = len1;
00220       else
00221        len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
00222       j = (pos > len2 ? pos : len2) + align1 + 64;
00223       if (j > 512)
00224        j = 512;
00225       for (i = 0; i < j; ++i)
00226        {
00227          p1[i] = random () & 255;
00228          if (i < len1 + align1 && !p1[i])
00229            {
00230              p1[i] = random () & 255;
00231              if (!p1[i])
00232               p1[i] = 1 + (random () & 127);
00233            }
00234        }
00235       for (i = 0; i < j; ++i)
00236        {
00237          p2[i] = random () & 255;
00238          if (i < len2 + align2 && !p2[i])
00239            {
00240              p2[i] = random () & 255;
00241              if (!p2[i])
00242               p2[i] = 1 + (random () & 127);
00243            }
00244        }
00245 
00246       result = 0;
00247       memcpy (p2 + align2, p1 + align1, pos);
00248       if (pos < len1)
00249        {
00250          if (p2[align2 + pos] == p1[align1 + pos])
00251            {
00252              p2[align2 + pos] = random () & 255;
00253              if (p2[align2 + pos] == p1[align1 + pos])
00254               p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
00255            }
00256 
00257          if (pos < size)
00258            {
00259              if (p1[align1 + pos] < p2[align2 + pos])
00260               result = -1;
00261              else
00262               result = 1;
00263            }
00264        }
00265       p1[len1 + align1] = 0;
00266       p2[len2 + align2] = 0;
00267 
00268       FOR_EACH_IMPL (impl, 1)
00269        {
00270          r = CALL (impl, (char*)(p1 + align1), (char*)(p2 + align2), size);
00271          /* Test whether on 64-bit architectures where ABI requires
00272             callee to promote has the promotion been done.  */
00273          asm ("" : "=g" (r) : "0" (r));
00274          if ((r == 0 && result)
00275              || (r < 0 && result >= 0)
00276              || (r > 0 && result <= 0))
00277            {
00278              error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
00279                    n, impl->name, align1, align2, len1, len2, pos, size, r, result, p1, p2);
00280              ret = 1;
00281            }
00282        }
00283     }
00284 }
00285 
00286 int
00287 test_main (void)
00288 {
00289   size_t i;
00290 
00291   test_init ();
00292 
00293   printf ("%23s", "");
00294   FOR_EACH_IMPL (impl, 0)
00295     printf ("\t%s", impl->name);
00296   putchar ('\n');
00297 
00298   for (i =0; i < 16; ++i)
00299     {
00300       do_test (0, 0, 8, i, 127, 0);
00301       do_test (0, 0, 8, i, 127, -1);
00302       do_test (0, 0, 8, i, 127, 1);
00303       do_test (i, i, 8, i, 127, 0);
00304       do_test (i, i, 8, i, 127, 1);
00305       do_test (i, i, 8, i, 127, -1);
00306       do_test (i, 2 * i, 8, i, 127, 0);
00307       do_test (2 * i, i, 8, i, 127, 1);
00308       do_test (i, 3 * i, 8, i, 127, -1);
00309       do_test (0, 0, 8, i, 255, 0);
00310       do_test (0, 0, 8, i, 255, -1);
00311       do_test (0, 0, 8, i, 255, 1);
00312       do_test (i, i, 8, i, 255, 0);
00313       do_test (i, i, 8, i, 255, 1);
00314       do_test (i, i, 8, i, 255, -1);
00315       do_test (i, 2 * i, 8, i, 255, 0);
00316       do_test (2 * i, i, 8, i, 255, 1);
00317       do_test (i, 3 * i, 8, i, 255, -1);
00318     }
00319 
00320   for (i = 1; i < 8; ++i)
00321     {
00322       do_test (0, 0, 8 << i, 16 << i, 127, 0);
00323       do_test (0, 0, 8 << i, 16 << i, 127, 1);
00324       do_test (0, 0, 8 << i, 16 << i, 127, -1);
00325       do_test (0, 0, 8 << i, 16 << i, 255, 0);
00326       do_test (0, 0, 8 << i, 16 << i, 255, 1);
00327       do_test (0, 0, 8 << i, 16 << i, 255, -1);
00328       do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 0);
00329       do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 1);
00330       do_test (2 * i, i, 8 << i, 16 << i, 255, 0);
00331       do_test (2 * i, i, 8 << i, 16 << i, 255, 1);
00332     }
00333     
00334   do_test_limit (0, 0, 0, 0, 127, 0);
00335   do_test_limit (4, 0, 21, 20, 127, 0);
00336   do_test_limit (0, 4, 21, 20, 127, 0);
00337   do_test_limit (8, 0, 25, 24, 127, 0);
00338   do_test_limit (0, 8, 25, 24, 127, 0);
00339 
00340   for (i = 0; i < 8; ++i)
00341     {
00342       do_test_limit (0, 0, 17 - i, 16 - i, 127, 0);
00343       do_test_limit (0, 0, 17 - i, 16 - i, 255, 0);
00344       do_test_limit (0, 0, 15 - i, 16 - i, 127, 0);
00345       do_test_limit (0, 0, 15 - i, 16 - i, 127, 1);
00346       do_test_limit (0, 0, 15 - i, 16 - i, 127, -1);
00347       do_test_limit (0, 0, 15 - i, 16 - i, 255, 0);
00348       do_test_limit (0, 0, 15 - i, 16 - i, 255, 1);
00349       do_test_limit (0, 0, 15 - i, 16 - i, 255, -1);
00350     }
00351 
00352   do_random_tests ();
00353   return ret;
00354 }
00355 
00356 #include "../test-skeleton.c"