[gcc]
[official-gcc.git] / gcc / selftest.c
blob76a4c4149479b041acd134dd8a47fde03b9f8737
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
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 int selftest::num_passes;
29 /* Record the successful outcome of some aspect of a test. */
31 void
32 selftest::pass (const location &/*loc*/, const char */*msg*/)
34 num_passes++;
37 /* Report the failed outcome of some aspect of a test and abort. */
39 void
40 selftest::fail (const location &loc, const char *msg)
42 fprintf (stderr,"%s:%i: %s: FAIL: %s\n", loc.m_file, loc.m_line,
43 loc.m_function, msg);
44 abort ();
47 /* As "fail", but using printf-style formatted output. */
49 void
50 selftest::fail_formatted (const location &loc, const char *fmt, ...)
52 va_list ap;
54 fprintf (stderr, "%s:%i: %s: FAIL: ", loc.m_file, loc.m_line,
55 loc.m_function);
56 va_start (ap, fmt);
57 vfprintf (stderr, fmt, ap);
58 va_end (ap);
59 fprintf (stderr, "\n");
60 abort ();
63 /* Implementation detail of ASSERT_STREQ.
64 Compare val_expected and val_actual with strcmp. They ought
65 to be non-NULL; fail gracefully if either are NULL. */
67 void
68 selftest::assert_streq (const location &loc,
69 const char *desc_expected, const char *desc_actual,
70 const char *val_expected, const char *val_actual)
72 /* If val_expected is NULL, the test is buggy. Fail gracefully. */
73 if (val_expected == NULL)
74 ::selftest::fail_formatted
75 (loc, "ASSERT_STREQ (%s, %s) expected=NULL",
76 desc_expected, desc_actual);
77 /* If val_actual is NULL, fail with a custom error message. */
78 if (val_actual == NULL)
79 ::selftest::fail_formatted
80 (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 ::selftest::pass (loc, "ASSERT_STREQ");
84 else
85 ::selftest::fail_formatted
86 (loc, "ASSERT_STREQ (%s, %s) expected=\"%s\" actual=\"%s\"",
87 desc_expected, desc_actual, val_expected, val_actual);
91 #endif /* #if CHECKING_P */