#include "d_enumerate.h" #include #define BOOST_TEST_DYN_LINK #define BOOST_TEST_MODULE Rebirth enumerate #include /* Test that an enumerated range is empty when the component range is empty. */ BOOST_AUTO_TEST_CASE(enumerate_empty) { bool empty = true; std::array a; for (auto &&[idx, v] : enumerate(a)) { (void)idx; (void)v; empty = false; } BOOST_TEST(empty); } /* Test that an enumerated range is the length of the underlying sequence. */ BOOST_AUTO_TEST_CASE(enumerate_length) { unsigned count = 0; std::array a; for (auto &&[idx, v] : enumerate(a)) { (void)idx; (void)v; ++ count; } BOOST_TEST(count == a.size()); } /* Test that an enumerated value references the underlying storage. */ BOOST_AUTO_TEST_CASE(enumerate_passthrough_modifications) { std::array a{{1}}; for (auto &&[idx, v] : enumerate(a)) { (void)idx; ++ v; } BOOST_TEST(a[0] == 2); } /* Test that an enumerated index tracks properly. */ BOOST_AUTO_TEST_CASE(enumerate_starting_value) { const std::array a{{1, 2, 3, 4}}; for (auto &&[idx, v] : enumerate(a)) BOOST_TEST(idx + 1 == v); for (auto &&[idx, v] : enumerate(a, 1)) BOOST_TEST(idx == v); } /* Type system tests can be done at compile-time. Applying them as * static_assert can produce a better error message than letting it fail * at runtime. */ template struct assert_index_type { static_assert(std::is_same::value); }; template struct assert_index_type &>()))>; template struct custom_index_type : std::array { using index_type = T; void operator[](typename std::remove_reference::type); }; enum class e1 : unsigned char; template struct assert_index_type&>()))>;