Add a testcase for BZ #14716
[glibc.git] / string / test-memmem.c
blob5f32a0e79128a3b584965030b7bd8f77d742711d
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.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #define TEST_MAIN
21 #define BUF1PAGES 20
22 #define ITERATIONS 500
23 #include "test-string.h"
25 typedef char *(*proto_t) (const void *, size_t, const void *, size_t);
26 void *simple_memmem (const void *, size_t, const void *, size_t);
28 IMPL (simple_memmem, 0)
29 IMPL (memmem, 1)
31 void *
32 simple_memmem (const void *haystack, size_t haystack_len, const void *needle,
33 size_t needle_len)
35 const char *begin;
36 const char *const last_possible
37 = (const char *) haystack + haystack_len - needle_len;
39 if (needle_len == 0)
40 /* The first occurrence of the empty string is deemed to occur at
41 the beginning of the string. */
42 return (void *) haystack;
44 /* Sanity check, otherwise the loop might search through the whole
45 memory. */
46 if (__builtin_expect (haystack_len < needle_len, 0))
47 return NULL;
49 for (begin = (const char *) haystack; begin <= last_possible; ++begin)
50 if (begin[0] == ((const char *) needle)[0] &&
51 !memcmp ((const void *) &begin[1],
52 (const void *) ((const char *) needle + 1),
53 needle_len - 1))
54 return (void *) begin;
56 return NULL;
59 static int
60 check_result (impl_t *impl, const void *haystack, size_t haystack_len,
61 const void *needle, size_t needle_len, const void *expected)
63 void *res;
65 res = CALL (impl, haystack, haystack_len, needle, needle_len);
66 if (res != expected)
68 error (0, 0, "Wrong result in function %s %p %p", impl->name,
69 res, expected);
70 ret = 1;
71 return -1;
74 return 0;
77 static void
78 do_one_test (impl_t *impl, const void *haystack, size_t haystack_len,
79 const void *needle, size_t needle_len, const void *expected)
81 if (check_result (impl, haystack, haystack_len, needle, needle_len,
82 expected) < 0)
83 return;
85 if (HP_TIMING_AVAIL)
87 hp_timing_t start __attribute ((unused));
88 hp_timing_t stop __attribute ((unused));
89 hp_timing_t best_time = ~ (hp_timing_t) 0;
90 size_t i;
92 for (i = 0; i < 32; ++i)
94 HP_TIMING_NOW (start);
95 CALL (impl, haystack, haystack_len, needle, needle_len);
96 HP_TIMING_NOW (stop);
97 HP_TIMING_BEST (best_time, start, stop);
100 printf ("\t%zd", (size_t) best_time);
104 static void
105 do_test (const char *str, size_t len, size_t idx)
107 char tmpbuf[len];
109 memcpy (tmpbuf, buf1 + idx, len);
110 memcpy (buf1 + idx, str, len);
112 if (HP_TIMING_AVAIL)
113 printf ("String %s, offset %zd:", str, idx);
115 FOR_EACH_IMPL (impl, 0)
116 do_one_test (impl, buf1, BUF1PAGES * page_size, str, len, buf1 + idx);
118 memcpy (buf1 + idx, tmpbuf, len);
120 if (HP_TIMING_AVAIL)
121 putchar ('\n');
124 static void
125 do_random_tests (void)
127 for (size_t n = 0; n < ITERATIONS; ++n)
129 char tmpbuf[32];
131 size_t shift = random () % 11;
132 size_t rel = random () % ((2 << (shift + 1)) * 64);
133 size_t idx = MIN ((2 << shift) * 64 + rel, BUF1PAGES * page_size - 2);
134 size_t len = random () % (sizeof (tmpbuf) - 1) + 1;
135 len = MIN (len, BUF1PAGES * page_size - idx - 1);
136 memcpy (tmpbuf, buf1 + idx, len);
137 for (size_t i = random () % len / 2 + 1; i > 0; --i)
139 size_t off = random () % len;
140 char ch = '0' + random () % 10;
142 buf1[idx + off] = ch;
145 if (HP_TIMING_AVAIL)
146 printf ("String %.*s, offset %zd:", (int) len, buf1 + idx, idx);
148 FOR_EACH_IMPL (impl, 0)
149 do_one_test (impl, buf1, BUF1PAGES * page_size, buf1 + idx, len,
150 buf1 + idx);
152 if (HP_TIMING_AVAIL)
153 putchar ('\n');
155 memcpy (buf1 + idx, tmpbuf, len);
159 static void
160 check1 (void)
163 const char search_buf_data[5] = { 0x56, 0x34, 0x12, 0x78, 0x78 };
164 const char pattern[2] = { 0x78, 0x56 };
165 void *search_buf = (void *) buf1 + page_size - sizeof search_buf_data;
166 void *exp_result;
168 memcpy (search_buf, search_buf_data, sizeof search_buf_data);
169 exp_result = simple_memmem (search_buf, sizeof search_buf_data,
170 pattern, sizeof pattern);
171 FOR_EACH_IMPL (impl, 0)
172 check_result (impl, search_buf, sizeof search_buf_data,
173 pattern, sizeof pattern, exp_result);
176 static const char *const strs[] =
178 "00000", "00112233", "0123456789", "0000111100001111",
179 "00000111110000022222", "012345678901234567890",
180 "abc0", "aaaa0", "abcabc0"
185 test_main (void)
187 size_t i;
189 test_init ();
191 check1 ();
193 printf ("%23s", "");
194 FOR_EACH_IMPL (impl, 0)
195 printf ("\t%s", impl->name);
196 putchar ('\n');
198 for (i = 0; i < BUF1PAGES * page_size; ++i)
199 buf1[i] = 60 + random () % 32;
201 for (i = 0; i < sizeof (strs) / sizeof (strs[0]); ++i)
202 for (size_t j = 0; j < 120; j += 7)
204 size_t len = strlen (strs[i]);
206 do_test (strs[i], len, j);
209 do_random_tests ();
210 return ret;
213 #include "../test-skeleton.c"