Skip to content

Commit

Permalink
[brief] Allow enum bitfield to receive assignment of the base type.
Browse files Browse the repository at this point in the history
[detailed]
- Mostly for symmetry with the other function.
  • Loading branch information
marovira committed Jun 27, 2024
1 parent cfda2d8 commit 5a0ab05
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
6 changes: 6 additions & 0 deletions include/zeus/enum_bitfield.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ namespace zeus
return *this;
}

constexpr EnumBitfield& operator=(BaseType field)
{
m_field = field;
return *this;
}

constexpr BaseType bits() const
{
return m_field;
Expand Down
42 changes: 33 additions & 9 deletions test/enum_bitfield_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,44 @@ TEST_CASE("[EnumBitfield] - operator=", "[zeus]")
{
SECTION("Runtime")
{
EnumBitfield<Bits> a;
a = Bits::b;
REQUIRE(a.bits() == 2);
SECTION("From enum")
{
EnumBitfield<Bits> a;
a = Bits::b;
REQUIRE(a.bits() == 2);
}

SECTION("From base type")
{
EnumBitfield<Bits> a;
a = 2;
REQUIRE(a.bits() == 2);
}
}

SECTION("Compile-time")
{
static constexpr auto val = []() {
EnumBitfield<Bits> a;
a = Bits::b;
return a.bits();
}();
SECTION("From enum")
{
static constexpr auto val = []() {
EnumBitfield<Bits> a;
a = Bits::b;
return a.bits();
}();

STATIC_REQUIRE(val == 2);
STATIC_REQUIRE(val == 2);
}

SECTION("From base type")
{
static constexpr auto val = []() {
EnumBitfield<Bits> a;
a = 2;
return a.bits();
}();

STATIC_REQUIRE(val == 2);
}
}
}

Expand Down

0 comments on commit 5a0ab05

Please sign in to comment.