1 /* A self-testing framework, for use by -fself-test.
2 Copyright (C) 2015-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
31 /* Record the successful outcome of some aspect of a test. */
34 pass (const location
&/*loc*/, const char */
*msg*/
)
39 /* Report the failed outcome of some aspect of a test and abort. */
42 fail (const location
&loc
, const char *msg
)
44 fprintf (stderr
,"%s:%i: %s: FAIL: %s\n", loc
.m_file
, loc
.m_line
,
49 /* As "fail", but using printf-style formatted output. */
52 fail_formatted (const location
&loc
, const char *fmt
, ...)
56 fprintf (stderr
, "%s:%i: %s: FAIL: ", loc
.m_file
, loc
.m_line
,
59 vfprintf (stderr
, fmt
, ap
);
61 fprintf (stderr
, "\n");
65 /* Implementation detail of ASSERT_STREQ.
66 Compare val_expected and val_actual with strcmp. They ought
67 to be non-NULL; fail gracefully if either are NULL. */
70 assert_streq (const location
&loc
,
71 const char *desc_expected
, const char *desc_actual
,
72 const char *val_expected
, const char *val_actual
)
74 /* If val_expected is NULL, the test is buggy. Fail gracefully. */
75 if (val_expected
== NULL
)
76 fail_formatted (loc
, "ASSERT_STREQ (%s, %s) expected=NULL",
77 desc_expected
, desc_actual
);
78 /* If val_actual is NULL, fail with a custom error message. */
79 if (val_actual
== NULL
)
80 fail_formatted (loc
, "ASSERT_STREQ (%s, %s) expected=\"%s\" actual=NULL",
81 desc_expected
, desc_actual
, val_expected
);
82 if (0 == strcmp (val_expected
, val_actual
))
83 pass (loc
, "ASSERT_STREQ");
85 fail_formatted (loc
, "ASSERT_STREQ (%s, %s) expected=\"%s\" actual=\"%s\"",
86 desc_expected
, desc_actual
, val_expected
, val_actual
);
89 /* Implementation detail of ASSERT_STR_CONTAINS.
90 Use strstr to determine if val_needle is is within val_haystack.
91 ::selftest::pass if it is found.
92 ::selftest::fail if it is not found. */
95 assert_str_contains (const location
&loc
,
96 const char *desc_haystack
,
97 const char *desc_needle
,
98 const char *val_haystack
,
99 const char *val_needle
)
101 /* If val_haystack is NULL, fail with a custom error message. */
102 if (val_haystack
== NULL
)
103 fail_formatted (loc
, "ASSERT_STR_CONTAINS (%s, %s) haystack=NULL",
104 desc_haystack
, desc_needle
);
106 /* If val_needle is NULL, fail with a custom error message. */
107 if (val_needle
== NULL
)
109 "ASSERT_STR_CONTAINS (%s, %s) haystack=\"%s\" needle=NULL",
110 desc_haystack
, desc_needle
, val_haystack
);
112 const char *test
= strstr (val_haystack
, val_needle
);
114 pass (loc
, "ASSERT_STR_CONTAINS");
117 (loc
, "ASSERT_STR_CONTAINS (%s, %s) haystack=\"%s\" needle=\"%s\"",
118 desc_haystack
, desc_needle
, val_haystack
, val_needle
);
121 /* Constructor. Generate a name for the file. */
123 named_temp_file::named_temp_file (const char *suffix
)
125 m_filename
= make_temp_file (suffix
);
126 ASSERT_NE (m_filename
, NULL
);
129 /* Destructor. Delete the tempfile. */
131 named_temp_file::~named_temp_file ()
134 diagnostics_file_cache_forcibly_evict_file (m_filename
);
138 /* Constructor. Create a tempfile using SUFFIX, and write CONTENT to
139 it. Abort if anything goes wrong, using LOC as the effective
140 location in the problem report. */
142 temp_source_file::temp_source_file (const location
&loc
,
145 : named_temp_file (suffix
)
147 FILE *out
= fopen (get_filename (), "w");
149 fail_formatted (loc
, "unable to open tempfile: %s", get_filename ());
150 fprintf (out
, "%s", content
);
154 /* Selftests for the selftest system itself. */
156 /* Sanity-check the ASSERT_ macros with various passing cases. */
162 ASSERT_FALSE (false);
164 ASSERT_EQ_AT (SELFTEST_LOCATION
, 1, 1);
166 ASSERT_STREQ ("test", "test");
167 ASSERT_STREQ_AT (SELFTEST_LOCATION
, "test", "test");
168 ASSERT_STR_CONTAINS ("foo bar baz", "bar");
171 /* Verify named_temp_file. */
174 test_named_temp_file ()
176 named_temp_file
t (".txt");
177 FILE *f
= fopen (t
.get_filename (), "w");
179 fail_formatted (SELFTEST_LOCATION
,
180 "unable to open %s for writing", t
.get_filename ());
184 /* Run all of the selftests within this file. */
190 test_named_temp_file ();
193 } // namespace selftest
195 #endif /* #if CHECKING_P */