From: David Malcolm Date: Mon, 13 Jun 2016 20:58:08 +0000 (+0000) Subject: selftest: show values when ASSERT_STREQ fails X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=755fa6662d06f5ab2f32043bab4728bbf654d29d;p=gcc.git selftest: show values when ASSERT_STREQ fails Rework ASSERT_STREQ so that it prints the actual and expected values to stderr when it fails (by moving it to a helper function). gcc/ChangeLog: * selftest.c (selftest::fail_formatted): New function. (selftest::assert_streq): New function. * selftest.h (selftests::fail_formatted): New decl. (selftest::assert_streq): New decl. (ASSERT_STREQ): Reimplement in terms of selftest::assert_streq. From-SVN: r237404 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 91befb5cc24..dc16da7879f 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2016-06-13 David Malcolm + + * selftest.c (selftest::fail_formatted): New function. + (selftest::assert_streq): New function. + * selftest.h (selftests::fail_formatted): New decl. + (selftest::assert_streq): New decl. + (ASSERT_STREQ): Reimplement in terms of selftest::assert_streq. + 2016-06-13 Jeff Law PR tree-optimization/71403 diff --git a/gcc/selftest.c b/gcc/selftest.c index de804df9f3e..e5332db2a0b 100644 --- a/gcc/selftest.c +++ b/gcc/selftest.c @@ -44,4 +44,36 @@ selftest::fail (const char *file, int line, const char *msg) abort (); } +/* As "fail", but using printf-style formatted output. */ + +void +selftest::fail_formatted (const char *file, int line, const char *fmt, ...) +{ + va_list ap; + + fprintf (stderr, "%s:%i: FAIL: ", file, line); + /* TODO: add calling function name as well? */ + va_start (ap, fmt); + vfprintf (stderr, fmt, ap); + va_end (ap); + fprintf (stderr, "\n"); + abort (); +} + +/* Implementation detail of ASSERT_STREQ. */ + +void +selftest::assert_streq (const char *file, int line, + const char *desc_expected, const char *desc_actual, + const char *val_expected, const char *val_actual) +{ + if (0 == strcmp (val_expected, val_actual)) + ::selftest::pass (file, line, "ASSERT_STREQ"); + else + ::selftest::fail_formatted + (file, line, "ASSERT_STREQ (%s, %s) expected=\"%s\" actual=\"%s\"", + desc_expected, desc_actual, val_expected, val_actual); +} + + #endif /* #if CHECKING_P */ diff --git a/gcc/selftest.h b/gcc/selftest.h index d1f8accfe12..67597341623 100644 --- a/gcc/selftest.h +++ b/gcc/selftest.h @@ -39,6 +39,17 @@ extern void pass (const char *file, int line, const char *msg); extern void fail (const char *file, int line, const char *msg); +/* As "fail", but using printf-style formatted output. */ + +extern void fail_formatted (const char *file, int line, const char *fmt, ...) + ATTRIBUTE_PRINTF_3; + +/* Implementation detail of ASSERT_STREQ. */ + +extern void assert_streq (const char *file, int line, + const char *desc_expected, const char *desc_actual, + const char *val_expected, const char *val_actual); + /* Declarations for specific families of tests (by source file), in alphabetical order. */ extern void bitmap_c_tests (); @@ -123,15 +134,10 @@ extern int num_passes; ::selftest::pass if they are equal, ::selftest::fail if they are non-equal. */ -#define ASSERT_STREQ(EXPECTED, ACTUAL) \ - SELFTEST_BEGIN_STMT \ - const char *desc = "ASSERT_STREQ (" #EXPECTED ", " #ACTUAL ")"; \ - const char *expected_ = (EXPECTED); \ - const char *actual_ = (ACTUAL); \ - if (0 == strcmp (expected_, actual_)) \ - ::selftest::pass (__FILE__, __LINE__, desc); \ - else \ - ::selftest::fail (__FILE__, __LINE__, desc); \ +#define ASSERT_STREQ(EXPECTED, ACTUAL) \ + SELFTEST_BEGIN_STMT \ + ::selftest::assert_streq (__FILE__, __LINE__, #EXPECTED, #ACTUAL, \ + (EXPECTED), (ACTUAL)); \ SELFTEST_END_STMT /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,