annotate gcc/selftest.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* A self-testing framework, for use by -fself-test.
kono
parents:
diff changeset
2 Copyright (C) 2015-2017 Free Software Foundation, Inc.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #include "config.h"
kono
parents:
diff changeset
21 #include "system.h"
kono
parents:
diff changeset
22 #include "coretypes.h"
kono
parents:
diff changeset
23 #include "selftest.h"
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 #if CHECKING_P
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 namespace selftest {
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 int num_passes;
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 /* Record the successful outcome of some aspect of a test. */
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 void
kono
parents:
diff changeset
34 pass (const location &/*loc*/, const char */*msg*/)
kono
parents:
diff changeset
35 {
kono
parents:
diff changeset
36 num_passes++;
kono
parents:
diff changeset
37 }
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 /* Report the failed outcome of some aspect of a test and abort. */
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 void
kono
parents:
diff changeset
42 fail (const location &loc, const char *msg)
kono
parents:
diff changeset
43 {
kono
parents:
diff changeset
44 fprintf (stderr,"%s:%i: %s: FAIL: %s\n", loc.m_file, loc.m_line,
kono
parents:
diff changeset
45 loc.m_function, msg);
kono
parents:
diff changeset
46 abort ();
kono
parents:
diff changeset
47 }
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 /* As "fail", but using printf-style formatted output. */
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 void
kono
parents:
diff changeset
52 fail_formatted (const location &loc, const char *fmt, ...)
kono
parents:
diff changeset
53 {
kono
parents:
diff changeset
54 va_list ap;
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 fprintf (stderr, "%s:%i: %s: FAIL: ", loc.m_file, loc.m_line,
kono
parents:
diff changeset
57 loc.m_function);
kono
parents:
diff changeset
58 va_start (ap, fmt);
kono
parents:
diff changeset
59 vfprintf (stderr, fmt, ap);
kono
parents:
diff changeset
60 va_end (ap);
kono
parents:
diff changeset
61 fprintf (stderr, "\n");
kono
parents:
diff changeset
62 abort ();
kono
parents:
diff changeset
63 }
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 /* Implementation detail of ASSERT_STREQ.
kono
parents:
diff changeset
66 Compare val_expected and val_actual with strcmp. They ought
kono
parents:
diff changeset
67 to be non-NULL; fail gracefully if either are NULL. */
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 void
kono
parents:
diff changeset
70 assert_streq (const location &loc,
kono
parents:
diff changeset
71 const char *desc_expected, const char *desc_actual,
kono
parents:
diff changeset
72 const char *val_expected, const char *val_actual)
kono
parents:
diff changeset
73 {
kono
parents:
diff changeset
74 /* If val_expected is NULL, the test is buggy. Fail gracefully. */
kono
parents:
diff changeset
75 if (val_expected == NULL)
kono
parents:
diff changeset
76 fail_formatted (loc, "ASSERT_STREQ (%s, %s) expected=NULL",
kono
parents:
diff changeset
77 desc_expected, desc_actual);
kono
parents:
diff changeset
78 /* If val_actual is NULL, fail with a custom error message. */
kono
parents:
diff changeset
79 if (val_actual == NULL)
kono
parents:
diff changeset
80 fail_formatted (loc, "ASSERT_STREQ (%s, %s) expected=\"%s\" actual=NULL",
kono
parents:
diff changeset
81 desc_expected, desc_actual, val_expected);
kono
parents:
diff changeset
82 if (0 == strcmp (val_expected, val_actual))
kono
parents:
diff changeset
83 pass (loc, "ASSERT_STREQ");
kono
parents:
diff changeset
84 else
kono
parents:
diff changeset
85 fail_formatted (loc, "ASSERT_STREQ (%s, %s) expected=\"%s\" actual=\"%s\"",
kono
parents:
diff changeset
86 desc_expected, desc_actual, val_expected, val_actual);
kono
parents:
diff changeset
87 }
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 /* Implementation detail of ASSERT_STR_CONTAINS.
kono
parents:
diff changeset
90 Use strstr to determine if val_needle is is within val_haystack.
kono
parents:
diff changeset
91 ::selftest::pass if it is found.
kono
parents:
diff changeset
92 ::selftest::fail if it is not found. */
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 void
kono
parents:
diff changeset
95 assert_str_contains (const location &loc,
kono
parents:
diff changeset
96 const char *desc_haystack,
kono
parents:
diff changeset
97 const char *desc_needle,
kono
parents:
diff changeset
98 const char *val_haystack,
kono
parents:
diff changeset
99 const char *val_needle)
kono
parents:
diff changeset
100 {
kono
parents:
diff changeset
101 /* If val_haystack is NULL, fail with a custom error message. */
kono
parents:
diff changeset
102 if (val_haystack == NULL)
kono
parents:
diff changeset
103 fail_formatted (loc, "ASSERT_STR_CONTAINS (%s, %s) haystack=NULL",
kono
parents:
diff changeset
104 desc_haystack, desc_needle);
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 /* If val_needle is NULL, fail with a custom error message. */
kono
parents:
diff changeset
107 if (val_needle == NULL)
kono
parents:
diff changeset
108 fail_formatted (loc,
kono
parents:
diff changeset
109 "ASSERT_STR_CONTAINS (%s, %s) haystack=\"%s\" needle=NULL",
kono
parents:
diff changeset
110 desc_haystack, desc_needle, val_haystack);
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 const char *test = strstr (val_haystack, val_needle);
kono
parents:
diff changeset
113 if (test)
kono
parents:
diff changeset
114 pass (loc, "ASSERT_STR_CONTAINS");
kono
parents:
diff changeset
115 else
kono
parents:
diff changeset
116 fail_formatted
kono
parents:
diff changeset
117 (loc, "ASSERT_STR_CONTAINS (%s, %s) haystack=\"%s\" needle=\"%s\"",
kono
parents:
diff changeset
118 desc_haystack, desc_needle, val_haystack, val_needle);
kono
parents:
diff changeset
119 }
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 /* Constructor. Generate a name for the file. */
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 named_temp_file::named_temp_file (const char *suffix)
kono
parents:
diff changeset
124 {
kono
parents:
diff changeset
125 m_filename = make_temp_file (suffix);
kono
parents:
diff changeset
126 ASSERT_NE (m_filename, NULL);
kono
parents:
diff changeset
127 }
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 /* Destructor. Delete the tempfile. */
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 named_temp_file::~named_temp_file ()
kono
parents:
diff changeset
132 {
kono
parents:
diff changeset
133 unlink (m_filename);
kono
parents:
diff changeset
134 diagnostics_file_cache_forcibly_evict_file (m_filename);
kono
parents:
diff changeset
135 free (m_filename);
kono
parents:
diff changeset
136 }
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 /* Constructor. Create a tempfile using SUFFIX, and write CONTENT to
kono
parents:
diff changeset
139 it. Abort if anything goes wrong, using LOC as the effective
kono
parents:
diff changeset
140 location in the problem report. */
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 temp_source_file::temp_source_file (const location &loc,
kono
parents:
diff changeset
143 const char *suffix,
kono
parents:
diff changeset
144 const char *content)
kono
parents:
diff changeset
145 : named_temp_file (suffix)
kono
parents:
diff changeset
146 {
kono
parents:
diff changeset
147 FILE *out = fopen (get_filename (), "w");
kono
parents:
diff changeset
148 if (!out)
kono
parents:
diff changeset
149 fail_formatted (loc, "unable to open tempfile: %s", get_filename ());
kono
parents:
diff changeset
150 fprintf (out, "%s", content);
kono
parents:
diff changeset
151 fclose (out);
kono
parents:
diff changeset
152 }
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 /* Read the contents of PATH into memory, returning a 0-terminated buffer
kono
parents:
diff changeset
155 that must be freed by the caller.
kono
parents:
diff changeset
156 Fail (and abort) if there are any problems, with LOC as the reported
kono
parents:
diff changeset
157 location of the failure. */
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 char *
kono
parents:
diff changeset
160 read_file (const location &loc, const char *path)
kono
parents:
diff changeset
161 {
kono
parents:
diff changeset
162 FILE *f_in = fopen (path, "r");
kono
parents:
diff changeset
163 if (!f_in)
kono
parents:
diff changeset
164 fail_formatted (loc, "unable to open file: %s", path);
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 /* Read content, allocating FIXME. */
kono
parents:
diff changeset
167 char *result = NULL;
kono
parents:
diff changeset
168 size_t total_sz = 0;
kono
parents:
diff changeset
169 size_t alloc_sz = 0;
kono
parents:
diff changeset
170 char buf[4096];
kono
parents:
diff changeset
171 size_t iter_sz_in;
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 while ( (iter_sz_in = fread (buf, 1, sizeof (buf), f_in)) )
kono
parents:
diff changeset
174 {
kono
parents:
diff changeset
175 gcc_assert (alloc_sz >= total_sz);
kono
parents:
diff changeset
176 size_t old_total_sz = total_sz;
kono
parents:
diff changeset
177 total_sz += iter_sz_in;
kono
parents:
diff changeset
178 /* Allow 1 extra byte for 0-termination. */
kono
parents:
diff changeset
179 if (alloc_sz < (total_sz + 1))
kono
parents:
diff changeset
180 {
kono
parents:
diff changeset
181 size_t new_alloc_sz = alloc_sz ? alloc_sz * 2: total_sz + 1;
kono
parents:
diff changeset
182 result = (char *)xrealloc (result, new_alloc_sz);
kono
parents:
diff changeset
183 alloc_sz = new_alloc_sz;
kono
parents:
diff changeset
184 }
kono
parents:
diff changeset
185 memcpy (result + old_total_sz, buf, iter_sz_in);
kono
parents:
diff changeset
186 }
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 if (!feof (f_in))
kono
parents:
diff changeset
189 fail_formatted (loc, "error reading from %s: %s", path,
kono
parents:
diff changeset
190 xstrerror (errno));
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 fclose (f_in);
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 /* 0-terminate the buffer. */
kono
parents:
diff changeset
195 gcc_assert (total_sz < alloc_sz);
kono
parents:
diff changeset
196 result[total_sz] = '\0';
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 return result;
kono
parents:
diff changeset
199 }
kono
parents:
diff changeset
200
kono
parents:
diff changeset
201 /* The path of SRCDIR/testsuite/selftests. */
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 const char *path_to_selftest_files = NULL;
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 /* Convert a path relative to SRCDIR/testsuite/selftests
kono
parents:
diff changeset
206 to a real path (either absolute, or relative to pwd).
kono
parents:
diff changeset
207 The result should be freed by the caller. */
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 char *
kono
parents:
diff changeset
210 locate_file (const char *name)
kono
parents:
diff changeset
211 {
kono
parents:
diff changeset
212 ASSERT_NE (NULL, path_to_selftest_files);
kono
parents:
diff changeset
213 return concat (path_to_selftest_files, "/", name, NULL);
kono
parents:
diff changeset
214 }
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 /* Selftests for libiberty. */
kono
parents:
diff changeset
217
kono
parents:
diff changeset
218 /* Verify that xstrndup generates EXPECTED when called on SRC and N. */
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 static void
kono
parents:
diff changeset
221 assert_xstrndup_eq (const char *expected, const char *src, size_t n)
kono
parents:
diff changeset
222 {
kono
parents:
diff changeset
223 char *buf = xstrndup (src, n);
kono
parents:
diff changeset
224 ASSERT_STREQ (expected, buf);
kono
parents:
diff changeset
225 free (buf);
kono
parents:
diff changeset
226 }
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 /* Verify that xstrndup works as expected. */
kono
parents:
diff changeset
229
kono
parents:
diff changeset
230 static void
kono
parents:
diff changeset
231 test_xstrndup ()
kono
parents:
diff changeset
232 {
kono
parents:
diff changeset
233 assert_xstrndup_eq ("", "test", 0);
kono
parents:
diff changeset
234 assert_xstrndup_eq ("t", "test", 1);
kono
parents:
diff changeset
235 assert_xstrndup_eq ("te", "test", 2);
kono
parents:
diff changeset
236 assert_xstrndup_eq ("tes", "test", 3);
kono
parents:
diff changeset
237 assert_xstrndup_eq ("test", "test", 4);
kono
parents:
diff changeset
238 assert_xstrndup_eq ("test", "test", 5);
kono
parents:
diff changeset
239
kono
parents:
diff changeset
240 /* Test on an string without zero termination. */
kono
parents:
diff changeset
241 const char src[4] = {'t', 'e', 's', 't'};
kono
parents:
diff changeset
242 assert_xstrndup_eq ("", src, 0);
kono
parents:
diff changeset
243 assert_xstrndup_eq ("t", src, 1);
kono
parents:
diff changeset
244 assert_xstrndup_eq ("te", src, 2);
kono
parents:
diff changeset
245 assert_xstrndup_eq ("tes", src, 3);
kono
parents:
diff changeset
246 assert_xstrndup_eq ("test", src, 4);
kono
parents:
diff changeset
247 }
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249 /* Run selftests for libiberty. */
kono
parents:
diff changeset
250
kono
parents:
diff changeset
251 static void
kono
parents:
diff changeset
252 test_libiberty ()
kono
parents:
diff changeset
253 {
kono
parents:
diff changeset
254 test_xstrndup ();
kono
parents:
diff changeset
255 }
kono
parents:
diff changeset
256
kono
parents:
diff changeset
257 /* Selftests for the selftest system itself. */
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 /* Sanity-check the ASSERT_ macros with various passing cases. */
kono
parents:
diff changeset
260
kono
parents:
diff changeset
261 static void
kono
parents:
diff changeset
262 test_assertions ()
kono
parents:
diff changeset
263 {
kono
parents:
diff changeset
264 ASSERT_TRUE (true);
kono
parents:
diff changeset
265 ASSERT_FALSE (false);
kono
parents:
diff changeset
266 ASSERT_EQ (1, 1);
kono
parents:
diff changeset
267 ASSERT_EQ_AT (SELFTEST_LOCATION, 1, 1);
kono
parents:
diff changeset
268 ASSERT_NE (1, 2);
kono
parents:
diff changeset
269 ASSERT_STREQ ("test", "test");
kono
parents:
diff changeset
270 ASSERT_STREQ_AT (SELFTEST_LOCATION, "test", "test");
kono
parents:
diff changeset
271 ASSERT_STR_CONTAINS ("foo bar baz", "bar");
kono
parents:
diff changeset
272 }
kono
parents:
diff changeset
273
kono
parents:
diff changeset
274 /* Verify named_temp_file. */
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 static void
kono
parents:
diff changeset
277 test_named_temp_file ()
kono
parents:
diff changeset
278 {
kono
parents:
diff changeset
279 named_temp_file t (".txt");
kono
parents:
diff changeset
280 FILE *f = fopen (t.get_filename (), "w");
kono
parents:
diff changeset
281 if (!f)
kono
parents:
diff changeset
282 fail_formatted (SELFTEST_LOCATION,
kono
parents:
diff changeset
283 "unable to open %s for writing", t.get_filename ());
kono
parents:
diff changeset
284 fclose (f);
kono
parents:
diff changeset
285 }
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 /* Verify read_file (and also temp_source_file). */
kono
parents:
diff changeset
288
kono
parents:
diff changeset
289 static void
kono
parents:
diff changeset
290 test_read_file ()
kono
parents:
diff changeset
291 {
kono
parents:
diff changeset
292 temp_source_file t (SELFTEST_LOCATION, "test1.s",
kono
parents:
diff changeset
293 "\tjmp\t.L2\n");
kono
parents:
diff changeset
294 char *buf = read_file (SELFTEST_LOCATION, t.get_filename ());
kono
parents:
diff changeset
295 ASSERT_STREQ ("\tjmp\t.L2\n", buf);
kono
parents:
diff changeset
296 free (buf);
kono
parents:
diff changeset
297 }
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 /* Verify locate_file (and read_file). */
kono
parents:
diff changeset
300
kono
parents:
diff changeset
301 static void
kono
parents:
diff changeset
302 test_locate_file ()
kono
parents:
diff changeset
303 {
kono
parents:
diff changeset
304 char *path = locate_file ("example.txt");
kono
parents:
diff changeset
305 char *buf = read_file (SELFTEST_LOCATION, path);
kono
parents:
diff changeset
306 ASSERT_STREQ ("example of a selftest file\n", buf);
kono
parents:
diff changeset
307 free (buf);
kono
parents:
diff changeset
308 free (path);
kono
parents:
diff changeset
309 }
kono
parents:
diff changeset
310
kono
parents:
diff changeset
311 /* Run all of the selftests within this file. */
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 void
kono
parents:
diff changeset
314 selftest_c_tests ()
kono
parents:
diff changeset
315 {
kono
parents:
diff changeset
316 test_libiberty ();
kono
parents:
diff changeset
317 test_assertions ();
kono
parents:
diff changeset
318 test_named_temp_file ();
kono
parents:
diff changeset
319 test_read_file ();
kono
parents:
diff changeset
320 test_locate_file ();
kono
parents:
diff changeset
321 }
kono
parents:
diff changeset
322
kono
parents:
diff changeset
323 } // namespace selftest
kono
parents:
diff changeset
324
kono
parents:
diff changeset
325 #endif /* #if CHECKING_P */