]>
Commit | Line | Data |
---|---|---|
97020474 | 1 | /* Measure strpbrk functions. |
f7a9f785 | 2 | Copyright (C) 2013-2016 Free Software Foundation, Inc. |
97020474 SP |
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 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
f0ba6598 SL |
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 */ | |
33 | ||
97020474 SP |
34 | #ifndef STRPBRK_RESULT |
35 | # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL) | |
f0ba6598 | 36 | # define RES_TYPE CHAR * |
97020474 | 37 | # define TEST_MAIN |
f0ba6598 SL |
38 | # ifndef WIDE |
39 | # define TEST_NAME "strpbrk" | |
40 | # else | |
41 | # define TEST_NAME "wcspbrk" | |
42 | # endif /* WIDE */ | |
97020474 SP |
43 | # include "bench-string.h" |
44 | ||
f0ba6598 SL |
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 */ | |
55 | ||
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 *); | |
59 | ||
60 | IMPL (STUPID_STRPBRK, 0) | |
61 | IMPL (SIMPLE_STRPBRK, 0) | |
62 | IMPL (STRPBRK, 1) | |
63 | ||
64 | CHAR * | |
65 | SIMPLE_STRPBRK (const CHAR *s, const CHAR *rej) | |
97020474 | 66 | { |
f0ba6598 SL |
67 | const CHAR *r; |
68 | CHAR c; | |
97020474 SP |
69 | |
70 | while ((c = *s++) != '\0') | |
71 | for (r = rej; *r != '\0'; ++r) | |
72 | if (*r == c) | |
f0ba6598 | 73 | return (CHAR *) s - 1; |
97020474 SP |
74 | return NULL; |
75 | } | |
76 | ||
f0ba6598 SL |
77 | CHAR * |
78 | STUPID_STRPBRK (const CHAR *s, const CHAR *rej) | |
97020474 | 79 | { |
f0ba6598 | 80 | size_t ns = STRLEN (s), nrej = STRLEN (rej); |
97020474 SP |
81 | size_t i, j; |
82 | ||
83 | for (i = 0; i < ns; ++i) | |
84 | for (j = 0; j < nrej; ++j) | |
85 | if (s[i] == rej[j]) | |
f0ba6598 | 86 | return (CHAR *) s + i; |
97020474 SP |
87 | return NULL; |
88 | } | |
f0ba6598 | 89 | #endif /* !STRPBRK_RESULT */ |
97020474 SP |
90 | |
91 | static void | |
f0ba6598 | 92 | do_one_test (impl_t *impl, const CHAR *s, const CHAR *rej, RES_TYPE exp_res) |
97020474 SP |
93 | { |
94 | RES_TYPE res = CALL (impl, s, rej); | |
44558701 WN |
95 | size_t i, iters = INNER_LOOP_ITERS; |
96 | timing_t start, stop, cur; | |
97 | ||
97020474 SP |
98 | if (res != exp_res) |
99 | { | |
100 | error (0, 0, "Wrong result in function %s %p %p", impl->name, | |
101 | (void *) res, (void *) exp_res); | |
102 | ret = 1; | |
103 | return; | |
104 | } | |
105 | ||
44558701 WN |
106 | TIMING_NOW (start); |
107 | for (i = 0; i < iters; ++i) | |
97020474 | 108 | { |
44558701 WN |
109 | CALL (impl, s, rej); |
110 | } | |
111 | TIMING_NOW (stop); | |
97020474 | 112 | |
44558701 | 113 | TIMING_DIFF (cur, start, stop); |
97020474 | 114 | |
44558701 | 115 | TIMING_PRINT_MEAN ((double) cur, (double) iters); |
97020474 SP |
116 | } |
117 | ||
118 | static void | |
119 | do_test (size_t align, size_t pos, size_t len) | |
120 | { | |
121 | size_t i; | |
122 | int c; | |
123 | RES_TYPE result; | |
f0ba6598 | 124 | CHAR *rej, *s; |
97020474 SP |
125 | |
126 | align &= 7; | |
f0ba6598 | 127 | if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240) |
97020474 SP |
128 | return; |
129 | ||
f0ba6598 SL |
130 | rej = (CHAR *) (buf2) + (random () & 255); |
131 | s = (CHAR *) (buf1) + align; | |
97020474 SP |
132 | |
133 | for (i = 0; i < len; ++i) | |
134 | { | |
f0ba6598 | 135 | rej[i] = random () & BIG_CHAR; |
97020474 | 136 | if (!rej[i]) |
f0ba6598 | 137 | rej[i] = random () & BIG_CHAR; |
97020474 | 138 | if (!rej[i]) |
f0ba6598 | 139 | rej[i] = 1 + (random () & SMALL_CHAR); |
97020474 SP |
140 | } |
141 | rej[len] = '\0'; | |
f0ba6598 SL |
142 | for (c = 1; c <= BIG_CHAR; ++c) |
143 | if (STRCHR (rej, c) == NULL) | |
97020474 SP |
144 | break; |
145 | ||
146 | for (i = 0; i < pos; ++i) | |
147 | { | |
f0ba6598 SL |
148 | s[i] = random () & BIG_CHAR; |
149 | if (STRCHR (rej, s[i])) | |
97020474 | 150 | { |
f0ba6598 SL |
151 | s[i] = random () & BIG_CHAR; |
152 | if (STRCHR (rej, s[i])) | |
97020474 SP |
153 | s[i] = c; |
154 | } | |
155 | } | |
156 | s[pos] = rej[random () % (len + 1)]; | |
157 | if (s[pos]) | |
158 | { | |
159 | for (i = pos + 1; i < pos + 10; ++i) | |
f0ba6598 | 160 | s[i] = random () & BIG_CHAR; |
97020474 SP |
161 | s[i] = '\0'; |
162 | } | |
163 | result = STRPBRK_RESULT (s, pos); | |
164 | ||
44558701 | 165 | printf ("Length %4zd, alignment %2zd, rej len %2zd:", pos, align, len); |
97020474 SP |
166 | |
167 | FOR_EACH_IMPL (impl, 0) | |
168 | do_one_test (impl, s, rej, result); | |
169 | ||
44558701 | 170 | putchar ('\n'); |
97020474 SP |
171 | } |
172 | ||
173 | int | |
174 | test_main (void) | |
175 | { | |
176 | size_t i; | |
177 | ||
178 | test_init (); | |
179 | ||
180 | printf ("%32s", ""); | |
181 | FOR_EACH_IMPL (impl, 0) | |
182 | printf ("\t%s", impl->name); | |
183 | putchar ('\n'); | |
184 | ||
185 | for (i = 0; i < 32; ++i) | |
186 | { | |
187 | do_test (0, 512, i); | |
188 | do_test (i, 512, i); | |
189 | } | |
190 | ||
191 | for (i = 1; i < 8; ++i) | |
192 | { | |
193 | do_test (0, 16 << i, 4); | |
194 | do_test (i, 16 << i, 4); | |
195 | } | |
196 | ||
197 | for (i = 1; i < 8; ++i) | |
198 | do_test (i, 64, 10); | |
199 | ||
200 | for (i = 0; i < 64; ++i) | |
201 | do_test (0, i, 6); | |
202 | ||
203 | return ret; | |
204 | } | |
205 | ||
b598e134 | 206 | #include <support/test-driver.c> |