Compile time bitmask string length check
This commit is contained in:
@@ -9,23 +9,22 @@
|
|||||||
class TstCppBitmask;
|
class TstCppBitmask;
|
||||||
|
|
||||||
namespace cpputils {
|
namespace cpputils {
|
||||||
template<typename T>
|
template<std::size_t BITS_COUNT>
|
||||||
class basic_bit_pattern
|
class bit_pattern
|
||||||
{
|
{
|
||||||
friend class ::TstCppBitmask;
|
friend class ::TstCppBitmask;
|
||||||
T expected{};
|
|
||||||
T mask{};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template<std::size_t Size>
|
using value_t = std::uint32_t; // TODO make dependant to BITS_COUNT
|
||||||
constexpr basic_bit_pattern(const char (&input)[Size])
|
|
||||||
|
constexpr bit_pattern(const char (&input)[BITS_COUNT+1])
|
||||||
{
|
{
|
||||||
T cur_bit = (1 << (Size - 2));
|
value_t cur_bit = (1 << (BITS_COUNT - 1));
|
||||||
for (const char val : input)
|
for (const char val : input)
|
||||||
{
|
{
|
||||||
switch (val)
|
switch (val)
|
||||||
{
|
{
|
||||||
case 0:
|
case '\0':
|
||||||
return;
|
return;
|
||||||
case '1':
|
case '1':
|
||||||
expected |= cur_bit;
|
expected |= cur_bit;
|
||||||
@@ -44,17 +43,19 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr bool match(const T value) const
|
constexpr bool match(const value_t value) const
|
||||||
{
|
{
|
||||||
return (value & mask) == expected;
|
return (value & mask) == expected;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr friend bool operator==(const basic_bit_pattern &l, const basic_bit_pattern &r)
|
constexpr friend bool operator==(const bit_pattern &l, const bit_pattern &r)
|
||||||
{
|
{
|
||||||
return l.expected == r.expected &&
|
return l.expected == r.expected &&
|
||||||
l.mask == r.mask;
|
l.mask == r.mask;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
using bit_pattern = basic_bit_pattern<std::uint32_t>;
|
private:
|
||||||
|
value_t expected{};
|
||||||
|
value_t mask{};
|
||||||
|
};
|
||||||
} // namespace cpputils
|
} // namespace cpputils
|
||||||
|
@@ -16,7 +16,7 @@ class TstCppBitmask : public QObject
|
|||||||
private slots:
|
private slots:
|
||||||
void test_basic_functionality()
|
void test_basic_functionality()
|
||||||
{
|
{
|
||||||
constexpr cpputils::bit_pattern pattern{"10XXX10"};
|
constexpr cpputils::bit_pattern<7> pattern{"10XXX10"};
|
||||||
QCOMPARE(pattern.expected, 0b1000010);
|
QCOMPARE(pattern.expected, 0b1000010);
|
||||||
QCOMPARE(pattern.mask, 0b1100011);
|
QCOMPARE(pattern.mask, 0b1100011);
|
||||||
QVERIFY(pattern.match(0b1001010));
|
QVERIFY(pattern.match(0b1001010));
|
||||||
@@ -27,39 +27,39 @@ private slots:
|
|||||||
|
|
||||||
void test_copy()
|
void test_copy()
|
||||||
{
|
{
|
||||||
const cpputils::bit_pattern pattern{"10XXX10"};
|
const cpputils::bit_pattern<7> pattern{"10XXX10"};
|
||||||
const cpputils::bit_pattern patternCopy{pattern};
|
const cpputils::bit_pattern<7> patternCopy{pattern};
|
||||||
QCOMPARE(patternCopy.expected, 0b1000010);
|
QCOMPARE(patternCopy.expected, 0b1000010);
|
||||||
QCOMPARE(patternCopy.mask, 0b1100011);
|
QCOMPARE(patternCopy.mask, 0b1100011);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_assign()
|
void test_assign()
|
||||||
{
|
{
|
||||||
const cpputils::bit_pattern pattern{"10XXX10"};
|
const cpputils::bit_pattern<9> pattern{"10XXX10X1"};
|
||||||
cpputils::bit_pattern patternCopy{"11110000XXXX"};
|
cpputils::bit_pattern<9> patternCopy{"11110000X"};
|
||||||
QCOMPARE(patternCopy.expected, 0b111100000000);
|
QCOMPARE(patternCopy.expected, 0b111100000);
|
||||||
QCOMPARE(patternCopy.mask, 0b111111110000);
|
QCOMPARE(patternCopy.mask, 0b111111110);
|
||||||
patternCopy = pattern;
|
patternCopy = pattern;
|
||||||
QCOMPARE(patternCopy.expected, 0b1000010);
|
QCOMPARE(patternCopy.expected, 0b100001001);
|
||||||
QCOMPARE(patternCopy.mask, 0b1100011);
|
QCOMPARE(patternCopy.mask, 0b110001101);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_swap()
|
void test_swap()
|
||||||
{
|
{
|
||||||
cpputils::bit_pattern pattern0{"10XXX10"};
|
cpputils::bit_pattern<9> pattern0{"10XXX10X1"};
|
||||||
cpputils::bit_pattern pattern1{"11110000XXXX"};
|
cpputils::bit_pattern<9> pattern1{"11110000X"};
|
||||||
|
|
||||||
QCOMPARE(pattern0.expected, 0b1000010);
|
QCOMPARE(pattern0.expected, 0b100001001);
|
||||||
QCOMPARE(pattern0.mask, 0b1100011);
|
QCOMPARE(pattern0.mask, 0b110001101);
|
||||||
QCOMPARE(pattern1.expected, 0b111100000000);
|
QCOMPARE(pattern1.expected, 0b111100000);
|
||||||
QCOMPARE(pattern1.mask, 0b111111110000);
|
QCOMPARE(pattern1.mask, 0b111111110);
|
||||||
|
|
||||||
std::swap(pattern0, pattern1);
|
std::swap(pattern0, pattern1);
|
||||||
|
|
||||||
QCOMPARE(pattern0.expected, 0b111100000000);
|
QCOMPARE(pattern0.expected, 0b111100000);
|
||||||
QCOMPARE(pattern0.mask, 0b111111110000);
|
QCOMPARE(pattern0.mask, 0b111111110);
|
||||||
QCOMPARE(pattern1.expected, 0b1000010);
|
QCOMPARE(pattern1.expected, 0b100001001);
|
||||||
QCOMPARE(pattern1.mask, 0b1100011);
|
QCOMPARE(pattern1.mask, 0b110001101);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user