Files
glibc/string/test-memset.c
T

268 lines
5.7 KiB
C
Raw Normal View History

2015-08-26 10:26:25 +02:00
/* Test memset functions.
Copyright (C) 1999-2018 Free Software Foundation, Inc.
2002-11-07 19:15:01 +00:00
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
2012-02-09 23:18:22 +00:00
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
2002-11-07 19:15:01 +00:00
#define TEST_MAIN
2012-10-19 22:04:19 -07:00
#ifdef TEST_BZERO
# ifdef TEST_EXPLICIT_BZERO
# define TEST_NAME "explicit_bzero"
# else
# define TEST_NAME "bzero"
# endif
2012-10-19 22:04:19 -07:00
#else
2015-08-26 10:26:25 +02:00
# ifndef WIDE
# define TEST_NAME "memset"
# else
# define TEST_NAME "wmemset"
# endif /* WIDE */
#endif /* !TEST_BZERO */
2002-11-07 19:15:01 +00:00
#define MIN_PAGE_SIZE 131072
#include "test-string.h"
2015-08-26 10:26:25 +02:00
#ifndef WIDE
# define MEMSET memset
# define CHAR char
# define UCHAR unsigned char
# define SIMPLE_MEMSET simple_memset
# define MEMCMP memcmp
# define BIG_CHAR CHAR_MAX
#else
# include <wchar.h>
# define MEMSET wmemset
# define CHAR wchar_t
# define UCHAR wchar_t
# define SIMPLE_MEMSET simple_wmemset
# define MEMCMP wmemcmp
# define BIG_CHAR WCHAR_MAX
#endif /* WIDE */
CHAR *SIMPLE_MEMSET (CHAR *, int, size_t);
#ifdef TEST_BZERO
typedef void (*proto_t) (char *, size_t);
void simple_bzero (char *, size_t);
void builtin_bzero (char *, size_t);
IMPL (simple_bzero, 0)
IMPL (builtin_bzero, 0)
#ifdef TEST_EXPLICIT_BZERO
IMPL (explicit_bzero, 1)
#else
IMPL (bzero, 1)
#endif
void
simple_bzero (char *s, size_t n)
{
2015-08-26 10:26:25 +02:00
SIMPLE_MEMSET (s, 0, n);
}
void
builtin_bzero (char *s, size_t n)
{
__builtin_bzero (s, n);
}
#else
2015-08-26 10:26:25 +02:00
typedef CHAR *(*proto_t) (CHAR *, int, size_t);
IMPL (SIMPLE_MEMSET, 0)
# ifndef WIDE
2002-11-07 19:15:01 +00:00
char *builtin_memset (char *, int, size_t);
IMPL (builtin_memset, 0)
2015-08-26 10:26:25 +02:00
# endif /* !WIDE */
IMPL (MEMSET, 1)
2002-11-07 19:15:01 +00:00
2015-08-26 10:26:25 +02:00
# ifndef WIDE
char *
builtin_memset (char *s, int c, size_t n)
{
return __builtin_memset (s, c, n);
}
2015-08-26 10:26:25 +02:00
# endif /* !WIDE */
#endif /* !TEST_BZERO */
2015-08-26 10:26:25 +02:00
CHAR *
2013-06-20 19:40:55 -05:00
inhibit_loop_to_libcall
2015-08-26 10:26:25 +02:00
SIMPLE_MEMSET (CHAR *s, int c, size_t n)
2002-11-07 19:15:01 +00:00
{
2015-08-26 10:26:25 +02:00
CHAR *r = s, *end = s + n;
2002-11-07 19:15:01 +00:00
while (r < end)
*r++ = c;
return s;
}
static void
2015-08-26 10:26:25 +02:00
do_one_test (impl_t *impl, CHAR *s, int c __attribute ((unused)), size_t n)
2002-11-07 19:15:01 +00:00
{
2015-08-26 10:26:25 +02:00
CHAR tstbuf[n];
#ifdef TEST_BZERO
simple_bzero (tstbuf, n);
CALL (impl, s, n);
if (memcmp (s, tstbuf, n) != 0)
#else
2015-08-26 10:26:25 +02:00
CHAR *res = CALL (impl, s, c, n);
if (res != s
2015-08-26 10:26:25 +02:00
|| SIMPLE_MEMSET (tstbuf, c, n) != tstbuf
|| MEMCMP (s, tstbuf, n) != 0)
#endif /* !TEST_BZERO */
2002-11-07 19:15:01 +00:00
{
error (0, 0, "Wrong result in function %s", impl->name);
2002-11-07 19:15:01 +00:00
ret = 1;
return;
}
}
static void
do_test (size_t align, int c, size_t len)
{
align &= 7;
2015-08-26 10:26:25 +02:00
if ((align + len) * sizeof (CHAR) > page_size)
2002-11-07 19:15:01 +00:00
return;
FOR_EACH_IMPL (impl, 0)
2015-08-26 10:26:25 +02:00
do_one_test (impl, (CHAR *) (buf1) + align, c, len);
2002-11-07 19:15:01 +00:00
}
#ifndef TEST_BZERO
2002-11-07 19:15:01 +00:00
static void
do_random_tests (void)
{
size_t i, j, k, n, align, len, size;
int c, o;
2015-08-26 10:26:25 +02:00
UCHAR *p, *res;
UCHAR *p2 = (UCHAR *) buf2;
2002-11-07 19:15:01 +00:00
2015-08-26 10:26:25 +02:00
for (i = 0; i < 65536 / sizeof (CHAR); ++i)
p2[i] = random () & BIG_CHAR;
2002-11-07 19:15:01 +00:00
for (n = 0; n < ITERATIONS; n++)
{
if ((random () & 31) == 0)
2015-08-26 10:26:25 +02:00
size = 65536 / sizeof (CHAR);
2002-11-07 19:15:01 +00:00
else
size = 512;
2015-08-26 10:26:25 +02:00
p = (UCHAR *) (buf1 + page_size) - size;
2002-11-07 19:15:01 +00:00
len = random () & (size - 1);
align = size - len - (random () & 31);
if (align > size)
align = size - len;
if ((random () & 7) == 0)
align &= ~63;
if ((random () & 7) == 0)
c = 0;
else
2015-08-26 10:26:25 +02:00
c = random () & BIG_CHAR;
o = random () & BIG_CHAR;
2002-11-07 19:15:01 +00:00
if (o == c)
2015-08-26 10:26:25 +02:00
o = (c + 1) & BIG_CHAR;
2002-11-07 19:15:01 +00:00
j = len + align + 128;
if (j > size)
j = size;
if (align >= 128)
k = align - 128;
else
k = 0;
for (i = k; i < align; ++i)
p[i] = o;
for (i = align + len; i < j; ++i)
p[i] = o;
FOR_EACH_IMPL (impl, 1)
{
for (i = 0; i < len; ++i)
{
2015-08-26 10:26:25 +02:00
p[i + align] = p2[i];
2002-11-07 19:15:01 +00:00
if (p[i + align] == c)
p[i + align] = o;
}
2015-08-26 10:26:25 +02:00
res = (UCHAR *) CALL (impl, (CHAR *) p + align, c, len);
2002-11-07 19:15:01 +00:00
if (res != p + align)
{
error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd) %p != %p",
n, impl->name, align, c, len, res, p + align);
ret = 1;
}
for (i = k; i < align; ++i)
if (p[i] != o)
{
error (0, 0, "Iteration %zd - garbage before %s (%zd, %d, %zd)",
n, impl->name, align, c, len);
ret = 1;
break;
}
for (; i < align + len; ++i)
if (p[i] != c)
{
error (0, 0, "Iteration %zd - not cleared correctly %s (%zd, %d, %zd)",
n, impl->name, align, c, len);
ret = 1;
break;
}
for (; i < j; ++i)
if (p[i] != o)
{
error (0, 0, "Iteration %zd - garbage after %s (%zd, %d, %zd)",
n, impl->name, align, c, len);
ret = 1;
break;
}
}
}
}
2015-08-26 10:26:25 +02:00
#endif /* !TEST_BZERO */
2002-11-07 19:15:01 +00:00
int
test_main (void)
{
size_t i;
int c = 0;
2002-11-07 19:15:01 +00:00
test_init ();
printf ("%24s", "");
FOR_EACH_IMPL (impl, 0)
printf ("\t%s", impl->name);
putchar ('\n');
#ifndef TEST_BZERO
for (c = -65; c <= 130; c += 65)
#endif
2002-11-07 19:15:01 +00:00
{
for (i = 0; i < 18; ++i)
do_test (0, c, 1 << i);
for (i = 1; i < 32; ++i)
{
do_test (i, c, i);
if (i & (i - 1))
do_test (0, c, i);
}
do_test (1, c, 14);
do_test (3, c, 1024);
do_test (4, c, 64);
do_test (2, c, 25);
}
#ifndef TEST_BZERO
2002-11-07 19:15:01 +00:00
do_random_tests ();
#endif
2002-11-07 19:15:01 +00:00
return ret;
}
#include <support/test-driver.c>