Revert 2 ::get to ::get_create for IPA summaries (PR ipa/86279).
[official-gcc.git] / gcc / selftest.c
blob27de9a41b794688952004fdd17808ade64896ca9
1 /* A self-testing framework, for use by -fself-test.
2 Copyright (C) 2015-2018 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
9 version.
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
14 for more details.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "selftest.h"
25 #if CHECKING_P
27 namespace selftest {
29 int num_passes;
31 /* Record the successful outcome of some aspect of a test. */
33 void
34 pass (const location &/*loc*/, const char */*msg*/)
36 num_passes++;
39 /* Report the failed outcome of some aspect of a test and abort. */
41 void
42 fail (const location &loc, const char *msg)
44 fprintf (stderr,"%s:%i: %s: FAIL: %s\n", loc.m_file, loc.m_line,
45 loc.m_function, msg);
46 abort ();
49 /* As "fail", but using printf-style formatted output. */
51 void
52 fail_formatted (const location &loc, const char *fmt, ...)
54 va_list ap;
56 fprintf (stderr, "%s:%i: %s: FAIL: ", loc.m_file, loc.m_line,
57 loc.m_function);
58 va_start (ap, fmt);
59 vfprintf (stderr, fmt, ap);
60 va_end (ap);
61 fprintf (stderr, "\n");
62 abort ();
65 /* Implementation detail of ASSERT_STREQ.
66 Compare val1 and val2 with strcmp. They ought
67 to be non-NULL; fail gracefully if either or both are NULL. */
69 void
70 assert_streq (const location &loc,
71 const char *desc_val1, const char *desc_val2,
72 const char *val1, const char *val2)
74 /* If val1 or val2 are NULL, fail with a custom error message. */
75 if (val1 == NULL)
76 if (val2 == NULL)
77 fail_formatted (loc, "ASSERT_STREQ (%s, %s) val1=NULL val2=NULL",
78 desc_val1, desc_val2);
79 else
80 fail_formatted (loc, "ASSERT_STREQ (%s, %s) val1=NULL val2=\"%s\"",
81 desc_val1, desc_val2, val2);
82 else
83 if (val2 == NULL)
84 fail_formatted (loc, "ASSERT_STREQ (%s, %s) val1=\"%s\" val2=NULL",
85 desc_val1, desc_val2, val1);
86 else
88 if (strcmp (val1, val2) == 0)
89 pass (loc, "ASSERT_STREQ");
90 else
91 fail_formatted (loc, "ASSERT_STREQ (%s, %s) val1=\"%s\" val2=\"%s\"",
92 desc_val1, desc_val2, val1, val2);
96 /* Implementation detail of ASSERT_STR_CONTAINS.
97 Use strstr to determine if val_needle is is within val_haystack.
98 ::selftest::pass if it is found.
99 ::selftest::fail if it is not found. */
101 void
102 assert_str_contains (const location &loc,
103 const char *desc_haystack,
104 const char *desc_needle,
105 const char *val_haystack,
106 const char *val_needle)
108 /* If val_haystack is NULL, fail with a custom error message. */
109 if (val_haystack == NULL)
110 fail_formatted (loc, "ASSERT_STR_CONTAINS (%s, %s) haystack=NULL",
111 desc_haystack, desc_needle);
113 /* If val_needle is NULL, fail with a custom error message. */
114 if (val_needle == NULL)
115 fail_formatted (loc,
116 "ASSERT_STR_CONTAINS (%s, %s) haystack=\"%s\" needle=NULL",
117 desc_haystack, desc_needle, val_haystack);
119 const char *test = strstr (val_haystack, val_needle);
120 if (test)
121 pass (loc, "ASSERT_STR_CONTAINS");
122 else
123 fail_formatted
124 (loc, "ASSERT_STR_CONTAINS (%s, %s) haystack=\"%s\" needle=\"%s\"",
125 desc_haystack, desc_needle, val_haystack, val_needle);
128 /* Implementation detail of ASSERT_STR_STARTSWITH.
129 Determine if VAL_STR starts with VAL_PREFIX.
130 ::selftest::pass if VAL_STR does start with VAL_PREFIX.
131 ::selftest::fail if it does not, or either is NULL (using
132 DESC_STR and DESC_PREFIX in the error message). */
134 void
135 assert_str_startswith (const location &loc,
136 const char *desc_str,
137 const char *desc_prefix,
138 const char *val_str,
139 const char *val_prefix)
141 /* If val_str is NULL, fail with a custom error message. */
142 if (val_str == NULL)
143 fail_formatted (loc, "ASSERT_STR_STARTSWITH (%s, %s) str=NULL",
144 desc_str, desc_prefix);
146 /* If val_prefix is NULL, fail with a custom error message. */
147 if (val_prefix == NULL)
148 fail_formatted (loc,
149 "ASSERT_STR_STARTSWITH (%s, %s) str=\"%s\" prefix=NULL",
150 desc_str, desc_prefix, val_str);
152 const char *test = strstr (val_str, val_prefix);
153 if (test == val_str)
154 pass (loc, "ASSERT_STR_STARTSWITH");
155 else
156 fail_formatted
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)
166 m_filename = make_temp_file (suffix);
167 ASSERT_NE (m_filename, NULL);
170 /* Destructor. Delete the tempfile. */
172 named_temp_file::~named_temp_file ()
174 unlink (m_filename);
175 diagnostics_file_cache_forcibly_evict_file (m_filename);
176 free (m_filename);
179 /* Constructor. Create a tempfile using SUFFIX, and write CONTENT to
180 it. Abort if anything goes wrong, using LOC as the effective
181 location in the problem report. */
183 temp_source_file::temp_source_file (const location &loc,
184 const char *suffix,
185 const char *content)
186 : named_temp_file (suffix)
188 FILE *out = fopen (get_filename (), "w");
189 if (!out)
190 fail_formatted (loc, "unable to open tempfile: %s", get_filename ());
191 fprintf (out, "%s", content);
192 fclose (out);
195 /* Read the contents of PATH into memory, returning a 0-terminated buffer
196 that must be freed by the caller.
197 Fail (and abort) if there are any problems, with LOC as the reported
198 location of the failure. */
200 char *
201 read_file (const location &loc, const char *path)
203 FILE *f_in = fopen (path, "r");
204 if (!f_in)
205 fail_formatted (loc, "unable to open file: %s", path);
207 /* Read content, allocating FIXME. */
208 char *result = NULL;
209 size_t total_sz = 0;
210 size_t alloc_sz = 0;
211 char buf[4096];
212 size_t iter_sz_in;
214 while ( (iter_sz_in = fread (buf, 1, sizeof (buf), f_in)) )
216 gcc_assert (alloc_sz >= total_sz);
217 size_t old_total_sz = total_sz;
218 total_sz += iter_sz_in;
219 /* Allow 1 extra byte for 0-termination. */
220 if (alloc_sz < (total_sz + 1))
222 size_t new_alloc_sz = alloc_sz ? alloc_sz * 2: total_sz + 1;
223 result = (char *)xrealloc (result, new_alloc_sz);
224 alloc_sz = new_alloc_sz;
226 memcpy (result + old_total_sz, buf, iter_sz_in);
229 if (!feof (f_in))
230 fail_formatted (loc, "error reading from %s: %s", path,
231 xstrerror (errno));
233 fclose (f_in);
235 /* 0-terminate the buffer. */
236 gcc_assert (total_sz < alloc_sz);
237 result[total_sz] = '\0';
239 return result;
242 /* The path of SRCDIR/testsuite/selftests. */
244 const char *path_to_selftest_files = NULL;
246 /* Convert a path relative to SRCDIR/testsuite/selftests
247 to a real path (either absolute, or relative to pwd).
248 The result should be freed by the caller. */
250 char *
251 locate_file (const char *name)
253 ASSERT_NE (NULL, path_to_selftest_files);
254 return concat (path_to_selftest_files, "/", name, NULL);
257 /* selftest::test_runner's ctor. */
259 test_runner::test_runner (const char *name)
260 : m_name (name),
261 m_start_time (get_run_time ())
265 /* selftest::test_runner's dtor. Print a summary line to stderr. */
267 test_runner::~test_runner ()
269 /* Finished running tests. */
270 long finish_time = get_run_time ();
271 long elapsed_time = finish_time - m_start_time;
273 fprintf (stderr,
274 "%s: %i pass(es) in %ld.%06ld seconds\n",
275 m_name, num_passes,
276 elapsed_time / 1000000, elapsed_time % 1000000);
279 /* Selftests for libiberty. */
281 /* Verify that xstrndup generates EXPECTED when called on SRC and N. */
283 static void
284 assert_xstrndup_eq (const char *expected, const char *src, size_t n)
286 char *buf = xstrndup (src, n);
287 ASSERT_STREQ (expected, buf);
288 free (buf);
291 /* Verify that xstrndup works as expected. */
293 static void
294 test_xstrndup ()
296 assert_xstrndup_eq ("", "test", 0);
297 assert_xstrndup_eq ("t", "test", 1);
298 assert_xstrndup_eq ("te", "test", 2);
299 assert_xstrndup_eq ("tes", "test", 3);
300 assert_xstrndup_eq ("test", "test", 4);
301 assert_xstrndup_eq ("test", "test", 5);
303 /* Test on an string without zero termination. */
304 const char src[4] = {'t', 'e', 's', 't'};
305 assert_xstrndup_eq ("", src, 0);
306 assert_xstrndup_eq ("t", src, 1);
307 assert_xstrndup_eq ("te", src, 2);
308 assert_xstrndup_eq ("tes", src, 3);
309 assert_xstrndup_eq ("test", src, 4);
312 /* Run selftests for libiberty. */
314 static void
315 test_libiberty ()
317 test_xstrndup ();
320 /* Selftests for the selftest system itself. */
322 /* Sanity-check the ASSERT_ macros with various passing cases. */
324 static void
325 test_assertions ()
327 ASSERT_TRUE (true);
328 ASSERT_FALSE (false);
329 ASSERT_EQ (1, 1);
330 ASSERT_EQ_AT (SELFTEST_LOCATION, 1, 1);
331 ASSERT_NE (1, 2);
332 ASSERT_GT (2, 1);
333 ASSERT_GT_AT (SELFTEST_LOCATION, 2, 1);
334 ASSERT_LT (1, 2);
335 ASSERT_LT_AT (SELFTEST_LOCATION, 1, 2);
336 ASSERT_STREQ ("test", "test");
337 ASSERT_STREQ_AT (SELFTEST_LOCATION, "test", "test");
338 ASSERT_STR_CONTAINS ("foo bar baz", "bar");
341 /* Verify named_temp_file. */
343 static void
344 test_named_temp_file ()
346 named_temp_file t (".txt");
347 FILE *f = fopen (t.get_filename (), "w");
348 if (!f)
349 fail_formatted (SELFTEST_LOCATION,
350 "unable to open %s for writing", t.get_filename ());
351 fclose (f);
354 /* Verify read_file (and also temp_source_file). */
356 static void
357 test_read_file ()
359 temp_source_file t (SELFTEST_LOCATION, "test1.s",
360 "\tjmp\t.L2\n");
361 char *buf = read_file (SELFTEST_LOCATION, t.get_filename ());
362 ASSERT_STREQ ("\tjmp\t.L2\n", buf);
363 free (buf);
366 /* Verify locate_file (and read_file). */
368 static void
369 test_locate_file ()
371 char *path = locate_file ("example.txt");
372 char *buf = read_file (SELFTEST_LOCATION, path);
373 ASSERT_STREQ ("example of a selftest file\n", buf);
374 free (buf);
375 free (path);
378 /* Run all of the selftests within this file. */
380 void
381 selftest_c_tests ()
383 test_libiberty ();
384 test_assertions ();
385 test_named_temp_file ();
386 test_read_file ();
387 test_locate_file ();
390 } // namespace selftest
392 #endif /* #if CHECKING_P */