diff options
| author | Roland McGrath <roland@gnu.org> | 2002-11-07 19:15:01 +0000 |
|---|---|---|
| committer | Roland McGrath <roland@gnu.org> | 2002-11-07 19:15:01 +0000 |
| commit | 58ef9ef7bcbe3df172d9ff5fa840a63d89a19df4 (patch) | |
| tree | 707f53302db4e5a0d10362716902c53222dfb939 /string | |
| parent | cbf900b43010058d8489ce32ae43fb06a66410c1 (diff) | |
| download | glibc-58ef9ef7bcbe3df172d9ff5fa840a63d89a19df4.tar.xz glibc-58ef9ef7bcbe3df172d9ff5fa840a63d89a19df4.zip | |
* string/test-string.h: New file.
* string/test-strlen.c: New file.
* string/test-string.h: New file.
* string/test-strcmp.c: New file.
* string/test-strchr.c: New file.
* string/test-strrchr.c: New file.
* string/test-strcpy.c: New file.
* string/test-stpcpy.c: New file.
* string/test-strncpy.c: New file.
* string/test-stpncpy.c: New file.
* string/test-strpbrk.c: New file.
* string/test-strcspn.c: New file.
* string/test-strspn.c: New file.
* string/test-strcat.c: New file.
* string/test-strncmp.c: New file.
* string/test-memchr.c: New file.
* string/test-memcmp.c: New file.
* string/test-memset.c: New file.
* string/test-memcpy.c: New file.
* string/test-mempcpy.c: New file.
* string/test-memmove.c: New file.
* string/Makefile (strop-tests): New variable.
(tests): Add strop-tests.
(distribute): Add test-string.h.
Diffstat (limited to 'string')
| -rw-r--r-- | string/Makefile | 8 | ||||
| -rw-r--r-- | string/test-memchr.c | 189 | ||||
| -rw-r--r-- | string/test-memcmp.c | 222 | ||||
| -rw-r--r-- | string/test-memcpy.c | 273 | ||||
| -rw-r--r-- | string/test-memmove.c | 279 | ||||
| -rw-r--r-- | string/test-mempcpy.c | 38 | ||||
| -rw-r--r-- | string/test-memset.c | 212 | ||||
| -rw-r--r-- | string/test-stpcpy.c | 37 | ||||
| -rw-r--r-- | string/test-stpncpy.c | 45 | ||||
| -rw-r--r-- | string/test-strcat.c | 260 | ||||
| -rw-r--r-- | string/test-strchr.c | 220 | ||||
| -rw-r--r-- | string/test-strcmp.c | 239 | ||||
| -rw-r--r-- | string/test-strcpy.c | 225 | ||||
| -rw-r--r-- | string/test-strcspn.c | 45 | ||||
| -rw-r--r-- | string/test-string.h | 148 | ||||
| -rw-r--r-- | string/test-strlen.c | 177 | ||||
| -rw-r--r-- | string/test-strncmp.c | 259 | ||||
| -rw-r--r-- | string/test-strncpy.c | 264 | ||||
| -rw-r--r-- | string/test-strpbrk.c | 228 | ||||
| -rw-r--r-- | string/test-strrchr.c | 235 | ||||
| -rw-r--r-- | string/test-strspn.c | 213 |
21 files changed, 3814 insertions, 2 deletions
diff --git a/string/Makefile b/string/Makefile index eed07fcace..7e2220ec3d 100644 --- a/string/Makefile +++ b/string/Makefile @@ -45,11 +45,15 @@ routines := strcat strchr strcmp strcoll strcpy strcspn \ # for -fbounded-pointer compiles. Glibc uses memchr for explicit checks. o-objects.ob := memcpy.o memset.o memchr.o +strop-tests := memchr memcmp memcpy memmove mempcpy memset stpcpy \ + stpncpy strcat strchr strcmp strcpy strcspn strlen \ + strncmp strncpy strpbrk strrchr strspn tests := tester inl-tester noinl-tester testcopy test-ffs \ tst-strlen stratcliff tst-svc tst-inlcall \ bug-strncat1 bug-strspn1 bug-strpbrk1 tst-bswap \ - tst-strtok tst-strxfrm bug-strcoll1 -distribute := memcopy.h pagecopy.h tst-svc.expect + tst-strtok tst-strxfrm bug-strcoll1 \ + $(addprefix test-,$(strop-tests)) +distribute := memcopy.h pagecopy.h tst-svc.expect test-string.h include ../Rules diff --git a/string/test-memchr.c b/string/test-memchr.c new file mode 100644 index 0000000000..e5c13a2c2d --- /dev/null +++ b/string/test-memchr.c @@ -0,0 +1,189 @@ +/* Test and measure memchr functions. + Copyright (C) 1999, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Written by Jakub Jelinek <jakub@redhat.com>, 1999. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#define TEST_MAIN +#include "test-string.h" + +typedef char *(*proto_t) (const char *, int, size_t); +char *simple_memchr (const char *, int, size_t); + +IMPL (simple_memchr, 0) +IMPL (memchr, 1) + +char * +simple_memchr (const char *s, int c, size_t n) +{ + while (n--) + if (*s++ == (char) c) + return (char *) s - 1; + return NULL; +} + +static void +do_one_test (impl_t *impl, const char *s, int c, size_t n, char *exp_res) +{ + char *res = CALL (impl, s, c, n); + if (res != exp_res) + { + error (0, 0, "Wrong result in function %s %p %p", impl->name, + res, exp_res); + ret = 1; + return; + } + + if (HP_TIMING_AVAIL) + { + hp_timing_t start, stop, best_time = ~ (hp_timing_t) 0; + size_t i; + + for (i = 0; i < 32; ++i) + { + HP_TIMING_NOW (start); + CALL (impl, s, c, n); + HP_TIMING_NOW (stop); + HP_TIMING_BEST (best_time, start, stop); + } + + printf ("\t%zd", (size_t) best_time); + } +} + +static void +do_test (size_t align, size_t pos, size_t len, int seek_char) +{ + size_t i; + char *result; + + align &= 7; + if (align + len >= page_size) + return; + + for (i = 0; i < len; ++i) + { + buf1[align + i] = 1 + 23 * i % 127; + if (buf1[align + i] == seek_char) + buf1[align + i] = seek_char + 1; + } + buf1[align + len] = 0; + + if (pos < len) + { + buf1[align + pos] = seek_char; + buf1[align + len] = -seek_char; + result = buf1 + align + pos; + } + else + { + result = NULL; + buf1[align + len] = seek_char; + } + + if (HP_TIMING_AVAIL) + printf ("Length %4zd, alignment %2zd:", pos, align); + + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, buf1 + align, seek_char, len, result); + + if (HP_TIMING_AVAIL) + putchar ('\n'); +} + +static void +do_random_tests (void) +{ + size_t i, j, n, align, pos, len; + int seek_char; + char *result; + unsigned char *p = buf1 + page_size - 512; + + for (n = 0; n < ITERATIONS; n++) + { + align = random () & 15; + pos = random () & 511; + if (pos + align >= 512) + pos = 511 - align - (random () & 7); + len = random () & 511; + if (pos >= len) + len = pos + (random () & 7); + if (len + align >= 512) + len = 512 - align - (random () & 7); + seek_char = random () & 255; + j = len + align + 64; + if (j > 512) + j = 512; + + for (i = 0; i < j; i++) + { + if (i == pos + align) + p[i] = seek_char; + else + { + p[i] = random () & 255; + if (i < pos + align && p[i] == seek_char) + p[i] = seek_char + 13; + } + } + + if (pos < len) + result = p + pos + align; + else + result = NULL; + + FOR_EACH_IMPL (impl, 1) + if (CALL (impl, p + align, seek_char, len) != result) + { + error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p", + n, impl->name, align, seek_char, len, pos, + CALL (impl, p + align, seek_char, len), result, p); + ret = 1; + } + } +} + +int +test_main (void) +{ + size_t i; + + test_init (); + + printf ("%20s", ""); + FOR_EACH_IMPL (impl, 0) + printf ("\t%s", impl->name); + putchar ('\n'); + + for (i = 1; i < 8; ++i) + { + do_test (0, 16 << i, 2048, 23); + do_test (i, 64, 256, 23); + do_test (0, 16 << i, 2048, 0); + do_test (i, 64, 256, 0); + } + for (i = 1; i < 32; ++i) + { + do_test (0, i, i + 1, 23); + do_test (0, i, i + 1, 0); + } + + do_random_tests (); + return ret; +} + +#include "../test-skeleton.c" diff --git a/string/test-memcmp.c b/string/test-memcmp.c new file mode 100644 index 0000000000..d3cc560470 --- /dev/null +++ b/string/test-memcmp.c @@ -0,0 +1,222 @@ +/* Test and measure memcmp functions. + Copyright (C) 1999, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Written by Jakub Jelinek <jakub@redhat.com>, 1999. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#define TEST_MAIN +#include "test-string.h" + +typedef int (*proto_t) (const char *, const char *, size_t); +int simple_memcmp (const char *, const char *, size_t); + +IMPL (simple_memcmp, 0) +IMPL (memcmp, 1) + +int +simple_memcmp (const char *s1, const char *s2, size_t n) +{ + int ret = 0; + + while (n-- + && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0); + return ret; +} + +static void +do_one_test (impl_t *impl, const char *s1, const char *s2, size_t len, + int exp_result) +{ + int result = CALL (impl, s1, s2, len); + if ((exp_result == 0 && result != 0) + || (exp_result < 0 && result >= 0) + || (exp_result > 0 && result <= 0)) + { + error (0, 0, "Wrong result in function %s %d %d", impl->name, + result, exp_result); + ret = 1; + return; + } + + if (HP_TIMING_AVAIL) + { + hp_timing_t start, stop, best_time = ~ (hp_timing_t) 0; + size_t i; + + for (i = 0; i < 32; ++i) + { + HP_TIMING_NOW (start); + CALL (impl, s1, s2, len); + HP_TIMING_NOW (stop); + HP_TIMING_BEST (best_time, start, stop); + } + + printf ("\t%zd", (size_t) best_time); + } +} + +static void +do_test (size_t align1, size_t align2, size_t len, int exp_result) +{ + size_t i; + char *s1, *s2; + + if (len == 0) + return; + + align1 &= 7; + if (align1 + len >= page_size) + return; + + align2 &= 7; + if (align2 + len >= page_size) + return; + + s1 = buf1 + align1; + s2 = buf2 + align2; + + for (i = 0; i < len; i++) + s1[i] = s2[i] = 1 + 23 * i % 255; + + s1[len] = align1; + s2[len] = align2; + s2[len - 1] -= exp_result; + + if (HP_TIMING_AVAIL) + printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2); + + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, s1, s2, len, exp_result); + + if (HP_TIMING_AVAIL) + putchar ('\n'); +} + +static void +do_random_tests (void) +{ + size_t i, j, n, align1, align2, pos, len; + int result, r; + unsigned char *p1 = buf1 + page_size - 512; + unsigned char *p2 = buf2 + page_size - 512; + + for (n = 0; n < ITERATIONS; n++) + { + align1 = random () & 31; + if (random () & 1) + align2 = random () & 31; + else + align2 = align1 + (random () & 24); + pos = random () & 511; + j = align1; + if (align2 > j) + j = align2; + if (pos + j >= 512) + pos = 511 - j - (random () & 7); + len = random () & 511; + if (len + j >= 512) + len = 511 - j - (random () & 7); + j = len + align1 + 64; + if (j > 512) j = 512; + for (i = 0; i < j; ++i) + p1[i] = random () & 255; + for (i = 0; i < j; ++i) + p2[i] = random () & 255; + + result = 0; + if (pos >= len) + memcpy (p2 + align2, p1 + align1, len); + else + { + memcpy (p2 + align2, p1 + align1, pos); + if (p2[align2 + pos] == p1[align1 + pos]) + { + p2[align2 + pos] = random () & 255; + if (p2[align2 + pos] == p1[align1 + pos]) + p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127); + } + + if (p1[align1 + pos] < p2[align2 + pos]) + result = -1; + else + result = 1; + } + + FOR_EACH_IMPL (impl, 1) + { + r = CALL (impl, p1 + align1, p2 + align2, len); + if ((r == 0 && result) + || (r < 0 && result >= 0) + || (r > 0 && result <= 0)) + { + error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %d != %d, p1 %p p2 %p", + n, impl->name, align1, align2, len, pos, r, result, p1, p2); + ret = 1; + } + } + } +} + +int +test_main (void) +{ + size_t i; + + test_init (); + + printf ("%23s", ""); + FOR_EACH_IMPL (impl, 0) + printf ("\t%s", impl->name); + putchar ('\n'); + + for (i = 1; i < 16; ++i) + { + do_test (i, i, i, 0); + do_test (i, i, i, 1); + do_test (i, i, i, -1); + } + + for (i = 0; i < 16; ++i) + { + do_test (0, 0, i, 0); + do_test (0, 0, i, 1); + do_test (0, 0, i, -1); + } + + for (i = 1; i < 10; ++i) + { + do_test (0, 0, 2 << i, 0); + do_test (0, 0, 2 << i, 1); + do_test (0, 0, 2 << i, -1); + do_test (0, 0, 16 << i, 0); + do_test (8 - i, 2 * i, 16 << i, 0); + do_test (0, 0, 16 << i, 1); + do_test (0, 0, 16 << i, -1); + } + + for (i = 1; i < 8; ++i) + { + do_test (i, 2 * i, 8 << i, 0); + do_test (i, 2 * i, 8 << i, 1); + do_test (i, 2 * i, 8 << i, -1); + } + + do_random_tests (); + return ret; +} + +#include "../test-skeleton.c" diff --git a/string/test-memcpy.c b/string/test-memcpy.c new file mode 100644 index 0000000000..5549ead1cd --- /dev/null +++ b/string/test-memcpy.c @@ -0,0 +1,273 @@ +/* Test and measure memcpy functions. + Copyright (C) 1999, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Written by Jakub Jelinek <jakub@redhat.com>, 1999. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#ifndef MEMCPY_RESULT +# define MEMCPY_RESULT(dst, len) dst +# define MIN_PAGE_SIZE 131072 +# define TEST_MAIN +# include "test-string.h" + +char *simple_memcpy (char *, const char *, size_t); +char *builtin_memcpy (char *, const char *, size_t); + +IMPL (simple_memcpy, 0) +IMPL (builtin_memcpy, 0) +IMPL (memcpy, 1) + +char * +simple_memcpy (char *dst, const char *src, size_t n) +{ + char *ret = dst; + while (n--) + *dst++ = *src++; + return ret; +} + +char * +builtin_memcpy (char *dst, const char *src, size_t n) +{ + return __builtin_memcpy (dst, src, n); +} +#endif + +typedef char *(*proto_t) (char *, const char *, size_t); + +static void +do_one_test (impl_t *impl, char *dst, const char *src, + size_t len) +{ + if (CALL (impl, dst, src, len) != MEMCPY_RESULT (dst, len)) + { + error (0, 0, "Wrong result in function %s %p %p", impl->name, + CALL (impl, dst, src, len), MEMCPY_RESULT (dst, len)); + ret = 1; + return; + } + + if (memcmp (dst, src, len) != 0) + { + error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"", + impl->name, dst, src); + ret = 1; + return; + } + + if (HP_TIMING_AVAIL) + { + hp_timing_t start, stop, best_time = ~ (hp_timing_t) 0; + size_t i; + + for (i = 0; i < 32; ++i) + { + HP_TIMING_NOW (start); + CALL (impl, dst, src, len); + HP_TIMING_NOW (stop); + HP_TIMING_BEST (best_time, start, stop); + } + + printf ("\t%zd", (size_t) best_time); + } +} + +static void +do_test (size_t align1, size_t align2, size_t len) +{ + size_t i, j; + char *s1, *s2; + + align1 &= 63; + if (align1 + len >= page_size) + return; + + align2 &= 63; + if (align2 + len >= page_size) + return; + + s1 = buf1 + align1; + s2 = buf2 + align2; + + for (i = 0, j = 1; i < len; i++, j += 23) + s1[i] = j; + + if (HP_TIMING_AVAIL) + printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2); + + FOR_EACH_IMPL (impl, 0) + do_one_test (impl, s2, s1, len); + + if (HP_TIMING_AVAIL) + putchar ('\n'); +} + +static void +do_random_tests (void) +{ + size_t i, j, n, align1, align2, len, size1, size2, size; + int c; + unsigned char *p1, *p2; + unsigned char *res; + + for (n = 0; n < ITERATIONS; n++) + { + if (n == 0) + { + len = getpagesize (); + size = len + 512; + size1 = size; + size2 = size; + align1 = 512; + align2 = 512; + } + else + { + if ((random () & 255) == 0) + size = 65536; + else + size = 768; + if (size > page_size) + size = page_size; + size1 = size; + size2 = size; + i = random (); + if (i & 3) + size -= 256; + if (i & 1) + size1 -= 256; + if (i & 2) + size2 -= 256; + if (i & 4) + { + len = random () % size; + align1 = size1 - len - (random () & 31); + align2 = size2 - len - (random () & 31); + if (align1 > size1) + align1 = 0; + if (align2 > size2) + align2 = 0; + } + else + { + align1 = random () & 63; + align2 = random () & 63; + len = random () % size; + if (align1 + len > size1) + align1 = size1 - len; + if (align2 + len > size2) + align2 = size2 - len; + } + } + p1 = buf1 + page_size - size1; + p2 = buf2 + page_size - size2; + c = random () & 255; + j = align1 + len + 256; |
