xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Test and measure memmove functions. |
| 2 | Copyright (C) 1999-2016 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 | #ifdef TEST_BCOPY |
| 22 | # define TEST_NAME "bcopy" |
| 23 | #else |
| 24 | # define TEST_NAME "memmove" |
| 25 | #endif |
| 26 | #include "test-string.h" |
| 27 | |
| 28 | char *simple_memmove (char *, const char *, size_t); |
| 29 | |
| 30 | #ifdef TEST_BCOPY |
| 31 | typedef void (*proto_t) (const char *, char *, size_t); |
| 32 | void simple_bcopy (const char *, char *, size_t); |
| 33 | |
| 34 | IMPL (simple_bcopy, 0) |
| 35 | IMPL (bcopy, 1) |
| 36 | |
| 37 | void |
| 38 | simple_bcopy (const char *src, char *dst, size_t n) |
| 39 | { |
| 40 | simple_memmove (dst, src, n); |
| 41 | } |
| 42 | #else |
| 43 | typedef char *(*proto_t) (char *, const char *, size_t); |
| 44 | |
| 45 | IMPL (simple_memmove, 0) |
| 46 | IMPL (memmove, 1) |
| 47 | #endif |
| 48 | |
| 49 | char * |
| 50 | inhibit_loop_to_libcall |
| 51 | simple_memmove (char *dst, const char *src, size_t n) |
| 52 | { |
| 53 | char *ret = dst; |
| 54 | if (src < dst) |
| 55 | { |
| 56 | dst += n; |
| 57 | src += n; |
| 58 | while (n--) |
| 59 | *--dst = *--src; |
| 60 | } |
| 61 | else |
| 62 | while (n--) |
| 63 | *dst++ = *src++; |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | static void |
| 68 | do_one_test (impl_t *impl, char *dst, char *src, const char *orig_src, |
| 69 | size_t len) |
| 70 | { |
| 71 | memcpy (src, orig_src, len); |
| 72 | #ifdef TEST_BCOPY |
| 73 | CALL (impl, src, dst, len); |
| 74 | #else |
| 75 | char *res; |
| 76 | |
| 77 | res = CALL (impl, dst, src, len); |
| 78 | if (res != dst) |
| 79 | { |
| 80 | error (0, 0, "Wrong result in function %s %p %p", impl->name, |
| 81 | res, dst); |
| 82 | ret = 1; |
| 83 | return; |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | if (memcmp (dst, orig_src, len) != 0) |
| 88 | { |
| 89 | error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"", |
| 90 | impl->name, dst, src); |
| 91 | ret = 1; |
| 92 | return; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | do_test (size_t align1, size_t align2, size_t len) |
| 98 | { |
| 99 | size_t i, j; |
| 100 | char *s1, *s2; |
| 101 | |
| 102 | align1 &= 63; |
| 103 | if (align1 + len >= page_size) |
| 104 | return; |
| 105 | |
| 106 | align2 &= 63; |
| 107 | if (align2 + len >= page_size) |
| 108 | return; |
| 109 | |
| 110 | s1 = (char *) (buf1 + align1); |
| 111 | s2 = (char *) (buf2 + align2); |
| 112 | |
| 113 | for (i = 0, j = 1; i < len; i++, j += 23) |
| 114 | s1[i] = j; |
| 115 | |
| 116 | FOR_EACH_IMPL (impl, 0) |
| 117 | do_one_test (impl, s2, (char *) (buf2 + align1), s1, len); |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | do_random_tests (void) |
| 122 | { |
| 123 | size_t i, n, align1, align2, len, size; |
| 124 | size_t srcstart, srcend, dststart, dstend; |
| 125 | int c; |
| 126 | unsigned char *p1, *p2; |
| 127 | #ifndef TEST_BCOPY |
| 128 | unsigned char *res; |
| 129 | #endif |
| 130 | |
| 131 | for (n = 0; n < ITERATIONS; n++) |
| 132 | { |
| 133 | if ((random () & 255) == 0) |
| 134 | size = 65536; |
| 135 | else |
| 136 | size = 512; |
| 137 | if (size > page_size) |
| 138 | size = page_size; |
| 139 | if ((random () & 3) == 0) |
| 140 | { |
| 141 | len = random () & (size - 1); |
| 142 | align1 = size - len - (random () & 31); |
| 143 | align2 = size - len - (random () & 31); |
| 144 | if (align1 > size) |
| 145 | align1 = 0; |
| 146 | if (align2 > size) |
| 147 | align2 = 0; |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | align1 = random () & (size / 2 - 1); |
| 152 | align2 = random () & (size / 2 - 1); |
| 153 | len = random () & (size - 1); |
| 154 | if (align1 + len > size) |
| 155 | align1 = size - len; |
| 156 | if (align2 + len > size) |
| 157 | align2 = size - len; |
| 158 | } |
| 159 | |
| 160 | p1 = buf1 + page_size - size; |
| 161 | p2 = buf2 + page_size - size; |
| 162 | c = random () & 255; |
| 163 | srcend = align1 + len + 256; |
| 164 | if (srcend > size) |
| 165 | srcend = size; |
| 166 | if (align1 > 256) |
| 167 | srcstart = align1 - 256; |
| 168 | else |
| 169 | srcstart = 0; |
| 170 | for (i = srcstart; i < srcend; ++i) |
| 171 | p1[i] = random () & 255; |
| 172 | dstend = align2 + len + 256; |
| 173 | if (dstend > size) |
| 174 | dstend = size; |
| 175 | if (align2 > 256) |
| 176 | dststart = align2 - 256; |
| 177 | else |
| 178 | dststart = 0; |
| 179 | |
| 180 | FOR_EACH_IMPL (impl, 1) |
| 181 | { |
| 182 | memset (p2 + dststart, c, dstend - dststart); |
| 183 | memcpy (p2 + srcstart, p1 + srcstart, srcend - srcstart); |
| 184 | #ifdef TEST_BCOPY |
| 185 | CALL (impl, (char *) (p2 + align1), (char *) (p2 + align2), len); |
| 186 | #else |
| 187 | res = (unsigned char *) CALL (impl, |
| 188 | (char *) (p2 + align2), |
| 189 | (char *) (p2 + align1), len); |
| 190 | if (res != p2 + align2) |
| 191 | { |
| 192 | error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p", |
| 193 | n, impl->name, align1, align2, len, res, p2 + align2); |
| 194 | ret = 1; |
| 195 | } |
| 196 | #endif |
| 197 | if (memcmp (p1 + align1, p2 + align2, len)) |
| 198 | { |
| 199 | error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)", |
| 200 | n, impl->name, align1, align2, len); |
| 201 | ret = 1; |
| 202 | } |
| 203 | for (i = dststart; i < dstend; ++i) |
| 204 | { |
| 205 | if (i >= align2 && i < align2 + len) |
| 206 | { |
| 207 | i = align2 + len - 1; |
| 208 | continue; |
| 209 | } |
| 210 | if (i >= srcstart && i < srcend) |
| 211 | { |
| 212 | i = srcend - 1; |
| 213 | continue; |
| 214 | } |
| 215 | if (p2[i] != c) |
| 216 | { |
| 217 | error (0, 0, "Iteration %zd - garbage in memset area, %s (%zd, %zd, %zd)", |
| 218 | n, impl->name, align1, align2, len); |
| 219 | ret = 1; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (srcstart < align2 |
| 225 | && memcmp (p2 + srcstart, p1 + srcstart, |
| 226 | (srcend > align2 ? align2 : srcend) - srcstart)) |
| 227 | { |
| 228 | error (0, 0, "Iteration %zd - garbage before dst, %s (%zd, %zd, %zd)", |
| 229 | n, impl->name, align1, align2, len); |
| 230 | ret = 1; |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | i = srcstart > align2 + len ? srcstart : align2 + len; |
| 235 | if (srcend > align2 + len |
| 236 | && memcmp (p2 + i, p1 + i, srcend - i)) |
| 237 | { |
| 238 | error (0, 0, "Iteration %zd - garbage after dst, %s (%zd, %zd, %zd)", |
| 239 | n, impl->name, align1, align2, len); |
| 240 | ret = 1; |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | int |
| 248 | test_main (void) |
| 249 | { |
| 250 | size_t i; |
| 251 | |
| 252 | test_init (); |
| 253 | |
| 254 | printf ("%23s", ""); |
| 255 | FOR_EACH_IMPL (impl, 0) |
| 256 | printf ("\t%s", impl->name); |
| 257 | putchar ('\n'); |
| 258 | |
| 259 | for (i = 0; i < 14; ++i) |
| 260 | { |
| 261 | do_test (0, 32, 1 << i); |
| 262 | do_test (32, 0, 1 << i); |
| 263 | do_test (0, i, 1 << i); |
| 264 | do_test (i, 0, 1 << i); |
| 265 | } |
| 266 | |
| 267 | for (i = 0; i < 32; ++i) |
| 268 | { |
| 269 | do_test (0, 32, i); |
| 270 | do_test (32, 0, i); |
| 271 | do_test (0, i, i); |
| 272 | do_test (i, 0, i); |
| 273 | } |
| 274 | |
| 275 | for (i = 3; i < 32; ++i) |
| 276 | { |
| 277 | if ((i & (i - 1)) == 0) |
| 278 | continue; |
| 279 | do_test (0, 32, 16 * i); |
| 280 | do_test (32, 0, 16 * i); |
| 281 | do_test (0, i, 16 * i); |
| 282 | do_test (i, 0, 16 * i); |
| 283 | } |
| 284 | |
| 285 | do_random_tests (); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | #include "../test-skeleton.c" |