lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Test and measure strpbrk functions. |
| 2 | Copyright (C) 1999-2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Written by Jakub Jelinek <jakub@redhat.com>, 1999. |
| 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 | #ifndef STRPBRK_RESULT |
| 21 | # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL) |
| 22 | # define RES_TYPE char * |
| 23 | # define TEST_MAIN |
| 24 | # define TEST_NAME "strpbrk" |
| 25 | # include "test-string.h" |
| 26 | |
| 27 | typedef char *(*proto_t) (const char *, const char *); |
| 28 | char *simple_strpbrk (const char *, const char *); |
| 29 | char *stupid_strpbrk (const char *, const char *); |
| 30 | |
| 31 | IMPL (stupid_strpbrk, 0) |
| 32 | IMPL (simple_strpbrk, 0) |
| 33 | IMPL (strpbrk, 1) |
| 34 | |
| 35 | char * |
| 36 | simple_strpbrk (const char *s, const char *rej) |
| 37 | { |
| 38 | const char *r; |
| 39 | char c; |
| 40 | |
| 41 | while ((c = *s++) != '\0') |
| 42 | for (r = rej; *r != '\0'; ++r) |
| 43 | if (*r == c) |
| 44 | return (char *) s - 1; |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | char * |
| 49 | stupid_strpbrk (const char *s, const char *rej) |
| 50 | { |
| 51 | size_t ns = strlen (s), nrej = strlen (rej); |
| 52 | size_t i, j; |
| 53 | |
| 54 | for (i = 0; i < ns; ++i) |
| 55 | for (j = 0; j < nrej; ++j) |
| 56 | if (s[i] == rej[j]) |
| 57 | return (char *) s + i; |
| 58 | return NULL; |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | static void |
| 63 | do_one_test (impl_t *impl, const char *s, const char *rej, RES_TYPE exp_res) |
| 64 | { |
| 65 | RES_TYPE res = CALL (impl, s, rej); |
| 66 | if (res != exp_res) |
| 67 | { |
| 68 | error (0, 0, "Wrong result in function %s %p %p", impl->name, |
| 69 | (void *) res, (void *) exp_res); |
| 70 | ret = 1; |
| 71 | return; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static void |
| 76 | do_test (size_t align, size_t pos, size_t len) |
| 77 | { |
| 78 | size_t i; |
| 79 | int c; |
| 80 | RES_TYPE result; |
| 81 | char *rej, *s; |
| 82 | |
| 83 | align &= 7; |
| 84 | if (align + pos + 10 >= page_size || len > 240) |
| 85 | return; |
| 86 | |
| 87 | rej = (char *) (buf2 + (random () & 255)); |
| 88 | s = (char *) (buf1 + align); |
| 89 | |
| 90 | for (i = 0; i < len; ++i) |
| 91 | { |
| 92 | rej[i] = random () & 255; |
| 93 | if (!rej[i]) |
| 94 | rej[i] = random () & 255; |
| 95 | if (!rej[i]) |
| 96 | rej[i] = 1 + (random () & 127); |
| 97 | } |
| 98 | rej[len] = '\0'; |
| 99 | for (c = 1; c <= 255; ++c) |
| 100 | if (strchr (rej, c) == NULL) |
| 101 | break; |
| 102 | |
| 103 | for (i = 0; i < pos; ++i) |
| 104 | { |
| 105 | s[i] = random () & 255; |
| 106 | if (strchr (rej, s[i])) |
| 107 | { |
| 108 | s[i] = random () & 255; |
| 109 | if (strchr (rej, s[i])) |
| 110 | s[i] = c; |
| 111 | } |
| 112 | } |
| 113 | s[pos] = rej[random () % (len + 1)]; |
| 114 | if (s[pos]) |
| 115 | { |
| 116 | for (i = pos + 1; i < pos + 10; ++i) |
| 117 | s[i] = random () & 255; |
| 118 | s[i] = '\0'; |
| 119 | } |
| 120 | result = STRPBRK_RESULT (s, pos); |
| 121 | |
| 122 | FOR_EACH_IMPL (impl, 0) |
| 123 | do_one_test (impl, s, rej, result); |
| 124 | } |
| 125 | |
| 126 | static void |
| 127 | do_random_tests (void) |
| 128 | { |
| 129 | size_t i, j, n, align, pos, len, rlen; |
| 130 | RES_TYPE result; |
| 131 | int c; |
| 132 | unsigned char *p = buf1 + page_size - 512; |
| 133 | unsigned char *rej; |
| 134 | |
| 135 | for (n = 0; n < ITERATIONS; n++) |
| 136 | { |
| 137 | align = random () & 15; |
| 138 | pos = random () & 511; |
| 139 | if (pos + align >= 511) |
| 140 | pos = 510 - align - (random () & 7); |
| 141 | len = random () & 511; |
| 142 | if (pos >= len && (random () & 1)) |
| 143 | len = pos + 1 + (random () & 7); |
| 144 | if (len + align >= 512) |
| 145 | len = 511 - align - (random () & 7); |
| 146 | if (random () & 1) |
| 147 | rlen = random () & 63; |
| 148 | else |
| 149 | rlen = random () & 15; |
| 150 | rej = buf2 + page_size - rlen - 1 - (random () & 7); |
| 151 | for (i = 0; i < rlen; ++i) |
| 152 | { |
| 153 | rej[i] = random () & 255; |
| 154 | if (!rej[i]) |
| 155 | rej[i] = random () & 255; |
| 156 | if (!rej[i]) |
| 157 | rej[i] = 1 + (random () & 127); |
| 158 | } |
| 159 | rej[i] = '\0'; |
| 160 | for (c = 1; c <= 255; ++c) |
| 161 | if (strchr ((char *) rej, c) == NULL) |
| 162 | break; |
| 163 | j = (pos > len ? pos : len) + align + 64; |
| 164 | if (j > 512) |
| 165 | j = 512; |
| 166 | |
| 167 | for (i = 0; i < j; i++) |
| 168 | { |
| 169 | if (i == len + align) |
| 170 | p[i] = '\0'; |
| 171 | else if (i == pos + align) |
| 172 | p[i] = rej[random () % (rlen + 1)]; |
| 173 | else if (i < align || i > pos + align) |
| 174 | p[i] = random () & 255; |
| 175 | else |
| 176 | { |
| 177 | p[i] = random () & 255; |
| 178 | if (strchr ((char *) rej, p[i])) |
| 179 | { |
| 180 | p[i] = random () & 255; |
| 181 | if (strchr ((char *) rej, p[i])) |
| 182 | p[i] = c; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | result = STRPBRK_RESULT ((char *) (p + align), pos < len ? pos : len); |
| 188 | |
| 189 | FOR_EACH_IMPL (impl, 1) |
| 190 | if (CALL (impl, (char *) (p + align), (char *) rej) != result) |
| 191 | { |
| 192 | error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %p != %p", |
| 193 | n, impl->name, align, rej, rlen, pos, len, |
| 194 | (void *) CALL (impl, (char *) (p + align), (char *) rej), |
| 195 | (void *) result); |
| 196 | ret = 1; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | int |
| 202 | test_main (void) |
| 203 | { |
| 204 | size_t i; |
| 205 | |
| 206 | test_init (); |
| 207 | |
| 208 | printf ("%32s", ""); |
| 209 | FOR_EACH_IMPL (impl, 0) |
| 210 | printf ("\t%s", impl->name); |
| 211 | putchar ('\n'); |
| 212 | |
| 213 | for (i = 0; i < 32; ++i) |
| 214 | { |
| 215 | do_test (0, 512, i); |
| 216 | do_test (i, 512, i); |
| 217 | } |
| 218 | |
| 219 | for (i = 1; i < 8; ++i) |
| 220 | { |
| 221 | do_test (0, 16 << i, 4); |
| 222 | do_test (i, 16 << i, 4); |
| 223 | } |
| 224 | |
| 225 | for (i = 1; i < 8; ++i) |
| 226 | do_test (i, 64, 10); |
| 227 | |
| 228 | for (i = 0; i < 64; ++i) |
| 229 | do_test (0, i, 6); |
| 230 | |
| 231 | do_random_tests (); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | #include "../test-skeleton.c" |