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