test_containers.cpp
Go to the documentation of this file.
1 // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
2 // SPDX-License-Identifier: MIT
3 // Copyright (c) 2019 - 2024 Daniil Goncharov <neargye@gmail.com>.
4 // Copyright (c) 2022 - 2023 Bela Schaum <schaumb@gmail.com>.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 
24 #if defined(__clang__)
25 # pragma clang diagnostic push
26 #elif defined(__GNUC__)
27 # pragma GCC diagnostic push
28 #elif defined(_MSC_VER)
29 # pragma warning(push)
30 # pragma warning(disable : 4244) // warning C4244: 'argument': conversion from 'const T' to 'unsigned int', possible loss of data.
31 #endif
32 
33 #define CATCH_CONFIG_MAIN
34 #include <catch2/catch.hpp>
35 
38 
39 #include <functional>
40 
41 enum class Color { RED = 1, GREEN = 2, BLUE = 4 };
42 template <>
44  static constexpr bool is_flags = true;
45 };
46 
47 enum class Empty {};
48 
49 struct RGB {
50 
51  std::uint8_t r {};
52  std::uint8_t g {};
53  std::uint8_t b {};
54 
55  [[nodiscard]] constexpr bool empty() { return std::equal_to{}(r, g) && std::equal_to{}(g, b) && std::equal_to{}(b, 0); }
56 
57  [[nodiscard]] constexpr bool operator==(RGB rgb) const noexcept { return std::equal_to{}(r, rgb.r) && std::equal_to{}(g, rgb.g) && std::equal_to{}(b, rgb.b); }
58 
59  friend std::ostream& operator<<(std::ostream& ostream, RGB rgb) {
60 
61  ostream << "R=" << static_cast<std::uint32_t>(rgb.r) << " G=" << static_cast<std::uint32_t>(rgb.g) << " B=" << static_cast<std::uint32_t>(rgb.b);
62  return ostream;
63  }
64 };
65 
66 template <typename T> bool check_const([[maybe_unused]]T& element) { return false; }
67 template <typename T> bool check_const([[maybe_unused]]T const& element) { return true; }
68 
69 constexpr std::uint8_t color_max = std::numeric_limits<std::uint8_t>::max();
70 
71 TEST_CASE("containers_array") {
72 
73  using namespace magic_enum::bitwise_operators;
74  using namespace magic_enum::ostream_operators;
75 
76  constexpr magic_enum::containers::array<Color, RGB> color_rgb_initializer {{{{color_max, 0, 0}, {0, color_max, 0}, {0, 0, color_max}}}};
77  REQUIRE(color_rgb_initializer.at(Color::RED) == RGB{color_max, 0, 0});
78  REQUIRE(color_rgb_initializer.at(Color::GREEN) == RGB{0, color_max, 0});
79  REQUIRE(color_rgb_initializer.at(Color::BLUE) == RGB{0, 0, color_max});
80 
81  /* BUG: a sort will not survive the data integration */
82  magic_enum::containers::array<Color, std::uint8_t> color_rgb_container_int{{1U, 4U, 2U}};
83 
84  // ENC: Direct convert to std::array
85  // std::array compare_before {1U, 4U, 2U};
86  constexpr magic_enum::containers::array<Color, std::uint8_t> compare_before{{1U, 4U, 2U}};
87  REQUIRE(color_rgb_container_int == compare_before);
88 
89  constexpr auto colors = magic_enum::enum_values<Color>();
90 
91  std::ignore = magic_enum::containers::get<0>(compare_before);
92  std::ignore = magic_enum::containers::get<1>(compare_before);
93  std::ignore = magic_enum::containers::get<2>(compare_before);
94 
95  std::ignore = magic_enum::containers::get<Color::RED>(compare_before);
96  std::ignore = magic_enum::containers::get<Color::GREEN>(compare_before);
97  std::ignore = magic_enum::containers::get<Color::BLUE>(compare_before);
98 
99  REQUIRE(std::make_pair(colors[0], color_rgb_container_int[colors[0]]) == std::make_pair<Color, std::uint8_t>(Color::RED, 1U));
100  REQUIRE(std::make_pair(colors[1], color_rgb_container_int[colors[1]]) == std::make_pair<Color, std::uint8_t>(Color::GREEN, 4U));
101  REQUIRE(std::make_pair(colors[2], color_rgb_container_int[colors[2]]) == std::make_pair<Color, std::uint8_t>(Color::BLUE, 2U));
102 
103  std::sort(std::begin(color_rgb_container_int), std::end(color_rgb_container_int));
104 
105  // Missing: Direct convert to std::array
106  // std::array compare_after {1U, 2U, 4U};
107  constexpr magic_enum::containers::array<Color, std::uint8_t> compare_after{{1U, 2U, 4U}};
108  REQUIRE(color_rgb_container_int == compare_after);
109 
110  std::ignore = magic_enum::containers::get<0>(compare_after);
111  std::ignore = magic_enum::containers::get<1>(compare_after);
112  std::ignore = magic_enum::containers::get<2>(compare_after);
113 
114  std::ignore = magic_enum::containers::get<Color::RED>(compare_after);
115  std::ignore = magic_enum::containers::get<Color::GREEN>(compare_after);
116  std::ignore = magic_enum::containers::get<Color::BLUE>(compare_after);
117 
118  REQUIRE(std::make_pair(colors[0], color_rgb_container_int[colors[0]]) == std::make_pair<Color, std::uint8_t>(Color::RED, 1U));
119  REQUIRE(std::make_pair(colors[1], color_rgb_container_int[colors[1]]) == std::make_pair<Color, std::uint8_t>(Color::GREEN, 2U));
120  REQUIRE(std::make_pair(colors[2], color_rgb_container_int[colors[2]]) == std::make_pair<Color, std::uint8_t>(Color::BLUE, 4U));
121 
122  auto color_rgb_container = magic_enum::containers::array<Color, RGB>();
123  REQUIRE_FALSE(color_rgb_container.empty());
124  REQUIRE(color_rgb_container.size() == 3);
125  REQUIRE(magic_enum::enum_count<Color>() == color_rgb_container.size());
126 
127  REQUIRE(color_rgb_container.at(Color::RED).empty());
128  REQUIRE(color_rgb_container.at(Color::GREEN).empty());
129  REQUIRE(color_rgb_container.at(Color::BLUE).empty());
130  REQUIRE_THROWS(color_rgb_container.at(Color::BLUE|Color::GREEN).empty());
131 
132  color_rgb_container[Color::RED] = {color_max, 0, 0};
133  color_rgb_container[Color::GREEN] = {0, color_max, 0};
134  color_rgb_container[Color::BLUE] = {0, 0, color_max};
135 
136  REQUIRE(color_rgb_container.at(Color::RED) == RGB{color_max, 0, 0});
137  REQUIRE(color_rgb_container.at(Color::GREEN) == RGB{0, color_max, 0});
138  REQUIRE(color_rgb_container.at(Color::BLUE) == RGB{0, 0, color_max});
139 
140  REQUIRE(color_rgb_container.front() == RGB{color_max, 0, 0});
141  REQUIRE(color_rgb_container.back() == RGB{0, 0, color_max});
142 
143  REQUIRE(magic_enum::containers::get<Color::RED>(color_rgb_container) == RGB{color_max, 0, 0});
144  REQUIRE(magic_enum::containers::get<Color::GREEN>(color_rgb_container) == RGB{0, color_max, 0});
145  REQUIRE(magic_enum::containers::get<Color::BLUE>(color_rgb_container) == RGB{0, 0, color_max});
146 
147  auto iterator = color_rgb_container.begin();
148  REQUIRE_FALSE(check_const(iterator));
149  REQUIRE(check_const(color_rgb_container.begin()));
150  REQUIRE(check_const(color_rgb_container.cbegin()));
151 
152  auto color_rgb_container_compare = magic_enum::containers::array<Color, RGB>();
153  color_rgb_container_compare.fill({color_max, color_max, color_max});
154  REQUIRE_FALSE(color_rgb_container == color_rgb_container_compare);
155 
156  color_rgb_container_compare[Color::RED] = {color_max, 0, 0};
157  color_rgb_container_compare[Color::GREEN] = {0, color_max, 0};
158  color_rgb_container_compare[Color::BLUE] = {0, 0, color_max};
159  REQUIRE(color_rgb_container == color_rgb_container_compare);
160 
161  constexpr auto from_to_array = magic_enum::containers::to_array<Color, RGB>({{color_max, 0, 0}, {0, color_max, 0}, {0, 0, color_max}});
162  REQUIRE(from_to_array.at(Color::RED) == RGB{color_max, 0, 0});
163  REQUIRE(from_to_array.at(Color::GREEN) == RGB{0, color_max, 0});
164  REQUIRE(from_to_array.at(Color::BLUE) == RGB{0, 0, color_max});
165 }
166 
167 TEST_CASE("containers_bitset") {
168 
169  using namespace magic_enum::bitwise_operators;
170 
171  auto color_bitset = magic_enum::containers::bitset<Color>();
172  REQUIRE(color_bitset.to_string().empty());
173  REQUIRE(color_bitset.size() == 3);
174  REQUIRE(magic_enum::enum_count<Color>() == color_bitset.size());
175  REQUIRE_FALSE(color_bitset.all());
176  REQUIRE_FALSE(color_bitset.any());
177  REQUIRE(color_bitset.none());
178  REQUIRE(color_bitset.count() == 0);
179 
180  color_bitset.set(Color::GREEN);
181  REQUIRE_FALSE(color_bitset.all());
182  REQUIRE(color_bitset.any());
183  REQUIRE_FALSE(color_bitset.none());
184  REQUIRE(color_bitset.count() == 1);
185  REQUIRE_FALSE(color_bitset.test(Color::RED));
186  REQUIRE(color_bitset.test(Color::GREEN));
187  REQUIRE_FALSE(color_bitset.test(Color::BLUE));
188 
189  color_bitset.set(Color::BLUE);
190  REQUIRE_FALSE(color_bitset.all());
191  REQUIRE(color_bitset.any());
192  REQUIRE_FALSE(color_bitset.none());
193  REQUIRE(color_bitset.count() == 2);
194  REQUIRE_FALSE(color_bitset.test(Color::RED));
195  REQUIRE(color_bitset.test(Color::GREEN));
196  REQUIRE(color_bitset.test(Color::BLUE));
197 
198  color_bitset.set(Color::RED);
199  REQUIRE(color_bitset.all());
200  REQUIRE(color_bitset.any());
201  REQUIRE_FALSE(color_bitset.none());
202  REQUIRE(color_bitset.count() == 3);
203  REQUIRE(color_bitset.test(Color::RED));
204  REQUIRE(color_bitset.test(Color::GREEN));
205  REQUIRE(color_bitset.test(Color::BLUE));
206 
207  color_bitset.reset();
208  REQUIRE_FALSE(color_bitset.all());
209  REQUIRE_FALSE(color_bitset.any());
210  REQUIRE(color_bitset.none());
211  REQUIRE(color_bitset.count() == 0);
212  REQUIRE_FALSE(color_bitset.test(Color::RED));
213  REQUIRE_FALSE(color_bitset.test(Color::GREEN));
214  REQUIRE_FALSE(color_bitset.test(Color::BLUE));
215 
216  color_bitset.set(Color::RED);
217  REQUIRE(color_bitset.test(Color::RED));
218  REQUIRE_FALSE(color_bitset.test(Color::GREEN));
219  REQUIRE_FALSE(color_bitset.test(Color::BLUE));
220 
221  color_bitset.flip();
222  REQUIRE_FALSE(color_bitset.test(Color::RED));
223  REQUIRE(color_bitset.test(Color::GREEN));
224  REQUIRE(color_bitset.test(Color::BLUE));
225 
227  REQUIRE(color_bitset_all.to_string() == "RED|GREEN|BLUE");
228  REQUIRE(color_bitset_all.to_string( {}, '0', '1' ) == "111");
229  REQUIRE(color_bitset_all.to_ulong( {} ) == 7);
230  REQUIRE(color_bitset_all.to_ullong( {} ) == 7);
231  REQUIRE(color_bitset_all.all());
232  REQUIRE(color_bitset_all.any());
233  REQUIRE_FALSE(color_bitset_all.none());
234 
235  constexpr magic_enum::containers::bitset<Color> color_bitset_red_green {Color::RED|Color::GREEN};
236  REQUIRE(color_bitset_red_green.to_string() == "RED|GREEN");
237  REQUIRE(color_bitset_red_green.to_string( {}, '0', '1' ) == "110");
238  REQUIRE(color_bitset_red_green.to_ulong( {} ) == 3);
239  REQUIRE(color_bitset_red_green.to_ullong( {} ) == 3);
240  REQUIRE_FALSE(color_bitset_red_green.all());
241  REQUIRE(color_bitset_red_green.any());
242  REQUIRE_FALSE(color_bitset_red_green.none());
243 }
244 
245 TEST_CASE("containers_set") {
246 
247  using namespace magic_enum::bitwise_operators;
248  using namespace magic_enum::ostream_operators;
249 
250  auto color_set = magic_enum::containers::set<Color>();
251  REQUIRE(color_set.empty());
252  REQUIRE(color_set.size() == 0);
253  REQUIRE_FALSE(magic_enum::enum_count<Color>() == color_set.size());
254 
255  color_set.insert(Color::RED);
256  std::ignore = color_set.insert(Color::RED);
257  color_set.insert(Color::GREEN);
258  color_set.insert(Color::BLUE);
259  color_set.insert(Color::RED);
260  color_set.insert(Color::RED|Color::GREEN);
261  color_set.insert(Color::RED|Color::BLUE);
262  color_set.insert(Color::GREEN|Color::BLUE);
263  color_set.insert(Color::RED|Color::GREEN|Color::BLUE);
264 
265  REQUIRE_FALSE(color_set.empty());
266  REQUIRE(color_set.size() == 3);
267  REQUIRE(magic_enum::enum_count<Color>() == color_set.size());
268  color_set.erase(Color::RED);
269  color_set.erase(Color::GREEN);
270  REQUIRE(magic_enum::enum_count<Color>() - 2 == color_set.size());
271  REQUIRE(color_set.count(Color::RED) == 0);
272  REQUIRE_FALSE(color_set.contains(Color::GREEN));
273  REQUIRE(color_set.contains(Color::BLUE));
274 
275  auto color_set_compare = magic_enum::containers::set<Color>();
276  color_set_compare.insert(Color::BLUE);
277  color_set_compare.insert(Color::RED);
278  color_set_compare.insert(Color::GREEN);
279 
280  constexpr magic_enum::containers::set color_set_filled = {Color::RED, Color::GREEN, Color::BLUE};
281  REQUIRE_FALSE(color_set_filled.empty());
282  REQUIRE(color_set_filled.size() == 3);
283  REQUIRE(magic_enum::enum_count<Color>() == color_set_filled.size());
284 
286  REQUIRE_FALSE(color_set_not_const.empty());
287  REQUIRE(color_set_not_const.size() == 3);
288  REQUIRE(magic_enum::enum_count<Color>() == color_set_not_const.size());
289  color_set_not_const.clear();
290  REQUIRE(color_set_not_const.empty());
291  REQUIRE(color_set_not_const.size() == 0);
292  REQUIRE_FALSE(magic_enum::enum_count<Color>() == color_set_not_const.size());
293 }
294 
295 TEST_CASE("map_like_container") {
296 
297  using namespace magic_enum::ostream_operators;
298 
299  std::vector<std::pair<Color, RGB>> map {{Color::GREEN, {0, color_max, 0}}, {Color::BLUE, {0, 0, color_max}}, {Color::RED, {color_max, 0, 0}}};
300  for (auto [key, value] : map) {
301 
302  std::cout << "Key=" << key << " Value=" << value << std::endl;
303  }
304  auto compare = [](std::pair<Color, RGB>& lhs,
305  std::pair<Color, RGB>& rhs) {
306  return static_cast<std::int32_t>(lhs.first) < static_cast<std::int32_t>(rhs.first);
307  };
308  std::sort(std::begin(map), std::end(map), compare);
309  for (auto [key, value] : map) {
310 
311  std::cout << "Key=" << key << " Value=" << value << std::endl;
312  }
313 }
Catch::Generators::map
GeneratorWrapper< T > map(Func &&function, GeneratorWrapper< U > &&generator)
Definition: catch.hpp:4276
RGB
Definition: example_containers_array.cpp:34
Color::GREEN
@ GREEN
magic_enum::bitwise_operators
Definition: magic_enum.hpp:1464
magic_enum::containers::bitset
Definition: magic_enum_containers.hpp:485
TEST_CASE
TEST_CASE("containers_array")
Definition: test_containers.cpp:71
magic_enum::containers::set::empty
constexpr bool empty() const noexcept
Definition: magic_enum_containers.hpp:922
magic_enum_iostream.hpp
magic_enum::detail::value
constexpr E value(std::size_t i) noexcept
Definition: magic_enum.hpp:679
Empty
Empty
Definition: test_containers.cpp:47
magic_enum::containers::array
Definition: magic_enum_containers.hpp:335
magic_enum::ostream_operators
Definition: magic_enum_iostream.hpp:44
check_const
bool check_const([[maybe_unused]]T &element)
Definition: test_containers.cpp:66
RGB::operator==
constexpr bool operator==(RGB rgb) const noexcept
Definition: test_containers.cpp:57
color_max
constexpr std::uint8_t color_max
Definition: test_containers.cpp:69
RGB::operator<<
friend std::ostream & operator<<(std::ostream &ostream, RGB rgb)
Definition: test_containers.cpp:59
REQUIRE_THROWS
#define REQUIRE_THROWS(...)
Definition: catch.hpp:17640
REQUIRE
#define REQUIRE(...)
Definition: catch.hpp:17637
Catch::cout
std::ostream & cout()
RGB::r
std::uint8_t r
Definition: example_containers_array.cpp:36
magic_enum::containers::set
Definition: magic_enum_containers.hpp:841
magic_enum_containers.hpp
RGB::b
std::uint8_t b
Definition: example_containers_array.cpp:38
magic_enum::containers::set::clear
constexpr void clear() noexcept
Definition: magic_enum_containers.hpp:928
Color::RED
@ RED
magic_enum::customize::enum_range
Definition: magic_enum.hpp:172
catch.hpp
REQUIRE_FALSE
#define REQUIRE_FALSE(...)
Definition: catch.hpp:17638
Color
Color
Definition: example.cpp:28
RGB::empty
constexpr bool empty()
Definition: test_containers.cpp:55
magic_enum::containers::set::size
constexpr size_type size() const noexcept
Definition: magic_enum_containers.hpp:924
Color::BLUE
@ BLUE
RGB::g
std::uint8_t g
Definition: example_containers_array.cpp:37


magic_enum
Author(s):
autogenerated on Fri Feb 21 2025 03:20:19