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

source code of glibc/string/test-memmem.c