Skip to content

use ctest and set path to dbc files with a compile definition #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ jobs:
- uses: actions/checkout@v3

- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
run: |
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ..

- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -- -j
run: |
cd build
make

- name: Test
working-directory: ${{github.workspace}}/build
# Didn't configure to use ctest. Opted for a custom target to run instead
run: cmake --build ${{github.workspace}}/build --target test -- -j
run: |
cd build
ctest --output-on-failure

7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.10)
project(dbc)

option(DEBUG "use debug flag" NO)
option(ENABLE_TESTS "Enable Unittests" ON)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
Expand Down Expand Up @@ -31,7 +32,11 @@ list(APPEND SOURCE ${PROJECT_SOURCE_DIR}/src/utils.cpp
include_directories(src)
include_directories(include)

add_subdirectory(test)
if(ENABLE_TESTS)
include(CTest)
add_subdirectory(test)
endif()

add_subdirectory(doc)

add_library(${PROJECT_NAME} STATIC ${SOURCE})
Expand Down
44 changes: 43 additions & 1 deletion src/dbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,56 @@

#include <regex>

namespace {

const auto floatPattern = "(-?\\d+\\.?(\\d+)?)"; // Can be negative

const auto signalIdentifierPattern = "(SG_)";
const auto namePattern = "(\\w+)";
const auto bitStartPattern = "(\\d+)"; // Cannot be negative
const auto lengthPattern = "(\\d+)"; // Cannot be negative
const auto byteOrderPattern = "([0-1])";
const auto signPattern = "(\\+|\\-)";
const auto scalePattern = "(\\d+\\.?(\\d+)?)"; // Non negative float
const auto offsetPattern = floatPattern;
const auto offsetScalePattern = std::string("\\(") + scalePattern + "\\," + offsetPattern + "\\)";
const auto minPattern = floatPattern;
const auto maxPattern = floatPattern;
const auto minMaxPattern = std::string("\\[") + minPattern + "\\|" + maxPattern + "\\]";
const auto unitPattern = "\"(.*)\""; // Random string
const auto receiverPattern = "([\\w\\,]+|Vector__XXX)*";
const auto whiteSpace = "\\s";

} // anonymous namespace

namespace libdbc {

DbcParser::DbcParser() : version(""), nodes(),
version_re("^(VERSION)\\s\"(.*)\""), bit_timing_re("^(BS_:)"),
name_space_re("^(NS_)\\s\\:"), node_re("^(BU_:)\\s((?:[\\w]+?\\s?)*)"),
message_re("^(BO_)\\s(\\d+)\\s(\\w+)\\:\\s(\\d+)\\s(\\w+|Vector__XXX)"),
// NOTE: No multiplex support yet
signal_re("\\s(SG_)\\s(\\w+)\\s\\:\\s(\\d+)\\|(\\d+)\\@(\\d+)(\\+|\\-)\\s\\((\\d+\\.?(\\d+)?)\\,(\\d+\\.?(\\d+)?)\\)\\s\\[(-?\\d+\\.?(\\d+)?)\\|(-?\\d+\\.?(\\d+)?)\\]\\s\"(\\w*)\"\\s([\\w\\,]+|Vector__XXX)*") {
signal_re(std::string(whiteSpace) +
signalIdentifierPattern +
whiteSpace +
namePattern +
whiteSpace +
"\\:" +
whiteSpace +
bitStartPattern +
"\\|" +
lengthPattern +
"\\@" +
byteOrderPattern +
signPattern +
whiteSpace +
offsetScalePattern +
whiteSpace +
minMaxPattern +
whiteSpace +
unitPattern +
whiteSpace +
receiverPattern) {

}

Expand Down
2 changes: 1 addition & 1 deletion src/signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace libdbc {
Signal::Signal(std::string name, bool is_multiplexed, uint32_t start_bit, uint32_t size, bool is_bigendian, bool is_signed, double factor, double offset, double min, double max, std::string unit, std::vector<std::string> receivers) :
name(name), is_multiplexed(is_multiplexed), start_bit(start_bit), size(size), is_bigendian(is_bigendian), is_signed(is_signed), offset(offset), min(min), max(max), unit(unit), receivers(receivers) {}
name(name), is_multiplexed(is_multiplexed), start_bit(start_bit), size(size), is_bigendian(is_bigendian), is_signed(is_signed), factor(factor), offset(offset), min(min), max(max), unit(unit), receivers(receivers) {}

bool Signal::operator==(const Signal& rhs) const {
return (this->name == rhs.name) && (this->is_multiplexed == rhs.is_multiplexed) &&
Expand Down
18 changes: 10 additions & 8 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(tests VERSION 0.1.0)
enable_testing()

# Download and build Catch2 test framework
Include(FetchContent)
Expand All @@ -8,15 +8,17 @@ FetchContent_Declare(
GIT_TAG v3.2.1
)
FetchContent_MakeAvailable(Catch2)
include(Catch)

list(APPEND TEST_SOURCES
test_dbc.cpp
test_utils.cpp)

add_executable(tests ${TEST_SOURCES} ${SOURCE})
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)

add_custom_target(test
COMMAND ${PROJECT_NAME}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
DEPENDS ${PROJECT_NAME})
add_executable(tests ${TEST_SOURCES})
target_compile_definitions(tests PRIVATE TESTDBCFILES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/dbcs")
target_link_libraries(tests PRIVATE dbc Catch2::Catch2WithMain)
target_sources(tests INTERFACE FILE_SET HEADERS
TYPE HEADERS
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}
FILES defines.hpp)
catch_discover_tests(tests)
23 changes: 17 additions & 6 deletions test/defines.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
#include <string>

// Correctly formated files
static const std::string COMPLEX_DBC_FILE = "./dbcs/Complex.dbc";
static const std::string SIMPLE_DBC_FILE = "./dbcs/Simple.dbc";
static const std::string COMPLEX_DBC_FILE = std::string(TESTDBCFILES_PATH) + "/Complex.dbc";
static const std::string SIMPLE_DBC_FILE = std::string(TESTDBCFILES_PATH) + "/Simple.dbc";

// Files with Errors
static const std::string MISSING_NEW_SYMBOLS_DBC_FILE = "./dbcs/MissingNewSymbols.dbc";
static const std::string MISSING_VERSION_DBC_FILE = "./dbcs/MissingVersion.dbc";
static const std::string MISSING_BIT_TIMING_DBC_FILE = "./dbcs/MissingBitTiming.dbc";
static const std::string TEXT_FILE = "./dbcs/TextFile.txt";
static const std::string MISSING_NEW_SYMBOLS_DBC_FILE = std::string(TESTDBCFILES_PATH) + "/MissingNewSymbols.dbc";
static const std::string MISSING_VERSION_DBC_FILE = std::string(TESTDBCFILES_PATH) + "/MissingVersion.dbc";
static const std::string MISSING_BIT_TIMING_DBC_FILE = std::string(TESTDBCFILES_PATH) + "/MissingBitTiming.dbc";
static const std::string TEXT_FILE = std::string(TESTDBCFILES_PATH) + "/TextFile.txt";

static const std::string PRIMITIVE_DBC =
R"(VERSION "1.0.0"

NS_ :

BS_:

BU_: DBG DRIVER IO MOTOR SENSOR

)";
77 changes: 76 additions & 1 deletion test/test_dbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
#include "defines.hpp"
#include <libdbc/dbc.hpp>

void create_tmp_dbc_with(const char* filename, const char* content)
{
auto* file = std::fopen(filename, "w");
CHECK(file);

std::fputs(PRIMITIVE_DBC.c_str(), file);
std::fputs(content, file);
std::fclose(file);
}


TEST_CASE("Testing dbc file loading error issues", "[fileio][error]") {
auto parser = std::unique_ptr<libdbc::DbcParser>(new libdbc::DbcParser());

Expand Down Expand Up @@ -49,4 +60,68 @@ TEST_CASE("Testing dbc file loading", "[fileio]") {
REQUIRE(parser->get_messages().front().signals == msg.signals);
}

}
}

TEST_CASE("Testing negative values") {
const auto* filename = std::tmpnam(NULL);

create_tmp_dbc_with(filename, R"(BO_ 234 MSG1: 8 Vector__XXX
SG_ Sig1 : 55|16@0- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX
SG_ Sig2 : 39|16@0- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX
SG_ Sig3 : 23|16@0- (10,0) [-3276.8|-3276.7] "C" Vector__XXX
SG_ Sig4 : 7|16@0- (1,-10) [0|32767] "" Vector__XXX)");

auto parser = libdbc::DbcParser();
parser.parse_file(std::string(filename));

REQUIRE(parser.get_messages().size() == 1);
REQUIRE(parser.get_messages().at(0).signals.size() == 4);

SECTION("Evaluating first message") {
const auto signal = parser.get_messages().at(0).signals.at(0);
REQUIRE(signal.factor == 0.1);
REQUIRE(signal.offset == 0);
REQUIRE(signal.min == -3276.8);
REQUIRE(signal.max == -3276.7);
}
SECTION("Evaluating second message") {
const auto signal = parser.get_messages().at(0).signals.at(1);
REQUIRE(signal.factor == 0.1);
REQUIRE(signal.offset == 0);
REQUIRE(signal.min == -3276.8);
REQUIRE(signal.max == -3276.7);
}
SECTION("Evaluating third message"){
const auto signal = parser.get_messages().at(0).signals.at(2);
REQUIRE(signal.factor == 10);
REQUIRE(signal.offset == 0);
REQUIRE(signal.min == -3276.8);
REQUIRE(signal.max == -3276.7);
}
SECTION("Evaluating fourth message"){
const auto signal = parser.get_messages().at(0).signals.at(3);
REQUIRE(signal.factor == 1);
REQUIRE(signal.offset == -10);
REQUIRE(signal.min == 0);
REQUIRE(signal.max == 32767);
}
}


TEST_CASE("Special characters in unit") {
const auto* filename = std::tmpnam(NULL);

create_tmp_dbc_with(filename, R"(BO_ 234 MSG1: 8 Vector__XXX
SG_ Speed : 0|8@1+ (1,0) [0|204] "Km/h" DEVICE1,DEVICE2,DEVICE3)");


auto parser = libdbc::DbcParser();
parser.parse_file(std::string(filename));

REQUIRE(parser.get_messages().size() == 1);
REQUIRE(parser.get_messages().at(0).signals.size() == 1);
SECTION("Checking that signal with special characters as unit is parsed correctly") {
const auto signal = parser.get_messages().at(0).signals.at(0);
REQUIRE(signal.unit.compare("Km/h") == 0);
}
}