Skip to content

Commit d00599c

Browse files
committed
add tests
1 parent 3af2c0c commit d00599c

File tree

6 files changed

+172
-29
lines changed

6 files changed

+172
-29
lines changed

src/message.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,27 @@ namespace libdbc {
1919
if (size > 8)
2020
return false; // not supported yet
2121

22-
// Only little endian supported yet!
23-
// All signals must be little endian
24-
for (const auto& signal: m_signals) {
25-
if (signal.is_bigendian)
26-
return false;
27-
}
28-
29-
const uint32_t len = size * 8;
30-
31-
uint64_t d = 0;
22+
uint64_t data_little_endian = 0;
23+
uint64_t data_big_endian = 0;
3224
for (int i=0; i < size; i++) {
33-
d |= ((uint64_t)data[i]) << i * 8;
25+
data_little_endian |= ((uint64_t)data[i]) << i * 8;
26+
data_big_endian |= (uint64_t)data[i] << (size - 1 - i);
3427
}
3528

29+
// TODO: does this also work on a big endian machine?
30+
31+
const uint32_t len = size * 8;
3632
uint64_t v = 0;
3733
for (const auto& signal: m_signals) {
38-
const uint32_t shiftLeft = (len - (signal.size + signal.start_bit));
39-
v = d << shiftLeft;
40-
v = v >> (shiftLeft + signal.start_bit);
34+
if (signal.is_bigendian) {
35+
const uint32_t shiftLeft = signal.start_bit;
36+
v = data_big_endian << shiftLeft;
37+
v = v >> (shiftLeft + signal.start_bit);
38+
} else {
39+
const uint32_t shiftLeft = (len - (signal.size + signal.start_bit));
40+
v = data_little_endian << shiftLeft;
41+
v = v >> (shiftLeft + signal.start_bit);
42+
}
4143
values.push_back(v * signal.factor + signal.offset);
4244
}
4345
return true;

test/CMakeLists.txt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ FetchContent_Declare(
1010
FetchContent_MakeAvailable(Catch2)
1111
include(Catch)
1212

13-
list(APPEND TEST_SOURCES
14-
test_dbc.cpp
15-
test_utils.cpp)
16-
17-
add_executable(dbcParserTests ${TEST_SOURCES})
13+
add_executable(dbcParserTests test_dbc.cpp test_utils.cpp common.cpp)
1814
target_compile_definitions(dbcParserTests PRIVATE TESTDBCFILES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/dbcs")
1915
target_link_libraries(dbcParserTests PRIVATE dbc Catch2::Catch2WithMain)
2016
if (${CMAKE_MINOR_VERSION} GREATER_EQUAL 23)
@@ -26,3 +22,16 @@ else()
2622
target_include_directories(dbcParserTests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
2723
endif()
2824
catch_discover_tests(dbcParserTests)
25+
26+
add_executable(dbcParserParsemessageTests test_parseMessage.cpp common.cpp)
27+
target_compile_definitions(dbcParserParsemessageTests PRIVATE TESTDBCFILES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/dbcs")
28+
target_link_libraries(dbcParserParsemessageTests PRIVATE dbc Catch2::Catch2WithMain)
29+
if (${CMAKE_MINOR_VERSION} GREATER_EQUAL 23)
30+
target_sources(dbcParserParsemessageTests PRIVATE FILE_SET HEADERS
31+
TYPE HEADERS
32+
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}
33+
FILES defines.hpp)
34+
else()
35+
target_include_directories(dbcParserParsemessageTests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
36+
endif()
37+
catch_discover_tests(dbcParserParsemessageTests)

test/common.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "common.hpp"
2+
#include "defines.hpp"
3+
4+
5+
bool create_tmp_dbc_with(const char* filename, const char* content)
6+
{
7+
auto* file = std::fopen(filename, "w");
8+
if (!file) {
9+
return false;
10+
}
11+
12+
std::fputs(PRIMITIVE_DBC.c_str(), file);
13+
std::fputs(content, file);
14+
std::fclose(file);
15+
return true;
16+
}

test/common.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef COMMON_H
2+
#define COMMON_H
3+
4+
bool create_tmp_dbc_with(const char* filename, const char* content);
5+
6+
#endif // COMMON_H

test/test_dbc.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@
33
#include <catch2/matchers/catch_matchers.hpp>
44
#include "defines.hpp"
55
#include <libdbc/dbc.hpp>
6-
7-
void create_tmp_dbc_with(const char* filename, const char* content)
8-
{
9-
auto* file = std::fopen(filename, "w");
10-
CHECK(file);
11-
12-
std::fputs(PRIMITIVE_DBC.c_str(), file);
13-
std::fputs(content, file);
14-
std::fclose(file);
15-
}
6+
#include "common.hpp"
167

178

189
TEST_CASE("Testing dbc file loading error issues", "[fileio][error]") {

test/test_parseMessage.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
#include <catch2/catch_approx.hpp>
3+
#include <catch2/matchers/catch_matchers.hpp>
4+
5+
#include <libdbc/dbc.hpp>
6+
7+
#include "common.hpp"
8+
9+
// Testing of parsing messages
10+
11+
TEST_CASE("Parse Message 1 Big Endian") {
12+
libdbc::DbcParser parser(true);
13+
14+
const auto dbcContent = R"(BO_ 234 MSG1: 8 Vector__XXX
15+
SG_ Sig1 : 0|8@0- (0.1,-3) [-3276.8|-3276.7] "C" Vector__XXX
16+
SG_ Sig2 : 8|8@0- (0.15,7) [-3276.8|-3276.7] "C" Vector__XXX
17+
)";
18+
19+
const auto* filename = std::tmpnam(NULL);
20+
CHECK(create_tmp_dbc_with(filename, dbcContent));
21+
22+
parser.parse_file(filename);
23+
24+
SECTION("Evaluating first message") {
25+
std::vector<double> out_values;
26+
CHECK(parser.parseMessage(234, std::vector<uint8_t>({0x01, 0x02}), out_values) == true);
27+
CHECK(out_values.size() == 2);
28+
CHECK(out_values.at(0) == 0x01 * 0.1 - 3);
29+
CHECK(out_values.at(1) == 0x02 * 0.15 + 7);
30+
}
31+
32+
}
33+
34+
TEST_CASE("Parse Message 2 Big Endian") {
35+
libdbc::DbcParser parser(true);
36+
37+
const auto dbcContent = R"(BO_ 234 MSG1: 8 Vector__XXX
38+
SG_ Msg1Sig1 : 0|8@0+ (1,0) [-3276.8|-3276.7] "C" Vector__XXX
39+
SG_ MsgSig2 : 8|8@0+ (1,0) [-3276.8|-3276.7] "C" Vector__XXX
40+
BO_ 123 MSG2: 8 Vector__XXX
41+
SG_ Msg2Sig1 : 0|8@0+ (1,0) [-3276.8|-3276.7] "C" Vector__XXX
42+
SG_ Msg2Sig1 : 8|8@0+ (1,0) [-3276.8|-3276.7] "C" Vector__XXX
43+
)";
44+
45+
const auto* filename = std::tmpnam(NULL);
46+
CHECK(create_tmp_dbc_with(filename, dbcContent));
47+
48+
parser.parse_file(filename);
49+
50+
SECTION("Evaluating first message") {
51+
std::vector<double> out_values;
52+
CHECK(parser.parseMessage(234, std::vector<uint8_t>({0x01, 0x02}), out_values) == true);
53+
std::vector<double> refData{0x01, 0x02};
54+
CHECK(refData.size() == 2);
55+
CHECK(out_values.size() == refData.size());
56+
for (int i=0; i < refData.size(); i++) {
57+
CHECK(out_values.at(i) == refData.at(i));
58+
}
59+
}
60+
61+
SECTION("Evaluating unknown message id") {
62+
std::vector<double> out_values;
63+
CHECK(parser.parseMessage(578, std::vector<uint8_t>({0xFF, 0xA2}), out_values) == false);
64+
}
65+
}
66+
67+
TEST_CASE("Parse Message Big Number not aligned little endian") {
68+
libdbc::DbcParser parser(true);
69+
70+
const auto dbcContent = R"(BO_ 337 STATUS: 8 Vector__XXX
71+
SG_ Value6 : 27|3@1+ (1,0) [0|7] "" Vector__XXX
72+
SG_ Value5 : 16|11@1+ (0.1,-102) [-102|102] "%" Vector__XXX
73+
SG_ Value2 : 8|2@1+ (1,0) [0|2] "" Vector__XXX
74+
SG_ Value3 : 10|1@1+ (1,0) [0|1] "" Vector__XXX
75+
SG_ Value7 : 30|2@1+ (1,0) [0|3] "" Vector__XXX
76+
SG_ Value4 : 11|4@1+ (1,0) [0|3] "" Vector__XXX
77+
SG_ Value1 : 0|8@1+ (1,0) [0|204] "Km/h" Vector__XXX
78+
)";
79+
80+
const auto* filename = std::tmpnam(NULL);
81+
CHECK(create_tmp_dbc_with(filename, dbcContent));
82+
83+
parser.parse_file(filename);
84+
85+
SECTION("Evaluating first message") {
86+
std::vector<double> out_values;
87+
CHECK(parser.parseMessage(337, std::vector<uint8_t>({0, 4, 252, 19, 0, 0, 0, 0}), out_values) == true);
88+
std::vector<double> refData{0, 0, 1, 0, 0, 2, 0};
89+
CHECK(refData.size() == 7);
90+
CHECK(out_values.size() == refData.size());
91+
for (int i=0; i < refData.size(); i++) {
92+
CHECK(out_values.at(i) == refData.at(i));
93+
}
94+
}
95+
96+
SECTION("Evaluating second message") {
97+
std::vector<double> out_values;
98+
CHECK(parser.parseMessage(337, std::vector<uint8_t>({47, 4, 60, 29, 0, 0, 0, 0}), out_values) == true);
99+
std::vector<double> refData{47, 0, 1, 0, 32, 3, 0};
100+
CHECK(refData.size() == 7);
101+
CHECK(out_values.size() == refData.size());
102+
for (int i=0; i < refData.size(); i++) {
103+
CHECK(out_values.at(i) == refData.at(i));
104+
}
105+
}
106+
107+
SECTION("Evaluating third message") {
108+
std::vector<double> out_values;
109+
CHECK(parser.parseMessage(337, std::vector<uint8_t>({57, 4, 250, 29, 0, 0, 0, 0}), out_values) == true);
110+
std::vector<double> refData{57, 0, 1, 0, 51, 3, 0};
111+
CHECK(refData.size() == 7);
112+
CHECK(out_values.size() == refData.size());
113+
for (int i=0; i < refData.size(); i++) {
114+
CHECK(out_values.at(i) == refData.at(i));
115+
}
116+
}
117+
}
118+
119+
// TODO: create also for big endian!

0 commit comments

Comments
 (0)