Modes: reusing submode for mode mask, more unittests #133

Merged
muellerr merged 1 commits from mohr/submode_mask into develop 2023-03-10 11:22:12 +01:00
2 changed files with 61 additions and 12 deletions

View File

@ -21,7 +21,6 @@ class ModeListEntry : public SerialLinkedListAdapter<SerializeIF>,
SerializeElement<uint32_t> value2 = 0; SerializeElement<uint32_t> value2 = 0;
SerializeElement<uint8_t> value3 = 0; SerializeElement<uint8_t> value3 = 0;
SerializeElement<uint8_t> value4 = 0; SerializeElement<uint8_t> value4 = 0;
SerializeElement<uint8_t> value5 = 0;
ModeListEntry(const ModeListEntry& other) ModeListEntry(const ModeListEntry& other)
: SerialLinkedListAdapter(), LinkedElement<ModeListEntry>(this) { : SerialLinkedListAdapter(), LinkedElement<ModeListEntry>(this) {
@ -29,7 +28,6 @@ class ModeListEntry : public SerialLinkedListAdapter<SerializeIF>,
value2.entry = other.value2.entry; value2.entry = other.value2.entry;
value3.entry = other.value3.entry; value3.entry = other.value3.entry;
value4.entry = other.value4.entry; value4.entry = other.value4.entry;
value5.entry = other.value5.entry;
setLinks(); setLinks();
} }
@ -38,7 +36,6 @@ class ModeListEntry : public SerialLinkedListAdapter<SerializeIF>,
this->value2.entry = other.value2.entry; this->value2.entry = other.value2.entry;
this->value3.entry = other.value3.entry; this->value3.entry = other.value3.entry;
this->value4.entry = other.value4.entry; this->value4.entry = other.value4.entry;
this->value5.entry = other.value5.entry;
return *this; return *this;
} }
@ -47,7 +44,6 @@ class ModeListEntry : public SerialLinkedListAdapter<SerializeIF>,
value1.setNext(&value2); value1.setNext(&value2);
value2.setNext(&value3); value2.setNext(&value3);
value3.setNext(&value4); value3.setNext(&value4);
value4.setNext(&value5);
} }
// for Sequences // for Sequences
@ -84,7 +80,7 @@ class ModeListEntry : public SerialLinkedListAdapter<SerializeIF>,
bool submodesAllowed = (value4.entry & mode::SpecialSubmodeFlags::ALLOWED_MASK) == bool submodesAllowed = (value4.entry & mode::SpecialSubmodeFlags::ALLOWED_MASK) ==
mode::SpecialSubmodeFlags::ALLOWED_MASK; mode::SpecialSubmodeFlags::ALLOWED_MASK;
if (submodesAllowed and mask != nullptr) { if (submodesAllowed and mask != nullptr) {
*mask = value5.entry; *mask = value3.entry;
} }
return submodesAllowed; return submodesAllowed;
} }
@ -113,14 +109,14 @@ class ModeListEntry : public SerialLinkedListAdapter<SerializeIF>,
*/ */
void enableSubmodeAllowed(uint8_t mask) { void enableSubmodeAllowed(uint8_t mask) {
value4.entry |= mode::SpecialSubmodeFlags::ALLOWED_MASK; value4.entry |= mode::SpecialSubmodeFlags::ALLOWED_MASK;
value5.entry = mask; value3.entry = mask;
} }
/** /**
* Enforce the equality of submodes for mode checks. This is the default. * Enforce the equality of submodes for mode checks. This is the default.
*/ */
void disableSubmodeAllowed() { void disableSubmodeAllowed() {
value4.entry &= ~mode::SpecialSubmodeFlags::ALLOWED_MASK; value4.entry &= ~mode::SpecialSubmodeFlags::ALLOWED_MASK;
value5.entry = 0; value3.entry = 0;
} }
}; };

View File

@ -16,20 +16,30 @@ TEST_CASE("Mode Definitions", "[mode]") {
CHECK(entry.submodesAllowed(&mask) == false); CHECK(entry.submodesAllowed(&mask) == false);
} }
SECTION("Inherit submode") {
entry.enableInheritSubmode();
CHECK(entry.inheritSubmode() == true);
entry.disableInheritSubmode();
CHECK(entry.inheritSubmode() == false);
}
SECTION("Allowed submode mask") { SECTION("Allowed submode mask") {
entry.allowAllSubmodes(); entry.allowAllSubmodes();
uint8_t mask; uint8_t mask;
CHECK(entry.submodesAllowed(&mask) == true); CHECK(entry.submodesAllowed(&mask) == true);
CHECK(mask == 0xff); CHECK(mask == 0xff);
entry.enableSubmodeAllowed(0x32);
CHECK(entry.submodesAllowed(&mask) == true);
CHECK(mask == 0x32);
entry.disableSubmodeAllowed();
CHECK(entry.submodesAllowed(&mask) == false);
} }
SECTION("Serialization") { SECTION("Serialization nominal") {
std::array<uint8_t, 32> buf{}; std::array<uint8_t, 32> buf{};
entry.setObject(0x1f2f3f4f); entry.setObject(0x1f2f3f4f);
entry.setMode(HasModesIF::MODE_ON); entry.setMode(HasModesIF::MODE_ON);
entry.setSubmode(2); entry.setSubmode(2);
entry.enableInheritSubmode();
entry.enableSubmodeAllowed(0x1f);
uint8_t* serPtr = buf.data(); uint8_t* serPtr = buf.data();
size_t serLen = 0; size_t serLen = 0;
REQUIRE(entry.serialize(&serPtr, &serLen, buf.size(), SerializeIF::Endianness::NETWORK) == REQUIRE(entry.serialize(&serPtr, &serLen, buf.size(), SerializeIF::Endianness::NETWORK) ==
@ -43,7 +53,50 @@ TEST_CASE("Mode Definitions", "[mode]") {
CHECK(buf[6] == 0); CHECK(buf[6] == 0);
CHECK(buf[7] == HasModesIF::MODE_ON); CHECK(buf[7] == HasModesIF::MODE_ON);
CHECK(buf[8] == 2); CHECK(buf[8] == 2);
CHECK(buf[9] == (mode::SpecialSubmodeFlags::ALLOWED_MASK | mode::SpecialSubmodeFlags::INHERIT)); CHECK(buf[9] == 0);
CHECK(buf[10] == 0x1f); }
SECTION("Serialization inherit submode") {
std::array<uint8_t, 32> buf{};
entry.setObject(0x1f2f3f4f);
entry.setMode(HasModesIF::MODE_ON);
entry.setSubmode(2);
entry.enableInheritSubmode();
uint8_t* serPtr = buf.data();
size_t serLen = 0;
REQUIRE(entry.serialize(&serPtr, &serLen, buf.size(), SerializeIF::Endianness::NETWORK) ==
returnvalue::OK);
CHECK(buf[0] == 0x1f);
CHECK(buf[1] == 0x2f);
CHECK(buf[2] == 0x3f);
CHECK(buf[3] == 0x4f);
CHECK(buf[4] == 0);
CHECK(buf[5] == 0);
CHECK(buf[6] == 0);
CHECK(buf[7] == HasModesIF::MODE_ON);
CHECK(buf[8] == 2);
CHECK(buf[9] == mode::SpecialSubmodeFlags::INHERIT);
}
SECTION("Serialization submode mask") {
std::array<uint8_t, 32> buf{};
entry.setObject(0x1f2f3f4f);
entry.setMode(HasModesIF::MODE_ON);
entry.setSubmode(2);
entry.enableSubmodeAllowed(0x1f);
uint8_t* serPtr = buf.data();
size_t serLen = 0;
REQUIRE(entry.serialize(&serPtr, &serLen, buf.size(), SerializeIF::Endianness::NETWORK) ==
returnvalue::OK);
CHECK(buf[0] == 0x1f);
CHECK(buf[1] == 0x2f);
CHECK(buf[2] == 0x3f);
CHECK(buf[3] == 0x4f);
CHECK(buf[4] == 0);
CHECK(buf[5] == 0);
CHECK(buf[6] == 0);
CHECK(buf[7] == HasModesIF::MODE_ON);
CHECK(buf[8] == 0x1f);
CHECK(buf[9] == mode::SpecialSubmodeFlags::ALLOWED_MASK);
} }
} }