Back to index

glibc  2.9
test-memmem.c
Go to the documentation of this file.
00001 /* Test and measure memmem functions.
00002    Copyright (C) 2008 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Written by Ulrich Drepper <drepper@redhat.com>, 2008.
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 #define BUF1PAGES 20
00023 #define ITERATIONS 500
00024 #include "test-string.h"
00025 
00026 typedef char *(*proto_t) (const void *, size_t, const void *, size_t);
00027 void *simple_memmem (const void *, size_t, const void *, size_t);
00028 
00029 IMPL (simple_memmem, 0)
00030 IMPL (memmem, 1)
00031 
00032 void *
00033 simple_memmem (const void *haystack, size_t haystack_len, const void *needle,
00034               size_t needle_len)
00035 {
00036   const char *begin;
00037   const char *const last_possible
00038     = (const char *) haystack + haystack_len - needle_len;
00039 
00040   if (needle_len == 0)
00041     /* The first occurrence of the empty string is deemed to occur at
00042        the beginning of the string.  */
00043     return (void *) haystack;
00044 
00045   /* Sanity check, otherwise the loop might search through the whole
00046      memory.  */
00047   if (__builtin_expect (haystack_len < needle_len, 0))
00048     return NULL;
00049 
00050   for (begin = (const char *) haystack; begin <= last_possible; ++begin)
00051     if (begin[0] == ((const char *) needle)[0] &&
00052         !memcmp ((const void *) &begin[1],
00053                  (const void *) ((const char *) needle + 1),
00054                  needle_len - 1))
00055       return (void *) begin;
00056 
00057   return NULL;
00058 }
00059 
00060 static void
00061 do_one_test (impl_t *impl, const void *haystack, size_t haystack_len,
00062             const void *needle, size_t needle_len, const void *expected)
00063 {
00064   void *res;
00065 
00066   res = CALL (impl, haystack, haystack_len, needle, needle_len);
00067   if (res != expected)
00068     {
00069       error (0, 0, "Wrong result in function %s %p %p", impl->name,
00070             res, expected);
00071       ret = 1;
00072       return;
00073     }
00074 
00075   if (HP_TIMING_AVAIL)
00076     {
00077       hp_timing_t start __attribute ((unused));
00078       hp_timing_t stop __attribute ((unused));
00079       hp_timing_t best_time = ~ (hp_timing_t) 0;
00080       size_t i;
00081 
00082       for (i = 0; i < 32; ++i)
00083        {
00084          HP_TIMING_NOW (start);
00085          CALL (impl, haystack, haystack_len, needle, needle_len);
00086          HP_TIMING_NOW (stop);
00087          HP_TIMING_BEST (best_time, start, stop);
00088        }
00089 
00090       printf ("\t%zd", (size_t) best_time);
00091     }
00092 }
00093 
00094 static void
00095 do_test (const char *str, size_t len, size_t idx)
00096 {
00097   char tmpbuf[len];
00098 
00099   memcpy (tmpbuf, buf1 + idx, len);
00100   memcpy (buf1 + idx, str, len);
00101 
00102   if (HP_TIMING_AVAIL)
00103     printf ("String %s, offset %zd:", str, idx);
00104 
00105   FOR_EACH_IMPL (impl, 0)
00106     do_one_test (impl, buf1, BUF1PAGES * page_size, str, len, buf1 + idx);
00107 
00108   memcpy (buf1 + idx, tmpbuf, len);
00109 
00110   if (HP_TIMING_AVAIL)
00111     putchar ('\n');
00112 }
00113 
00114 static void
00115 do_random_tests (void)
00116 {
00117   for (size_t n = 0; n < ITERATIONS; ++n)
00118     {
00119       char tmpbuf[32];
00120 
00121       size_t shift = random () % 11;
00122       size_t rel = random () % ((2 << (shift + 1)) * 64);
00123       size_t idx = MIN ((2 << shift) * 64 + rel, BUF1PAGES * page_size - 2);
00124       size_t len = random () % (sizeof (tmpbuf) - 1) + 1;
00125       len = MIN (len, BUF1PAGES * page_size - idx - 1);
00126       memcpy (tmpbuf, buf1 + idx, len);
00127       for (size_t i = random () % len / 2 + 1; i > 0; --i)
00128        {
00129          size_t off = random () % len;
00130          char ch = '0' + random () % 10;
00131 
00132          buf1[idx + off] = ch;
00133        }
00134 
00135       if (HP_TIMING_AVAIL)
00136        printf ("String %.*s, offset %zd:", (int) len, buf1 + idx, idx);
00137 
00138       FOR_EACH_IMPL (impl, 0)
00139        do_one_test (impl, buf1, BUF1PAGES * page_size, buf1 + idx, len,
00140                    buf1 + idx);
00141 
00142       if (HP_TIMING_AVAIL)
00143        putchar ('\n');
00144 
00145       memcpy (buf1 + idx, tmpbuf, len);
00146     }
00147 }
00148 
00149 
00150 static const char *const strs[] =
00151   {
00152     "00000", "00112233", "0123456789", "0000111100001111",
00153     "00000111110000022222", "012345678901234567890",
00154     "abc0", "aaaa0", "abcabc0"
00155   };
00156 
00157 
00158 int
00159 test_main (void)
00160 {
00161   size_t i;
00162 
00163   test_init ();
00164 
00165   printf ("%23s", "");
00166   FOR_EACH_IMPL (impl, 0)
00167     printf ("\t%s", impl->name);
00168   putchar ('\n');
00169 
00170   for (i = 0; i < BUF1PAGES * page_size; ++i)
00171     buf1[i] = 60 + random () % 32;
00172 
00173   for (i = 0; i < sizeof (strs) / sizeof (strs[0]); ++i)
00174     for (size_t j = 0; j < 120; j += 7)
00175       {
00176        size_t len = strlen (strs[i]);
00177 
00178        do_test (strs[i], len, j);
00179       }
00180 
00181   do_random_tests ();
00182   return ret;
00183 }
00184 
00185 #include "../test-skeleton.c"