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