Fix tst-memstream3 build failure
[glibc.git] / benchtests / bench-strpbrk.c
blob6591a83723aec804d536e79c91882e25114f3296
1 /* Measure strpbrk functions.
2 Copyright (C) 2013-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #ifndef WIDE
20 # define CHAR char
21 # define STRLEN strlen
22 # define STRCHR strchr
23 # define BIG_CHAR CHAR_MAX
24 # define SMALL_CHAR 127
25 #else
26 # include <wchar.h>
27 # define CHAR wchar_t
28 # define STRLEN wcslen
29 # define STRCHR wcschr
30 # define BIG_CHAR WCHAR_MAX
31 # define SMALL_CHAR 1273
32 #endif /* WIDE */
34 #ifndef STRPBRK_RESULT
35 # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL)
36 # define RES_TYPE CHAR *
37 # define TEST_MAIN
38 # ifndef WIDE
39 # define TEST_NAME "strpbrk"
40 # else
41 # define TEST_NAME "wcspbrk"
42 # endif /* WIDE */
43 # include "bench-string.h"
45 # ifndef WIDE
46 # define STRPBRK strpbrk
47 # define SIMPLE_STRPBRK simple_strpbrk
48 # define STUPID_STRPBRK stupid_strpbrk
49 # else
50 # include <wchar.h>
51 # define STRPBRK wcspbrk
52 # define SIMPLE_STRPBRK simple_wcspbrk
53 # define STUPID_STRPBRK stupid_wcspbrk
54 # endif /* WIDE */
56 typedef CHAR *(*proto_t) (const CHAR *, const CHAR *);
57 CHAR *SIMPLE_STRPBRK (const CHAR *, const CHAR *);
58 CHAR *STUPID_STRPBRK (const CHAR *, const CHAR *);
60 IMPL (STUPID_STRPBRK, 0)
61 IMPL (SIMPLE_STRPBRK, 0)
62 IMPL (STRPBRK, 1)
64 CHAR *
65 SIMPLE_STRPBRK (const CHAR *s, const CHAR *rej)
67 const CHAR *r;
68 CHAR c;
70 while ((c = *s++) != '\0')
71 for (r = rej; *r != '\0'; ++r)
72 if (*r == c)
73 return (CHAR *) s - 1;
74 return NULL;
77 CHAR *
78 STUPID_STRPBRK (const CHAR *s, const CHAR *rej)
80 size_t ns = STRLEN (s), nrej = STRLEN (rej);
81 size_t i, j;
83 for (i = 0; i < ns; ++i)
84 for (j = 0; j < nrej; ++j)
85 if (s[i] == rej[j])
86 return (CHAR *) s + i;
87 return NULL;
89 #endif /* !STRPBRK_RESULT */
91 static void
92 do_one_test (impl_t *impl, const CHAR *s, const CHAR *rej, RES_TYPE exp_res)
94 RES_TYPE res = CALL (impl, s, rej);
95 size_t i, iters = INNER_LOOP_ITERS;
96 timing_t start, stop, cur;
98 if (res != exp_res)
100 error (0, 0, "Wrong result in function %s %p %p", impl->name,
101 (void *) res, (void *) exp_res);
102 ret = 1;
103 return;
106 TIMING_NOW (start);
107 for (i = 0; i < iters; ++i)
109 CALL (impl, s, rej);
111 TIMING_NOW (stop);
113 TIMING_DIFF (cur, start, stop);
115 TIMING_PRINT_MEAN ((double) cur, (double) iters);
118 static void
119 do_test (size_t align, size_t pos, size_t len)
121 size_t i;
122 int c;
123 RES_TYPE result;
124 CHAR *rej, *s;
126 align &= 7;
127 if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240)
128 return;
130 rej = (CHAR *) (buf2) + (random () & 255);
131 s = (CHAR *) (buf1) + align;
133 for (i = 0; i < len; ++i)
135 rej[i] = random () & BIG_CHAR;
136 if (!rej[i])
137 rej[i] = random () & BIG_CHAR;
138 if (!rej[i])
139 rej[i] = 1 + (random () & SMALL_CHAR);
141 rej[len] = '\0';
142 for (c = 1; c <= BIG_CHAR; ++c)
143 if (STRCHR (rej, c) == NULL)
144 break;
146 for (i = 0; i < pos; ++i)
148 s[i] = random () & BIG_CHAR;
149 if (STRCHR (rej, s[i]))
151 s[i] = random () & BIG_CHAR;
152 if (STRCHR (rej, s[i]))
153 s[i] = c;
156 s[pos] = rej[random () % (len + 1)];
157 if (s[pos])
159 for (i = pos + 1; i < pos + 10; ++i)
160 s[i] = random () & BIG_CHAR;
161 s[i] = '\0';
163 result = STRPBRK_RESULT (s, pos);
165 printf ("Length %4zd, alignment %2zd, rej len %2zd:", pos, align, len);
167 FOR_EACH_IMPL (impl, 0)
168 do_one_test (impl, s, rej, result);
170 putchar ('\n');
174 test_main (void)
176 size_t i;
178 test_init ();
180 printf ("%32s", "");
181 FOR_EACH_IMPL (impl, 0)
182 printf ("\t%s", impl->name);
183 putchar ('\n');
185 for (i = 0; i < 32; ++i)
187 do_test (0, 512, i);
188 do_test (i, 512, i);
191 for (i = 1; i < 8; ++i)
193 do_test (0, 16 << i, 4);
194 do_test (i, 16 << i, 4);
197 for (i = 1; i < 8; ++i)
198 do_test (i, 64, 10);
200 for (i = 0; i < 64; ++i)
201 do_test (0, i, 6);
203 return ret;
206 #include "../test-skeleton.c"