selftest: show values when ASSERT_STREQ fails
authorDavid Malcolm <dmalcolm@redhat.com>
Mon, 13 Jun 2016 20:58:08 +0000 (20:58 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Mon, 13 Jun 2016 20:58:08 +0000 (20:58 +0000)
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

gcc/ChangeLog
gcc/selftest.c
gcc/selftest.h

index 91befb5cc249ecaf121f68799f7105f170de4505..dc16da7879f965c5b12737eea0429d00e5b6d88e 100644 (file)
@@ -1,3 +1,11 @@
+2016-06-13  David Malcolm  <dmalcolm@redhat.com>
+
+       * 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  <law@redhat.com>
 
        PR tree-optimization/71403
index de804df9f3e47576b0eaa9a1f9f06e1792898d75..e5332db2a0bc8807678891d30056bfd6c4ca377c 100644 (file)
@@ -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 */
index d1f8accfe126144ec80e4f7a2d047404157711a5..6759734162359e0617a323ac5e2e24ee2adf089b 100644 (file)
@@ -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,