Set LC_ALL=C before sed.
[glibc.git] / string / test-memmem.c
blobfec8b55990619637b3cd1bec3d229ff534775dfd
1 /* Test and measure memmem functions.
2 Copyright (C) 2008-2013 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 TEST_NAME "memmem"
22 #define BUF1PAGES 20
23 #define ITERATIONS 500
24 #include "test-string.h"
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);
29 IMPL (simple_memmem, 0)
30 IMPL (memmem, 1)
32 void *
33 simple_memmem (const void *haystack, size_t haystack_len, const void *needle,
34 size_t needle_len)
36 const char *begin;
37 const char *const last_possible
38 = (const char *) haystack + haystack_len - needle_len;
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;
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;
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;
57 return NULL;
60 static int
61 check_result (impl_t *impl, const void *haystack, size_t haystack_len,
62 const void *needle, size_t needle_len, const void *expected)
64 void *res;
66 res = CALL (impl, haystack, haystack_len, needle, needle_len);
67 if (res != expected)
69 error (0, 0, "Wrong result in function %s %p %p", impl->name,
70 res, expected);
71 ret = 1;
72 return -1;
75 return 0;
78 static void
79 do_one_test (impl_t *impl, const void *haystack, size_t haystack_len,
80 const void *needle, size_t needle_len, const void *expected)
82 if (check_result (impl, haystack, haystack_len, needle, needle_len,
83 expected) < 0)
84 return;
86 if (HP_TIMING_AVAIL)
88 hp_timing_t start __attribute ((unused));
89 hp_timing_t stop __attribute ((unused));
90 hp_timing_t best_time = ~ (hp_timing_t) 0;
91 size_t i;
93 for (i = 0; i < 32; ++i)
95 HP_TIMING_NOW (start);
96 CALL (impl, haystack, haystack_len, needle, needle_len);
97 HP_TIMING_NOW (stop);
98 HP_TIMING_BEST (best_time, start, stop);
101 printf ("\t%zd", (size_t) best_time);
105 static void
106 do_test (const char *str, size_t len, size_t idx)
108 char tmpbuf[len];
110 memcpy (tmpbuf, buf1 + idx, len);
111 memcpy (buf1 + idx, str, len);
113 if (HP_TIMING_AVAIL)
114 printf ("String %s, offset %zd:", str, idx);
116 FOR_EACH_IMPL (impl, 0)
117 do_one_test (impl, buf1, BUF1PAGES * page_size, str, len, buf1 + idx);
119 memcpy (buf1 + idx, tmpbuf, len);
121 if (HP_TIMING_AVAIL)
122 putchar ('\n');
125 static void
126 do_random_tests (void)
128 for (size_t n = 0; n < ITERATIONS; ++n)
130 char tmpbuf[32];
132 size_t shift = random () % 11;
133 size_t rel = random () % ((2 << (shift + 1)) * 64);
134 size_t idx = MIN ((2 << shift) * 64 + rel, BUF1PAGES * page_size - 2);
135 size_t len = random () % (sizeof (tmpbuf) - 1) + 1;
136 len = MIN (len, BUF1PAGES * page_size - idx - 1);
137 memcpy (tmpbuf, buf1 + idx, len);
138 for (size_t i = random () % len / 2 + 1; i > 0; --i)
140 size_t off = random () % len;
141 char ch = '0' + random () % 10;
143 buf1[idx + off] = ch;
146 if (HP_TIMING_AVAIL)
147 printf ("String %.*s, offset %zd:", (int) len, buf1 + idx, idx);
149 FOR_EACH_IMPL (impl, 0)
150 do_one_test (impl, buf1, BUF1PAGES * page_size, buf1 + idx, len,
151 buf1 + idx);
153 if (HP_TIMING_AVAIL)
154 putchar ('\n');
156 memcpy (buf1 + idx, tmpbuf, len);
160 static void
161 check1 (void)
164 const char search_buf_data[5] = { 0x56, 0x34, 0x12, 0x78, 0x78 };
165 const char pattern[2] = { 0x78, 0x56 };
166 void *search_buf = (void *) buf1 + page_size - sizeof search_buf_data;
167 void *exp_result;
169 memcpy (search_buf, search_buf_data, sizeof search_buf_data);
170 exp_result = simple_memmem (search_buf, sizeof search_buf_data,
171 pattern, sizeof pattern);
172 FOR_EACH_IMPL (impl, 0)
173 check_result (impl, search_buf, sizeof search_buf_data,
174 pattern, sizeof pattern, exp_result);
177 static const char *const strs[] =
179 "00000", "00112233", "0123456789", "0000111100001111",
180 "00000111110000022222", "012345678901234567890",
181 "abc0", "aaaa0", "abcabc0"
186 test_main (void)
188 size_t i;
190 test_init ();
192 check1 ();
194 printf ("%23s", "");
195 FOR_EACH_IMPL (impl, 0)
196 printf ("\t%s", impl->name);
197 putchar ('\n');
199 for (i = 0; i < BUF1PAGES * page_size; ++i)
200 buf1[i] = 60 + random () % 32;
202 for (i = 0; i < sizeof (strs) / sizeof (strs[0]); ++i)
203 for (size_t j = 0; j < 120; j += 7)
205 size_t len = strlen (strs[i]);
207 do_test (strs[i], len, j);
210 do_random_tests ();
211 return ret;
214 #include "../test-skeleton.c"