OpenTTD Source 20250312-master-gcdcc6b491d
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
20enum TestEnum : int8_t {
21 ZERO,
22 ONE,
23 TWO
24};
25
26TEST_CASE("EnumOverOptimisation_BoundsCheck")
27{
28 TestEnum negative_one = static_cast<TestEnum>(-1);
29 CHECK(negative_one < ZERO);
30
31 TestEnum three = static_cast<TestEnum>(3);
32 CHECK(TWO < three);
33}
34
35enum class TestEnumFlags : uint8_t {
36 Zero = 0,
37 One = 1 << 0,
38 Two = 1 << 1,
39};
40DECLARE_ENUM_AS_BIT_SET(TestEnumFlags)
41
42TEST_CASE("EnumOverOptimisation_Bitmask")
43{
44 TestEnumFlags three = TestEnumFlags::One | TestEnumFlags::Two;
45 CHECK(HasFlag(three, TestEnumFlags::One));
46 CHECK(HasFlag(three, TestEnumFlags::Two));
47}
#define DECLARE_ENUM_AS_BIT_SET(enum_type)
Operators to allow to work with enum as with type safe bit set in C++.
Definition enum_type.hpp:70
debug_inline constexpr bool HasFlag(const T x, const T y)
Checks if a value in a bitset enum is set.
Definition enum_type.hpp:93