This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Add selftest for pretty-print.c
- From: David Malcolm <dmalcolm at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org
- Cc: David Malcolm <dmalcolm at redhat dot com>
- Date: Mon, 6 Jun 2016 17:28:29 -0400
- Subject: [PATCH] Add selftest for pretty-print.c
- Authentication-results: sourceware.org; auth=none
This adds another set of test cases to -fself-test, this time
for the basic functionality within pretty-print.c.
Successfully bootstrapped®rtested on x86_64-pc-linux-gnu.
OK for trunk?
gcc/ChangeLog:
* pretty-print.c: Include "selftest.h".
(pp_format): Fix comment.
(selftest::test_basic_printing): New function.
(selftest::assert_pp_format_va): New function.
(selftest::assert_pp_format): New function.
(selftest::assert_pp_format_colored): New function.
(selftest::test_pp_format): New function.
(selftest::pretty_print_c_tests): New function.
* selftest-run-tests.c (selftest::run_tests): Call
selftest::pretty_print_c_tests.
* selftest.h (selftest::pretty_print_c_tests): New declaration.
---
gcc/pretty-print.c | 153 ++++++++++++++++++++++++++++++++++++++++++++++-
gcc/selftest-run-tests.c | 1 +
gcc/selftest.h | 1 +
3 files changed, 154 insertions(+), 1 deletion(-)
diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c
index cc2b8cc..d1829f3 100644
--- a/gcc/pretty-print.c
+++ b/gcc/pretty-print.c
@@ -24,6 +24,7 @@ along with GCC; see the file COPYING3. If not see
#include "intl.h"
#include "pretty-print.h"
#include "diagnostic-color.h"
+#include "selftest.h"
#if HAVE_ICONV
#include <iconv.h>
@@ -304,7 +305,7 @@ pp_indent (pretty_printer *pp)
/* Formatting phases 1 and 2: render TEXT->format_spec plus
TEXT->args_ptr into a series of chunks in pp_buffer (PP)->args[].
- Phase 3 is in pp_format_text. */
+ Phase 3 is in pp_output_formatted_text. */
void
pp_format (pretty_printer *pp, text_info *text)
@@ -1203,3 +1204,153 @@ identifier_to_locale (const char *ident)
return ret;
}
}
+
+#if CHECKING_P
+
+namespace selftest {
+
+/* Smoketest for pretty_printer. */
+
+static void
+test_basic_printing ()
+{
+ pretty_printer pp;
+ pp_string (&pp, "hello");
+ pp_space (&pp);
+ pp_string (&pp, "world");
+
+ ASSERT_STREQ ("hello world", pp_formatted_text (&pp));
+}
+
+/* Helper function for testing pp_format.
+ Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
+ prints EXPECTED, assuming that pp_show_color is SHOW_COLOR. */
+
+static void
+assert_pp_format_va (const char *expected, bool show_color, const char *fmt,
+ va_list *ap)
+{
+ pretty_printer pp;
+ text_info ti;
+ rich_location rich_loc (line_table, UNKNOWN_LOCATION);
+
+ ti.format_spec = fmt;
+ ti.args_ptr = ap;
+ ti.err_no = 0;
+ ti.x_data = NULL;
+ ti.m_richloc = &rich_loc;
+
+ pp_show_color (&pp) = show_color;
+ pp_format (&pp, &ti);
+ pp_output_formatted_text (&pp);
+ ASSERT_STREQ (expected, pp_formatted_text (&pp));
+}
+
+/* Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
+ prints EXPECTED, with show_color disabled. */
+
+static void
+assert_pp_format (const char *expected, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start (ap, fmt);
+ assert_pp_format_va (expected, false, fmt, &ap);
+ va_end (ap);
+}
+
+/* As above, but with colorization enabled. */
+
+static void
+assert_pp_format_colored (const char *expected, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start (ap, fmt);
+ assert_pp_format_va (expected, true, fmt, &ap);
+ va_end (ap);
+}
+
+/* Verify that pp_format works, for various format codes. */
+
+static void
+test_pp_format ()
+{
+ /* Avoid introducing locale-specific differences in the results
+ by hardcoding open_quote and close_quote. */
+ const char *old_open_quote = open_quote;
+ const char *old_close_quote = close_quote;
+ open_quote = "`";
+ close_quote = "'";
+
+ /* Verify that plain text is passed through unchanged. */
+ assert_pp_format ("unformatted", "unformatted");
+
+ /* Verify various individual format codes, in the order listed in the
+ comment for pp_format above. */
+ assert_pp_format ("-27", "%d", -27);
+ assert_pp_format ("-5", "%i", -5);
+ assert_pp_format ("10", "%u", 10);
+ assert_pp_format ("17", "%o", 15);
+ assert_pp_format ("cafebabe", "%x", 0xcafebabe);
+ assert_pp_format ("-27", "%ld", (long)-27);
+ assert_pp_format ("-5", "%li", (long)-5);
+ assert_pp_format ("10", "%lu", (long)10);
+ assert_pp_format ("17", "%lo", (long)15);
+ assert_pp_format ("cafebabe", "%lx", (long)0xcafebabe);
+ assert_pp_format ("-27", "%lld", (long long)-27);
+ assert_pp_format ("-5", "%lli", (long long)-5);
+ assert_pp_format ("10", "%llu", (long long)10);
+ assert_pp_format ("17", "%llo", (long long)15);
+ assert_pp_format ("cafebabe", "%llx", (long long)0xcafebabe);
+ assert_pp_format ("-27", "%wd", (HOST_WIDE_INT)-27);
+ assert_pp_format ("-5", "%wi", (HOST_WIDE_INT)-5);
+ assert_pp_format ("10", "%wu", (unsigned HOST_WIDE_INT)10);
+ assert_pp_format ("17", "%wo", (HOST_WIDE_INT)15);
+ assert_pp_format ("0xcafebabe", "%wx", (HOST_WIDE_INT)0xcafebabe);
+ assert_pp_format ("A", "%c", 'A');
+ assert_pp_format ("hello world", "%s", "hello world");
+ assert_pp_format ("0xcafebabe", "%p", (void *)0xcafebabe);
+ assert_pp_format ("normal colored normal", "normal %rcolored%R normal",
+ "error");
+ /* The following assumes an empty value for GCC_COLORS. */
+ assert_pp_format_colored ("normal \33[01;31m\33[Kcolored\33[m\33[K normal",
+ "normal %rcolored%R normal", "error");
+ /* TODO:
+ %m: strerror(text->err_no) - does not consume a value from args_ptr. */
+ assert_pp_format ("%", "%%");
+ assert_pp_format ("`", "%<");
+ assert_pp_format ("'", "%>");
+ assert_pp_format ("'", "%'");
+ assert_pp_format ("abc", "%.*s", 3, "abcdef");
+ assert_pp_format ("abc", "%.3s", "abcdef");
+
+ /* Verify flag 'q'. */
+ assert_pp_format ("`foo'", "%qs", "foo");
+ assert_pp_format_colored ("`\33[01m\33[Kfoo\33[m\33[K'", "%qs", "foo");
+
+ /* Verify that combinations work, along with unformatted text. */
+ assert_pp_format ("the quick brown fox jumps over the lazy dog",
+ "the %s %s %s jumps over the %s %s",
+ "quick", "brown", "fox", "lazy", "dog");
+ assert_pp_format ("item 3 of 7", "item %i of %i", 3, 7);
+ assert_pp_format ("problem with `bar' at line 10",
+ "problem with %qs at line %i", "bar", 10);
+
+ /* Restore old values of open_quote and close_quote. */
+ open_quote = old_open_quote;
+ close_quote = old_close_quote;
+}
+
+/* Run all of the selftests within this file. */
+
+void
+pretty_print_c_tests ()
+{
+ test_basic_printing ();
+ test_pp_format ();
+}
+
+} // namespace selftest
+
+#endif /* CHECKING_P */
diff --git a/gcc/selftest-run-tests.c b/gcc/selftest-run-tests.c
index ab334aa..e9121b8 100644
--- a/gcc/selftest-run-tests.c
+++ b/gcc/selftest-run-tests.c
@@ -46,6 +46,7 @@ selftest::run_tests ()
hash_map_tests_c_tests ();
hash_set_tests_c_tests ();
vec_c_tests ();
+ pretty_print_c_tests ();
wide_int_cc_tests ();
/* Mid-level data structures. */
diff --git a/gcc/selftest.h b/gcc/selftest.h
index a1d3074..dba4bb7 100644
--- a/gcc/selftest.h
+++ b/gcc/selftest.h
@@ -50,6 +50,7 @@ extern void gimple_c_tests ();
extern void hash_map_tests_c_tests ();
extern void hash_set_tests_c_tests ();
extern void input_c_tests ();
+extern void pretty_print_c_tests ();
extern void rtl_tests_c_tests ();
extern void spellcheck_c_tests ();
extern void tree_c_tests ();
--
1.8.5.3