OpenTTD Source 20250521-master-g82876c25e0
enum_over_optimisation.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
14#include "../stdafx.h"
15
16#include "../core/enum_type.hpp"
17
18#include "../3rdparty/catch2/catch.hpp"
19
20#include "../safeguards.h"
21
22enum TestEnum : int8_t {
23 ZERO,
24 ONE,
25 TWO
26};
27
28TEST_CASE("EnumOverOptimisation_BoundsCheck")
29{
30 TestEnum negative_one = static_cast<TestEnum>(-1);
31 CHECK(negative_one < ZERO);
32
33 TestEnum three = static_cast<TestEnum>(3);
34 CHECK(TWO < three);
35}
36
37enum class TestEnumFlags : uint8_t {
38 Zero = 0,
39 One = 1 << 0,
40 Two = 1 << 1,
41};
42DECLARE_ENUM_AS_BIT_SET(TestEnumFlags)
43
44TEST_CASE("EnumOverOptimisation_Bitmask")
45{
46 TestEnumFlags three = TestEnumFlags::One | TestEnumFlags::Two;
47 CHECK(HasFlag(three, TestEnumFlags::One));
48 CHECK(HasFlag(three, TestEnumFlags::Two));
49}
#define DECLARE_ENUM_AS_BIT_SET(enum_type)
Operators to allow to work with enum as with type safe bit set in C++.
debug_inline constexpr bool HasFlag(const T x, const T y)
Checks if a value in a bitset enum is set.