aboutsummaryrefslogtreecommitdiff
path: root/gcc/selftest.c
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2016-08-23 16:51:57 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2016-08-23 16:51:57 +0000
commit9f5897866862e760d8276fc1ef7979852f83a86a (patch)
treef17d1999677ddbab47de54aad4f2b81fe533d38e /gcc/selftest.c
parenta181ec03012c1884f45482ba2d5b8cd00ef96a71 (diff)
downloadgcc-9f5897866862e760d8276fc1ef7979852f83a86a.zip
gcc-9f5897866862e760d8276fc1ef7979852f83a86a.tar.gz
gcc-9f5897866862e760d8276fc1ef7979852f83a86a.tar.bz2
selftest.h: add ASSERT_STR_CONTAINS
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. From-SVN: r239707
Diffstat (limited to 'gcc/selftest.c')
-rw-r--r--gcc/selftest.c34
1 files changed, 34 insertions, 0 deletions
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. */