diff mbox

selftest.h: add ASSERT_STR_CONTAINS

Message ID 1471653425-12391-1-git-send-email-dmalcolm@redhat.com
State New
Headers show

Commit Message

David Malcolm Aug. 20, 2016, 12:37 a.m. UTC
More enabling work for some new selftests I'm working on.

Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu.

OK for trunk?

gcc/ChangeLog:
	* selftest.c (selftest::assert_str_contains): New function.
	(selftest::test_assertions): Verify ASSERT_STR_CONTAINS.
	* selftest.h (selftest::assert_str_contains): New decl.
	(ASSERT_STR_CONTAINS): New macro.
---
 gcc/selftest.c | 34 ++++++++++++++++++++++++++++++++++
 gcc/selftest.h | 19 +++++++++++++++++++
 2 files changed, 53 insertions(+)

Comments

Jeff Law Aug. 23, 2016, 3:25 p.m. UTC | #1
On 08/19/2016 06:37 PM, David Malcolm wrote:
> More enabling work for some new selftests I'm working on.
>
> Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu.
>
> OK for trunk?
>
> gcc/ChangeLog:
> 	* selftest.c (selftest::assert_str_contains): New function.
> 	(selftest::test_assertions): Verify ASSERT_STR_CONTAINS.
> 	* selftest.h (selftest::assert_str_contains): New decl.
> 	(ASSERT_STR_CONTAINS): New macro.
OK.
jeff
diff mbox

Patch

diff --git a/gcc/selftest.c b/gcc/selftest.c
index d25f5c0..629db98 100644
--- a/gcc/selftest.c
+++ b/gcc/selftest.c
@@ -87,6 +87,39 @@  selftest::assert_streq (const location &loc,
 	 desc_expected, desc_actual, val_expected, val_actual);
 }
 
+/* Implementation detail of ASSERT_STR_CONTAINS.
+   Use strstr to determine if val_needle is is within val_haystack.
+   ::selftest::pass if it is found.
+   ::selftest::fail if it is not found.  */
+
+void
+selftest::assert_str_contains (const location &loc,
+			       const char *desc_haystack,
+			       const char *desc_needle,
+			       const char *val_haystack,
+			       const char *val_needle)
+{
+  /* If val_haystack is NULL, fail with a custom error message.  */
+  if (val_haystack == NULL)
+    ::selftest::fail_formatted
+	(loc, "ASSERT_STR_CONTAINS (%s, %s) haystack=NULL",
+	 desc_haystack, desc_needle);
+
+  /* If val_needle is NULL, fail with a custom error message.  */
+  if (val_needle == NULL)
+    ::selftest::fail_formatted
+	(loc, "ASSERT_STR_CONTAINS (%s, %s) haystack=\"%s\" needle=NULL",
+	 desc_haystack, desc_needle, val_haystack);
+
+  const char *test = strstr (val_haystack, val_needle);
+  if (test)
+    ::selftest::pass (loc, "ASSERT_STR_CONTAINS");
+  else
+    ::selftest::fail_formatted
+	(loc, "ASSERT_STR_CONTAINS (%s, %s) haystack=\"%s\" needle=\"%s\"",
+	 desc_haystack, desc_needle, val_haystack, val_needle);
+}
+
 /* Constructor.  Create a tempfile using SUFFIX, and write CONTENT to
    it.  Abort if anything goes wrong, using LOC as the effective
    location in the problem report.  */
@@ -131,6 +164,7 @@  test_assertions ()
   ASSERT_NE (1, 2);
   ASSERT_STREQ ("test", "test");
   ASSERT_STREQ_AT (SELFTEST_LOCATION, "test", "test");
+  ASSERT_STR_CONTAINS ("foo bar baz", "bar");
 }
 
 /* Run all of the selftests within this file.  */
diff --git a/gcc/selftest.h b/gcc/selftest.h
index 58a40f6..b073ed6 100644
--- a/gcc/selftest.h
+++ b/gcc/selftest.h
@@ -69,6 +69,14 @@  extern void assert_streq (const location &loc,
 			  const char *desc_expected, const char *desc_actual,
 			  const char *val_expected, const char *val_actual);
 
+/* Implementation detail of ASSERT_STR_CONTAINS.  */
+
+extern void assert_str_contains (const location &loc,
+				 const char *desc_haystack,
+				 const char *desc_needle,
+				 const char *val_haystack,
+				 const char *val_needle);
+
 /* A class for writing out a temporary sourcefile for use in selftests
    of input handling.  */
 
@@ -249,6 +257,17 @@  extern int num_passes;
 			    (EXPECTED), (ACTUAL));		    \
   SELFTEST_END_STMT
 
+/* Evaluate HAYSTACK and NEEDLE and use strstr to determine if NEEDLE
+   is within HAYSTACK.
+   ::selftest::pass if NEEDLE is found.
+   ::selftest::fail if it is not found.  */
+
+#define ASSERT_STR_CONTAINS(HAYSTACK, NEEDLE)				\
+  SELFTEST_BEGIN_STMT							\
+  ::selftest::assert_str_contains (SELFTEST_LOCATION, #HAYSTACK, #NEEDLE, \
+				   (HAYSTACK), (NEEDLE));		\
+  SELFTEST_END_STMT
+
 /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
    ::selftest::fail if it is false.  */