xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Test and measure strstr functions. |
| 2 | Copyright (C) 2010-2016 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Written by Ulrich Drepper <drepper@redhat.com>, 2010. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #define TEST_MAIN |
| 21 | #define TEST_NAME "strstr" |
| 22 | #include "test-string.h" |
| 23 | |
| 24 | |
| 25 | #define STRSTR simple_strstr |
| 26 | #include "strstr.c" |
| 27 | |
| 28 | |
| 29 | static char * |
| 30 | stupid_strstr (const char *s1, const char *s2) |
| 31 | { |
| 32 | ssize_t s1len = strlen (s1); |
| 33 | ssize_t s2len = strlen (s2); |
| 34 | |
| 35 | if (s2len > s1len) |
| 36 | return NULL; |
| 37 | |
| 38 | for (ssize_t i = 0; i <= s1len - s2len; ++i) |
| 39 | { |
| 40 | size_t j; |
| 41 | for (j = 0; j < s2len; ++j) |
| 42 | if (s1[i + j] != s2[j]) |
| 43 | break; |
| 44 | if (j == s2len) |
| 45 | return (char *) s1 + i; |
| 46 | } |
| 47 | |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | typedef char *(*proto_t) (const char *, const char *); |
| 53 | |
| 54 | IMPL (stupid_strstr, 0) |
| 55 | IMPL (simple_strstr, 0) |
| 56 | IMPL (strstr, 1) |
| 57 | |
| 58 | |
| 59 | static int |
| 60 | check_result (impl_t *impl, const char *s1, const char *s2, |
| 61 | char *exp_result) |
| 62 | { |
| 63 | char *result = CALL (impl, s1, s2); |
| 64 | if (result != exp_result) |
| 65 | { |
| 66 | error (0, 0, "Wrong result in function %s %s %s", impl->name, |
| 67 | result, exp_result); |
| 68 | ret = 1; |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static void |
| 76 | do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result) |
| 77 | { |
| 78 | if (check_result (impl, s1, s2, exp_result) < 0) |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | static void |
| 84 | do_test (size_t align1, size_t align2, size_t len1, size_t len2, |
| 85 | int fail) |
| 86 | { |
| 87 | char *s1 = (char *) (buf1 + align1); |
| 88 | char *s2 = (char *) (buf2 + align2); |
| 89 | |
| 90 | static const char d[] = "1234567890abcdef"; |
| 91 | #define dl (sizeof (d) - 1) |
| 92 | char *ss2 = s2; |
| 93 | for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0) |
| 94 | { |
| 95 | size_t t = l > dl ? dl : l; |
| 96 | ss2 = mempcpy (ss2, d, t); |
| 97 | } |
| 98 | s2[len2] = '\0'; |
| 99 | |
| 100 | if (fail) |
| 101 | { |
| 102 | char *ss1 = s1; |
| 103 | for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0) |
| 104 | { |
| 105 | size_t t = l > dl ? dl : l; |
| 106 | memcpy (ss1, d, t); |
| 107 | ++ss1[len2 > 7 ? 7 : len2 - 1]; |
| 108 | ss1 += t; |
| 109 | } |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | memset (s1, '0', len1); |
| 114 | memcpy (s1 + len1 - len2, s2, len2); |
| 115 | } |
| 116 | s1[len1] = '\0'; |
| 117 | |
| 118 | FOR_EACH_IMPL (impl, 0) |
| 119 | do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2); |
| 120 | |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | check1 (void) |
| 125 | { |
| 126 | const char s1[] = |
| 127 | "F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD_C3_A7_20_EF_BF_BD"; |
| 128 | const char s2[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD"; |
| 129 | char *exp_result; |
| 130 | |
| 131 | exp_result = stupid_strstr (s1, s2); |
| 132 | FOR_EACH_IMPL (impl, 0) |
| 133 | check_result (impl, s1, s2, exp_result); |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | check2 (void) |
| 138 | { |
| 139 | const char s1[] = ", enable_static, \0, enable_shared, "; |
| 140 | char *exp_result; |
| 141 | char *s2 = (void *) buf1 + page_size - 18; |
| 142 | |
| 143 | strcpy (s2, s1); |
| 144 | exp_result = stupid_strstr (s1, s1 + 18); |
| 145 | FOR_EACH_IMPL (impl, 0) |
| 146 | { |
| 147 | check_result (impl, s1, s1 + 18, exp_result); |
| 148 | check_result (impl, s2, s1 + 18, exp_result); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | static int |
| 153 | test_main (void) |
| 154 | { |
| 155 | test_init (); |
| 156 | |
| 157 | check1 (); |
| 158 | check2 (); |
| 159 | |
| 160 | printf ("%23s", ""); |
| 161 | FOR_EACH_IMPL (impl, 0) |
| 162 | printf ("\t%s", impl->name); |
| 163 | putchar ('\n'); |
| 164 | |
| 165 | for (size_t klen = 2; klen < 32; ++klen) |
| 166 | for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen) |
| 167 | { |
| 168 | do_test (0, 0, hlen, klen, 0); |
| 169 | do_test (0, 0, hlen, klen, 1); |
| 170 | do_test (0, 3, hlen, klen, 0); |
| 171 | do_test (0, 3, hlen, klen, 1); |
| 172 | do_test (0, 9, hlen, klen, 0); |
| 173 | do_test (0, 9, hlen, klen, 1); |
| 174 | do_test (0, 15, hlen, klen, 0); |
| 175 | do_test (0, 15, hlen, klen, 1); |
| 176 | |
| 177 | do_test (3, 0, hlen, klen, 0); |
| 178 | do_test (3, 0, hlen, klen, 1); |
| 179 | do_test (3, 3, hlen, klen, 0); |
| 180 | do_test (3, 3, hlen, klen, 1); |
| 181 | do_test (3, 9, hlen, klen, 0); |
| 182 | do_test (3, 9, hlen, klen, 1); |
| 183 | do_test (3, 15, hlen, klen, 0); |
| 184 | do_test (3, 15, hlen, klen, 1); |
| 185 | |
| 186 | do_test (9, 0, hlen, klen, 0); |
| 187 | do_test (9, 0, hlen, klen, 1); |
| 188 | do_test (9, 3, hlen, klen, 0); |
| 189 | do_test (9, 3, hlen, klen, 1); |
| 190 | do_test (9, 9, hlen, klen, 0); |
| 191 | do_test (9, 9, hlen, klen, 1); |
| 192 | do_test (9, 15, hlen, klen, 0); |
| 193 | do_test (9, 15, hlen, klen, 1); |
| 194 | |
| 195 | do_test (15, 0, hlen, klen, 0); |
| 196 | do_test (15, 0, hlen, klen, 1); |
| 197 | do_test (15, 3, hlen, klen, 0); |
| 198 | do_test (15, 3, hlen, klen, 1); |
| 199 | do_test (15, 9, hlen, klen, 0); |
| 200 | do_test (15, 9, hlen, klen, 1); |
| 201 | do_test (15, 15, hlen, klen, 0); |
| 202 | do_test (15, 15, hlen, klen, 1); |
| 203 | } |
| 204 | |
| 205 | do_test (0, 0, page_size - 1, 16, 0); |
| 206 | do_test (0, 0, page_size - 1, 16, 1); |
| 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | #include "../test-skeleton.c" |