1/* Measure strpbrk functions.
2 Copyright (C) 2013-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 BIG_CHAR MAX_CHAR
20
21#ifndef WIDE
22# define SMALL_CHAR 127
23#else
24# define SMALL_CHAR 1273
25#endif /* WIDE */
26
27#ifndef STRPBRK_RESULT
28# define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL)
29# define RES_TYPE CHAR *
30# define TEST_MAIN
31# ifndef WIDE
32# define TEST_NAME "strpbrk"
33# else
34# define TEST_NAME "wcspbrk"
35# endif /* WIDE */
36# include "bench-string.h"
37
38# ifndef WIDE
39# define SIMPLE_STRPBRK simple_strpbrk
40# else
41# define SIMPLE_STRPBRK simple_wcspbrk
42# endif /* WIDE */
43
44typedef CHAR *(*proto_t) (const CHAR *, const CHAR *);
45CHAR *SIMPLE_STRPBRK (const CHAR *, const CHAR *);
46
47IMPL (SIMPLE_STRPBRK, 0)
48IMPL (STRPBRK, 1)
49
50CHAR *
51SIMPLE_STRPBRK (const CHAR *s, const CHAR *rej)
52{
53 const CHAR *r;
54 CHAR c;
55
56 while ((c = *s++) != '\0')
57 for (r = rej; *r != '\0'; ++r)
58 if (*r == c)
59 return (CHAR *) s - 1;
60 return NULL;
61}
62
63#endif /* !STRPBRK_RESULT */
64
65static void
66do_one_test (impl_t *impl, const CHAR *s, const CHAR *rej, RES_TYPE exp_res)
67{
68 RES_TYPE res = CALL (impl, s, rej);
69 size_t i, iters = INNER_LOOP_ITERS_MEDIUM;
70 timing_t start, stop, cur;
71
72 if (res != exp_res)
73 {
74 error (status: 0, errnum: 0, format: "Wrong result in function %s %p %p", impl->name,
75 (void *) res, (void *) exp_res);
76 ret = 1;
77 return;
78 }
79
80 TIMING_NOW (start);
81 for (i = 0; i < iters; ++i)
82 {
83 CALL (impl, s, rej);
84 }
85 TIMING_NOW (stop);
86
87 TIMING_DIFF (cur, start, stop);
88
89 TIMING_PRINT_MEAN ((double) cur, (double) iters);
90}
91
92static void
93do_test (size_t align, size_t pos, size_t len)
94{
95 size_t i;
96 int c;
97 RES_TYPE result;
98 CHAR *rej, *s;
99
100 align &= 7;
101 if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240)
102 return;
103
104 rej = (CHAR *) (buf2) + (random () & 255);
105 s = (CHAR *) (buf1) + align;
106
107 for (i = 0; i < len; ++i)
108 {
109 rej[i] = random () & BIG_CHAR;
110 if (!rej[i])
111 rej[i] = random () & BIG_CHAR;
112 if (!rej[i])
113 rej[i] = 1 + (random () & SMALL_CHAR);
114 }
115 rej[len] = '\0';
116 for (c = 1; c <= BIG_CHAR; ++c)
117 if (STRCHR (rej, c) == NULL)
118 break;
119
120 for (i = 0; i < pos; ++i)
121 {
122 s[i] = random () & BIG_CHAR;
123 if (STRCHR (rej, s[i]))
124 {
125 s[i] = random () & BIG_CHAR;
126 if (STRCHR (rej, s[i]))
127 s[i] = c;
128 }
129 }
130 s[pos] = rej[random () % (len + 1)];
131 if (s[pos])
132 {
133 for (i = pos + 1; i < pos + 10; ++i)
134 s[i] = random () & BIG_CHAR;
135 s[i] = '\0';
136 }
137 result = STRPBRK_RESULT (s, pos);
138
139 printf (format: "Length %4zd, alignment %2zd, rej len %2zd:", pos, align, len);
140
141 FOR_EACH_IMPL (impl, 0)
142 do_one_test (impl, s, rej, exp_res: result);
143
144 putchar (c: '\n');
145}
146
147int
148test_main (void)
149{
150 size_t i;
151
152 test_init ();
153
154 printf (format: "%32s", "");
155 FOR_EACH_IMPL (impl, 0)
156 printf (format: "\t%s", impl->name);
157 putchar (c: '\n');
158
159 for (i = 0; i < 32; ++i)
160 {
161 do_test (align: 0, pos: 512, len: i);
162 do_test (align: i, pos: 512, len: i);
163 }
164
165 for (i = 1; i < 8; ++i)
166 {
167 do_test (align: 0, pos: 16 << i, len: 4);
168 do_test (align: i, pos: 16 << i, len: 4);
169 }
170
171 for (i = 1; i < 8; ++i)
172 do_test (align: i, pos: 64, len: 10);
173
174 for (i = 0; i < 64; ++i)
175 do_test (align: 0, pos: i, len: 6);
176
177 return ret;
178}
179
180#include <support/test-driver.c>
181

source code of glibc/benchtests/bench-strpbrk.c