OpenTTD Source 20250708-master-g6d86c760c6
flatset_type.cpp
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
10#include "../stdafx.h"
11
12#include <ranges>
13
14#include "../3rdparty/catch2/catch.hpp"
15
16#include "../core/flatset_type.hpp"
17
18#include "../safeguards.h"
19
20TEST_CASE("FlatSet - basic")
21{
22 /* Sorted array of expected values. */
23 const auto values = std::to_array<uint8_t>({5, 10, 15, 20, 25});
24
26
27 /* Set should be empty. */
28 CHECK(set.empty());
29
30 /* Insert in a random order,. */
31 CHECK(set.insert(values[1]).second);
32 CHECK(set.insert(values[2]).second);
33 CHECK(set.insert(values[4]).second);
34 CHECK(set.insert(values[3]).second);
35 CHECK(set.insert(values[0]).second);
36 CHECK(set.size() == 5);
37 CHECK(set.contains(values[0]));
38 CHECK(set.contains(values[1]));
39 CHECK(set.contains(values[2]));
40 CHECK(set.contains(values[3]));
41 CHECK(set.contains(values[4]));
42 CHECK(std::ranges::equal(set, values));
43
44 /* Test inserting an existing value does not affect order. */
45 CHECK_FALSE(set.insert(values[1]).second);
46 CHECK(set.size() == 5);
47 CHECK(set.contains(values[0]));
48 CHECK(set.contains(values[1]));
49 CHECK(set.contains(values[2]));
50 CHECK(set.contains(values[3]));
51 CHECK(set.contains(values[4]));
52 CHECK(std::ranges::equal(set, values));
53
54 /* Insert a value multiple times. */
55 CHECK(set.insert(0).second);
56 CHECK_FALSE(set.insert(0).second);
57 CHECK_FALSE(set.insert(0).second);
58 CHECK(set.size() == 6);
59 CHECK(set.contains(0));
60
61 /* Remove a value multiple times. */
62 CHECK(set.erase(0) == 1);
63 CHECK(set.erase(0) == 0);
64 CHECK(set.erase(0) == 0);
65 CHECK(set.size() == 5);
66 CHECK(!set.contains(0));
67}
Flat set implementation that uses a sorted vector for storage.