1 /* Tests for interactions between C++ and assert.
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 /* Undefine NDEBUG to ensure the build system e.g. CFLAGS/CXXFLAGS
20 does not disable the asserts we want to test. */
21 #undef NDEBUG
22 #include <assert.h>
23
24 /* The C++ standard requires that if the assert argument is a constant
25 subexpression, then the assert itself is one, too. */
26 constexpr int
check_constexpr()27 check_constexpr ()
28 {
29 return (assert (true), 1);
30 }
31
32 /* Objects of this class can be contextually converted to bool, but
33 cannot be compared to int. */
34 struct no_int
35 {
36 no_int () = default;
37 no_int (const no_int &) = delete;
38
operator boolno_int39 explicit operator bool () const
40 {
41 return true;
42 }
43
44 bool operator! () const; /* No definition. */
45 template <class T> bool operator== (T) const; /* No definition. */
46 template <class T> bool operator!= (T) const; /* No definition. */
47 };
48
49 /* This class tests that operator== is not used by assert. */
50 struct bool_and_int
51 {
52 bool_and_int () = default;
53 bool_and_int (const no_int &) = delete;
54
operator boolbool_and_int55 explicit operator bool () const
56 {
57 return true;
58 }
59
60 bool operator! () const; /* No definition. */
61 template <class T> bool operator== (T) const; /* No definition. */
62 template <class T> bool operator!= (T) const; /* No definition. */
63 };
64
65 static int
do_test()66 do_test ()
67 {
68 {
69 no_int value;
70 assert (value);
71 }
72
73 {
74 bool_and_int value;
75 assert (value);
76 }
77
78 return 0;
79 }
80
81 #include <support/test-driver.c>
82