PR c++/70869
[official-gcc.git] / gcc / selftest.c
blobed6e517f00efeb8c3f0b2c98512cfc8b41fc3c4c
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. */
65 void
66 selftest::assert_streq (const location &loc,
67 const char *desc_expected, const char *desc_actual,
68 const char *val_expected, const char *val_actual)
70 if (0 == strcmp (val_expected, val_actual))
71 ::selftest::pass (loc, "ASSERT_STREQ");
72 else
73 ::selftest::fail_formatted
74 (loc, "ASSERT_STREQ (%s, %s) expected=\"%s\" actual=\"%s\"",
75 desc_expected, desc_actual, val_expected, val_actual);
79 #endif /* #if CHECKING_P */