| 1 | /* Test and measure strpbrk functions.
|
|---|
| 2 | Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
|
|---|
| 3 | This file is part of the GNU C Library.
|
|---|
| 4 | Written by Jakub Jelinek <[email protected]>, 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, write to the Free
|
|---|
| 18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA. */
|
|---|
| 20 |
|
|---|
| 21 | #ifndef STRPBRK_RESULT
|
|---|
| 22 | # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL)
|
|---|
| 23 | # define RES_TYPE char *
|
|---|
| 24 | # define TEST_MAIN
|
|---|
| 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 | if (HP_TIMING_AVAIL)
|
|---|
| 75 | {
|
|---|
| 76 | hp_timing_t start __attribute ((unused));
|
|---|
| 77 | hp_timing_t stop __attribute ((unused));
|
|---|
| 78 | hp_timing_t best_time = ~ (hp_timing_t) 0;
|
|---|
| 79 | size_t i;
|
|---|
| 80 |
|
|---|
| 81 | for (i = 0; i < 32; ++i)
|
|---|
| 82 | {
|
|---|
| 83 | HP_TIMING_NOW (start);
|
|---|
| 84 | CALL (impl, s, rej);
|
|---|
| 85 | HP_TIMING_NOW (stop);
|
|---|
| 86 | HP_TIMING_BEST (best_time, start, stop);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | printf ("\t%zd", (size_t) best_time);
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | static void
|
|---|
| 94 | do_test (size_t align, size_t pos, size_t len)
|
|---|
| 95 | {
|
|---|
| 96 | size_t i;
|
|---|
| 97 | int c;
|
|---|
| 98 | RES_TYPE result;
|
|---|
| 99 | char *rej, *s;
|
|---|
| 100 |
|
|---|
| 101 | align &= 7;
|
|---|
| 102 | if (align + pos + 10 >= page_size || len > 240)
|
|---|
| 103 | return;
|
|---|
| 104 |
|
|---|
| 105 | rej = buf2 + (random () & 255);
|
|---|
| 106 | s = buf1 + align;
|
|---|
| 107 |
|
|---|
| 108 | for (i = 0; i < len; ++i)
|
|---|
| 109 | {
|
|---|
| 110 | rej[i] = random () & 255;
|
|---|
| 111 | if (!rej[i])
|
|---|
| 112 | rej[i] = random () & 255;
|
|---|
| 113 | if (!rej[i])
|
|---|
| 114 | rej[i] = 1 + (random () & 127);
|
|---|
| 115 | }
|
|---|
| 116 | rej[len] = '\0';
|
|---|
| 117 | for (c = 1; c <= 255; ++c)
|
|---|
| 118 | if (strchr (rej, c) == NULL)
|
|---|
| 119 | break;
|
|---|
| 120 |
|
|---|
| 121 | for (i = 0; i < pos; ++i)
|
|---|
| 122 | {
|
|---|
| 123 | s[i] = random () & 255;
|
|---|
| 124 | if (strchr (rej, s[i]))
|
|---|
| 125 | {
|
|---|
| 126 | s[i] = random () & 255;
|
|---|
| 127 | if (strchr (rej, s[i]))
|
|---|
| 128 | s[i] = c;
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 | s[pos] = rej[random () % (len + 1)];
|
|---|
| 132 | if (s[pos])
|
|---|
| 133 | {
|
|---|
| 134 | for (i = pos + 1; i < pos + 10; ++i)
|
|---|
| 135 | s[i] = random () & 255;
|
|---|
| 136 | s[i] = '\0';
|
|---|
| 137 | }
|
|---|
| 138 | result = STRPBRK_RESULT (s, pos);
|
|---|
| 139 |
|
|---|
| 140 | if (HP_TIMING_AVAIL)
|
|---|
| 141 | printf ("Length %4zd, alignment %2zd, rej len %2zd:", pos, align, len);
|
|---|
| 142 |
|
|---|
| 143 | FOR_EACH_IMPL (impl, 0)
|
|---|
| 144 | do_one_test (impl, s, rej, result);
|
|---|
| 145 |
|
|---|
| 146 | if (HP_TIMING_AVAIL)
|
|---|
| 147 | putchar ('\n');
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | static void
|
|---|
| 151 | do_random_tests (void)
|
|---|
| 152 | {
|
|---|
| 153 | size_t i, j, n, align, pos, len, rlen;
|
|---|
| 154 | RES_TYPE result;
|
|---|
| 155 | int c;
|
|---|
| 156 | unsigned char *p = buf1 + page_size - 512;
|
|---|
| 157 | unsigned char *rej;
|
|---|
| 158 |
|
|---|
| 159 | for (n = 0; n < ITERATIONS; n++)
|
|---|
| 160 | {
|
|---|
| 161 | align = random () & 15;
|
|---|
| 162 | pos = random () & 511;
|
|---|
| 163 | if (pos + align >= 511)
|
|---|
| 164 | pos = 510 - align - (random () & 7);
|
|---|
| 165 | len = random () & 511;
|
|---|
| 166 | if (pos >= len && (random () & 1))
|
|---|
| 167 | len = pos + 1 + (random () & 7);
|
|---|
| 168 | if (len + align >= 512)
|
|---|
| 169 | len = 511 - align - (random () & 7);
|
|---|
| 170 | if (random () & 1)
|
|---|
| 171 | rlen = random () & 63;
|
|---|
| 172 | else
|
|---|
| 173 | rlen = random () & 15;
|
|---|
| 174 | rej = buf2 + page_size - rlen - 1 - (random () & 7);
|
|---|
| 175 | for (i = 0; i < rlen; ++i)
|
|---|
| 176 | {
|
|---|
| 177 | rej[i] = random () & 255;
|
|---|
| 178 | if (!rej[i])
|
|---|
| 179 | rej[i] = random () & 255;
|
|---|
| 180 | if (!rej[i])
|
|---|
| 181 | rej[i] = 1 + (random () & 127);
|
|---|
| 182 | }
|
|---|
| 183 | rej[i] = '\0';
|
|---|
| 184 | for (c = 1; c <= 255; ++c)
|
|---|
| 185 | if (strchr (rej, c) == NULL)
|
|---|
| 186 | break;
|
|---|
| 187 | j = (pos > len ? pos : len) + align + 64;
|
|---|
| 188 | if (j > 512)
|
|---|
| 189 | j = 512;
|
|---|
| 190 |
|
|---|
| 191 | for (i = 0; i < j; i++)
|
|---|
| 192 | {
|
|---|
| 193 | if (i == len + align)
|
|---|
| 194 | p[i] = '\0';
|
|---|
| 195 | else if (i == pos + align)
|
|---|
| 196 | p[i] = rej[random () % (rlen + 1)];
|
|---|
| 197 | else if (i < align || i > pos + align)
|
|---|
| 198 | p[i] = random () & 255;
|
|---|
| 199 | else
|
|---|
| 200 | {
|
|---|
| 201 | p[i] = random () & 255;
|
|---|
| 202 | if (strchr (rej, p[i]))
|
|---|
| 203 | {
|
|---|
| 204 | p[i] = random () & 255;
|
|---|
| 205 | if (strchr (rej, p[i]))
|
|---|
| 206 | p[i] = c;
|
|---|
| 207 | }
|
|---|
| 208 | }
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | result = STRPBRK_RESULT (p + align, pos < len ? pos : len);
|
|---|
| 212 |
|
|---|
| 213 | FOR_EACH_IMPL (impl, 1)
|
|---|
| 214 | if (CALL (impl, p + align, rej) != result)
|
|---|
| 215 | {
|
|---|
| 216 | error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %p != %p",
|
|---|
| 217 | n, impl->name, align, rej, rlen, pos, len,
|
|---|
| 218 | (void *) CALL (impl, p + align, rej), (void *) result);
|
|---|
| 219 | ret = 1;
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | int
|
|---|
| 225 | test_main (void)
|
|---|
| 226 | {
|
|---|
| 227 | size_t i;
|
|---|
| 228 |
|
|---|
| 229 | test_init ();
|
|---|
| 230 |
|
|---|
| 231 | printf ("%32s", "");
|
|---|
| 232 | FOR_EACH_IMPL (impl, 0)
|
|---|
| 233 | printf ("\t%s", impl->name);
|
|---|
| 234 | putchar ('\n');
|
|---|
| 235 |
|
|---|
| 236 | for (i = 0; i < 32; ++i)
|
|---|
| 237 | {
|
|---|
| 238 | do_test (0, 512, i);
|
|---|
| 239 | do_test (i, 512, i);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | for (i = 1; i < 8; ++i)
|
|---|
| 243 | {
|
|---|
| 244 | do_test (0, 16 << i, 4);
|
|---|
| 245 | do_test (i, 16 << i, 4);
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | for (i = 1; i < 8; ++i)
|
|---|
| 249 | do_test (i, 64, 10);
|
|---|
| 250 |
|
|---|
| 251 | for (i = 0; i < 64; ++i)
|
|---|
| 252 | do_test (0, i, 6);
|
|---|
| 253 |
|
|---|
| 254 | do_random_tests ();
|
|---|
| 255 | return ret;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | #include "../test-skeleton.c"
|
|---|