summaryrefslogtreecommitdiff
path: root/tests/unistr
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2011-01-21 14:12:24 +0100
committerBruno Haible <bruno@clisp.org>2011-01-21 14:13:10 +0100
commit50dc8e08d7a91f0616663305569871720b83fe83 (patch)
tree067684b3dd9f35a72f4b804751f7861e3f8d660f /tests/unistr
parent5df9ff919db13d7edeefd1d48ce942ba0d8cc0dc (diff)
downloadgnulib-50dc8e08d7a91f0616663305569871720b83fe83.tar.gz
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.
Diffstat (limited to 'tests/unistr')
-rw-r--r--tests/unistr/test-u-strstr.h210
-rw-r--r--tests/unistr/test-u16-strstr.c47
-rw-r--r--tests/unistr/test-u32-strstr.c47
-rw-r--r--tests/unistr/test-u8-strstr.c50
4 files changed, 354 insertions, 0 deletions
diff --git a/tests/unistr/test-u-strstr.h b/tests/unistr/test-u-strstr.h
new file mode 100644
index 0000000000..6265b3bd4b
--- /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);
+ }
+}
diff --git a/tests/unistr/test-u16-strstr.c b/tests/unistr/test-u16-strstr.c
new file mode 100644
index 0000000000..bc792b9562
--- /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;
+}
diff --git a/tests/unistr/test-u32-strstr.c b/tests/unistr/test-u32-strstr.c
new file mode 100644
index 0000000000..6dc412283e
--- /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;
+}
diff --git a/tests/unistr/test-u8-strstr.c b/tests/unistr/test-u8-strstr.c
new file mode 100644
index 0000000000..b51cfa4a52
--- /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;
+}