Mercurial > hg > octave-lojdl > gnulib-hg
changeset 14206:423e831c85c7
uN_strstr: New unit tests.
* modules/unistr/u8-strstr-tests: New file.
* modules/unistr/u16-strstr-tests: New file.
* modules/unistr/u32-strstr-tests: New file.
* tests/unistr/test-u-strstr.h: New file, based on tests/test-strstr.c.
* tests/unistr/test-u8-strstr.c: New file.
* tests/unistr/test-u16-strstr.c: New file.
* tests/unistr/test-u32-strstr.c: New file.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Fri, 21 Jan 2011 14:12:24 +0100 |
parents | 613f9258ecc8 |
children | e5e6425dfcfb |
files | ChangeLog modules/unistr/u16-strstr-tests modules/unistr/u32-strstr-tests modules/unistr/u8-strstr-tests tests/unistr/test-u-strstr.h tests/unistr/test-u16-strstr.c tests/unistr/test-u32-strstr.c tests/unistr/test-u8-strstr.c |
diffstat | 8 files changed, 411 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2011-01-21 Pádraig Brady <P@draigBrady.com> + Bruno Haible <bruno@clisp.org> + + uN_strstr: New unit tests. + * modules/unistr/u8-strstr-tests: New file. + * modules/unistr/u16-strstr-tests: New file. + * modules/unistr/u32-strstr-tests: New file. + * tests/unistr/test-u-strstr.h: New file, based on tests/test-strstr.c. + * tests/unistr/test-u8-strstr.c: New file. + * tests/unistr/test-u16-strstr.c: New file. + * tests/unistr/test-u32-strstr.c: New file. + 2011-01-21 Pádraig Brady <P@draigBrady.com> Bruno Haible <bruno@clisp.org>
new file mode 100644 --- /dev/null +++ b/modules/unistr/u16-strstr-tests @@ -0,0 +1,15 @@ +Files: +tests/unistr/test-u16-strstr.c +tests/unistr/test-u-strstr.h +tests/macros.h + +Depends-on: + +configure.ac: +AC_CHECK_DECLS_ONCE([alarm]) + +Makefile.am: +TESTS += test-u16-strstr +check_PROGRAMS += test-u16-strstr +test_u16_strstr_SOURCES = unistr/test-u16-strstr.c +test_u16_strstr_LDADD = $(LDADD) $(LIBUNISTRING)
new file mode 100644 --- /dev/null +++ b/modules/unistr/u32-strstr-tests @@ -0,0 +1,15 @@ +Files: +tests/unistr/test-u32-strstr.c +tests/unistr/test-u-strstr.h +tests/macros.h + +Depends-on: + +configure.ac: +AC_CHECK_DECLS_ONCE([alarm]) + +Makefile.am: +TESTS += test-u32-strstr +check_PROGRAMS += test-u32-strstr +test_u32_strstr_SOURCES = unistr/test-u32-strstr.c +test_u32_strstr_LDADD = $(LDADD) $(LIBUNISTRING)
new file mode 100644 --- /dev/null +++ b/modules/unistr/u8-strstr-tests @@ -0,0 +1,15 @@ +Files: +tests/unistr/test-u8-strstr.c +tests/unistr/test-u-strstr.h +tests/macros.h + +Depends-on: + +configure.ac: +AC_CHECK_DECLS_ONCE([alarm]) + +Makefile.am: +TESTS += test-u8-strstr +check_PROGRAMS += test-u8-strstr +test_u8_strstr_SOURCES = unistr/test-u8-strstr.c +test_u8_strstr_LDADD = $(LDADD) $(LIBUNISTRING)
new file mode 100644 --- /dev/null +++ b/tests/unistr/test-u-strstr.h @@ -0,0 +1,210 @@ +/* Test of uN_strstr() functions. + Copyright (C) 2004, 2007-2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +static void +test_u_strstr (void) +{ + { + const UNIT input[] = { 'f', 'o', 'o', 0 }; + const UNIT needle[] = { 0 }; + const UNIT *result = U_STRSTR (input, needle); + ASSERT (result == input); + } + + { + const UNIT input[] = { 'f', 'o', 'o', 0 }; + const UNIT needle[] = { 'o', 0 }; + const UNIT *result = U_STRSTR (input, needle); + ASSERT (result == input + 1); + } + + { + const UNIT input[] = + { 'A', 'B', 'C', ' ', 'A', 'B', 'C', 'D', 'A', 'B', ' ', 'A', 'B', 'C', + 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'D', 'E', 0 + }; + const UNIT needle[] = { 'A', 'B', 'C', 'D', 'A', 'B', 'D', 0 }; + const UNIT *result = U_STRSTR (input, needle); + ASSERT (result == input + 15); + } + + { + const UNIT input[] = + { 'A', 'B', 'C', ' ', 'A', 'B', 'C', 'D', 'A', 'B', ' ', 'A', 'B', 'C', + 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'D', 'E', 0 + }; + const UNIT needle[] = { 'A', 'B', 'C', 'D', 'A', 'B', 'E', 0 }; + const UNIT *result = U_STRSTR (input, needle); + ASSERT (result == NULL); + } + + { + const UNIT input[] = + { 'A', 'B', 'C', ' ', 'A', 'B', 'C', 'D', 'A', 'B', ' ', 'A', 'B', 'C', + 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'D', 'E', 0 + }; + const UNIT needle[] = { 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 0 }; + const UNIT *result = U_STRSTR (input, needle); + ASSERT (result == input + 11); + } + + /* Check that a long periodic needle does not cause false positives. */ + { + const UNIT input[] = + { 'F', '_', 'B', 'D', '_', 'C', 'E', '_', 'B', 'D', '_', 'E', 'F', + '_', 'B', 'F', '_', 'B', 'D', '_', 'E', 'F', '_', 'B', 'F', + '_', 'B', 'D', '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', + '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', '_', 'C', '3', + '_', '8', '8', '_', '2', '0', '_', 'E', 'F', '_', 'B', 'F', + '_', 'B', 'D', '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', + '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', '_', 'C', '3', + '_', 'A', '7', '_', '2', '0', '_', 'E', 'F', '_', 'B', 'F', + '_', 'B', 'D', 0 + }; + const UNIT needle[] = + { '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', '_', 'E', 'F', + '_', 'B', 'F', '_', 'B', 'D', '_', 'E', 'F', '_', 'B', 'F', + '_', 'B', 'D', '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', + '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', 0 + }; + const UNIT *result = U_STRSTR (input, needle); + ASSERT (result == NULL); + } + { + const UNIT input[] = + { 'F', '_', 'B', 'D', '_', 'C', 'E', '_', 'B', 'D', '_', 'E', 'F', + '_', 'B', 'F', '_', 'B', 'D', '_', 'E', 'F', '_', 'B', 'F', + '_', 'B', 'D', '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', + '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', '_', 'C', '3', + '_', '8', '8', '_', '2', '0', '_', 'E', 'F', '_', 'B', 'F', + '_', 'B', 'D', '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', + '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', '_', 'C', '3', + '_', 'A', '7', '_', '2', '0', '_', 'E', 'F', '_', 'B', 'F', + '_', 'B', 'D', '_', 'D', 'A', '_', 'B', '5', '_', 'C', '2', + '_', 'A', '6', '_', '2', '0', '_', 'E', 'F', '_', 'B', 'F', + '_', 'B', 'D', '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', + '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', '_', 'E', 'F', + '_', 'B', 'F', '_', 'B', 'D', '_', 'E', 'F', '_', 'B', 'F', + '_', 'B', 'D', 0 + }; + const UNIT needle[] = + { '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', '_', 'E', 'F', + '_', 'B', 'F', '_', 'B', 'D', '_', 'E', 'F', '_', 'B', 'F', + '_', 'B', 'D', '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', + '_', 'E', 'F', '_', 'B', 'F', '_', 'B', 'D', 0 + }; + const UNIT *result = U_STRSTR (input, needle); + ASSERT (result == input + 115); + } + + /* Check that a very long haystack is handled quickly if the needle is + short and occurs near the beginning. */ + { + size_t repeat = 10000; + size_t m = 1000000; + const UNIT needle[] = + { 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', + 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', + 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', + 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', + 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', + 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', + 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', + 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', + 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', + 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 0 + }; + UNIT *haystack = (UNIT *) malloc ((m + 1) * sizeof (UNIT)); + if (haystack != NULL) + { + size_t i; + + haystack[0] = 'B'; + for (i = 1; i < m; i++) + haystack[i] = 'A'; + haystack[m] = '\0'; + + for (; repeat > 0; repeat--) + { + ASSERT (U_STRSTR (haystack, needle) == haystack + 1); + } + + free (haystack); + } + } + + /* Check that a very long needle is discarded quickly if the haystack is + short. */ + { + size_t repeat = 10000; + size_t m = 1000000; + const UNIT haystack[] = + { 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', + 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', + 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', + 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', + 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', + 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', + 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', + 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', + 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', + 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 0 + }; + UNIT *needle = (UNIT *) malloc ((m + 1) * sizeof (UNIT)); + if (needle != NULL) + { + size_t i; + + for (i = 0; i < m; i++) + needle[i] = 'A'; + needle[m] = '\0'; + + for (; repeat > 0; repeat--) + { + ASSERT (U_STRSTR (haystack, needle) == NULL); + } + + free (needle); + } + } + + /* Check that the asymptotic worst-case complexity is not quadratic. */ + { + size_t m = 1000000; + UNIT *haystack = (UNIT *) malloc ((2 * m + 2) * sizeof (UNIT)); + UNIT *needle = (UNIT *) malloc ((m + 2) * sizeof (UNIT)); + if (haystack != NULL && needle != NULL) + { + size_t i; + const UNIT *result; + + for (i = 0; i < 2 * m; i++) + haystack[i] = 'A'; + haystack[2 * m] = 'B'; + haystack[2 * m + 1] = 0; + + for (i = 0; i < m; i++) + needle[i] = 'A'; + needle[m] = 'B'; + needle[m + 1] = 0; + + result = U_STRSTR (haystack, needle); + ASSERT (result == haystack + m); + } + free (needle); + free (haystack); + } +}
new file mode 100644 --- /dev/null +++ b/tests/unistr/test-u16-strstr.c @@ -0,0 +1,47 @@ +/* Test of u16_strstr() function. + Copyright (C) 2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +/* Written by Pádraig Brady <P@draigBrady.com>, 2011. */ + +#include <config.h> + +#include "unistr.h" + +#include <stdint.h> +#include <stdlib.h> +#include <signal.h> /* For signal. */ +#include <unistd.h> /* For alarm. */ + +#include "macros.h" + +#define UNIT uint16_t +#define U_STRSTR u16_strstr +#include "test-u-strstr.h" + +int +main (void) +{ +#if HAVE_DECL_ALARM + /* Declare failure if test takes too long, by using default abort + caused by SIGALRM. */ + signal (SIGALRM, SIG_DFL); + alarm (5); +#endif + + test_u_strstr (); + + return 0; +}
new file mode 100644 --- /dev/null +++ b/tests/unistr/test-u32-strstr.c @@ -0,0 +1,47 @@ +/* Test of u32_strstr() function. + Copyright (C) 2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +/* Written by Pádraig Brady <P@draigBrady.com>, 2011. */ + +#include <config.h> + +#include "unistr.h" + +#include <stdint.h> +#include <stdlib.h> +#include <signal.h> /* For signal. */ +#include <unistd.h> /* For alarm. */ + +#include "macros.h" + +#define UNIT uint32_t +#define U_STRSTR u32_strstr +#include "test-u-strstr.h" + +int +main (void) +{ +#if HAVE_DECL_ALARM + /* Declare failure if test takes too long, by using default abort + caused by SIGALRM. */ + signal (SIGALRM, SIG_DFL); + alarm (5); +#endif + + test_u_strstr (); + + return 0; +}
new file mode 100644 --- /dev/null +++ b/tests/unistr/test-u8-strstr.c @@ -0,0 +1,50 @@ +/* Test of u8_strstr() function. + Copyright (C) 2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +/* Written by Pádraig Brady <P@draigBrady.com>, 2011. */ + +#include <config.h> + +#include "unistr.h" + +#include <stdint.h> +#include <stdlib.h> +#include <signal.h> /* For signal. */ +#include <unistd.h> /* For alarm. */ + +#include "macros.h" + +#define UNIT uint8_t +#define U_STRSTR u8_strstr +#include "test-u-strstr.h" + +int +main (void) +{ +#if HAVE_DECL_ALARM + /* Declare failure if test takes too long, by using default abort + caused by SIGALRM. Note since we defer to strstr() in this + case, we're assuming that we're running this test on the + same system that we did the check to ensure it has linear + performance characteristics. */ + signal (SIGALRM, SIG_DFL); + alarm (5); +#endif + + test_u_strstr (); + + return 0; +}