1 /* "True" vs "False" vs "Unknown".
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 tristate::as_string () const
44 tristate::not_ () const
51 return tristate (TS_UNKNOWN
);
53 return tristate (TS_FALSE
);
55 return tristate (TS_TRUE
);
60 tristate::or_ (tristate other
) const
68 return tristate (TS_TRUE
);
70 return tristate (TS_UNKNOWN
);
74 return tristate (TS_TRUE
);
79 tristate::and_ (tristate other
) const
86 if (other
.is_false ())
87 return tristate (TS_FALSE
);
89 return tristate (TS_UNKNOWN
);
93 return tristate (TS_FALSE
);
101 #define ASSERT_TRISTATE_TRUE(TRISTATE) \
102 SELFTEST_BEGIN_STMT \
103 ASSERT_EQ (TRISTATE, tristate (tristate::TS_TRUE)); \
106 #define ASSERT_TRISTATE_FALSE(TRISTATE) \
107 SELFTEST_BEGIN_STMT \
108 ASSERT_EQ (TRISTATE, tristate (tristate::TS_FALSE)); \
111 #define ASSERT_TRISTATE_UNKNOWN(TRISTATE) \
112 SELFTEST_BEGIN_STMT \
113 ASSERT_EQ (TRISTATE, tristate (tristate::TS_UNKNOWN)); \
116 /* Test tristate's ctors, along with is_*, as_string, operator==, and
122 tristate
u (tristate::TS_UNKNOWN
);
123 ASSERT_FALSE (u
.is_known ());
124 ASSERT_FALSE (u
.is_true ());
125 ASSERT_FALSE (u
.is_false ());
126 ASSERT_STREQ (u
.as_string (), "UNKNOWN");
128 tristate
t (tristate::TS_TRUE
);
129 ASSERT_TRUE (t
.is_known ());
130 ASSERT_TRUE (t
.is_true ());
131 ASSERT_FALSE (t
.is_false ());
132 ASSERT_STREQ (t
.as_string (), "TRUE");
134 tristate
f (tristate::TS_FALSE
);
135 ASSERT_TRUE (f
.is_known ());
136 ASSERT_FALSE (f
.is_true ());
137 ASSERT_TRUE (f
.is_false ());
138 ASSERT_STREQ (f
.as_string (), "FALSE");
148 ASSERT_TRUE (t2
.is_true ());
152 ASSERT_TRUE (f2
.is_false ());
155 tristate
u2 (tristate::unknown ());
156 ASSERT_TRUE (!u2
.is_known ());
160 /* Test && on tristate instances. */
165 ASSERT_TRISTATE_UNKNOWN (tristate::unknown () && tristate::unknown ());
167 ASSERT_TRISTATE_FALSE (tristate (false) && tristate (false));
168 ASSERT_TRISTATE_FALSE (tristate (false) && tristate (true));
169 ASSERT_TRISTATE_FALSE (tristate (true) && tristate (false));
170 ASSERT_TRISTATE_TRUE (tristate (true) && tristate (true));
172 ASSERT_TRISTATE_UNKNOWN (tristate::unknown () && tristate (true));
173 ASSERT_TRISTATE_UNKNOWN (tristate (true) && tristate::unknown ());
175 ASSERT_TRISTATE_FALSE (tristate::unknown () && tristate (false));
176 ASSERT_TRISTATE_FALSE (tristate (false) && tristate::unknown ());
179 /* Test || on tristate instances. */
184 ASSERT_TRISTATE_UNKNOWN (tristate::unknown () || tristate::unknown ());
186 ASSERT_TRISTATE_FALSE (tristate (false) || tristate (false));
187 ASSERT_TRISTATE_TRUE (tristate (false) || tristate (true));
188 ASSERT_TRISTATE_TRUE (tristate (true) || tristate (false));
189 ASSERT_TRISTATE_TRUE (tristate (true) || tristate (true));
191 ASSERT_TRISTATE_TRUE (tristate::unknown () || tristate (true));
192 ASSERT_TRISTATE_TRUE (tristate (true) || tristate::unknown ());
194 ASSERT_TRISTATE_UNKNOWN (tristate::unknown () || tristate (false));
195 ASSERT_TRISTATE_UNKNOWN (tristate (false) || tristate::unknown ());
198 /* Test ! on tristate instances. */
203 ASSERT_TRISTATE_UNKNOWN (!tristate::unknown ());
204 ASSERT_TRISTATE_FALSE (!tristate (true));
205 ASSERT_TRISTATE_TRUE (!tristate (false));
208 /* Run all of the selftests within this file. */
219 } // namespace selftest
221 #endif /* CHECKING_P */