lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Measure strcasestr 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 | #define TEST_MAIN |
| 20 | #define TEST_NAME "strcasestr" |
| 21 | #include "bench-string.h" |
| 22 | |
| 23 | |
| 24 | #define STRCASESTR simple_strcasestr |
| 25 | #define NO_ALIAS |
| 26 | #define __strncasecmp strncasecmp |
| 27 | #include "../string/strcasestr.c" |
| 28 | |
| 29 | |
| 30 | static char * |
| 31 | stupid_strcasestr (const char *s1, const char *s2) |
| 32 | { |
| 33 | ssize_t s1len = strlen (s1); |
| 34 | ssize_t s2len = strlen (s2); |
| 35 | |
| 36 | if (s2len > s1len) |
| 37 | return NULL; |
| 38 | |
| 39 | for (ssize_t i = 0; i <= s1len - s2len; ++i) |
| 40 | { |
| 41 | size_t j; |
| 42 | for (j = 0; j < s2len; ++j) |
| 43 | if (tolower (s1[i + j]) != tolower (s2[j])) |
| 44 | break; |
| 45 | if (j == s2len) |
| 46 | return (char *) s1 + i; |
| 47 | } |
| 48 | |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | typedef char *(*proto_t) (const char *, const char *); |
| 54 | |
| 55 | IMPL (stupid_strcasestr, 0) |
| 56 | IMPL (simple_strcasestr, 0) |
| 57 | IMPL (strcasestr, 1) |
| 58 | |
| 59 | |
| 60 | static void |
| 61 | do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result) |
| 62 | { |
| 63 | size_t i, iters = INNER_LOOP_ITERS; |
| 64 | timing_t start, stop, cur; |
| 65 | |
| 66 | TIMING_NOW (start); |
| 67 | for (i = 0; i < iters; ++i) |
| 68 | { |
| 69 | CALL (impl, s1, s2); |
| 70 | } |
| 71 | TIMING_NOW (stop); |
| 72 | |
| 73 | TIMING_DIFF (cur, start, stop); |
| 74 | |
| 75 | TIMING_PRINT_MEAN ((double) cur, (double) iters); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | static void |
| 80 | do_test (size_t align1, size_t align2, size_t len1, size_t len2, |
| 81 | int fail) |
| 82 | { |
| 83 | char *s1 = (char *) (buf1 + align1); |
| 84 | char *s2 = (char *) (buf2 + align2); |
| 85 | |
| 86 | static const char d[] = "1234567890abcxyz"; |
| 87 | #define dl (sizeof (d) - 1) |
| 88 | char *ss2 = s2; |
| 89 | for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0) |
| 90 | { |
| 91 | size_t t = l > dl ? dl : l; |
| 92 | ss2 = mempcpy (ss2, d, t); |
| 93 | } |
| 94 | s2[len2] = '\0'; |
| 95 | |
| 96 | if (fail) |
| 97 | { |
| 98 | char *ss1 = s1; |
| 99 | for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0) |
| 100 | { |
| 101 | size_t t = l > dl ? dl : l; |
| 102 | memcpy (ss1, d, t); |
| 103 | ++ss1[len2 > 7 ? 7 : len2 - 1]; |
| 104 | ss1 += t; |
| 105 | } |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | memset (s1, '0', len1); |
| 110 | for (size_t i = 0; i < len2; ++i) |
| 111 | s1[len1 - len2 + i] = toupper (s2[i]); |
| 112 | } |
| 113 | s1[len1] = '\0'; |
| 114 | |
| 115 | printf ("Length %4zd/%zd, alignment %2zd/%2zd, %s:", |
| 116 | len1, len2, align1, align2, fail ? "fail" : "found"); |
| 117 | |
| 118 | FOR_EACH_IMPL (impl, 0) |
| 119 | do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2); |
| 120 | |
| 121 | putchar ('\n'); |
| 122 | } |
| 123 | |
| 124 | static int |
| 125 | test_main (void) |
| 126 | { |
| 127 | test_init (); |
| 128 | |
| 129 | printf ("%23s", ""); |
| 130 | FOR_EACH_IMPL (impl, 0) |
| 131 | printf ("\t%s", impl->name); |
| 132 | putchar ('\n'); |
| 133 | |
| 134 | for (size_t klen = 2; klen < 32; ++klen) |
| 135 | for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen) |
| 136 | { |
| 137 | do_test (0, 0, hlen, klen, 0); |
| 138 | do_test (0, 0, hlen, klen, 1); |
| 139 | do_test (0, 3, hlen, klen, 0); |
| 140 | do_test (0, 3, hlen, klen, 1); |
| 141 | do_test (0, 9, hlen, klen, 0); |
| 142 | do_test (0, 9, hlen, klen, 1); |
| 143 | do_test (0, 15, hlen, klen, 0); |
| 144 | do_test (0, 15, hlen, klen, 1); |
| 145 | |
| 146 | do_test (3, 0, hlen, klen, 0); |
| 147 | do_test (3, 0, hlen, klen, 1); |
| 148 | do_test (3, 3, hlen, klen, 0); |
| 149 | do_test (3, 3, hlen, klen, 1); |
| 150 | do_test (3, 9, hlen, klen, 0); |
| 151 | do_test (3, 9, hlen, klen, 1); |
| 152 | do_test (3, 15, hlen, klen, 0); |
| 153 | do_test (3, 15, hlen, klen, 1); |
| 154 | |
| 155 | do_test (9, 0, hlen, klen, 0); |
| 156 | do_test (9, 0, hlen, klen, 1); |
| 157 | do_test (9, 3, hlen, klen, 0); |
| 158 | do_test (9, 3, hlen, klen, 1); |
| 159 | do_test (9, 9, hlen, klen, 0); |
| 160 | do_test (9, 9, hlen, klen, 1); |
| 161 | do_test (9, 15, hlen, klen, 0); |
| 162 | do_test (9, 15, hlen, klen, 1); |
| 163 | |
| 164 | do_test (15, 0, hlen, klen, 0); |
| 165 | do_test (15, 0, hlen, klen, 1); |
| 166 | do_test (15, 3, hlen, klen, 0); |
| 167 | do_test (15, 3, hlen, klen, 1); |
| 168 | do_test (15, 9, hlen, klen, 0); |
| 169 | do_test (15, 9, hlen, klen, 1); |
| 170 | do_test (15, 15, hlen, klen, 0); |
| 171 | do_test (15, 15, hlen, klen, 1); |
| 172 | } |
| 173 | |
| 174 | do_test (0, 0, page_size - 1, 16, 0); |
| 175 | do_test (0, 0, page_size - 1, 16, 1); |
| 176 | |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | #include "../test-skeleton.c" |