OpenTTD Source: src/tests/enum_over_optimisation.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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};
42 DECLARE_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}
DECLARE_ENUM_AS_BIT_SET
#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:119
HasFlag
constexpr bool HasFlag(const T x, const T y)
Checks if a value in a bitset enum is set.
Definition enum_type.hpp:142