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
44 typedef CHAR *(*proto_t) (const CHAR *, const CHAR *);
45 CHAR *SIMPLE_STRPBRK (const CHAR *, const CHAR *);
46
47 IMPL (SIMPLE_STRPBRK, 0)
48 IMPL (STRPBRK, 1)
49
50 CHAR *
SIMPLE_STRPBRK(const CHAR * s,const CHAR * rej)51 SIMPLE_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
65 #include "json-lib.h"
66
67 static void
do_one_test(json_ctx_t * json_ctx,impl_t * impl,const CHAR * s,const CHAR * rej,RES_TYPE exp_res)68 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s,
69 const CHAR *rej, RES_TYPE exp_res)
70 {
71 RES_TYPE res = CALL (impl, s, rej);
72 size_t i, iters = INNER_LOOP_ITERS;
73 timing_t start, stop, cur;
74
75 if (res != exp_res)
76 {
77 error (0, 0, "Wrong result in function %s %p %p", impl->name,
78 (void *) res, (void *) exp_res);
79 ret = 1;
80 return;
81 }
82
83 TIMING_NOW (start);
84 for (i = 0; i < iters; ++i)
85 {
86 CALL (impl, s, rej);
87 }
88 TIMING_NOW (stop);
89
90 TIMING_DIFF (cur, start, stop);
91
92 json_element_double (json_ctx, (double)cur / (double)iters);
93 }
94
95 static void
do_test(json_ctx_t * json_ctx,size_t align1,size_t align2,size_t pos,size_t len)96 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t pos,
97 size_t len)
98 {
99 size_t i;
100 int c;
101 RES_TYPE result;
102 CHAR *rej, *s;
103
104 align1 &= 7;
105 if ((align1 + pos + 10) * sizeof (CHAR) >= page_size || len > 240)
106 return;
107 if ((align2 + len) * sizeof (CHAR) >= page_size)
108 return;
109
110 rej = (CHAR *) (buf2) + align2;
111 s = (CHAR *) (buf1) + align1;
112
113 for (i = 0; i < len; ++i)
114 {
115 rej[i] = random () & BIG_CHAR;
116 if (!rej[i])
117 rej[i] = random () & BIG_CHAR;
118 if (!rej[i])
119 rej[i] = 1 + (random () & SMALL_CHAR);
120 }
121 rej[len] = '\0';
122 for (c = 1; c <= BIG_CHAR; ++c)
123 if (STRCHR (rej, c) == NULL)
124 break;
125
126 for (i = 0; i < pos; ++i)
127 {
128 s[i] = random () & BIG_CHAR;
129 if (STRCHR (rej, s[i]))
130 {
131 s[i] = random () & BIG_CHAR;
132 if (STRCHR (rej, s[i]))
133 s[i] = c;
134 }
135 }
136 s[pos] = rej[random () % (len + 1)];
137 if (s[pos])
138 {
139 for (i = pos + 1; i < pos + 10; ++i)
140 s[i] = random () & BIG_CHAR;
141 s[i] = '\0';
142 }
143 result = STRPBRK_RESULT (s, pos);
144
145 json_element_object_begin (json_ctx);
146 json_attr_uint (json_ctx, "len", len);
147 json_attr_uint (json_ctx, "pos", pos);
148 json_attr_uint (json_ctx, "align1", align1);
149 json_attr_uint (json_ctx, "align2", align2);
150 json_array_begin (json_ctx, "timings");
151
152 FOR_EACH_IMPL (impl, 0)
153 do_one_test (json_ctx, impl, s, rej, result);
154
155 json_array_end (json_ctx);
156 json_element_object_end (json_ctx);
157 }
158
159 int
test_main(void)160 test_main (void)
161 {
162 json_ctx_t json_ctx;
163 size_t i;
164
165 test_init ();
166
167 json_init (&json_ctx, 0, stdout);
168
169 json_document_begin (&json_ctx);
170 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
171
172 json_attr_object_begin (&json_ctx, "functions");
173 json_attr_object_begin (&json_ctx, TEST_NAME);
174 json_attr_string (&json_ctx, "bench-variant", "");
175
176 json_array_begin (&json_ctx, "ifuncs");
177 FOR_EACH_IMPL (impl, 0)
178 json_element_string (&json_ctx, impl->name);
179 json_array_end (&json_ctx);
180
181 json_array_begin (&json_ctx, "results");
182
183
184 for (i = 0; i < 32; ++i)
185 {
186 do_test (&json_ctx, 0, 0, 512, i);
187 do_test (&json_ctx, i, 0, 512, i);
188 do_test (&json_ctx, 0, i, 512, i);
189 do_test (&json_ctx, i, i, 512, i);
190
191 }
192
193 for (i = 1; i < 8; ++i)
194 {
195 do_test (&json_ctx, 0, 0, 16 << i, 4);
196 do_test (&json_ctx, i, 0, 16 << i, 4);
197 do_test (&json_ctx, 0, i, 16 << i, 4);
198 do_test (&json_ctx, i, i, 16 << i, 4);
199 }
200
201 for (i = 1; i < 8; ++i)
202 {
203 do_test (&json_ctx, i, 0, 64, 10);
204 do_test (&json_ctx, i, i, 64, 10);
205 }
206
207 for (i = 0; i < 64; ++i)
208 {
209 do_test (&json_ctx, 0, 0, i, 6);
210 do_test (&json_ctx, 0, i, i, 6);
211 }
212
213 json_array_end (&json_ctx);
214 json_attr_object_end (&json_ctx);
215 json_attr_object_end (&json_ctx);
216 json_document_end (&json_ctx);
217
218 return ret;
219 }
220
221 #include <support/test-driver.c>
222