based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

commit 5769377617f46869ceef0cdf1fb807eed3cb58c7
parent de074c7f42b48b82fa4c10b1ae5542c608741f4c
author Dimitrije Dobrota < mail@dimitrijedobrota.com >
date Thu, 8 May 2025 16:55:42 +0200

Improve enum tests and consistency

Diffstat:
M include/based/enum/enum.hpp | +++++++++++
M test/source/enum_test.cpp | ++++++++ ---

2 files changed, 19 insertions(+), 3 deletions(-)


diff --git a/ include/based/enum/enum.hpp b/ include/based/enum/enum.hpp

@@ -57,6 +57,7 @@

{ \
using base = std::array<T, Name::type::size>; \
using base::operator[]; \
using base::at; \
\
public: \
constexpr array() noexcept \

@@ -81,6 +82,16 @@

{ \
return base::operator[](val.value); \
} \
\
const T& at(Name::type val) const \
{ \
return base::operator[](val.value); \
} \
\
T& at(Name::type val) \
{ \
return base::operator[](val.value); \
} \
};

#define BASED_ENUM_DECLARE(Name, Type, ...) \

diff --git a/ test/source/enum_test.cpp b/ test/source/enum_test.cpp

@@ -9,6 +9,7 @@

#include "based/concepts/comparable/greater_equal.hpp"
#include "based/concepts/comparable/less.hpp"
#include "based/concepts/comparable/less_equal.hpp"
#include "based/concepts/is/invocable.hpp"
#include "based/concepts/is/same.hpp"
#include "based/types/types.hpp"

@@ -43,6 +44,7 @@ inline int test::get_var(var::type req) const

TEST_CASE("types", "[enum/enum]")
{
STATIC_REQUIRE(requires { typename test::var; });
STATIC_REQUIRE(requires { test::var::type::size == 3; });
STATIC_REQUIRE(requires { test::var::a; });
STATIC_REQUIRE(requires { test::var::b; });
STATIC_REQUIRE(requires { test::var::c; });

@@ -52,9 +54,12 @@ TEST_CASE("safety", "[enum/enum]")

{
const test crnt;

STATIC_REQUIRE(requires { crnt.get_var(test::var::a) == 1; });
STATIC_REQUIRE(requires { crnt.get_var(test::var::b) == 2; });
STATIC_REQUIRE(requires { crnt.get_var(test::var::c) == 3; });
REQUIRE(crnt.get_var(test::var::a) == 1);
REQUIRE(crnt.get_var(test::var::b) == 2);
REQUIRE(crnt.get_var(test::var::c) == 3);

REQUIRE(!based::Invocable<decltype(&test::get_var), based::u8>);
REQUIRE(!based::Invocable<decltype(&test::get_var), int>);
}

TEST_CASE("names", "[enum/enum]")