lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Test and measure strlen 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 "strnlen" |
| 22 | #include "test-string.h" |
| 23 | |
| 24 | typedef size_t (*proto_t) (const char *, size_t); |
| 25 | size_t simple_strnlen (const char *, size_t); |
| 26 | |
| 27 | IMPL (simple_strnlen, 0) |
| 28 | IMPL (strnlen, 1) |
| 29 | |
| 30 | size_t |
| 31 | simple_strnlen (const char *s, size_t maxlen) |
| 32 | { |
| 33 | size_t i; |
| 34 | |
| 35 | for (i = 0; i < maxlen && s[i]; ++i); |
| 36 | return i; |
| 37 | } |
| 38 | |
| 39 | static void |
| 40 | do_one_test (impl_t *impl, const char *s, size_t maxlen, size_t exp_len) |
| 41 | { |
| 42 | size_t len = CALL (impl, s, maxlen); |
| 43 | if (len != exp_len) |
| 44 | { |
| 45 | error (0, 0, "Wrong result in function %s %zd %zd", impl->name, |
| 46 | len, exp_len); |
| 47 | ret = 1; |
| 48 | return; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | static void |
| 53 | do_test (size_t align, size_t len, size_t maxlen, int max_char) |
| 54 | { |
| 55 | size_t i; |
| 56 | |
| 57 | align &= 7; |
| 58 | if (align + len >= page_size) |
| 59 | return; |
| 60 | |
| 61 | for (i = 0; i < len; ++i) |
| 62 | buf1[align + i] = 1 + 7 * i % max_char; |
| 63 | buf1[align + len] = 0; |
| 64 | |
| 65 | FOR_EACH_IMPL (impl, 0) |
| 66 | do_one_test (impl, (char *) (buf1 + align), maxlen, MIN (len, maxlen)); |
| 67 | } |
| 68 | |
| 69 | static void |
| 70 | do_random_tests (void) |
| 71 | { |
| 72 | size_t i, j, n, align, len; |
| 73 | unsigned char *p = buf1 + page_size - 512; |
| 74 | |
| 75 | for (n = 0; n < ITERATIONS; n++) |
| 76 | { |
| 77 | align = random () & 15; |
| 78 | len = random () & 511; |
| 79 | if (len + align > 510) |
| 80 | len = 511 - align - (random () & 7); |
| 81 | j = len + align + 64; |
| 82 | if (j > 512) |
| 83 | j = 512; |
| 84 | |
| 85 | for (i = 0; i < j; i++) |
| 86 | { |
| 87 | if (i == len + align) |
| 88 | p[i] = 0; |
| 89 | else |
| 90 | { |
| 91 | p[i] = random () & 255; |
| 92 | if (i >= align && i < len + align && !p[i]) |
| 93 | p[i] = (random () & 127) + 1; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | FOR_EACH_IMPL (impl, 1) |
| 98 | { |
| 99 | if (len > 0 |
| 100 | && CALL (impl, (char *) (p + align), len - 1) != len - 1) |
| 101 | { |
| 102 | error (0, 0, "Iteration %zd (limited) - wrong result in function %s (%zd) %zd != %zd, p %p", |
| 103 | n, impl->name, align, |
| 104 | CALL (impl, (char *) (p + align), len - 1), len - 1, p); |
| 105 | ret = 1; |
| 106 | } |
| 107 | if (CALL (impl, (char *) (p + align), len) != len) |
| 108 | { |
| 109 | error (0, 0, "Iteration %zd (exact) - wrong result in function %s (%zd) %zd != %zd, p %p", |
| 110 | n, impl->name, align, |
| 111 | CALL (impl, (char *) (p + align), len), len, p); |
| 112 | ret = 1; |
| 113 | } |
| 114 | if (CALL (impl, (char *) (p + align), len + 1) != len) |
| 115 | { |
| 116 | error (0, 0, "Iteration %zd (long) - wrong result in function %s (%zd) %zd != %zd, p %p", |
| 117 | n, impl->name, align, |
| 118 | CALL (impl, (char *) (p + align), len + 1), len, p); |
| 119 | ret = 1; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | int |
| 126 | test_main (void) |
| 127 | { |
| 128 | size_t i; |
| 129 | |
| 130 | test_init (); |
| 131 | |
| 132 | printf ("%20s", ""); |
| 133 | FOR_EACH_IMPL (impl, 0) |
| 134 | printf ("\t%s", impl->name); |
| 135 | putchar ('\n'); |
| 136 | |
| 137 | for (i = 1; i < 8; ++i) |
| 138 | { |
| 139 | do_test (0, i, i - 1, 127); |
| 140 | do_test (0, i, i, 127); |
| 141 | do_test (0, i, i + 1, 127); |
| 142 | } |
| 143 | |
| 144 | for (i = 1; i < 8; ++i) |
| 145 | { |
| 146 | do_test (i, i, i - 1, 127); |
| 147 | do_test (i, i, i, 127); |
| 148 | do_test (i, i, i + 1, 127); |
| 149 | } |
| 150 | |
| 151 | for (i = 2; i <= 10; ++i) |
| 152 | { |
| 153 | do_test (0, 1 << i, 5000, 127); |
| 154 | do_test (1, 1 << i, 5000, 127); |
| 155 | } |
| 156 | |
| 157 | for (i = 1; i < 8; ++i) |
| 158 | do_test (0, i, 5000, 255); |
| 159 | |
| 160 | for (i = 1; i < 8; ++i) |
| 161 | do_test (i, i, 5000, 255); |
| 162 | |
| 163 | for (i = 2; i <= 10; ++i) |
| 164 | { |
| 165 | do_test (0, 1 << i, 5000, 255); |
| 166 | do_test (1, 1 << i, 5000, 255); |
| 167 | } |
| 168 | |
| 169 | do_random_tests (); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | #include "../test-skeleton.c" |