Skip to content

Commit

Permalink
🐛 Fix Flags<N> data storage width (MarlinFirmware#26995)
Browse files Browse the repository at this point in the history
* Fix Flags and associated unit tests
  • Loading branch information
sjasonsmith authored and RPGFabi committed May 5, 2024
1 parent 1a63620 commit 0eed6d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Marlin/src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ template <class L, class R> struct IF<true, L, R> { typedef L type; };
// General Flags for some number of states
template<size_t N>
struct Flags {
typedef uvalue_t(N) flagbits_t;
typedef bits_t(N) flagbits_t;
typedef struct { bool b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1; } N8;
typedef struct { bool b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1, b8:1, b9:1, b10:1, b11:1, b12:1, b13:1, b14:1, b15:1; } N16;
typedef struct { bool b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1, b8:1, b9:1, b10:1, b11:1, b12:1, b13:1, b14:1, b15:1,
Expand Down
39 changes: 14 additions & 25 deletions Marlin/tests/core/test_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ MARLIN_TEST(types, XYval_magnitude) {

MARLIN_TEST(types, XYval_small_large) {
XYval<int> xy;

xy.set(3, 4);
TEST_ASSERT_EQUAL(3, xy.small());
TEST_ASSERT_EQUAL(4, xy.large());
Expand Down Expand Up @@ -187,7 +187,7 @@ MARLIN_TEST(types, XYZval_magnitude) {

MARLIN_TEST(types, XYZval_small_large) {
XYZval<int> xyz;

xyz.set(3, 4, 5);
TEST_ASSERT_EQUAL(3, xyz.small());
TEST_ASSERT_EQUAL(5, xyz.large());
Expand Down Expand Up @@ -325,7 +325,7 @@ MARLIN_TEST(types, XYZEval_magnitude) {

MARLIN_TEST(types, XYZEval_small_large) {
XYZEval<int> xyze;

xyze.set(3, 4, 5, 6);
TEST_ASSERT_EQUAL(3, xyze.small());
TEST_ASSERT_EQUAL(6, xyze.large());
Expand Down Expand Up @@ -484,26 +484,23 @@ MARLIN_TEST(types, Flags_16) {

flags.set(0, true);
flags.set(15, true);
// BUG: The storage can only contain 8 bits!
// TEST_ASSERT_EQUAL(32769, flags.b);
TEST_ASSERT_EQUAL(1, flags.b);
TEST_ASSERT_EQUAL(32769, flags.b);

flags.clear(0);
TEST_ASSERT_EQUAL(0, flags.b);
TEST_ASSERT_EQUAL(32768, flags.b);

flags.reset();
flags.set(7, true);
flags.set(15, true);
TEST_ASSERT_EQUAL(true, flags.test(7));
// BUG: This can't store a value above bit 7 right now
TEST_ASSERT_EQUAL(false, flags.test(15));
TEST_ASSERT_EQUAL(false, flags.test(8));
TEST_ASSERT_EQUAL(true, flags.test(15));

TEST_ASSERT_EQUAL(true, flags[7]);
// BUG: This can't store a value above bit 7 right now
TEST_ASSERT_EQUAL(false, flags[15]);
TEST_ASSERT_EQUAL(false, flags[8]);
TEST_ASSERT_EQUAL(true, flags[15]);

// BUG: This size should be 2, but is incorrectly 1
TEST_ASSERT_EQUAL(1, flags.size());
TEST_ASSERT_EQUAL(2, flags.size());
}

MARLIN_TEST(types, Flags_32) {
Expand All @@ -514,9 +511,7 @@ MARLIN_TEST(types, Flags_32) {

flags.set(0, true);
flags.set(31, true);
// BUG: The storage can only contain 8 bits!
//TEST_ASSERT_EQUAL(2147483649, flags.b);
TEST_ASSERT_EQUAL(1, flags.b);
TEST_ASSERT_EQUAL(2147483649, flags.b);

flags.clear(0);
flags.clear(31);
Expand All @@ -525,22 +520,16 @@ MARLIN_TEST(types, Flags_32) {
flags.set(0, true);
flags.set(31, true);
TEST_ASSERT_EQUAL(true, flags.test(0));
// BUG: This can't store a value above bit 7 right now
TEST_ASSERT_EQUAL(false, flags.test(31));
// TEST_ASSERT_EQUAL(true, flags.test(31));
TEST_ASSERT_EQUAL(true, flags.test(31));
TEST_ASSERT_EQUAL(false, flags.test(1));
TEST_ASSERT_EQUAL(false, flags.test(30));

TEST_ASSERT_EQUAL(true, flags[0]);
// BUG: This can't store a value above bit 7 right now
TEST_ASSERT_EQUAL(false, flags[31]);
// TEST_ASSERT_EQUAL(true, flags[31]);
TEST_ASSERT_EQUAL(true, flags[31]);
TEST_ASSERT_EQUAL(false, flags[1]);
TEST_ASSERT_EQUAL(false, flags[30]);

// BUG: This size should be 4, but is incorrectly 1
TEST_ASSERT_EQUAL(1, flags.size());
// TEST_ASSERT_EQUAL(4, flags.size());
TEST_ASSERT_EQUAL(4, flags.size());
}

MARLIN_TEST(types, AxisFlags_const_as_bools) {
Expand Down

0 comments on commit 0eed6d4

Please sign in to comment.