Commit 9f589786 by David Malcolm Committed by David Malcolm

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
parent a181ec03
2016-08-23 David Malcolm <dmalcolm@redhat.com>
* 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 <rguenther@suse.de> 2016-08-23 Richard Biener <rguenther@suse.de>
PR tree-optimization/77286 PR tree-optimization/77286
......
...@@ -87,6 +87,39 @@ selftest::assert_streq (const location &loc, ...@@ -87,6 +87,39 @@ selftest::assert_streq (const location &loc,
desc_expected, desc_actual, val_expected, val_actual); 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 /* Constructor. Create a tempfile using SUFFIX, and write CONTENT to
it. Abort if anything goes wrong, using LOC as the effective it. Abort if anything goes wrong, using LOC as the effective
location in the problem report. */ location in the problem report. */
...@@ -131,6 +164,7 @@ test_assertions () ...@@ -131,6 +164,7 @@ test_assertions ()
ASSERT_NE (1, 2); ASSERT_NE (1, 2);
ASSERT_STREQ ("test", "test"); ASSERT_STREQ ("test", "test");
ASSERT_STREQ_AT (SELFTEST_LOCATION, "test", "test"); ASSERT_STREQ_AT (SELFTEST_LOCATION, "test", "test");
ASSERT_STR_CONTAINS ("foo bar baz", "bar");
} }
/* Run all of the selftests within this file. */ /* Run all of the selftests within this file. */
......
...@@ -69,6 +69,14 @@ extern void assert_streq (const location &loc, ...@@ -69,6 +69,14 @@ extern void assert_streq (const location &loc,
const char *desc_expected, const char *desc_actual, const char *desc_expected, const char *desc_actual,
const char *val_expected, const char *val_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 /* A class for writing out a temporary sourcefile for use in selftests
of input handling. */ of input handling. */
...@@ -249,6 +257,17 @@ extern int num_passes; ...@@ -249,6 +257,17 @@ extern int num_passes;
(EXPECTED), (ACTUAL)); \ (EXPECTED), (ACTUAL)); \
SELFTEST_END_STMT 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, /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
::selftest::fail if it is false. */ ::selftest::fail if it is false. */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment