Added cppbitmask with tests, added travis CI build

This commit is contained in:
2021-03-22 10:33:31 +01:00
parent 315e5d1dd2
commit 1aedce965a
7 changed files with 134 additions and 0 deletions

35
.travis.yml Normal file
View File

@ -0,0 +1,35 @@
language: cpp
os: linux
dist: focal
sudo: false
compiler:
- gcc
addons:
apt:
update: true
packages:
- "ca-certificates"
- "libqt5core5a"
- "libqt5gui5"
- "libqt5network5"
- "libqt5multimedia5"
- "libqt5sql5"
- "libqt5sql5-mysql"
- "libqt5sql5-sqlite"
- "libqt5serialport5"
- "git-core"
- "g++"
- "make"
- "cmake"
- "qt5-default"
- "qtmultimedia5-dev"
- "qttools5-dev"
- "qttools5-dev-tools"
- "libqt5serialport5-dev"
- "libqt5charts5-dev"
script:
- "qmake test/tstcpputils.pro"
- "make -j2"
- ./tstcpputils

View File

@ -1,5 +1,6 @@
set(headers
src/cleanuphelper.h
src/cppbitmask.h
src/crc32builder.h
src/cppflags.h
src/cppmacros.h

View File

@ -1,5 +1,6 @@
HEADERS += \
$$PWD/src/cleanuphelper.h \
$$PWD/src/cppbitmask.h \
$$PWD/src/cppflags.h \
$$PWD/src/cppmacros.h \
$$PWD/src/cppoverloadutils.h \

49
src/cppbitmask.h Normal file
View File

@ -0,0 +1,49 @@
#pragma once
// system includes
#include <cstdint>
#include <stdexcept>
namespace cpputils {
template<typename T>
class basic_bit_pattern
{
T expected{};
T mask{};
public:
template<std::size_t Size>
explicit constexpr basic_bit_pattern(const char (&input)[Size])
{
T cur_bit = (1 << (Size - 2));
for (const char val : input)
{
switch (val)
{
case 0:
return;
case '1':
expected |= cur_bit;
[[fallthrough]];
case '0':
mask |= cur_bit;
break;
case 'x':
case 'X':
break;
default:
throw std::logic_error{"Unknown characters in bit pattern input"};
}
cur_bit >>= 1;
}
}
constexpr friend bool operator==(const T value, const basic_bit_pattern &pattern)
{
return (value & pattern.mask) == pattern.expected;
}
};
using bit_pattern = basic_bit_pattern<std::uint32_t>;
} // namespace cpputils

1
test/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.user*

29
test/tst_cppbitmask.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <QtTest>
#include <QDebug>
#include <QByteArray>
#include <chrono>
#include <string>
#include "cpputilstestutils.h"
#include "cppbitmask.h"
class TstCppBitmask : public QObject
{
Q_OBJECT
private slots:
void test_simple()
{
constexpr cpputils::bit_pattern pattern{"11XXX10"};
QVERIFY(0b1101010 == pattern);
QVERIFY(0b1110110 == pattern);
QVERIFY(!(0b1100000 == pattern));
QVERIFY(!(0b1000010 == pattern));
}
};
QTEST_APPLESS_MAIN(TstCppBitmask)
#include "tst_cppbitmask.moc"

18
test/tstcpputils.pro Normal file
View File

@ -0,0 +1,18 @@
TEMPLATE = app
QT += core testlib
QT -= gui widgets
CONFIG += c++17 qt console warn_on depend_includepath testcase
CONFIG -= app_bundle
SOURCES += tst_cppbitmask.cpp
CPPUTILS_DIR = $$PWD/..
include($$CPPUTILS_DIR/cpputils.pri)
include($$CPPUTILS_DIR/cpputils_src.pri)
include($$CPPUTILS_DIR/test/cpputilstestutils.pri)
include($$CPPUTILS_DIR/test/cpputilstestutils_src.pri)
QMAKE_CXXFLAGS += -Wno-missing-field-initializers