1 /* A self-testing framework, for use by -fself-test.
2 Copyright (C) 2015-2024 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"
32 /* Record the successful outcome of some aspect of a test. */
35 pass (const location
&/*loc*/, const char */
*msg*/
)
40 /* Report the failed outcome of some aspect of a test and abort. */
43 fail (const location
&loc
, const char *msg
)
45 fprintf (stderr
,"%s:%i: %s: FAIL: %s\n", loc
.m_file
, loc
.m_line
,
50 /* As "fail", but using printf-style formatted output. */
53 fail_formatted (const location
&loc
, const char *fmt
, ...)
57 fprintf (stderr
, "%s:%i: %s: FAIL: ", loc
.m_file
, loc
.m_line
,
60 vfprintf (stderr
, fmt
, ap
);
62 fprintf (stderr
, "\n");
66 /* Implementation detail of ASSERT_STREQ.
67 Compare val1 and val2 with strcmp. They ought
68 to be non-NULL; fail gracefully if either or both are NULL. */
71 assert_streq (const location
&loc
,
72 const char *desc_val1
, const char *desc_val2
,
73 const char *val1
, const char *val2
)
75 /* If val1 or val2 are NULL, fail with a custom error message. */
78 fail_formatted (loc
, "ASSERT_STREQ (%s, %s) val1=NULL val2=NULL",
79 desc_val1
, desc_val2
);
81 fail_formatted (loc
, "ASSERT_STREQ (%s, %s) val1=NULL val2=\"%s\"",
82 desc_val1
, desc_val2
, val2
);
85 fail_formatted (loc
, "ASSERT_STREQ (%s, %s) val1=\"%s\" val2=NULL",
86 desc_val1
, desc_val2
, val1
);
89 if (strcmp (val1
, val2
) == 0)
90 pass (loc
, "ASSERT_STREQ");
92 fail_formatted (loc
, "ASSERT_STREQ (%s, %s)\n val1=\"%s\"\n val2=\"%s\"\n",
93 desc_val1
, desc_val2
, val1
, val2
);
97 /* Implementation detail of ASSERT_STR_CONTAINS.
98 Use strstr to determine if val_needle is within val_haystack.
99 ::selftest::pass if it is found.
100 ::selftest::fail if it is not found. */
103 assert_str_contains (const location
&loc
,
104 const char *desc_haystack
,
105 const char *desc_needle
,
106 const char *val_haystack
,
107 const char *val_needle
)
109 /* If val_haystack is NULL, fail with a custom error message. */
110 if (val_haystack
== NULL
)
111 fail_formatted (loc
, "ASSERT_STR_CONTAINS (%s, %s) haystack=NULL",
112 desc_haystack
, desc_needle
);
114 /* If val_needle is NULL, fail with a custom error message. */
115 if (val_needle
== NULL
)
117 "ASSERT_STR_CONTAINS (%s, %s) haystack=\"%s\" needle=NULL",
118 desc_haystack
, desc_needle
, val_haystack
);
120 const char *test
= strstr (val_haystack
, val_needle
);
122 pass (loc
, "ASSERT_STR_CONTAINS");
125 (loc
, "ASSERT_STR_CONTAINS (%s, %s) haystack=\"%s\" needle=\"%s\"",
126 desc_haystack
, desc_needle
, val_haystack
, val_needle
);
129 /* Implementation detail of ASSERT_STR_STARTSWITH.
130 Determine if VAL_STR starts with VAL_PREFIX.
131 ::selftest::pass if VAL_STR does start with VAL_PREFIX.
132 ::selftest::fail if it does not, or either is NULL (using
133 DESC_STR and DESC_PREFIX in the error message). */
136 assert_str_startswith (const location
&loc
,
137 const char *desc_str
,
138 const char *desc_prefix
,
140 const char *val_prefix
)
142 /* If val_str is NULL, fail with a custom error message. */
144 fail_formatted (loc
, "ASSERT_STR_STARTSWITH (%s, %s) str=NULL",
145 desc_str
, desc_prefix
);
147 /* If val_prefix is NULL, fail with a custom error message. */
148 if (val_prefix
== NULL
)
150 "ASSERT_STR_STARTSWITH (%s, %s) str=\"%s\" prefix=NULL",
151 desc_str
, desc_prefix
, val_str
);
153 if (startswith (val_str
, val_prefix
))
154 pass (loc
, "ASSERT_STR_STARTSWITH");
157 (loc
, "ASSERT_STR_STARTSWITH (%s, %s) str=\"%s\" prefix=\"%s\"",
158 desc_str
, desc_prefix
, val_str
, val_prefix
);
162 /* Constructor. Generate a name for the file. */
164 named_temp_file::named_temp_file (const char *suffix
,
167 m_filename
= make_temp_file (suffix
);
168 ASSERT_NE (m_filename
, NULL
);
172 /* Destructor. Delete the tempfile. */
174 named_temp_file::~named_temp_file ()
178 m_file_cache
->forcibly_evict_file (m_filename
);
182 /* Constructor. Create a tempfile using SUFFIX, and write CONTENT to
183 it. Abort if anything goes wrong, using LOC as the effective
184 location in the problem report. */
186 temp_source_file::temp_source_file (const location
&loc
,
190 : named_temp_file (suffix
, fc
)
192 FILE *out
= fopen (get_filename (), "w");
194 fail_formatted (loc
, "unable to open tempfile: %s", get_filename ());
195 fprintf (out
, "%s", content
);
199 /* As above, but with a size, to allow for NUL bytes in CONTENT. */
201 temp_source_file::temp_source_file (const location
&loc
,
205 : named_temp_file (suffix
)
207 FILE *out
= fopen (get_filename (), "w");
209 fail_formatted (loc
, "unable to open tempfile: %s", get_filename ());
210 fwrite (content
, sz
, 1, out
);
214 /* Avoid introducing locale-specific differences in the results
215 by hardcoding open_quote and close_quote. */
217 auto_fix_quotes::auto_fix_quotes ()
219 m_saved_open_quote
= open_quote
;
220 m_saved_close_quote
= close_quote
;
225 /* Restore old values of open_quote and close_quote. */
227 auto_fix_quotes::~auto_fix_quotes ()
229 open_quote
= m_saved_open_quote
;
230 close_quote
= m_saved_close_quote
;
233 /* Read the contents of PATH into memory, returning a 0-terminated buffer
234 that must be freed by the caller.
235 Fail (and abort) if there are any problems, with LOC as the reported
236 location of the failure. */
239 read_file (const location
&loc
, const char *path
)
241 FILE *f_in
= fopen (path
, "r");
243 fail_formatted (loc
, "unable to open file: %s", path
);
245 /* Read content, allocating FIXME. */
252 while ( (iter_sz_in
= fread (buf
, 1, sizeof (buf
), f_in
)) )
254 gcc_assert (alloc_sz
>= total_sz
);
255 size_t old_total_sz
= total_sz
;
256 total_sz
+= iter_sz_in
;
257 /* Allow 1 extra byte for 0-termination. */
258 if (alloc_sz
< (total_sz
+ 1))
260 size_t new_alloc_sz
= alloc_sz
? alloc_sz
* 2: total_sz
+ 1;
261 result
= (char *)xrealloc (result
, new_alloc_sz
);
262 alloc_sz
= new_alloc_sz
;
264 memcpy (result
+ old_total_sz
, buf
, iter_sz_in
);
268 fail_formatted (loc
, "error reading from %s: %s", path
,
273 /* 0-terminate the buffer. */
274 gcc_assert (total_sz
< alloc_sz
);
275 result
[total_sz
] = '\0';
280 /* The path of SRCDIR/testsuite/selftests. */
282 const char *path_to_selftest_files
= NULL
;
284 /* Convert a path relative to SRCDIR/testsuite/selftests
285 to a real path (either absolute, or relative to pwd).
286 The result should be freed by the caller. */
289 locate_file (const char *name
)
291 ASSERT_NE (NULL
, path_to_selftest_files
);
292 return concat (path_to_selftest_files
, "/", name
, NULL
);
295 /* selftest::test_runner's ctor. */
297 test_runner::test_runner (const char *name
)
299 m_start_time (get_run_time ())
303 /* selftest::test_runner's dtor. Print a summary line to stderr. */
305 test_runner::~test_runner ()
307 /* Finished running tests. */
308 long finish_time
= get_run_time ();
309 long elapsed_time
= finish_time
- m_start_time
;
312 "%s: %i pass(es) in %ld.%06ld seconds\n",
314 elapsed_time
/ 1000000, elapsed_time
% 1000000);
317 /* Selftests for libiberty. */
319 /* Verify that xstrndup generates EXPECTED when called on SRC and N. */
322 assert_xstrndup_eq (const char *expected
, const char *src
, size_t n
)
324 char *buf
= xstrndup (src
, n
);
325 ASSERT_STREQ (expected
, buf
);
329 /* Verify that xstrndup works as expected. */
334 assert_xstrndup_eq ("", "test", 0);
335 assert_xstrndup_eq ("t", "test", 1);
336 assert_xstrndup_eq ("te", "test", 2);
337 assert_xstrndup_eq ("tes", "test", 3);
338 assert_xstrndup_eq ("test", "test", 4);
339 assert_xstrndup_eq ("test", "test", 5);
341 /* Test on an string without zero termination. */
342 const char src
[4] = {'t', 'e', 's', 't'};
343 assert_xstrndup_eq ("", src
, 0);
344 assert_xstrndup_eq ("t", src
, 1);
345 assert_xstrndup_eq ("te", src
, 2);
346 assert_xstrndup_eq ("tes", src
, 3);
347 assert_xstrndup_eq ("test", src
, 4);
350 /* Run selftests for libiberty. */
358 /* Selftests for the selftest system itself. */
360 /* Sanity-check the ASSERT_ macros with various passing cases. */
366 ASSERT_FALSE (false);
368 ASSERT_EQ_AT (SELFTEST_LOCATION
, 1, 1);
371 ASSERT_GT_AT (SELFTEST_LOCATION
, 2, 1);
373 ASSERT_LT_AT (SELFTEST_LOCATION
, 1, 2);
374 ASSERT_STREQ ("test", "test");
375 ASSERT_STREQ_AT (SELFTEST_LOCATION
, "test", "test");
376 ASSERT_STR_CONTAINS ("foo bar baz", "bar");
379 /* Verify named_temp_file. */
382 test_named_temp_file ()
384 named_temp_file
t (".txt");
385 FILE *f
= fopen (t
.get_filename (), "w");
387 fail_formatted (SELFTEST_LOCATION
,
388 "unable to open %s for writing", t
.get_filename ());
392 /* Verify read_file (and also temp_source_file). */
397 temp_source_file
t (SELFTEST_LOCATION
, "test1.s",
399 char *buf
= read_file (SELFTEST_LOCATION
, t
.get_filename ());
400 ASSERT_STREQ ("\tjmp\t.L2\n", buf
);
404 /* Verify locate_file (and read_file). */
409 char *path
= locate_file ("example.txt");
410 char *buf
= read_file (SELFTEST_LOCATION
, path
);
411 ASSERT_STREQ ("example of a selftest file\n", buf
);
416 /* Run all of the selftests within this file. */
423 test_named_temp_file ();
428 } // namespace selftest
430 #endif /* #if CHECKING_P */