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
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"
27 int selftest::num_passes
;
29 /* Record the successful outcome of some aspect of a test. */
32 selftest::pass (const location
&/*loc*/, const char */
*msg*/
)
37 /* Report the failed outcome of some aspect of a test and abort. */
40 selftest::fail (const location
&loc
, const char *msg
)
42 fprintf (stderr
,"%s:%i: %s: FAIL: %s\n", loc
.m_file
, loc
.m_line
,
47 /* As "fail", but using printf-style formatted output. */
50 selftest::fail_formatted (const location
&loc
, const char *fmt
, ...)
54 fprintf (stderr
, "%s:%i: %s: FAIL: ", loc
.m_file
, loc
.m_line
,
57 vfprintf (stderr
, fmt
, ap
);
59 fprintf (stderr
, "\n");
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. */
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");
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 */