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