From 9f5897866862e760d8276fc1ef7979852f83a86a Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Tue, 23 Aug 2016 16:51:57 +0000 Subject: [PATCH] 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 --- gcc/ChangeLog | 7 +++++++ gcc/selftest.c | 34 ++++++++++++++++++++++++++++++++++ gcc/selftest.h | 19 +++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 5cf4158a628..d5016e90c7b 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2016-08-23 David Malcolm + + * 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. + 2016-08-23 Richard Biener PR tree-optimization/77286 diff --git a/gcc/selftest.c b/gcc/selftest.c index d25f5c08db7..629db98b9f6 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 58a40f67cb1..b073ed6e03d 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. */ -- 2.30.2