|
|
94084c |
commit de82cb0da4b8fa5b3d56c457438d2568c67ab1b1
|
|
|
94084c |
Author: Joseph Myers <joseph@codesourcery.com>
|
|
|
94084c |
Date: Tue Oct 12 13:48:39 2021 +0000
|
|
|
94084c |
|
|
|
94084c |
Add TEST_COMPARE_STRING_WIDE to support/check.h
|
|
|
94084c |
|
|
|
94084c |
I'd like to be able to test narrow and wide string interfaces, with
|
|
|
94084c |
the narrow string tests using TEST_COMPARE_STRING and the wide string
|
|
|
94084c |
tests using something analogous (possibly generated using macros from
|
|
|
94084c |
a common test template for both the narrow and wide string tests where
|
|
|
94084c |
appropriate).
|
|
|
94084c |
|
|
|
94084c |
Add such a TEST_COMPARE_STRING_WIDE, along with functions
|
|
|
94084c |
support_quote_blob_wide and support_test_compare_string_wide that it
|
|
|
94084c |
builds on. Those functions are built using macros from common
|
|
|
94084c |
templates shared by the narrow and wide string implementations, though
|
|
|
94084c |
I didn't do that for the tests of test functions. In
|
|
|
94084c |
support_quote_blob_wide, I chose to use the \x{} delimited escape
|
|
|
94084c |
sequence syntax proposed for C2X in N2785, rather than e.g. trying to
|
|
|
94084c |
generate the end of a string and the start of a new string when
|
|
|
94084c |
ambiguity would result from undelimited \x (when the next character
|
|
|
94084c |
after such an escape sequence is valid hex) or forcing an escape
|
|
|
94084c |
sequence to be used for the next character in the case of such
|
|
|
94084c |
ambiguity.
|
|
|
94084c |
|
|
|
94084c |
Tested for x86_64.
|
|
|
94084c |
|
|
|
94084c |
diff --git a/support/Makefile b/support/Makefile
|
|
|
94084c |
index 75bad6715ac3d08c..3c941e1ba9e29aa4 100644
|
|
|
94084c |
--- a/support/Makefile
|
|
|
94084c |
+++ b/support/Makefile
|
|
|
94084c |
@@ -70,6 +70,7 @@ libsupport-routines = \
|
|
|
94084c |
support_openpty \
|
|
|
94084c |
support_paths \
|
|
|
94084c |
support_quote_blob \
|
|
|
94084c |
+ support_quote_blob_wide \
|
|
|
94084c |
support_quote_string \
|
|
|
94084c |
support_record_failure \
|
|
|
94084c |
support_run_diff \
|
|
|
94084c |
@@ -83,6 +84,7 @@ libsupport-routines = \
|
|
|
94084c |
support_test_compare_blob \
|
|
|
94084c |
support_test_compare_failure \
|
|
|
94084c |
support_test_compare_string \
|
|
|
94084c |
+ support_test_compare_string_wide \
|
|
|
94084c |
support_test_main \
|
|
|
94084c |
support_test_verify_impl \
|
|
|
94084c |
support_wait_for_thread_exit \
|
|
|
94084c |
@@ -275,11 +277,13 @@ tests = \
|
|
|
94084c |
tst-support-open-dev-null-range \
|
|
|
94084c |
tst-support-process_state \
|
|
|
94084c |
tst-support_quote_blob \
|
|
|
94084c |
+ tst-support_quote_blob_wide \
|
|
|
94084c |
tst-support_quote_string \
|
|
|
94084c |
tst-support_record_failure \
|
|
|
94084c |
tst-test_compare \
|
|
|
94084c |
tst-test_compare_blob \
|
|
|
94084c |
tst-test_compare_string \
|
|
|
94084c |
+ tst-test_compare_string_wide \
|
|
|
94084c |
tst-timespec \
|
|
|
94084c |
tst-xreadlink \
|
|
|
94084c |
tst-xsigstack \
|
|
|
94084c |
diff --git a/support/check.h b/support/check.h
|
|
|
94084c |
index 83662b2d10c8cf58..9b1844352f32513a 100644
|
|
|
94084c |
--- a/support/check.h
|
|
|
94084c |
+++ b/support/check.h
|
|
|
94084c |
@@ -20,6 +20,7 @@
|
|
|
94084c |
#define SUPPORT_CHECK_H
|
|
|
94084c |
|
|
|
94084c |
#include <sys/cdefs.h>
|
|
|
94084c |
+#include <stddef.h>
|
|
|
94084c |
|
|
|
94084c |
__BEGIN_DECLS
|
|
|
94084c |
|
|
|
94084c |
@@ -171,11 +172,25 @@ void support_test_compare_blob (const void *left,
|
|
|
94084c |
(support_test_compare_string (left, right, __FILE__, __LINE__, \
|
|
|
94084c |
#left, #right))
|
|
|
94084c |
|
|
|
94084c |
+/* Compare the wide strings LEFT and RIGHT and report a test failure
|
|
|
94084c |
+ if they are different. Also report failure if one of the arguments
|
|
|
94084c |
+ is a null pointer and the other is not. The strings should be
|
|
|
94084c |
+ reasonably short because on mismatch, both are printed. */
|
|
|
94084c |
+#define TEST_COMPARE_STRING_WIDE(left, right) \
|
|
|
94084c |
+ (support_test_compare_string_wide (left, right, __FILE__, __LINE__, \
|
|
|
94084c |
+ #left, #right))
|
|
|
94084c |
+
|
|
|
94084c |
void support_test_compare_string (const char *left, const char *right,
|
|
|
94084c |
const char *file, int line,
|
|
|
94084c |
const char *left_expr,
|
|
|
94084c |
const char *right_expr);
|
|
|
94084c |
|
|
|
94084c |
+void support_test_compare_string_wide (const wchar_t *left,
|
|
|
94084c |
+ const wchar_t *right,
|
|
|
94084c |
+ const char *file, int line,
|
|
|
94084c |
+ const char *left_expr,
|
|
|
94084c |
+ const char *right_expr);
|
|
|
94084c |
+
|
|
|
94084c |
/* Internal function called by the test driver. */
|
|
|
94084c |
int support_report_failure (int status)
|
|
|
94084c |
__attribute__ ((weak, warn_unused_result));
|
|
|
94084c |
diff --git a/support/support.h b/support/support.h
|
|
|
94084c |
index c219e0d9d1aef046..29d56c7c891ee34b 100644
|
|
|
94084c |
--- a/support/support.h
|
|
|
94084c |
+++ b/support/support.h
|
|
|
94084c |
@@ -73,6 +73,12 @@ void support_write_file_string (const char *path, const char *contents);
|
|
|
94084c |
the result). */
|
|
|
94084c |
char *support_quote_blob (const void *blob, size_t length);
|
|
|
94084c |
|
|
|
94084c |
+/* Quote the contents of the wide character array starting at BLOB, of
|
|
|
94084c |
+ LENGTH wide characters, in such a way that the result string can be
|
|
|
94084c |
+ included in a C wide string literal (in single/double quotes,
|
|
|
94084c |
+ without putting the quotes into the result). */
|
|
|
94084c |
+char *support_quote_blob_wide (const void *blob, size_t length);
|
|
|
94084c |
+
|
|
|
94084c |
/* Quote the contents of the string, in such a way that the result
|
|
|
94084c |
string can be included in a C literal (in single/double quotes,
|
|
|
94084c |
without putting the quotes into the result). */
|
|
|
94084c |
diff --git a/support/support_quote_blob.c b/support/support_quote_blob.c
|
|
|
94084c |
index b5e70125f13eb081..611980c9a2108670 100644
|
|
|
94084c |
--- a/support/support_quote_blob.c
|
|
|
94084c |
+++ b/support/support_quote_blob.c
|
|
|
94084c |
@@ -1,4 +1,4 @@
|
|
|
94084c |
-/* Quote a blob so that it can be used in C literals.
|
|
|
94084c |
+/* Quote a narrow string blob so that it can be used in C literals.
|
|
|
94084c |
Copyright (C) 2018-2021 Free Software Foundation, Inc.
|
|
|
94084c |
This file is part of the GNU C Library.
|
|
|
94084c |
|
|
|
94084c |
@@ -16,68 +16,9 @@
|
|
|
94084c |
License along with the GNU C Library; if not, see
|
|
|
94084c |
<https://www.gnu.org/licenses/>. */
|
|
|
94084c |
|
|
|
94084c |
-#include <support/support.h>
|
|
|
94084c |
-#include <support/xmemstream.h>
|
|
|
94084c |
+#define CHAR unsigned char
|
|
|
94084c |
+#define L_(C) C
|
|
|
94084c |
+#define SUPPORT_QUOTE_BLOB support_quote_blob
|
|
|
94084c |
+#define WIDE 0
|
|
|
94084c |
|
|
|
94084c |
-char *
|
|
|
94084c |
-support_quote_blob (const void *blob, size_t length)
|
|
|
94084c |
-{
|
|
|
94084c |
- struct xmemstream out;
|
|
|
94084c |
- xopen_memstream (&out;;
|
|
|
94084c |
-
|
|
|
94084c |
- const unsigned char *p = blob;
|
|
|
94084c |
- for (size_t i = 0; i < length; ++i)
|
|
|
94084c |
- {
|
|
|
94084c |
- unsigned char ch = p[i];
|
|
|
94084c |
-
|
|
|
94084c |
- /* Use C backslash escapes for those control characters for
|
|
|
94084c |
- which they are defined. */
|
|
|
94084c |
- switch (ch)
|
|
|
94084c |
- {
|
|
|
94084c |
- case '\a':
|
|
|
94084c |
- putc_unlocked ('\\', out.out);
|
|
|
94084c |
- putc_unlocked ('a', out.out);
|
|
|
94084c |
- break;
|
|
|
94084c |
- case '\b':
|
|
|
94084c |
- putc_unlocked ('\\', out.out);
|
|
|
94084c |
- putc_unlocked ('b', out.out);
|
|
|
94084c |
- break;
|
|
|
94084c |
- case '\f':
|
|
|
94084c |
- putc_unlocked ('\\', out.out);
|
|
|
94084c |
- putc_unlocked ('f', out.out);
|
|
|
94084c |
- break;
|
|
|
94084c |
- case '\n':
|
|
|
94084c |
- putc_unlocked ('\\', out.out);
|
|
|
94084c |
- putc_unlocked ('n', out.out);
|
|
|
94084c |
- break;
|
|
|
94084c |
- case '\r':
|
|
|
94084c |
- putc_unlocked ('\\', out.out);
|
|
|
94084c |
- putc_unlocked ('r', out.out);
|
|
|
94084c |
- break;
|
|
|
94084c |
- case '\t':
|
|
|
94084c |
- putc_unlocked ('\\', out.out);
|
|
|
94084c |
- putc_unlocked ('t', out.out);
|
|
|
94084c |
- break;
|
|
|
94084c |
- case '\v':
|
|
|
94084c |
- putc_unlocked ('\\', out.out);
|
|
|
94084c |
- putc_unlocked ('v', out.out);
|
|
|
94084c |
- break;
|
|
|
94084c |
- case '\\':
|
|
|
94084c |
- case '\'':
|
|
|
94084c |
- case '\"':
|
|
|
94084c |
- putc_unlocked ('\\', out.out);
|
|
|
94084c |
- putc_unlocked (ch, out.out);
|
|
|
94084c |
- break;
|
|
|
94084c |
- default:
|
|
|
94084c |
- if (ch < ' ' || ch > '~')
|
|
|
94084c |
- /* Use octal sequences because they are fixed width,
|
|
|
94084c |
- unlike hexadecimal sequences. */
|
|
|
94084c |
- fprintf (out.out, "\\%03o", ch);
|
|
|
94084c |
- else
|
|
|
94084c |
- putc_unlocked (ch, out.out);
|
|
|
94084c |
- }
|
|
|
94084c |
- }
|
|
|
94084c |
-
|
|
|
94084c |
- xfclose_memstream (&out;;
|
|
|
94084c |
- return out.buffer;
|
|
|
94084c |
-}
|
|
|
94084c |
+#include "support_quote_blob_main.c"
|
|
|
94084c |
diff --git a/support/support_quote_blob_main.c b/support/support_quote_blob_main.c
|
|
|
94084c |
new file mode 100644
|
|
|
94084c |
index 0000000000000000..19ccfad59311bfee
|
|
|
94084c |
--- /dev/null
|
|
|
94084c |
+++ b/support/support_quote_blob_main.c
|
|
|
94084c |
@@ -0,0 +1,88 @@
|
|
|
94084c |
+/* Quote a blob so that it can be used in C literals.
|
|
|
94084c |
+ Copyright (C) 2018-2021 Free Software Foundation, Inc.
|
|
|
94084c |
+ This file is part of the GNU C Library.
|
|
|
94084c |
+
|
|
|
94084c |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
94084c |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
94084c |
+ License as published by the Free Software Foundation; either
|
|
|
94084c |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
94084c |
+
|
|
|
94084c |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
94084c |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
94084c |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
94084c |
+ Lesser General Public License for more details.
|
|
|
94084c |
+
|
|
|
94084c |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
94084c |
+ License along with the GNU C Library; if not, see
|
|
|
94084c |
+ <https://www.gnu.org/licenses/>. */
|
|
|
94084c |
+
|
|
|
94084c |
+#include <support/support.h>
|
|
|
94084c |
+#include <support/xmemstream.h>
|
|
|
94084c |
+
|
|
|
94084c |
+char *
|
|
|
94084c |
+SUPPORT_QUOTE_BLOB (const void *blob, size_t length)
|
|
|
94084c |
+{
|
|
|
94084c |
+ struct xmemstream out;
|
|
|
94084c |
+ xopen_memstream (&out;;
|
|
|
94084c |
+
|
|
|
94084c |
+ const CHAR *p = blob;
|
|
|
94084c |
+ for (size_t i = 0; i < length; ++i)
|
|
|
94084c |
+ {
|
|
|
94084c |
+ CHAR ch = p[i];
|
|
|
94084c |
+
|
|
|
94084c |
+ /* Use C backslash escapes for those control characters for
|
|
|
94084c |
+ which they are defined. */
|
|
|
94084c |
+ switch (ch)
|
|
|
94084c |
+ {
|
|
|
94084c |
+ case L_('\a'):
|
|
|
94084c |
+ putc_unlocked ('\\', out.out);
|
|
|
94084c |
+ putc_unlocked ('a', out.out);
|
|
|
94084c |
+ break;
|
|
|
94084c |
+ case L_('\b'):
|
|
|
94084c |
+ putc_unlocked ('\\', out.out);
|
|
|
94084c |
+ putc_unlocked ('b', out.out);
|
|
|
94084c |
+ break;
|
|
|
94084c |
+ case L_('\f'):
|
|
|
94084c |
+ putc_unlocked ('\\', out.out);
|
|
|
94084c |
+ putc_unlocked ('f', out.out);
|
|
|
94084c |
+ break;
|
|
|
94084c |
+ case L_('\n'):
|
|
|
94084c |
+ putc_unlocked ('\\', out.out);
|
|
|
94084c |
+ putc_unlocked ('n', out.out);
|
|
|
94084c |
+ break;
|
|
|
94084c |
+ case L_('\r'):
|
|
|
94084c |
+ putc_unlocked ('\\', out.out);
|
|
|
94084c |
+ putc_unlocked ('r', out.out);
|
|
|
94084c |
+ break;
|
|
|
94084c |
+ case L_('\t'):
|
|
|
94084c |
+ putc_unlocked ('\\', out.out);
|
|
|
94084c |
+ putc_unlocked ('t', out.out);
|
|
|
94084c |
+ break;
|
|
|
94084c |
+ case L_('\v'):
|
|
|
94084c |
+ putc_unlocked ('\\', out.out);
|
|
|
94084c |
+ putc_unlocked ('v', out.out);
|
|
|
94084c |
+ break;
|
|
|
94084c |
+ case L_('\\'):
|
|
|
94084c |
+ case L_('\''):
|
|
|
94084c |
+ case L_('\"'):
|
|
|
94084c |
+ putc_unlocked ('\\', out.out);
|
|
|
94084c |
+ putc_unlocked (ch, out.out);
|
|
|
94084c |
+ break;
|
|
|
94084c |
+ default:
|
|
|
94084c |
+ if (ch < L_(' ') || ch > L_('~'))
|
|
|
94084c |
+ /* For narrow characters, use octal sequences because they
|
|
|
94084c |
+ are fixed width, unlike hexadecimal sequences. For
|
|
|
94084c |
+ wide characters, use N2785 delimited escape
|
|
|
94084c |
+ sequences. */
|
|
|
94084c |
+ if (WIDE)
|
|
|
94084c |
+ fprintf (out.out, "\\x{%x}", (unsigned int) ch);
|
|
|
94084c |
+ else
|
|
|
94084c |
+ fprintf (out.out, "\\%03o", (unsigned int) ch);
|
|
|
94084c |
+ else
|
|
|
94084c |
+ putc_unlocked (ch, out.out);
|
|
|
94084c |
+ }
|
|
|
94084c |
+ }
|
|
|
94084c |
+
|
|
|
94084c |
+ xfclose_memstream (&out;;
|
|
|
94084c |
+ return out.buffer;
|
|
|
94084c |
+}
|
|
|
94084c |
diff --git a/support/support_quote_blob_wide.c b/support/support_quote_blob_wide.c
|
|
|
94084c |
new file mode 100644
|
|
|
94084c |
index 0000000000000000..c451ed889c21c626
|
|
|
94084c |
--- /dev/null
|
|
|
94084c |
+++ b/support/support_quote_blob_wide.c
|
|
|
94084c |
@@ -0,0 +1,24 @@
|
|
|
94084c |
+/* Quote a wide string blob so that it can be used in C literals.
|
|
|
94084c |
+ Copyright (C) 2018-2021 Free Software Foundation, Inc.
|
|
|
94084c |
+ This file is part of the GNU C Library.
|
|
|
94084c |
+
|
|
|
94084c |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
94084c |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
94084c |
+ License as published by the Free Software Foundation; either
|
|
|
94084c |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
94084c |
+
|
|
|
94084c |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
94084c |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
94084c |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
94084c |
+ Lesser General Public License for more details.
|
|
|
94084c |
+
|
|
|
94084c |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
94084c |
+ License along with the GNU C Library; if not, see
|
|
|
94084c |
+ <https://www.gnu.org/licenses/>. */
|
|
|
94084c |
+
|
|
|
94084c |
+#define CHAR wchar_t
|
|
|
94084c |
+#define L_(C) L ## C
|
|
|
94084c |
+#define SUPPORT_QUOTE_BLOB support_quote_blob_wide
|
|
|
94084c |
+#define WIDE 1
|
|
|
94084c |
+
|
|
|
94084c |
+#include "support_quote_blob_main.c"
|
|
|
94084c |
diff --git a/support/support_test_compare_string.c b/support/support_test_compare_string.c
|
|
|
94084c |
index cbeaf7b1eeea8ca8..12bafe43d44ae3d7 100644
|
|
|
94084c |
--- a/support/support_test_compare_string.c
|
|
|
94084c |
+++ b/support/support_test_compare_string.c
|
|
|
94084c |
@@ -16,76 +16,13 @@
|
|
|
94084c |
License along with the GNU C Library; if not, see
|
|
|
94084c |
<https://www.gnu.org/licenses/>. */
|
|
|
94084c |
|
|
|
94084c |
-#include <stdio.h>
|
|
|
94084c |
-#include <stdlib.h>
|
|
|
94084c |
-#include <string.h>
|
|
|
94084c |
-#include <support/check.h>
|
|
|
94084c |
-#include <support/support.h>
|
|
|
94084c |
-#include <support/xmemstream.h>
|
|
|
94084c |
-
|
|
|
94084c |
-static void
|
|
|
94084c |
-report_length (const char *what, const char *str, size_t length)
|
|
|
94084c |
-{
|
|
|
94084c |
- if (str == NULL)
|
|
|
94084c |
- printf (" %s string: NULL\n", what);
|
|
|
94084c |
- else
|
|
|
94084c |
- printf (" %s string: %zu bytes\n", what, length);
|
|
|
94084c |
-}
|
|
|
94084c |
-
|
|
|
94084c |
-static void
|
|
|
94084c |
-report_string (const char *what, const unsigned char *blob,
|
|
|
94084c |
- size_t length, const char *expr)
|
|
|
94084c |
-{
|
|
|
94084c |
- if (length > 0)
|
|
|
94084c |
- {
|
|
|
94084c |
- printf (" %s (evaluated from %s):\n", what, expr);
|
|
|
94084c |
- char *quoted = support_quote_blob (blob, length);
|
|
|
94084c |
- printf (" \"%s\"\n", quoted);
|
|
|
94084c |
- free (quoted);
|
|
|
94084c |
-
|
|
|
94084c |
- fputs (" ", stdout);
|
|
|
94084c |
- for (size_t i = 0; i < length; ++i)
|
|
|
94084c |
- printf (" %02X", blob[i]);
|
|
|
94084c |
- putc ('\n', stdout);
|
|
|
94084c |
- }
|
|
|
94084c |
-}
|
|
|
94084c |
-
|
|
|
94084c |
-static size_t
|
|
|
94084c |
-string_length_or_zero (const char *str)
|
|
|
94084c |
-{
|
|
|
94084c |
- if (str == NULL)
|
|
|
94084c |
- return 0;
|
|
|
94084c |
- else
|
|
|
94084c |
- return strlen (str);
|
|
|
94084c |
-}
|
|
|
94084c |
-
|
|
|
94084c |
-void
|
|
|
94084c |
-support_test_compare_string (const char *left, const char *right,
|
|
|
94084c |
- const char *file, int line,
|
|
|
94084c |
- const char *left_expr, const char *right_expr)
|
|
|
94084c |
-{
|
|
|
94084c |
- /* Two null pointers are accepted. */
|
|
|
94084c |
- if (left == NULL && right == NULL)
|
|
|
94084c |
- return;
|
|
|
94084c |
-
|
|
|
94084c |
- size_t left_length = string_length_or_zero (left);
|
|
|
94084c |
- size_t right_length = string_length_or_zero (right);
|
|
|
94084c |
-
|
|
|
94084c |
- if (left_length != right_length || left == NULL || right == NULL
|
|
|
94084c |
- || memcmp (left, right, left_length) != 0)
|
|
|
94084c |
- {
|
|
|
94084c |
- support_record_failure ();
|
|
|
94084c |
- printf ("%s:%d: error: string comparison failed\n", file, line);
|
|
|
94084c |
- if (left_length == right_length && right != NULL && left != NULL)
|
|
|
94084c |
- printf (" string length: %zu bytes\n", left_length);
|
|
|
94084c |
- else
|
|
|
94084c |
- {
|
|
|
94084c |
- report_length ("left", left, left_length);
|
|
|
94084c |
- report_length ("right", right, right_length);
|
|
|
94084c |
- }
|
|
|
94084c |
- report_string ("left", (const unsigned char *) left,
|
|
|
94084c |
- left_length, left_expr);
|
|
|
94084c |
- report_string ("right", (const unsigned char *) right,
|
|
|
94084c |
- right_length, right_expr);
|
|
|
94084c |
- }
|
|
|
94084c |
-}
|
|
|
94084c |
+#define CHAR char
|
|
|
94084c |
+#define UCHAR unsigned char
|
|
|
94084c |
+#define LPREFIX ""
|
|
|
94084c |
+#define STRLEN strlen
|
|
|
94084c |
+#define MEMCMP memcmp
|
|
|
94084c |
+#define SUPPORT_QUOTE_BLOB support_quote_blob
|
|
|
94084c |
+#define SUPPORT_TEST_COMPARE_STRING support_test_compare_string
|
|
|
94084c |
+#define WIDE 0
|
|
|
94084c |
+
|
|
|
94084c |
+#include "support_test_compare_string_main.c"
|
|
|
94084c |
diff --git a/support/support_test_compare_string_main.c b/support/support_test_compare_string_main.c
|
|
|
94084c |
new file mode 100644
|
|
|
94084c |
index 0000000000000000..0edc0ca97d79d71e
|
|
|
94084c |
--- /dev/null
|
|
|
94084c |
+++ b/support/support_test_compare_string_main.c
|
|
|
94084c |
@@ -0,0 +1,94 @@
|
|
|
94084c |
+/* Check two strings for equality.
|
|
|
94084c |
+ Copyright (C) 2018-2021 Free Software Foundation, Inc.
|
|
|
94084c |
+ This file is part of the GNU C Library.
|
|
|
94084c |
+
|
|
|
94084c |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
94084c |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
94084c |
+ License as published by the Free Software Foundation; either
|
|
|
94084c |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
94084c |
+
|
|
|
94084c |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
94084c |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
94084c |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
94084c |
+ Lesser General Public License for more details.
|
|
|
94084c |
+
|
|
|
94084c |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
94084c |
+ License along with the GNU C Library; if not, see
|
|
|
94084c |
+ <https://www.gnu.org/licenses/>. */
|
|
|
94084c |
+
|
|
|
94084c |
+#include <stdio.h>
|
|
|
94084c |
+#include <stdlib.h>
|
|
|
94084c |
+#include <string.h>
|
|
|
94084c |
+#include <wchar.h>
|
|
|
94084c |
+#include <support/check.h>
|
|
|
94084c |
+#include <support/support.h>
|
|
|
94084c |
+#include <support/xmemstream.h>
|
|
|
94084c |
+
|
|
|
94084c |
+static void
|
|
|
94084c |
+report_length (const char *what, const CHAR *str, size_t length)
|
|
|
94084c |
+{
|
|
|
94084c |
+ if (str == NULL)
|
|
|
94084c |
+ printf (" %s string: NULL\n", what);
|
|
|
94084c |
+ else
|
|
|
94084c |
+ printf (" %s string: %zu %s\n", what, length,
|
|
|
94084c |
+ WIDE ? "wide characters" : "bytes");
|
|
|
94084c |
+}
|
|
|
94084c |
+
|
|
|
94084c |
+static void
|
|
|
94084c |
+report_string (const char *what, const UCHAR *blob,
|
|
|
94084c |
+ size_t length, const char *expr)
|
|
|
94084c |
+{
|
|
|
94084c |
+ if (length > 0)
|
|
|
94084c |
+ {
|
|
|
94084c |
+ printf (" %s (evaluated from %s):\n", what, expr);
|
|
|
94084c |
+ char *quoted = SUPPORT_QUOTE_BLOB (blob, length);
|
|
|
94084c |
+ printf (" %s\"%s\"\n", LPREFIX, quoted);
|
|
|
94084c |
+ free (quoted);
|
|
|
94084c |
+
|
|
|
94084c |
+ fputs (" ", stdout);
|
|
|
94084c |
+ for (size_t i = 0; i < length; ++i)
|
|
|
94084c |
+ printf (" %02X", (unsigned int) blob[i]);
|
|
|
94084c |
+ putc ('\n', stdout);
|
|
|
94084c |
+ }
|
|
|
94084c |
+}
|
|
|
94084c |
+
|
|
|
94084c |
+static size_t
|
|
|
94084c |
+string_length_or_zero (const CHAR *str)
|
|
|
94084c |
+{
|
|
|
94084c |
+ if (str == NULL)
|
|
|
94084c |
+ return 0;
|
|
|
94084c |
+ else
|
|
|
94084c |
+ return STRLEN (str);
|
|
|
94084c |
+}
|
|
|
94084c |
+
|
|
|
94084c |
+void
|
|
|
94084c |
+SUPPORT_TEST_COMPARE_STRING (const CHAR *left, const CHAR *right,
|
|
|
94084c |
+ const char *file, int line,
|
|
|
94084c |
+ const char *left_expr, const char *right_expr)
|
|
|
94084c |
+{
|
|
|
94084c |
+ /* Two null pointers are accepted. */
|
|
|
94084c |
+ if (left == NULL && right == NULL)
|
|
|
94084c |
+ return;
|
|
|
94084c |
+
|
|
|
94084c |
+ size_t left_length = string_length_or_zero (left);
|
|
|
94084c |
+ size_t right_length = string_length_or_zero (right);
|
|
|
94084c |
+
|
|
|
94084c |
+ if (left_length != right_length || left == NULL || right == NULL
|
|
|
94084c |
+ || MEMCMP (left, right, left_length) != 0)
|
|
|
94084c |
+ {
|
|
|
94084c |
+ support_record_failure ();
|
|
|
94084c |
+ printf ("%s:%d: error: string comparison failed\n", file, line);
|
|
|
94084c |
+ if (left_length == right_length && right != NULL && left != NULL)
|
|
|
94084c |
+ printf (" string length: %zu %s\n", left_length,
|
|
|
94084c |
+ WIDE ? "wide characters" : "bytes");
|
|
|
94084c |
+ else
|
|
|
94084c |
+ {
|
|
|
94084c |
+ report_length ("left", left, left_length);
|
|
|
94084c |
+ report_length ("right", right, right_length);
|
|
|
94084c |
+ }
|
|
|
94084c |
+ report_string ("left", (const UCHAR *) left,
|
|
|
94084c |
+ left_length, left_expr);
|
|
|
94084c |
+ report_string ("right", (const UCHAR *) right,
|
|
|
94084c |
+ right_length, right_expr);
|
|
|
94084c |
+ }
|
|
|
94084c |
+}
|
|
|
94084c |
diff --git a/support/support_test_compare_string_wide.c b/support/support_test_compare_string_wide.c
|
|
|
94084c |
new file mode 100644
|
|
|
94084c |
index 0000000000000000..88b560b142a3c356
|
|
|
94084c |
--- /dev/null
|
|
|
94084c |
+++ b/support/support_test_compare_string_wide.c
|
|
|
94084c |
@@ -0,0 +1,28 @@
|
|
|
94084c |
+/* Check two wide strings for equality.
|
|
|
94084c |
+ Copyright (C) 2018-2021 Free Software Foundation, Inc.
|
|
|
94084c |
+ This file is part of the GNU C Library.
|
|
|
94084c |
+
|
|
|
94084c |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
94084c |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
94084c |
+ License as published by the Free Software Foundation; either
|
|
|
94084c |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
94084c |
+
|
|
|
94084c |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
94084c |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
94084c |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
94084c |
+ Lesser General Public License for more details.
|
|
|
94084c |
+
|
|
|
94084c |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
94084c |
+ License along with the GNU C Library; if not, see
|
|
|
94084c |
+ <https://www.gnu.org/licenses/>. */
|
|
|
94084c |
+
|
|
|
94084c |
+#define CHAR wchar_t
|
|
|
94084c |
+#define UCHAR wchar_t
|
|
|
94084c |
+#define LPREFIX "L"
|
|
|
94084c |
+#define STRLEN wcslen
|
|
|
94084c |
+#define MEMCMP wmemcmp
|
|
|
94084c |
+#define SUPPORT_QUOTE_BLOB support_quote_blob_wide
|
|
|
94084c |
+#define SUPPORT_TEST_COMPARE_STRING support_test_compare_string_wide
|
|
|
94084c |
+#define WIDE 1
|
|
|
94084c |
+
|
|
|
94084c |
+#include "support_test_compare_string_main.c"
|
|
|
94084c |
diff --git a/support/tst-support_quote_blob_wide.c b/support/tst-support_quote_blob_wide.c
|
|
|
94084c |
new file mode 100644
|
|
|
94084c |
index 0000000000000000..ea71a1f7f873b23a
|
|
|
94084c |
--- /dev/null
|
|
|
94084c |
+++ b/support/tst-support_quote_blob_wide.c
|
|
|
94084c |
@@ -0,0 +1,66 @@
|
|
|
94084c |
+/* Test the support_quote_blob_wide function.
|
|
|
94084c |
+ Copyright (C) 2018-2021 Free Software Foundation, Inc.
|
|
|
94084c |
+ This file is part of the GNU C Library.
|
|
|
94084c |
+
|
|
|
94084c |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
94084c |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
94084c |
+ License as published by the Free Software Foundation; either
|
|
|
94084c |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
94084c |
+
|
|
|
94084c |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
94084c |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
94084c |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
94084c |
+ Lesser General Public License for more details.
|
|
|
94084c |
+
|
|
|
94084c |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
94084c |
+ License along with the GNU C Library; if not, see
|
|
|
94084c |
+ <https://www.gnu.org/licenses/>. */
|
|
|
94084c |
+
|
|
|
94084c |
+#include <support/check.h>
|
|
|
94084c |
+#include <support/support.h>
|
|
|
94084c |
+#include <string.h>
|
|
|
94084c |
+#include <stdlib.h>
|
|
|
94084c |
+
|
|
|
94084c |
+static int
|
|
|
94084c |
+do_test (void)
|
|
|
94084c |
+{
|
|
|
94084c |
+ /* Check handling of the empty blob, both with and without trailing
|
|
|
94084c |
+ NUL byte. */
|
|
|
94084c |
+ char *p = support_quote_blob_wide (L"", 0);
|
|
|
94084c |
+ TEST_COMPARE (strlen (p), 0);
|
|
|
94084c |
+ free (p);
|
|
|
94084c |
+ p = support_quote_blob_wide (L"X", 0);
|
|
|
94084c |
+ TEST_COMPARE (strlen (p), 0);
|
|
|
94084c |
+ free (p);
|
|
|
94084c |
+
|
|
|
94084c |
+ /* Check escaping of backslash-escaped characters, and lack of
|
|
|
94084c |
+ escaping for other shell meta-characters. */
|
|
|
94084c |
+ p = support_quote_blob_wide (L"$()*?`@[]{}~\'\"X", 14);
|
|
|
94084c |
+ TEST_COMPARE (strcmp (p, "$()*?`@[]{}~\\'\\\""), 0);
|
|
|
94084c |
+ free (p);
|
|
|
94084c |
+
|
|
|
94084c |
+ /* Check lack of escaping for letters and digits. */
|
|
|
94084c |
+#define LETTERS_AND_DIGTS \
|
|
|
94084c |
+ "abcdefghijklmnopqrstuvwxyz" \
|
|
|
94084c |
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
|
|
94084c |
+ "0123456789"
|
|
|
94084c |
+#define CONCATX(X, Y) X ## Y
|
|
|
94084c |
+#define CONCAT(X, Y) CONCATX (X, Y)
|
|
|
94084c |
+#define WLETTERS_AND_DIGTS CONCAT (L, LETTERS_AND_DIGTS)
|
|
|
94084c |
+ p = support_quote_blob_wide (WLETTERS_AND_DIGTS "@", 2 * 26 + 10);
|
|
|
94084c |
+ TEST_COMPARE (strcmp (p, LETTERS_AND_DIGTS), 0);
|
|
|
94084c |
+ free (p);
|
|
|
94084c |
+
|
|
|
94084c |
+ /* Check escaping of control characters and other non-printable
|
|
|
94084c |
+ characters. */
|
|
|
94084c |
+ p = support_quote_blob_wide (L"\r\n\t\a\b\f\v\1\177\200\377"
|
|
|
94084c |
+ "\x123\x76543210\xfedcba98\0@", 17);
|
|
|
94084c |
+ TEST_COMPARE (strcmp (p, "\\r\\n\\t\\a\\b\\f\\v\\x{1}"
|
|
|
94084c |
+ "\\x{7f}\\x{80}\\x{ff}\\x{123}\\x{76543210}"
|
|
|
94084c |
+ "\\x{fedcba98}\\x{0}@\\x{0}"), 0);
|
|
|
94084c |
+ free (p);
|
|
|
94084c |
+
|
|
|
94084c |
+ return 0;
|
|
|
94084c |
+}
|
|
|
94084c |
+
|
|
|
94084c |
+#include <support/test-driver.c>
|
|
|
94084c |
diff --git a/support/tst-test_compare_string_wide.c b/support/tst-test_compare_string_wide.c
|
|
|
94084c |
new file mode 100644
|
|
|
94084c |
index 0000000000000000..548f7dcdc60b82d8
|
|
|
94084c |
--- /dev/null
|
|
|
94084c |
+++ b/support/tst-test_compare_string_wide.c
|
|
|
94084c |
@@ -0,0 +1,107 @@
|
|
|
94084c |
+/* Basic test for the TEST_COMPARE_STRING_WIDE macro.
|
|
|
94084c |
+ Copyright (C) 2018-2021 Free Software Foundation, Inc.
|
|
|
94084c |
+ This file is part of the GNU C Library.
|
|
|
94084c |
+
|
|
|
94084c |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
94084c |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
94084c |
+ License as published by the Free Software Foundation; either
|
|
|
94084c |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
94084c |
+
|
|
|
94084c |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
94084c |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
94084c |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
94084c |
+ Lesser General Public License for more details.
|
|
|
94084c |
+
|
|
|
94084c |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
94084c |
+ License along with the GNU C Library; if not, see
|
|
|
94084c |
+ <https://www.gnu.org/licenses/>. */
|
|
|
94084c |
+
|
|
|
94084c |
+#include <string.h>
|
|
|
94084c |
+#include <support/check.h>
|
|
|
94084c |
+#include <support/capture_subprocess.h>
|
|
|
94084c |
+
|
|
|
94084c |
+static void
|
|
|
94084c |
+subprocess (void *closure)
|
|
|
94084c |
+{
|
|
|
94084c |
+ /* These tests should fail. They were chosen to cover differences
|
|
|
94084c |
+ in length (with the same contents), single-bit mismatches, and
|
|
|
94084c |
+ mismatching null pointers. */
|
|
|
94084c |
+ TEST_COMPARE_STRING_WIDE (L"", NULL); /* Line 29. */
|
|
|
94084c |
+ TEST_COMPARE_STRING_WIDE (L"X", L""); /* Line 30. */
|
|
|
94084c |
+ TEST_COMPARE_STRING_WIDE (NULL, L"X"); /* Line 31. */
|
|
|
94084c |
+ TEST_COMPARE_STRING_WIDE (L"abcd", L"abcD"); /* Line 32. */
|
|
|
94084c |
+ TEST_COMPARE_STRING_WIDE (L"abcd", NULL); /* Line 33. */
|
|
|
94084c |
+ TEST_COMPARE_STRING_WIDE (NULL, L"abcd"); /* Line 34. */
|
|
|
94084c |
+}
|
|
|
94084c |
+
|
|
|
94084c |
+/* Same contents, different addresses. */
|
|
|
94084c |
+wchar_t buffer_abc_1[] = L"abc";
|
|
|
94084c |
+wchar_t buffer_abc_2[] = L"abc";
|
|
|
94084c |
+
|
|
|
94084c |
+static int
|
|
|
94084c |
+do_test (void)
|
|
|
94084c |
+{
|
|
|
94084c |
+ /* This should succeed. Even if the pointers and array contents are
|
|
|
94084c |
+ different, zero-length inputs are not different. */
|
|
|
94084c |
+ TEST_COMPARE_STRING_WIDE (NULL, NULL);
|
|
|
94084c |
+ TEST_COMPARE_STRING_WIDE (L"", L"");
|
|
|
94084c |
+ TEST_COMPARE_STRING_WIDE (buffer_abc_1, buffer_abc_2);
|
|
|
94084c |
+ TEST_COMPARE_STRING_WIDE (buffer_abc_1, L"abc");
|
|
|
94084c |
+
|
|
|
94084c |
+ struct support_capture_subprocess proc = support_capture_subprocess
|
|
|
94084c |
+ (&subprocess, NULL);
|
|
|
94084c |
+
|
|
|
94084c |
+ /* Discard the reported error. */
|
|
|
94084c |
+ support_record_failure_reset ();
|
|
|
94084c |
+
|
|
|
94084c |
+ puts ("info: *** subprocess output starts ***");
|
|
|
94084c |
+ fputs (proc.out.buffer, stdout);
|
|
|
94084c |
+ puts ("info: *** subprocess output ends ***");
|
|
|
94084c |
+
|
|
|
94084c |
+ TEST_VERIFY
|
|
|
94084c |
+ (strcmp (proc.out.buffer,
|
|
|
94084c |
+"tst-test_compare_string_wide.c:29: error: string comparison failed\n"
|
|
|
94084c |
+" left string: 0 wide characters\n"
|
|
|
94084c |
+" right string: NULL\n"
|
|
|
94084c |
+"tst-test_compare_string_wide.c:30: error: string comparison failed\n"
|
|
|
94084c |
+" left string: 1 wide characters\n"
|
|
|
94084c |
+" right string: 0 wide characters\n"
|
|
|
94084c |
+" left (evaluated from L\"X\"):\n"
|
|
|
94084c |
+" L\"X\"\n"
|
|
|
94084c |
+" 58\n"
|
|
|
94084c |
+"tst-test_compare_string_wide.c:31: error: string comparison failed\n"
|
|
|
94084c |
+" left string: NULL\n"
|
|
|
94084c |
+" right string: 1 wide characters\n"
|
|
|
94084c |
+" right (evaluated from L\"X\"):\n"
|
|
|
94084c |
+" L\"X\"\n"
|
|
|
94084c |
+" 58\n"
|
|
|
94084c |
+"tst-test_compare_string_wide.c:32: error: string comparison failed\n"
|
|
|
94084c |
+" string length: 4 wide characters\n"
|
|
|
94084c |
+" left (evaluated from L\"abcd\"):\n"
|
|
|
94084c |
+" L\"abcd\"\n"
|
|
|
94084c |
+" 61 62 63 64\n"
|
|
|
94084c |
+" right (evaluated from L\"abcD\"):\n"
|
|
|
94084c |
+" L\"abcD\"\n"
|
|
|
94084c |
+" 61 62 63 44\n"
|
|
|
94084c |
+"tst-test_compare_string_wide.c:33: error: string comparison failed\n"
|
|
|
94084c |
+" left string: 4 wide characters\n"
|
|
|
94084c |
+" right string: NULL\n"
|
|
|
94084c |
+" left (evaluated from L\"abcd\"):\n"
|
|
|
94084c |
+" L\"abcd\"\n"
|
|
|
94084c |
+" 61 62 63 64\n"
|
|
|
94084c |
+"tst-test_compare_string_wide.c:34: error: string comparison failed\n"
|
|
|
94084c |
+" left string: NULL\n"
|
|
|
94084c |
+" right string: 4 wide characters\n"
|
|
|
94084c |
+" right (evaluated from L\"abcd\"):\n"
|
|
|
94084c |
+" L\"abcd\"\n"
|
|
|
94084c |
+" 61 62 63 64\n"
|
|
|
94084c |
+ ) == 0);
|
|
|
94084c |
+
|
|
|
94084c |
+ /* Check that there is no output on standard error. */
|
|
|
94084c |
+ support_capture_subprocess_check (&proc, "TEST_COMPARE_STRING_WIDE",
|
|
|
94084c |
+ 0, sc_allow_stdout);
|
|
|
94084c |
+
|
|
|
94084c |
+ return 0;
|
|
|
94084c |
+}
|
|
|
94084c |
+
|
|
|
94084c |
+#include <support/test-driver.c>
|