Add wcschr test cases

This commit is contained in:
Liubov Dmitrieva 2011-10-23 14:14:26 -04:00 committed by Ulrich Drepper
parent 619fcccaa3
commit 2d09f82f8a
4 changed files with 88 additions and 54 deletions

View File

@ -1,7 +1,14 @@
2011-10-19 Liubov Dmitrieva <liubov.dmitrieva@gmail.com>
* wcsmbs/Makefile (strop-tests): Add wcschr.
* wcsmbs/test-wcschr.c: New file.
* string/test-strchr.c: Update.
Add wcschr support.
(WIDE): New macro.
2011-10-18 Liubov Dmitrieva <liubov.dmitrieva@gmail.com> 2011-10-18 Liubov Dmitrieva <liubov.dmitrieva@gmail.com>
* wcsmbs/Makefile * wcsmbs/Makefile (strop-tests): Add wcslen.
(strop-tests): Add wcslen.
* wcsmbs/test-wcslen.c: New file. * wcsmbs/test-wcslen.c: New file.
* string/test-strlen.c: Update. * string/test-strlen.c: Update.
Add wcslen support. Add wcslen support.

View File

@ -1,7 +1,8 @@
/* Test and measure strchr functions. /* Test and measure STRCHR functions.
Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc. Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
This file is part of the GNU C Library. This file is part of the GNU C Library.
Written by Jakub Jelinek <jakub@redhat.com>, 1999. Written by Jakub Jelinek <jakub@redhat.com>, 1999.
Added wcschr support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>, 2011
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
@ -21,38 +22,55 @@
#define TEST_MAIN #define TEST_MAIN
#include "test-string.h" #include "test-string.h"
typedef char *(*proto_t) (const char *, int); #ifndef WIDE
char *simple_strchr (const char *, int); # define STRCHR strchr
char *stupid_strchr (const char *, int); # define STRLEN strlen
# define CHAR char
# define BIG_CHAR CHAR_MAX
# define MIDDLE_CHAR 127
# define SMALL_CHAR 23
# define UCHAR unsigned char
#else
# include <wchar.h>
# define STRCHR wcschr
# define STRLEN wcslen
# define CHAR wchar_t
# define BIG_CHAR WCHAR_MAX
# define MIDDLE_CHAR 1121
# define SMALL_CHAR 851
# define UCHAR wchar_t
#endif
IMPL (stupid_strchr, 0) typedef CHAR *(*proto_t) (const CHAR *, int);
IMPL (simple_strchr, 0)
IMPL (strchr, 1)
char * CHAR *
simple_strchr (const char *s, int c) simple_STRCHR (const CHAR *s, int c)
{ {
for (; *s != (char) c; ++s) for (; *s != (CHAR) c; ++s)
if (*s == '\0') if (*s == '\0')
return NULL; return NULL;
return (char *) s; return (CHAR *) s;
} }
char * CHAR *
stupid_strchr (const char *s, int c) stupid_STRCHR (const CHAR *s, int c)
{ {
size_t n = strlen (s) + 1; size_t n = STRLEN (s) + 1;
while (n--) while (n--)
if (*s++ == (char) c) if (*s++ == (CHAR) c)
return (char *) s - 1; return (CHAR *) s - 1;
return NULL; return NULL;
} }
IMPL (stupid_STRCHR, 1)
IMPL (simple_STRCHR, 1)
IMPL (STRCHR, 1)
static void static void
do_one_test (impl_t *impl, const char *s, int c, char *exp_res) do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
{ {
char *res = CALL (impl, s, c); CHAR *res = CALL (impl, s, c);
if (res != exp_res) if (res != exp_res)
{ {
error (0, 0, "Wrong result in function %s %p %p", impl->name, error (0, 0, "Wrong result in function %s %p %p", impl->name,
@ -82,37 +100,40 @@ do_one_test (impl_t *impl, const char *s, int c, char *exp_res)
static void static void
do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char) do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
/* for wcschr: align here means align not in bytes,
* but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
* len for wcschr here isn't in bytes but it's number of wchar_t symbols */
{ {
size_t i; size_t i;
char *result; CHAR *result;
CHAR *buf = (CHAR *) (buf1);
align &= 7; align &= 15;
if (align + len >= page_size) if ((align + len) * sizeof(CHAR) >= page_size)
return; return;
for (i = 0; i < len; ++i) for (i = 0; i < len; ++i)
{ {
buf1[align + i] = 32 + 23 * i % (max_char - 32); buf[align + i] = 32 + 23 * i % max_char;
if (buf1[align + i] == seek_char) if (buf[align + i] == seek_char)
buf1[align + i] = seek_char + 1; buf[align + i] = seek_char + 1;
} }
buf1[align + len] = 0; buf[align + len] = 0;
if (pos < len) if (pos < len)
{ {
buf1[align + pos] = seek_char; buf[align + pos] = seek_char;
result = (char *) (buf1 + align + pos); result = buf + align + pos;
} }
else if (seek_char == 0) else if (seek_char == 0)
result = (char *) (buf1 + align + len); result = buf + align + len;
else else
result = NULL; result = NULL;
if (HP_TIMING_AVAIL) if (HP_TIMING_AVAIL)
printf ("Length %4zd, alignment %2zd:", pos, align); printf ("Length %4zd, alignment in bytes %2zd:", pos, align * sizeof (CHAR));
FOR_EACH_IMPL (impl, 0) FOR_EACH_IMPL (impl, 0)
do_one_test (impl, (char *) (buf1 + align), seek_char, result); do_one_test (impl, buf + align, seek_char, result);
if (HP_TIMING_AVAIL) if (HP_TIMING_AVAIL)
putchar ('\n'); putchar ('\n');
@ -123,16 +144,19 @@ do_random_tests (void)
{ {
size_t i, j, n, align, pos, len; size_t i, j, n, align, pos, len;
int seek_char; int seek_char;
char *result; CHAR *result;
unsigned char *p = buf1 + page_size - 512; UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
for (n = 0; n < ITERATIONS; n++) for (n = 0; n < ITERATIONS; n++)
{ {
/* for wcschr: align here means align not in bytes, but in wchar_ts,
* in bytes it will equal to align * (sizeof (wchar_t)) */
align = random () & 15; align = random () & 15;
pos = random () & 511; pos = random () & 511;
seek_char = random () & 255; seek_char = random () & 255;
if (pos + align >= 511) if (pos + align >= 511)
pos = 510 - align - (random () & 7); pos = 510 - align - (random () & 7);
/* len for wcschr here isn't in bytes but it's number of wchar_t symbols */
len = random () & 511; len = random () & 511;
if ((pos == len && seek_char) if ((pos == len && seek_char)
|| (pos > len && (random () & 1))) || (pos > len && (random () & 1)))
@ -166,18 +190,19 @@ do_random_tests (void)
} }
if (pos <= len) if (pos <= len)
result = (char *) (p + pos + align); result = (CHAR *) (p + pos + align);
else if (seek_char == 0) else if (seek_char == 0)
result = (char *) (p + len + align); result = (CHAR *) (p + len + align);
else else
result = NULL; result = NULL;
FOR_EACH_IMPL (impl, 1) FOR_EACH_IMPL (impl, 1)
if (CALL (impl, (char *) (p + align), seek_char) != result) if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
{ {
error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p", error (0, 0, "Iteration %zd - wrong result in function \
n, impl->name, align, seek_char, len, pos, %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
CALL (impl, (char *) (p + align), seek_char), result, p); n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
CALL (impl, (CHAR *) (p + align), seek_char), result, p);
ret = 1; ret = 1;
} }
} }
@ -197,38 +222,38 @@ test_main (void)
for (i = 1; i < 8; ++i) for (i = 1; i < 8; ++i)
{ {
do_test (0, 16 << i, 2048, 23, 127); do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
do_test (i, 16 << i, 2048, 23, 127); do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
} }
for (i = 1; i < 8; ++i) for (i = 1; i < 8; ++i)
{ {
do_test (i, 64, 256, 23, 127); do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
do_test (i, 64, 256, 23, 255); do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
} }
for (i = 0; i < 32; ++i) for (i = 0; i < 32; ++i)
{ {
do_test (0, i, i + 1, 23, 127); do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
do_test (0, i, i + 1, 23, 255); do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
} }
for (i = 1; i < 8; ++i) for (i = 1; i < 8; ++i)
{ {
do_test (0, 16 << i, 2048, 0, 127); do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
do_test (i, 16 << i, 2048, 0, 127); do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
} }
for (i = 1; i < 8; ++i) for (i = 1; i < 8; ++i)
{ {
do_test (i, 64, 256, 0, 127); do_test (i, 64, 256, 0, MIDDLE_CHAR);
do_test (i, 64, 256, 0, 255); do_test (i, 64, 256, 0, BIG_CHAR);
} }
for (i = 0; i < 32; ++i) for (i = 0; i < 32; ++i)
{ {
do_test (0, i, i + 1, 0, 127); do_test (0, i, i + 1, 0, MIDDLE_CHAR);
do_test (0, i, i + 1, 0, 255); do_test (0, i, i + 1, 0, BIG_CHAR);
} }
do_random_tests (); do_random_tests ();

View File

@ -42,7 +42,7 @@ routines := wcscat wcschr wcscmp wcscpy wcscspn wcsdup wcslen wcsncat \
isoc99_wscanf isoc99_vwscanf isoc99_fwscanf isoc99_vfwscanf \ isoc99_wscanf isoc99_vwscanf isoc99_fwscanf isoc99_vfwscanf \
isoc99_swscanf isoc99_vswscanf isoc99_swscanf isoc99_vswscanf
strop-tests := wcscmp wmemcmp wcslen strop-tests := wcscmp wmemcmp wcslen wcschr
tests := tst-wcstof wcsmbs-tst1 tst-wcsnlen tst-btowc tst-mbrtowc \ tests := tst-wcstof wcsmbs-tst1 tst-wcsnlen tst-btowc tst-mbrtowc \
tst-wcrtomb tst-wcpncpy tst-mbsrtowcs tst-wchar-h tst-mbrtowc2 \ tst-wcrtomb tst-wcpncpy tst-mbsrtowcs tst-wchar-h tst-mbrtowc2 \
wcsatcliff $(addprefix test-,$(strop-tests)) wcsatcliff $(addprefix test-,$(strop-tests))

2
wcsmbs/test-wcschr.c Normal file
View File

@ -0,0 +1,2 @@
#define WIDE 1
#include "../string/test-strchr.c"