chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / string / test-memmem.c
1 /* Test and measure memmem functions.
2    Copyright (C) 2008 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Written by Ulrich Drepper <drepper@redhat.com>, 2008.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #define TEST_MAIN
22 #define BUF1PAGES 20
23 #define ITERATIONS 500
24 #include "test-string.h"
25
26 typedef char *(*proto_t) (const void *, size_t, const void *, size_t);
27 void *simple_memmem (const void *, size_t, const void *, size_t);
28
29 IMPL (simple_memmem, 0)
30 IMPL (memmem, 1)
31
32 void *
33 simple_memmem (const void *haystack, size_t haystack_len, const void *needle,
34                size_t needle_len)
35 {
36   const char *begin;
37   const char *const last_possible
38     = (const char *) haystack + haystack_len - needle_len;
39
40   if (needle_len == 0)
41     /* The first occurrence of the empty string is deemed to occur at
42        the beginning of the string.  */
43     return (void *) haystack;
44
45   /* Sanity check, otherwise the loop might search through the whole
46      memory.  */
47   if (__builtin_expect (haystack_len < needle_len, 0))
48     return NULL;
49
50   for (begin = (const char *) haystack; begin <= last_possible; ++begin)
51     if (begin[0] == ((const char *) needle)[0] &&
52         !memcmp ((const void *) &begin[1],
53                  (const void *) ((const char *) needle + 1),
54                  needle_len - 1))
55       return (void *) begin;
56
57   return NULL;
58 }
59
60 static void
61 do_one_test (impl_t *impl, const void *haystack, size_t haystack_len,
62              const void *needle, size_t needle_len, const void *expected)
63 {
64   void *res;
65
66   res = CALL (impl, haystack, haystack_len, needle, needle_len);
67   if (res != expected)
68     {
69       error (0, 0, "Wrong result in function %s %p %p", impl->name,
70              res, expected);
71       ret = 1;
72       return;
73     }
74
75   if (HP_TIMING_AVAIL)
76     {
77       hp_timing_t start __attribute ((unused));
78       hp_timing_t stop __attribute ((unused));
79       hp_timing_t best_time = ~ (hp_timing_t) 0;
80       size_t i;
81
82       for (i = 0; i < 32; ++i)
83         {
84           HP_TIMING_NOW (start);
85           CALL (impl, haystack, haystack_len, needle, needle_len);
86           HP_TIMING_NOW (stop);
87           HP_TIMING_BEST (best_time, start, stop);
88         }
89
90       printf ("\t%zd", (size_t) best_time);
91     }
92 }
93
94 static void
95 do_test (const char *str, size_t len, size_t idx)
96 {
97   char tmpbuf[len];
98
99   memcpy (tmpbuf, buf1 + idx, len);
100   memcpy (buf1 + idx, str, len);
101
102   if (HP_TIMING_AVAIL)
103     printf ("String %s, offset %zd:", str, idx);
104
105   FOR_EACH_IMPL (impl, 0)
106     do_one_test (impl, buf1, BUF1PAGES * page_size, str, len, buf1 + idx);
107
108   memcpy (buf1 + idx, tmpbuf, len);
109
110   if (HP_TIMING_AVAIL)
111     putchar ('\n');
112 }
113
114 static void
115 do_random_tests (void)
116 {
117   for (size_t n = 0; n < ITERATIONS; ++n)
118     {
119       char tmpbuf[32];
120
121       size_t shift = random () % 11;
122       size_t rel = random () % ((2 << (shift + 1)) * 64);
123       size_t idx = MIN ((2 << shift) * 64 + rel, BUF1PAGES * page_size - 2);
124       size_t len = random () % (sizeof (tmpbuf) - 1) + 1;
125       len = MIN (len, BUF1PAGES * page_size - idx - 1);
126       memcpy (tmpbuf, buf1 + idx, len);
127       for (size_t i = random () % len / 2 + 1; i > 0; --i)
128         {
129           size_t off = random () % len;
130           char ch = '0' + random () % 10;
131
132           buf1[idx + off] = ch;
133         }
134
135       if (HP_TIMING_AVAIL)
136         printf ("String %.*s, offset %zd:", (int) len, buf1 + idx, idx);
137
138       FOR_EACH_IMPL (impl, 0)
139         do_one_test (impl, buf1, BUF1PAGES * page_size, buf1 + idx, len,
140                      buf1 + idx);
141
142       if (HP_TIMING_AVAIL)
143         putchar ('\n');
144
145       memcpy (buf1 + idx, tmpbuf, len);
146     }
147 }
148
149
150 static const char *const strs[] =
151   {
152     "00000", "00112233", "0123456789", "0000111100001111",
153     "00000111110000022222", "012345678901234567890",
154     "abc0", "aaaa0", "abcabc0"
155   };
156
157
158 int
159 test_main (void)
160 {
161   size_t i;
162
163   test_init ();
164
165   printf ("%23s", "");
166   FOR_EACH_IMPL (impl, 0)
167     printf ("\t%s", impl->name);
168   putchar ('\n');
169
170   for (i = 0; i < BUF1PAGES * page_size; ++i)
171     buf1[i] = 60 + random () % 32;
172
173   for (i = 0; i < sizeof (strs) / sizeof (strs[0]); ++i)
174     for (size_t j = 0; j < 120; j += 7)
175       {
176         size_t len = strlen (strs[i]);
177
178         do_test (strs[i], len, j);
179       }
180
181   do_random_tests ();
182   return ret;
183 }
184
185 #include "../test-skeleton.c"